From f7b9f3f95da8d482678fac404f9864a2d2b30dac Mon Sep 17 00:00:00 2001 From: christianalfoni Date: Thu, 17 Jan 2019 22:15:33 +0100 Subject: [PATCH] fix(overmind-react): improve passing updates to devtools using hooks --- .../node_modules/overmind-react/src/index.ts | 55 ++++++++----------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/packages/node_modules/overmind-react/src/index.ts b/packages/node_modules/overmind-react/src/index.ts index 2faa2d2c..bef9e492 100644 --- a/packages/node_modules/overmind-react/src/index.ts +++ b/packages/node_modules/overmind-react/src/index.ts @@ -95,48 +95,37 @@ export const createHook = ( [] ) } else { - const [debugging] = useState(() => ({ - isFirstRender: true, - currentFlushId: 0, - componentInstanceId: currentComponentInstanceId++, - })) + const [componentInstanceId] = useState( + () => currentComponentInstanceId++ + ) - tree.track((mutations, paths, flushId) => { - debugging.currentFlushId = flushId + tree.track((_, paths, flushId) => { updateComponent((state) => state) + overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, { + componentId: component.__componentId, + componentInstanceId, + name, + flushId, + paths, + }) }) - useLayoutEffect(() => { - if (debugging.isFirstRender) { - overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, { - componentId: component.__componentId, - componentInstanceId: debugging.componentInstanceId, - name, - paths: Array.from(tree.pathDependencies) as any, - }) - debugging.isFirstRender = false - } else { - overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, { - componentId: component.__componentId, - componentInstanceId: debugging.componentInstanceId, - name, - flushId: debugging.currentFlushId, - paths: Array.from(tree.pathDependencies as Set), - }) - } - }) - - useEffect( - () => () => { + useEffect(() => { + overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, { + componentId: component.__componentId, + componentInstanceId, + name, + paths: Array.from(tree.pathDependencies) as any, + }) + return () => { ;(overmind as any).proxyStateTree.disposeTree(tree) overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, { componentId: component.__componentId, - componentInstanceId: debugging.componentInstanceId, + componentInstanceId, name, }) - }, - [] - ) + } + }, []) } return {