delay preference set two seconds

This commit is contained in:
zeppi 2021-04-24 21:23:34 -04:00
parent 7e17344683
commit b4346a9ca6
2 changed files with 68 additions and 58 deletions

59
dist/bundle.es.js vendored
View file

@ -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,38 +1864,42 @@ 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
const nextState = getState();
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 preferenceKey = syncEnabled && hasVerifiedEmail ? 'shared' : 'local';
const shared = {};
Object.keys(sharedStateFilters).forEach(key => { function runPreferences() {
const filter = sharedStateFilters[key]; const nextState = getState();
const { source, property, transform } = filter; const syncEnabled = nextState.settings && nextState.settings.clientSettings && nextState.settings.clientSettings.enable_sync;
let value = nextState[source][property]; const hasVerifiedEmail = nextState.user && nextState.user.user && nextState.user.user.has_verified_email;
if (transform) { const preferenceKey = syncEnabled && hasVerifiedEmail ? 'shared' : 'local';
value = transform(value); const shared = {};
Object.keys(sharedStateFilters).forEach(key => {
const filter = sharedStateFilters[key];
const { source, property, transform } = filter;
let value = nextState[source][property];
if (transform) {
value = transform(value);
}
shared[key] = value;
});
if (!isEqual(oldShared, shared)) {
// only update if the preference changed from last call in the same session
oldShared = shared;
dispatch(doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION));
} }
shared[key] = value; if (sharedStateCb) {
}); // Pass dispatch to the callback to consumers can dispatch actions in response to preference set
sharedStateCb({ dispatch, getState });
if (!isEqual(oldShared, shared)) { }
// only update if the preference changed from last call in the same session clearTimeout(timeout);
oldShared = shared; return actionResult;
dispatch(doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION));
} }
timeout = setTimeout(runPreferences, RUN_PREFERENCES_DELAY_MS);
if (sharedStateCb) {
// Pass dispatch to the callback to consumers can dispatch actions in response to preference set
sharedStateCb({ dispatch, getState });
}
return actionResult;
}; };
// //

View file

@ -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,40 +23,44 @@ 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
const nextState: { user: any, settings: any } = getState();
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 preferenceKey = syncEnabled && hasVerifiedEmail ? 'shared' : 'local';
const shared = {};
Object.keys(sharedStateFilters).forEach(key => { function runPreferences() {
const filter = sharedStateFilters[key]; const nextState: { user: any, settings: any } = getState();
const { source, property, transform } = filter; const syncEnabled =
let value = nextState[source][property]; nextState.settings &&
if (transform) { nextState.settings.clientSettings &&
value = transform(value); nextState.settings.clientSettings.enable_sync;
const hasVerifiedEmail =
nextState.user && nextState.user.user && nextState.user.user.has_verified_email;
const preferenceKey = syncEnabled && hasVerifiedEmail ? 'shared' : 'local';
const shared = {};
Object.keys(sharedStateFilters).forEach(key => {
const filter = sharedStateFilters[key];
const { source, property, transform } = filter;
let value = nextState[source][property];
if (transform) {
value = transform(value);
}
shared[key] = value;
});
if (!isEqual(oldShared, shared)) {
// only update if the preference changed from last call in the same session
oldShared = shared;
dispatch(doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION));
} }
shared[key] = value; if (sharedStateCb) {
}); // Pass dispatch to the callback to consumers can dispatch actions in response to preference set
sharedStateCb({ dispatch, getState });
if (!isEqual(oldShared, shared)) { }
// only update if the preference changed from last call in the same session clearTimeout(timeout);
oldShared = shared; return actionResult;
dispatch(doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION));
} }
timeout = setTimeout(runPreferences, RUN_PREFERENCES_DELAY_MS);
if (sharedStateCb) {
// Pass dispatch to the callback to consumers can dispatch actions in response to preference set
sharedStateCb({ dispatch, getState });
}
return actionResult;
}; };