Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Removes unleash from Redux store #589

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const types = {
WALLET_RELOAD_DATA: 'WALLET_RELOAD_DATA',
WALLET_CHANGE_STATE: 'WALLET_CHANGE_STATE',
WALLET_RESET: 'WALLET_RESET',
WALLET_RESET_SUCCESS: 'WALLET_RESET_SUCCESS',
WALLET_REFRESH_SHARED_ADDRESS: 'WALLET_REFRESH_SHARED_ADDRESS',
SET_SERVER_INFO: 'SET_SERVER_INFO',
SET_NAVIGATE_TO: 'SET_NAVIGATE_TO',
Expand Down Expand Up @@ -471,18 +470,6 @@ export const setFeatureToggles = (toggles) => ({
payload: toggles,
});

/**
* unleashClient {UnleashClient} The unleash client to store
*/
export const setUnleashClient = (unleashClient) => ({
type: types.SET_UNLEASH_CLIENT,
payload: unleashClient,
});

export const walletResetSuccess = () => ({
type: types.WALLET_RESET_SUCCESS,
});

export const walletReset = () => ({
type: types.WALLET_RESET,
});
Expand Down
16 changes: 16 additions & 0 deletions src/modules/unleash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

let unleashClient = null;

export function setUnleashClient(_unleashClient) {
unleashClient = _unleashClient;
}

export function getUnleashClient() {
return unleashClient;
}
19 changes: 0 additions & 19 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ const initialState = {
status: TOKEN_DOWNLOAD_STATUS.READY
}
},
unleashClient: null,
featureTogglesInitialized: false,
featureToggles: {
...FEATURE_TOGGLE_DEFAULTS,
Expand Down Expand Up @@ -265,8 +264,6 @@ const rootReducer = (state = initialState, action) => {
return onSetFeatureToggles(state, action);
case types.FEATURE_TOGGLE_INITIALIZED:
return onFeatureToggleInitialized(state);
case types.WALLET_RESET_SUCCESS:
return onWalletResetSuccess(state);
case types.UPDATE_TX_HISTORY:
return onUpdateTxHistory(state, action);
case types.WALLET_CHANGE_STATE:
Expand Down Expand Up @@ -353,8 +350,6 @@ const onCleanData = (state) => {
isVersionAllowed: state.isVersionAllowed,
loadingAddresses: state.loadingAddresses,
ledgerWasClosed: state.ledgerWasClosed,
// Keep the unleashClient as it should continue running
unleashClient: state.unleashClient,
featureTogglesInitialized: state.featureTogglesInitialized,
});
};
Expand Down Expand Up @@ -987,20 +982,6 @@ const onSetFeatureToggles = (state, { payload }) => ({
featureToggles: payload,
});

/**
* @param {Object} action.payload The unleash client to store
*/
const onSetUnleashClient = (state, { payload }) => ({
...state,
unleashClient: payload,
});

const onWalletResetSuccess = (state) => ({
...state,
// Keep the unleashClient as it should continue running
unleashClient: state.unleashClient,
});

export const onUpdateTxHistory = (state, action) => {
const { tx, tokenId, balance } = action.payload;
const tokenHistory = state.tokensHistory[tokenId];
Expand Down
14 changes: 7 additions & 7 deletions src/sagas/featureToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import helpers from '../utils/helpers';
import { VERSION } from '../constants';

import {
setUnleashClient,
setFeatureToggles,
featureToggleInitialized,
} from '../actions';
Expand All @@ -39,18 +38,19 @@ import {
UNLEASH_POLLING_INTERVAL,
FEATURE_TOGGLE_DEFAULTS,
} from '../constants';
import { getUnleashClient, setUnleashClient } from '../modules/unleash';

const CONNECT_TIMEOUT = 10000;
const MAX_RETRIES = 5;

export function* handleInitFailed(currentRetry) {
if (currentRetry >= MAX_RETRIES) {
console.error('Max retries reached while trying to create the unleash-proxy client.');
const unleashClient = yield select((state) => state.unleashClient);
const unleashClient = getUnleashClient();

if (unleashClient) {
unleashClient.close();
yield put(setUnleashClient(null));
setUnleashClient(null);
}

// Even if unleash failed, we should allow the app to continue as it
Expand All @@ -68,7 +68,7 @@ export function* fetchTogglesRoutine() {
// Wait first so we don't double-check on initialization
yield delay(UNLEASH_POLLING_INTERVAL);

const unleashClient = yield select((state) => state.unleashClient);
const unleashClient = getUnleashClient();

try {
yield call(() => unleashClient.fetchToggles());
Expand Down Expand Up @@ -105,7 +105,7 @@ export function* monitorFeatureFlags(currentRetry = 0) {
try {
console.log('starting unleash with', options);
yield call(() => unleashClient.updateContext(options));
yield put(setUnleashClient(unleashClient));
setUnleashClient(unleashClient);

// Listeners should be set before unleashClient.start so we don't miss
// updates
Expand Down Expand Up @@ -142,7 +142,7 @@ export function* monitorFeatureFlags(currentRetry = 0) {
console.error('Error initializing unleash');
unleashClient.stop();

yield put(setUnleashClient(null));
setUnleashClient(null);

// Wait 500ms before retrying
yield delay(500);
Expand Down Expand Up @@ -209,7 +209,7 @@ function mapFeatureToggles(toggles) {
}

export function* handleToggleUpdate() {
const unleashClient = yield select((state) => state.unleashClient);
const unleashClient = getUnleashClient();
const featureTogglesInitialized = yield select((state) => state.featureTogglesInitialized);

if (!unleashClient || !featureTogglesInitialized) {
Expand Down
3 changes: 0 additions & 3 deletions src/sagas/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import {
setEnableAtomicSwap,
proposalListUpdated,
proposalFetchRequested,
walletResetSuccess,
reloadWalletRequested,
changeWalletState,
updateTxHistory,
Expand Down Expand Up @@ -737,8 +736,6 @@ export function* onWalletReset() {
yield call([wallet.storage, wallet.storage.cleanStorage], true, true);
}

yield put(walletResetSuccess());

yield put(setNavigateTo('/welcome'));
}

Expand Down
Loading