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

Improvement/3308 isolate persisted data #3325

Merged
merged 6 commits into from
Oct 22, 2021
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
24 changes: 24 additions & 0 deletions app/core/AnalyticsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Analytics from './Analytics';

class AnalyticsService {
/**
* Initializer for the AnalyticsService
*
* @param store - Redux store
*/
initalizeAnalytics = (store: any) => {
const reduxState = store.getState?.();
const analyticsEnabled = reduxState?.analytics?.enabled || true;

Analytics.init(analyticsEnabled);

Analytics.subscribe(() => {
store.dispatch({ type: 'UPDATE_ANALYTICS_STATE' });
});
};
}

/**
* AnalyticsService class for initializing and subscribing to analytics
*/
export default new AnalyticsService();
106 changes: 106 additions & 0 deletions app/core/EngineService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import UntypedEngine from './Engine';

class EngineService {
private engineInitialized = false;

/**
* Initializer for the EngineService
*
* @param store - Redux store
*/
initalizeEngine = (store: any) => {
const reduxState = store.getState?.();
const state = reduxState?.engine?.backgroundState || {};
const Engine = UntypedEngine as any;

Engine.init(state);

Engine?.datamodel?.subscribe?.(() => {
if (!this.engineInitialized) {
store.dispatch({ type: 'INIT_BG_STATE' });
this.engineInitialized = true;
}
});

Engine.context.AccountTrackerController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AccountTrackerController' });
});

Engine.context.AddressBookController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AddressBookController' });
});

Engine.context.AssetsContractController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AssetsContractController' });
});

Engine.context.CollectiblesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'CollectiblesController' });
});

Engine.context.TokensController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokensController' });
});

Engine.context.AssetsDetectionController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AssetsDetectionController' });
});

Engine.controllerMessenger.subscribe(`${Engine.context.TokenListController.name}:stateChange`, () => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokenListController' });
});

Engine.controllerMessenger.subscribe(`${Engine.context.CurrencyRateController.name}:stateChange`, () => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'CurrencyRateController' });
});

Engine.context.KeyringController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'KeyringController' });
});

Engine.context.PersonalMessageManager.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AccountTrackerController' });
});

Engine.context.NetworkController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'NetworkController' });
});

Engine.context.PhishingController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'PhishingController' });
});

Engine.context.PreferencesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'PreferencesController' });
});

Engine.context.TokenBalancesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokenBalancesController' });
});

Engine.context.TokenRatesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokenRatesController' });
});

Engine.context.TransactionController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TransactionController' });
});

Engine.context.TypedMessageManager.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TypedMessageManager' });
});

Engine.context.SwapsController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'SwapsController' });
});

Engine.controllerMessenger.subscribe(`${Engine.context.GasFeeController.name}:stateChange`, () => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'GasFeeController' });
});
};
}

/**
* EngineService class used for initializing and subscribing to the engine controllers
*/
export default new EngineService();
16 changes: 0 additions & 16 deletions app/reducers/analytics/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { REHYDRATE } from 'redux-persist';
import Analytics from '../../core/Analytics';
import { store } from '../../store';

const initialState = {
enabled: false,
};

function initalizeAnalytics(enabled = true) {
Analytics.init(enabled);

Analytics.subscribe(() => {
store.dispatch({ type: 'UPDATE_ANALYTICS_STATE' });
});
}

const analyticsReducer = (state = initialState, action) => {
switch (action.type) {
case REHYDRATE:
initalizeAnalytics(action.payload?.analytics?.enabled);
if (action.payload?.analytics) {
return { ...state, ...action.payload.analytics };
}
return state;
case 'UPDATE_ANALYTICS_STATE':
return { enabled: Analytics.getEnabled() };
default:
Expand Down
6 changes: 0 additions & 6 deletions app/reducers/bookmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { REHYDRATE } from 'redux-persist';
const bookmarksReducer = (state = [], action) => {
switch (action.type) {
case REHYDRATE:
if (action.payload && action.payload.bookmarks) {
return [...state, ...action.payload.bookmarks];
}
return state;
case 'ADD_BOOKMARK':
return [...state, action.bookmark];
case 'REMOVE_BOOKMARK':
Expand Down
7 changes: 0 additions & 7 deletions app/reducers/browser/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { REHYDRATE } from 'redux-persist';

const initialState = {
history: [],
whitelist: [],
Expand All @@ -8,11 +6,6 @@ const initialState = {
};
const browserReducer = (state = initialState, action) => {
switch (action.type) {
case REHYDRATE:
if (action.payload && action.payload.browser) {
return { ...state, ...action.payload.browser };
}
return state;
case 'ADD_TO_BROWSER_HISTORY':
return {
...state,
Expand Down
98 changes: 0 additions & 98 deletions app/reducers/engine/index.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,11 @@
import { REHYDRATE } from 'redux-persist';
import Engine from '../../core/Engine';
import { store } from '../../store';

const initialState = {
backgroundState: {},
};

let engineInitialized = false;

function initalizeEngine(state = {}) {
Engine.init(state);

Engine.datamodel &&
Engine.datamodel.subscribe(() => {
if (!engineInitialized) {
store.dispatch({ type: 'INIT_BG_STATE' });
engineInitialized = true;
}
});

Engine.context.AccountTrackerController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AccountTrackerController' });
});

Engine.context.AddressBookController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AddressBookController' });
});

Engine.context.AssetsContractController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AssetsContractController' });
});

Engine.context.CollectiblesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'CollectiblesController' });
});

