@@ -34,6 +34,7 @@ export interface IConnect<Config extends IConfiguration> {
3434 effects : Overmind < Config > [ 'effects' ]
3535 addMutationListener : Overmind < Config > [ 'addMutationListener' ]
3636 reaction : Overmind < Config > [ 'reaction' ]
37+ suspend : < T > ( cb : ( ) => T ) => T
3738 }
3839}
3940
@@ -108,7 +109,7 @@ export const createHook = <Config extends IConfiguration>(): (() => {
108109 reaction : overmind . reaction ,
109110 }
110111 }
111- const { current : tree } = react . useRef < any > (
112+ const [ tree ] = react . useState < any > ( ( ) =>
112113 ( overmind as any ) . proxyStateTree . getTrackStateTree ( )
113114 )
114115
@@ -170,12 +171,36 @@ export const createHook = <Config extends IConfiguration>(): (() => {
170171 tree . track ( forceRerender )
171172 }
172173
174+ const suspend = ( cb ) => {
175+ const value = cb ( )
176+
177+ if ( value === null || value === undefined ) {
178+ ; ( overmind as any ) . proxyStateTree . disposeTree ( tree )
179+
180+ throw new Promise ( ( resolve ) => {
181+ const dispose = overmind . addFlushListener ( ( ) => {
182+ const newValue = cb ( )
183+
184+ if ( newValue === null || newValue === undefined ) {
185+ return
186+ }
187+
188+ dispose ( )
189+ resolve ( )
190+ } )
191+ } )
192+ }
193+
194+ return value
195+ }
196+
173197 return {
174198 state : tree . state ,
175199 actions : overmind . actions ,
176200 effects : overmind . effects ,
177201 addMutationListener : overmind . addMutationListener ,
178202 reaction : overmind . reaction ,
203+ suspend,
179204 }
180205 }
181206}
0 commit comments