@@ -50,56 +50,54 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
5050 typeof component . __componentId === 'undefined'
5151 ? nextComponentId ++
5252 : component . __componentId
53- const [ overmindState , setOvermindState ] = useState ( null )
5453 const trackId = overmind . trackState ( )
54+ const [ overmindState , setOvermindState ] = useState ( ( ) => {
55+ const paths = overmind . clearTrackState ( trackId )
56+ componentInstanceId ++
57+ overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
58+ componentId : component . __componentId ,
59+ componentInstanceId,
60+ name,
61+ paths : Array . from ( paths ) ,
62+ } )
63+ return {
64+ componentInstanceId,
65+ listener : overmind . addFlushListener ( paths , ( flushId ) => {
66+ overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
67+ componentId : component . __componentId ,
68+ componentInstanceId,
69+ name,
70+ flushId,
71+ paths : Array . from ( paths ) ,
72+ } )
73+ setOvermindState ( ( state ) => state )
74+ } ) ,
75+ }
76+ } )
77+
5578 useEffect ( ( ) => {
5679 const paths = overmind . clearTrackState ( trackId )
57- if ( overmindState ) {
80+ if ( paths ) {
5881 overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
5982 componentId : component . __componentId ,
6083 componentInstanceId : overmindState . componentInstanceId ,
6184 name,
6285 paths : Array . from ( paths ) ,
6386 } )
6487 overmindState . listener . update ( paths )
65- } else {
66- const thisComponentInstanceId = componentInstanceId ++
67- setOvermindState ( {
68- componentInstanceId : thisComponentInstanceId ,
69- listener : overmind . addFlushListener ( paths , ( flushId ) => {
70- overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
71- componentId : component . __componentId ,
72- componentInstanceId : thisComponentInstanceId ,
73- name,
74- flushId,
75- paths : Array . from ( paths ) ,
76- } )
77- setOvermindState ( ( state ) => state )
78- } ) ,
79- } )
80- overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
88+ }
89+ } )
90+ useEffect ( ( ) => {
91+ return ( ) => {
92+ overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
8193 componentId : component . __componentId ,
82- componentInstanceId : thisComponentInstanceId ,
94+ componentInstanceId : overmindState . componentInstanceId ,
8395 name,
84- paths : Array . from ( paths ) ,
8596 } )
97+ overmindState . listener . dispose ( )
8698 }
87- } )
88- useEffect (
89- ( ) => {
90- return ( ) => {
91- if ( overmindState ) {
92- overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
93- componentId : component . __componentId ,
94- componentInstanceId : overmindState . componentInstanceId ,
95- name,
96- } )
97- overmindState . listener . dispose ( )
98- }
99- }
100- } ,
101- [ component . __componentId ]
102- )
99+ } , [ ] )
100+
103101 return overmind
104102 }
105103}
0 commit comments