Skip to content

Commit

Permalink
Fix filters, actionCreators, sanitizer for each instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Nov 12, 2016
1 parent de25210 commit 4951ce7
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions app/worker/reduxAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,23 @@ function configureStore(next, subscriber, options) {
const instances = { /* id, name, store */ };

let lastAction;
let filters;
let isExcess;
let listenerAdded;
let actionCreators;
let stateSanitizer;
let actionSanitizer;
let locked;
let paused;

function generateId(id) {
return id || Math.random().toString(36).substr(2);
}

function init(options) {
if (options.filters) {
filters = options.filters;
}
if (options.actionCreators) {
actionCreators = () => getActionsArray(options.actionCreators);
}
stateSanitizer = options.stateSanitizer;
actionSanitizer = options.actionSanitizer;
}

function getLiftedState(store) {
return filterStagedActions(store.liftedStore.getState());
}

function relay(type, state, instance, action, nextActionId) {
if (filters && isFiltered(action, filters)) return;
const { filters, stateSanitizer, actionSanitizer } = instance;

if (filters && !Array.isArray(action) && isFiltered(action, filters)) return;
const message = {
type,
id: instance.id,
Expand All @@ -66,8 +53,8 @@ function relay(type, state, instance, action, nextActionId) {

function dispatchRemotely(action, id) {
try {
const { store, actionCreators } = instances[id];
const result = evalAction(action, actionCreators);
const { store } = instances[id];
store.dispatch(result);
} catch (e) {
relay('ERROR', e.message, instances[id]);
Expand Down Expand Up @@ -110,14 +97,16 @@ function start(instance) {
});
listenerAdded = true;
}

if (typeof actionCreators === 'function') actionCreators = actionCreators();
relay('STATE', getLiftedState(instance.store), instance, actionCreators);
const { store, actionCreators } = instance;
if (typeof actionCreators === 'function') {
instance.actionCreators = actionCreators();
}
relay('STATE', getLiftedState(store), instance, instance.actionCreators);
}

function checkForReducerErrors(liftedState, instance) {
if (liftedState.computedStates[liftedState.currentStateIndex].error) {
relay('STATE', filterStagedActions(liftedState, filters), instance);
relay('STATE', filterStagedActions(liftedState, instance.filters), instance);
return true;
}
return false;
Expand Down Expand Up @@ -147,21 +136,23 @@ function handleChange(state, liftedState, maxAge, instance) {
if (lastAction) lastAction = undefined;
else return;
}
relay('STATE', filterStagedActions(liftedState, filters), instance);
relay('STATE', filterStagedActions(liftedState, instance.filters), instance);
}
}

export default function devToolsEnhancer(options = {}) {
init(options);

const defaultName = global.require ? global.require('Platform').OS : 'default';
const {
name,
maxAge = 30,
shouldHotReload,
shouldRecordChanges,
shouldStartLocked,
pauseActionType,
pauseActionType = '@@PAUSED',
actionCreators,
filters,
actionSanitizer,
stateSanitizer,
} = options;
const id = generateId(options.instanceId);

Expand All @@ -172,14 +163,18 @@ export default function devToolsEnhancer(options = {}) {
shouldHotReload,
shouldRecordChanges,
shouldStartLocked,
pauseActionType: pauseActionType || '@@PAUSED',
pauseActionType,
}
)(reducer, initialState);

instances[id] = {
name: name || `${defaultName}-${id}`,
id,
store,
filters,
actionCreators: actionCreators && (() => getActionsArray(actionCreators)),
stateSanitizer,
actionSanitizer,
};

start(instances[id]);
Expand Down

0 comments on commit 4951ce7

Please sign in to comment.