Skip to content

Commit

Permalink
Fix circular deps. Comment code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cal-L committed Oct 22, 2021
1 parent f0d7f84 commit 2ebf09f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
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();
14 changes: 11 additions & 3 deletions app/reducers/engine/EngineService.ts → app/core/EngineService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import UntypedEngine from '../../core/Engine';
import { store } from '../../store';
import UntypedEngine from './Engine';
const Engine = UntypedEngine as any;

class EngineService {
private engineInitialized = false;

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

Engine.init(state);

Engine?.datamodel?.subscribe?.(() => {
Expand Down
17 changes: 0 additions & 17 deletions app/reducers/analytics/AnalyticsService.ts

This file was deleted.

21 changes: 12 additions & 9 deletions app/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
import rootReducer from '../reducers';
import { migrations, version } from './migrations';
import Logger from '../util/Logger';
import EngineService from '../reducers/engine/EngineService';
import AnalyticsService from '../reducers/analytics/AnalyticsService';
import EngineService from '../core/EngineService';
import AnalyticsService from '../core/AnalyticsService';

const TIMEOUT = 40000;

Expand Down Expand Up @@ -98,10 +98,13 @@ const persistConfig = {
const pReducer = persistReducer(persistConfig, rootReducer);

export const store = createStore(pReducer);
export const persistor = persistStore(store, null, () => {
const reduxState = store.getState?.();
const backgroundState = reduxState?.engine?.backgroundState || {};
const analyticsEnabled = reduxState?.analytics?.enabled;
EngineService.initalizeEngine(backgroundState);
AnalyticsService.initalizeAnalytics(analyticsEnabled);
});

/**
* Initialize services after persist is completed
*/
const onPersistComplete = () => {
EngineService.initalizeEngine(store);
AnalyticsService.initalizeAnalytics(store);
};

export const persistor = persistStore(store, null, onPersistComplete);

0 comments on commit 2ebf09f

Please sign in to comment.