Skip to content

Commit

Permalink
fix(overmind-react): improve passing updates to devtools using hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Jan 17, 2019
1 parent 96963d2 commit f7b9f3f
Showing 1 changed file with 22 additions and 33 deletions.
55 changes: 22 additions & 33 deletions packages/node_modules/overmind-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,48 +95,37 @@ export const createHook = <Config extends Configuration>(
[]
)
} else {
const [debugging] = useState<any>(() => ({
isFirstRender: true,
currentFlushId: 0,
componentInstanceId: currentComponentInstanceId++,
}))
const [componentInstanceId] = useState<any>(
() => 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<string>),
})
}
})

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 {
Expand Down

0 comments on commit f7b9f3f

Please sign in to comment.