@@ -50,47 +50,58 @@ export const createHook = <A extends Overmind<Configuration>>(overmind: A) => {
5050 typeof component . __componentId === 'undefined'
5151 ? nextComponentId ++
5252 : component . __componentId
53- const [ thisComponentInstanceId ] = useState ( ( ) => componentInstanceId ++ )
54- const [ _ , forceUpdate ] = useState ( null )
55-
53+ const [ overmindState , setOvermindState ] = useState < any > ( null )
54+ const trackId = overmind . trackState ( )
5655 useEffect ( ( ) => {
57- overmind . eventHub . emitAsync ( EventType . COMPONENT_ADD , {
58- componentId : component . __componentId ,
59- componentInstanceId : thisComponentInstanceId ,
60- name,
61- paths : [ ] ,
62- } )
63- return ( ) => {
64- overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
56+ const paths = overmind . clearTrackState ( trackId )
57+ if ( overmindState ) {
58+ overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
59+ componentId : component . __componentId ,
60+ componentInstanceId : overmindState . componentInstanceId ,
61+ name,
62+ paths : Array . from ( paths ) ,
63+ } )
64+ 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 , {
6581 componentId : component . __componentId ,
6682 componentInstanceId : thisComponentInstanceId ,
6783 name,
84+ paths : Array . from ( paths ) ,
6885 } )
6986 }
70- } , [ ] )
71-
72- const trackId = overmind . trackState ( )
73-
74- useEffect ( ( ) => {
75- const paths = overmind . clearTrackState ( trackId )
87+ } )
88+ useEffect (
89+ ( ) => {
90+ return ( ) => {
91+ if ( ! overmindState ) {
92+ return
93+ }
7694
77- if ( paths && paths . size ) {
78- const listener = overmind . addFlushListener ( paths , ( flushId ) => {
79- forceUpdate ( flushId )
80- overmind . eventHub . emitAsync ( EventType . COMPONENT_UPDATE , {
95+ overmind . eventHub . emitAsync ( EventType . COMPONENT_REMOVE , {
8196 componentId : component . __componentId ,
82- componentInstanceId : thisComponentInstanceId ,
97+ componentInstanceId : overmindState . componentInstanceId ,
8398 name,
84- flushId,
85- paths : Array . from ( paths ) ,
8699 } )
87- } )
88- return ( ) => {
89- listener . dispose ( )
100+ overmindState . listener . dispose ( )
90101 }
91- }
92- } )
93-
102+ } ,
103+ [ overmindState ]
104+ )
94105 return overmind
95106 }
96107}
0 commit comments