Skip to content

Commit

Permalink
Fix reduxNativeDevToolsCompose
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Nov 3, 2016
1 parent 220c2f8 commit 2e1c246
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions app/containers/ReduxDevTools/reduxNativeDevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default function devToolsEnhancer(options = {}) {
shouldRecordChanges,
shouldStartLocked,
} = options;
const id = generateId();
const id = generateId(options.instanceId);

return next => (reducer, initialState) => {
const store = configureStore(
Expand All @@ -180,19 +180,22 @@ export default function devToolsEnhancer(options = {}) {
};
}

function preEnhancer(createStore) {
return (reducer, preloadedState, enhancer) => {
const store = createStore(reducer, preloadedState, enhancer);
const preEnhancer = instanceId => next =>
(reducer, initialState, enhancer) => {
const store = next(reducer, initialState, enhancer);

if (instances[instanceId]) {
instances[instanceId].store = store;
}
return {
...store,
dispatch: (action) => (
locked ? action : store.dispatch(action)
),
};
};
}

devToolsEnhancer.updateStore = (newStore, name) => {
devToolsEnhancer.updateStore = (newStore, instanceId) => {
console.warn(
'`reduxNativeDevTools.updateStore` is deprecated use `reduxNativeDevToolsCompose` instead:',
'https://github.com/jhen0409/react-native-debugger#advanced-store-setup'
Expand All @@ -201,26 +204,27 @@ devToolsEnhancer.updateStore = (newStore, name) => {
const keys = Object.keys(instances);
if (!keys.length) return;

if (keys.length > 1 && !name) {
if (keys.length > 1 && !instanceId) {
console.warn(
'You have multiple stores,',
'please provide `name` argument (`updateStore(store, name)`)'
'please provide `instanceId` argument (`updateStore(store, instanceId)`)'
);
} else {
instances[keys[0]].store = newStore;
}
if (name) {
const index = keys.findIndex(key => instances[key].name === name);
const instance = instances[keys[index]];
if (instanceId) {
const instance = instances[instanceId];
if (!instance) return;
instance.store = newStore;
} else {
instances[keys[0]].store = newStore;
}
};

const compose = (options) => (...funcs) => (...args) =>
[preEnhancer, ...funcs].reduceRight(
(composed, f) => f(composed), devToolsEnhancer(options)(...args)
const compose = options => (...funcs) => (...args) => {
const instanceId = generateId(options.instanceId);
return [preEnhancer(instanceId), ...funcs].reduceRight(
(composed, f) => f(composed), devToolsEnhancer({ ...options, instanceId })(...args)
);
};

export function composeWithDevTools(...funcs) {
if (funcs.length === 0) {
Expand Down

0 comments on commit 2e1c246

Please sign in to comment.