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

[RFC] Track nested changes with full path #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions examples/simple-todo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var todoFactory = function (title) {
finished: false
}
);
return remotedev(todo, { name: `Todo: ${title}` });
return todo;
};

var todoListFactory = function () {
Expand All @@ -41,7 +41,7 @@ var todoListFactory = function () {
}
});

return remotedev(todoList, { name: 'Todo list' });
return remotedev(todoList, { name: 'Todo list', onlyActions: true });
};

var TodoListView = mobxReact.observer(function TodoListView() {
Expand Down
27 changes: 25 additions & 2 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 children = {};

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

function track(change) {
if (change.type === 'splice') {
const path = mobx.extras.getDebugName(change.object).split('.');
const parent = path.shift();
const { added } = change;
if (Array.isArray(added)) {
added.forEach((el, i) => {
children[mobx.extras.getDebugName(el)] = {
parent,
path: [...path, i].join('.')
};
});
}
}
}

function schedule(name, action) {
let toSend;
if (action && !isFiltered(action, filters[name])) {
Expand All @@ -46,6 +63,7 @@ export default function spy(store, config) {
if (isSpyEnabled) return;
isSpyEnabled = true;
let objName;
let objPath;

mobx.spy((change) => {
if (change.spyReportStart) {
Expand All @@ -55,12 +73,16 @@ export default function spy(store, config) {
schedule(objName);
return;
}
if (!stores[objName] && children[objName]) {
objPath = children[objName].path;
objName = children[objName].parent;
}
if (!stores[objName] || stores[objName].__isRemotedevAction) {
schedule(objName);
return;
}
if (change.type === 'action') {
const action = createAction(change.name);
const action = createAction(change.name, objPath);
if (change.arguments && change.arguments.length) action.arguments = change.arguments;
if (!onlyActions[objName]) {
schedule(objName, { ...action, type: `┏ ${action.type}` });
Expand All @@ -70,7 +92,8 @@ export default function spy(store, config) {
schedule(objName, action);
}
} else if (change.type && mobx.isObservable(change.object)) {
schedule(objName, !onlyActions[objName] && createAction(change.type, change));
track(change);
schedule(objName, !onlyActions[objName] && createAction(change.type, objPath, change));
}
} else if (change.spyReportEnd) {
send();
Expand Down
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ const getPayload = (change) => {
};
};

export function createAction(name, change) {
export function createAction(objName, objPath, change) {
let name;
if (objPath) name = `${objPath}.${objName}`;
else name = objName;

if (!change) { // is action
return { type: name };
}
Expand Down