Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track nested changes #3

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 25 additions & 27 deletions src/spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const onlyActions = {};
const filters = {};
const monitors = {};
const scheduled = [];
const remotedevId = '__Remotedev';

function configure(name, config) {
if (!config) return;
Expand All @@ -26,47 +27,44 @@ function init(store, config) {
monitors[name] = devTools;
}

function schedule(name, action) {
let toSend;
if (action && !isFiltered(action, filters[name])) {
toSend = () => { monitors[name].send(action, mobx.toJS(stores[name])); };
function schedule(action) {
function toSend(name) {
if (!action || isFiltered(action, filters[name])) return;
monitors[name].send(action, mobx.toJS(stores[name]));
}
scheduled.push(toSend);
}

function send() {
if (scheduled.length) {
const toSend = scheduled.pop();
if (toSend) toSend();
function send(name) {
while (scheduled.length) {
let toSend = scheduled.shift();
toSend(name);
}
}

export default function spy(store, config) {
init(store, config);
mobx.reaction(getName(store)+remotedevId, () => mobx.toJS(store), () => {});
if (isSpyEnabled) return;
isSpyEnabled = true;
let objName;

mobx.spy((change) => {
if (change.spyReportStart) {
if (change.type === 'reaction') return; // TODO: show reactions
objName = getName(change.object || change.target);
if (!stores[objName] || stores[objName].__isRemotedevAction) return;
if (change.type === 'action') {
const action = createAction(change.name);
if (change.arguments && change.arguments.length) action.arguments = change.arguments;
if (!onlyActions[objName]) {
schedule(objName, { ...action, type: `┏ ${action.type}` });
send();
schedule(objName, { ...action, type: `┗ ${action.type}` });
} else {
schedule(objName, action);
}
} else if (change.type && mobx.isObservable(change.object)) {
schedule(objName, !onlyActions[objName] && createAction(change.type, change));
}
} else if (change.spyReportEnd && stores[objName]) {
send();
if (!change.spyReportStart) return;
objName = getName(change.object || change.target);
if (stores[objName] && stores[objName].__isRemotedevAction) return;
if (change.fn && change.fn.__isRemotedevAction) return;
if (change.type === 'reaction') {
objName = objName.replace(remotedevId, '');
if (stores[objName]) send(objName);
return; // TODO: show reactions
}
if (change.type === 'action') {
const action = createAction(change.name);
if (change.arguments && change.arguments.length) action.arguments = change.arguments;
schedule(action);
} else if (change.type && mobx.isObservable(change.object)) {
schedule(!onlyActions[objName] && createAction(change.type, change));
}
});
}
6 changes: 4 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getName(obj) {
}

/* eslint-disable no-param-reassign */
export const setValue = mobx.action(function setValue(store, state) {
function setValueAction(store, state) {
store.__isRemotedevAction = true;
if (store.importState) {
store.importState(state);
Expand All @@ -46,5 +46,7 @@ export const setValue = mobx.action(function setValue(store, state) {
});
}
delete store.__isRemotedevAction;
});
}
setValueAction.__isRemotedevAction = true;
export const setValue = mobx.action(setValueAction);
/* eslint-enable */