Compare commits
1 commit
master
...
feat-delay
Author | SHA1 | Date | |
---|---|---|---|
|
b4346a9ca6 |
2 changed files with 68 additions and 58 deletions
11
dist/bundle.es.js
vendored
11
dist/bundle.es.js
vendored
|
@ -1850,9 +1850,10 @@ function doPreferenceGet(key, success, fail) {
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
|
const RUN_PREFERENCES_DELAY_MS = 2000;
|
||||||
const SHARED_PREFERENCE_VERSION = '0.1';
|
const SHARED_PREFERENCE_VERSION = '0.1';
|
||||||
let oldShared = {};
|
let oldShared = {};
|
||||||
|
let timeout;
|
||||||
const buildSharedStateMiddleware = (actions, sharedStateFilters, sharedStateCb) => ({
|
const buildSharedStateMiddleware = (actions, sharedStateFilters, sharedStateCb) => ({
|
||||||
getState,
|
getState,
|
||||||
dispatch
|
dispatch
|
||||||
|
@ -1863,9 +1864,11 @@ const buildSharedStateMiddleware = (actions, sharedStateFilters, sharedStateCb)
|
||||||
if (!actions.includes(action.type) || typeof action === 'function') {
|
if (!actions.includes(action.type) || typeof action === 'function') {
|
||||||
return next(action);
|
return next(action);
|
||||||
}
|
}
|
||||||
|
clearTimeout(timeout);
|
||||||
const actionResult = next(action);
|
const actionResult = next(action);
|
||||||
// Call `getState` after calling `next` to ensure the state has updated in response to the action
|
// Call `getState` after calling `next` to ensure the state has updated in response to the action
|
||||||
|
|
||||||
|
function runPreferences() {
|
||||||
const nextState = getState();
|
const nextState = getState();
|
||||||
const syncEnabled = nextState.settings && nextState.settings.clientSettings && nextState.settings.clientSettings.enable_sync;
|
const syncEnabled = nextState.settings && nextState.settings.clientSettings && nextState.settings.clientSettings.enable_sync;
|
||||||
const hasVerifiedEmail = nextState.user && nextState.user.user && nextState.user.user.has_verified_email;
|
const hasVerifiedEmail = nextState.user && nextState.user.user && nextState.user.user.has_verified_email;
|
||||||
|
@ -1893,8 +1896,10 @@ const buildSharedStateMiddleware = (actions, sharedStateFilters, sharedStateCb)
|
||||||
// Pass dispatch to the callback to consumers can dispatch actions in response to preference set
|
// Pass dispatch to the callback to consumers can dispatch actions in response to preference set
|
||||||
sharedStateCb({ dispatch, getState });
|
sharedStateCb({ dispatch, getState });
|
||||||
}
|
}
|
||||||
|
clearTimeout(timeout);
|
||||||
return actionResult;
|
return actionResult;
|
||||||
|
}
|
||||||
|
timeout = setTimeout(runPreferences, RUN_PREFERENCES_DELAY_MS);
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
import isEqual from 'util/deep-equal';
|
import isEqual from 'util/deep-equal';
|
||||||
import { doPreferenceSet } from 'redux/actions/sync';
|
import { doPreferenceSet } from 'redux/actions/sync';
|
||||||
|
|
||||||
|
const RUN_PREFERENCES_DELAY_MS = 2000;
|
||||||
const SHARED_PREFERENCE_VERSION = '0.1';
|
const SHARED_PREFERENCE_VERSION = '0.1';
|
||||||
let oldShared = {};
|
let oldShared = {};
|
||||||
|
let timeout;
|
||||||
export const buildSharedStateMiddleware = (
|
export const buildSharedStateMiddleware = (
|
||||||
actions: Array<string>,
|
actions: Array<string>,
|
||||||
sharedStateFilters: {},
|
sharedStateFilters: {},
|
||||||
|
@ -22,9 +23,11 @@ export const buildSharedStateMiddleware = (
|
||||||
if (!actions.includes(action.type) || typeof action === 'function') {
|
if (!actions.includes(action.type) || typeof action === 'function') {
|
||||||
return next(action);
|
return next(action);
|
||||||
}
|
}
|
||||||
|
clearTimeout(timeout);
|
||||||
const actionResult = next(action);
|
const actionResult = next(action);
|
||||||
// Call `getState` after calling `next` to ensure the state has updated in response to the action
|
// Call `getState` after calling `next` to ensure the state has updated in response to the action
|
||||||
|
|
||||||
|
function runPreferences() {
|
||||||
const nextState: { user: any, settings: any } = getState();
|
const nextState: { user: any, settings: any } = getState();
|
||||||
const syncEnabled =
|
const syncEnabled =
|
||||||
nextState.settings &&
|
nextState.settings &&
|
||||||
|
@ -56,6 +59,8 @@ export const buildSharedStateMiddleware = (
|
||||||
// Pass dispatch to the callback to consumers can dispatch actions in response to preference set
|
// Pass dispatch to the callback to consumers can dispatch actions in response to preference set
|
||||||
sharedStateCb({ dispatch, getState });
|
sharedStateCb({ dispatch, getState });
|
||||||
}
|
}
|
||||||
|
clearTimeout(timeout);
|
||||||
return actionResult;
|
return actionResult;
|
||||||
|
}
|
||||||
|
timeout = setTimeout(runPreferences, RUN_PREFERENCES_DELAY_MS);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue