Skip to content

Commit ec69c18

Browse files
refactor(overmind-react): improve hook work in production
1 parent f5a5f1d commit ec69c18

File tree

1 file changed

+57
-41
lines changed
  • packages/node_modules/overmind-react/src

1 file changed

+57
-41
lines changed

packages/node_modules/overmind-react/src/index.ts

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from 'react'
1616
import { IMutation } from 'proxy-state-tree'
1717

18+
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
19+
1820
export type IReactComponent<P = any> =
1921
| StatelessComponent<P>
2022
| ComponentClass<P>
@@ -66,48 +68,62 @@ export const createHook = <A extends Overmind<Configuration>>(
6668
const [tree, updateComponent] = useState<any>(() =>
6769
(overmind as any).proxyStateTree.getTrackStateTree()
6870
)
69-
const [debugging] = useState<any>(() => ({
70-
isFirstRender: true,
71-
currentFlushId: 0,
72-
componentInstanceId: currentComponentInstanceId++,
73-
}))
74-
75-
tree.track((mutations, paths, flushId) => {
76-
debugging.currentFlushId = flushId
77-
updateComponent((state) => state)
78-
})
79-
80-
useLayoutEffect(() => {
81-
if (debugging.isFirstRender) {
82-
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
83-
componentId: component.__componentId,
84-
componentInstanceId: debugging.componentInstanceId,
85-
name,
86-
paths: Array.from(tree.pathDependencies) as any,
87-
})
88-
debugging.isFirstRender = false
89-
} else {
90-
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
91-
componentId: component.__componentId,
92-
componentInstanceId: debugging.componentInstanceId,
93-
name,
94-
flushId: debugging.currentFlushId,
95-
paths: Array.from(tree.pathDependencies as Set<string>),
96-
})
97-
}
98-
})
9971

100-
useEffect(
101-
() => () => {
102-
;(overmind as any).proxyStateTree.disposeTree(tree)
103-
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
104-
componentId: component.__componentId,
105-
componentInstanceId: debugging.componentInstanceId,
106-
name,
107-
})
108-
},
109-
[]
110-
)
72+
if (IS_PRODUCTION) {
73+
tree.track(() => {
74+
updateComponent((state) => state)
75+
})
76+
77+
useEffect(
78+
() => () => {
79+
;(overmind as any).proxyStateTree.disposeTree(tree)
80+
},
81+
[]
82+
)
83+
} else {
84+
const [debugging] = useState<any>(() => ({
85+
isFirstRender: true,
86+
currentFlushId: 0,
87+
componentInstanceId: currentComponentInstanceId++,
88+
}))
89+
90+
tree.track((mutations, paths, flushId) => {
91+
debugging.currentFlushId = flushId
92+
updateComponent((state) => state)
93+
})
94+
95+
useLayoutEffect(() => {
96+
if (debugging.isFirstRender) {
97+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
98+
componentId: component.__componentId,
99+
componentInstanceId: debugging.componentInstanceId,
100+
name,
101+
paths: Array.from(tree.pathDependencies) as any,
102+
})
103+
debugging.isFirstRender = false
104+
} else {
105+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
106+
componentId: component.__componentId,
107+
componentInstanceId: debugging.componentInstanceId,
108+
name,
109+
flushId: debugging.currentFlushId,
110+
paths: Array.from(tree.pathDependencies as Set<string>),
111+
})
112+
}
113+
})
114+
115+
useEffect(
116+
() => () => {
117+
;(overmind as any).proxyStateTree.disposeTree(tree)
118+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
119+
componentId: component.__componentId,
120+
componentInstanceId: debugging.componentInstanceId,
121+
name,
122+
})
123+
},
124+
[]
125+
)
126+
}
111127

112128
return {
113129
state: tree.state,

0 commit comments

Comments
 (0)