From 10dc242ab3d471ae9f254bd797786b0566ad1687 Mon Sep 17 00:00:00 2001 From: Zalmoxisus Date: Tue, 2 Aug 2016 21:56:42 +0300 Subject: [PATCH 1/2] Track path for children added in `splice` --- src/spy.js | 27 +++++++++++++++++++++++++-- src/utils.js | 6 +++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/spy.js b/src/spy.js index 39733d2..ae7a3e2 100644 --- a/src/spy.js +++ b/src/spy.js @@ -9,6 +9,7 @@ const onlyActions = {}; const filters = {}; const monitors = {}; const scheduled = []; +const children = {}; function configure(name, config) { if (!config) return; @@ -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])) { @@ -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) { @@ -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}` }); @@ -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(); diff --git a/src/utils.js b/src/utils.js index b15d57d..e896a8f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 }; } From c78345a02e54557cf7bcecf63ef31af342fdce3f Mon Sep 17 00:00:00 2001 From: Zalmoxisus Date: Tue, 2 Aug 2016 21:57:08 +0300 Subject: [PATCH 2/2] Update example --- examples/simple-todo/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/simple-todo/index.js b/examples/simple-todo/index.js index f7559b0..ba55baf 100644 --- a/examples/simple-todo/index.js +++ b/examples/simple-todo/index.js @@ -19,7 +19,7 @@ var todoFactory = function (title) { finished: false } ); - return remotedev(todo, { name: `Todo: ${title}` }); + return todo; }; var todoListFactory = function () { @@ -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() {