Engine.context.TokensController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokensController' });
});

Engine.context.AssetsDetectionController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AssetsDetectionController' });
});

Engine.controllerMessenger.subscribe(`${Engine.context.TokenListController.name}:stateChange`, () => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokenListController' });
});

Engine.controllerMessenger.subscribe(`${Engine.context.CurrencyRateController.name}:stateChange`, () => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'CurrencyRateController' });
});

Engine.context.KeyringController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'KeyringController' });
});

Engine.context.PersonalMessageManager.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'AccountTrackerController' });
});

Engine.context.NetworkController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'NetworkController' });
});

Engine.context.PhishingController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'PhishingController' });
});

Engine.context.PreferencesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'PreferencesController' });
});

Engine.context.TokenBalancesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokenBalancesController' });
});

Engine.context.TokenRatesController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TokenRatesController' });
});

Engine.context.TransactionController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TransactionController' });
});

Engine.context.TypedMessageManager.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'TypedMessageManager' });
});

Engine.context.SwapsController.subscribe(() => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'SwapsController' });
});

Engine.controllerMessenger.subscribe(`${Engine.context.GasFeeController.name}:stateChange`, () => {
store.dispatch({ type: 'UPDATE_BG_STATE', key: 'GasFeeController' });
});
}

const engineReducer = (state = initialState, action) => {
switch (action.type) {
case REHYDRATE:
initalizeEngine(action.payload && action.payload.engine && action.payload.engine.backgroundState);
if (action.payload && action.payload.engine) {
return { ...state, ...action.payload.engine };
}
return state;
case 'INIT_BG_STATE':
return { backgroundState: Engine.state };
case 'UPDATE_BG_STATE': {
Expand Down
7 changes: 0 additions & 7 deletions app/reducers/onboarding/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { REHYDRATE } from 'redux-persist';

const initialState = {
events: [],
};
Expand All @@ -10,11 +8,6 @@ const initialState = {
*/
const onboardingReducer = (state = initialState, action) => {
switch (action.type) {
case REHYDRATE:
if (action.payload && action.payload.onboarding) {
return { ...state, ...action.payload.onboarding };
}
return state;
case 'SAVE_EVENT':
return {
...state,
Expand Down
7 changes: 0 additions & 7 deletions app/reducers/privacy/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { REHYDRATE } from 'redux-persist';

const initialState = {
approvedHosts: {},
privacyMode: true,
Expand All @@ -9,11 +7,6 @@ const initialState = {
const privacyReducer = (state = initialState, action) => {
const newHosts = { ...state.approvedHosts };
switch (action.type) {
case REHYDRATE:
if (action.payload && action.payload.privacy) {
return { ...state, ...action.payload.privacy };
}
return state;
case 'APPROVE_HOST':
return {
...state,
Expand Down
6 changes: 0 additions & 6 deletions app/reducers/recents/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { REHYDRATE } from 'redux-persist';
const recentsLength = 3;
const recentsReducer = (state = [], action) => {
switch (action.type) {
case REHYDRATE:
if (action.payload?.recents) {
return [...state, ...action.payload.recents];
}
return state;
case 'ADD_RECENT':
if (action.recent && !state.includes(action.recent)) {
const recents = [action.recent, ...state];
Expand Down
6 changes: 0 additions & 6 deletions app/reducers/settings/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { REHYDRATE } from 'redux-persist';
import AppConstants from '../../core/AppConstants';

const initialState = {
Expand All @@ -11,11 +10,6 @@ const initialState = {

const settingsReducer = (state = initialState, action) => {
switch (action.type) {
case REHYDRATE:
if (action.payload && action.payload.settings) {
return { ...state, ...action.payload.settings };
}
return state;
case 'SET_SEARCH_ENGINE':
return {
...state,
Expand Down
7 changes: 0 additions & 7 deletions app/reducers/user/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { REHYDRATE } from 'redux-persist';

const initialState = {
loadingMsg: '',
loadingSet: false,
Expand All @@ -12,11 +10,6 @@ const initialState = {

const userReducer = (state = initialState, action) => {
switch (action.type) {
case REHYDRATE:
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
if (action.payload && action.payload.user) {
return { ...state, ...action.payload.user };
}
return state;
case 'LOADING_SET':
return {
...state,
Expand Down
Loading