Skip to content

Commit

Permalink
Merge pull request #6 from antitoxic/feature/global-default
Browse files Browse the repository at this point in the history
Enable marking store as global, a.k.a - fallback for unknown changes.
  • Loading branch information
zalmoxisus authored Aug 24, 2016
2 parents ace49b7 + a536521 commit e5e4a5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isFiltered } from './filters';
import { dispatchMonitorAction } from './monitorActions';

let isSpyEnabled = false;
let fallbackStoreName;
const stores = {};
const onlyActions = {};
const filters = {};
Expand All @@ -15,6 +16,10 @@ function configure(name, config = {}) {
if (typeof config.onlyActions === 'undefined') onlyActions[name] = mobx.useStrict();
else onlyActions[name] = config.onlyActions;
if (config.filters) filters[name] = config.filters;
if (config.global) {
if (fallbackStoreName) throw Error("You've already defined a global store");
fallbackStoreName = name;
}
}

function init(store, config) {
Expand Down Expand Up @@ -56,10 +61,15 @@ export default function spy(store, config) {
schedule(objName);
return;
}
if (!stores[objName]) objName = fallbackStoreName;
if (!stores[objName] || stores[objName].__isRemotedevAction) {
schedule(objName);
return;
}
if (change.fn && change.fn.__isRemotedevAction) {
schedule(objName);
return;
}
if (change.type === 'action') {
const action = createAction(change.name);
if (change.arguments && change.arguments.length) action.arguments = change.arguments;
Expand Down
6 changes: 4 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const silently = (fn, store) => {
delete store.__isRemotedevAction;
};

export const setValue = mobx.action('@@remotedev', (store, state) => {
function setValueAction(store, state) {
silently(() => {
if (store.importState) {
store.importState(state);
Expand All @@ -53,5 +53,7 @@ export const setValue = mobx.action('@@remotedev', (store, state) => {
}
}, store);
return state;
});
}
setValueAction.__isRemotedevAction = true;
export const setValue = mobx.action('@@remotedev', setValueAction);
/* eslint-enable */

0 comments on commit e5e4a5d

Please sign in to comment.