File tree Expand file tree Collapse file tree 4 files changed +26
-0
lines changed
packages/node_modules/overmind/src Expand file tree Collapse file tree 4 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,9 @@ function createDefaultOvermind() {
2929 await Promise . resolve ( )
3030 context . state . foo = 'bar2'
3131 }
32+ const whenFooChanges : Action < void , Promise < void > > = async ( context ) => {
33+ await context . when ( ( ) => context . state . foo === 'bar2' )
34+ }
3235 const changeValue : Action < { isAwesome : boolean } > = ( _ , value ) => {
3336 value . isAwesome = ! value . isAwesome
3437 }
@@ -47,6 +50,7 @@ function createDefaultOvermind() {
4750 changeFooWithEffect,
4851 changeValue,
4952 waitAndChangeFoo,
53+ whenFooChanges
5054 }
5155 const effects = {
5256 hello ( ) {
@@ -357,6 +361,15 @@ describe('Overmind', () => {
357361 ) . not . toThrow ( )
358362 expect ( app . state . item . isAwesome ) . toBe ( false )
359363 } )
364+ test ( 'should allow waiting for mutation' , async ( ) => {
365+ expect . assertions ( 1 )
366+ const app = createDefaultOvermind ( )
367+ await Promise . all ( [
368+ app . actions . whenFooChanges ( ) ,
369+ app . actions . changeFoo ( )
370+ ] )
371+ expect ( app . state . foo ) . toBe ( 'bar2' )
372+ } )
360373} )
361374
362375describe ( 'Overmind mock' , ( ) => {
Original file line number Diff line number Diff line change @@ -478,6 +478,16 @@ export class Overmind<ThisConfig extends IConfiguration>
478478 execution,
479479 proxyStateTree : this . proxyStateTree ,
480480 effects : this . trackEffects ( this . effects , execution ) ,
481+ when : ( cb : ( ) => boolean ) => {
482+ return new Promise ( ( resolve ) => {
483+ const dispose = this . addFlushListener ( ( ) => {
484+ if ( cb ( ) ) {
485+ dispose ( )
486+ resolve ( )
487+ }
488+ } )
489+ } )
490+ } ,
481491 revertable : ( cb : ( ) => void ) => {
482492 const mutations : IMutation [ ] = [ ]
483493 const dispose = this . addMutationListener ( ( mutation ) => {
Original file line number Diff line number Diff line change @@ -174,6 +174,7 @@ export function createOperator<ThisConfig extends IConfiguration>(
174174 actions : context . actions ,
175175 execution : context . execution ,
176176 revertable : context . revertable ,
177+ when : context . when
177178 } ,
178179 context . value ,
179180 ( err , value , options = { } ) => {
@@ -276,6 +277,7 @@ export function createMutationOperator<ThisConfig extends IConfiguration>(
276277 actions : context . actions ,
277278 execution : context . execution ,
278279 revertable : context . revertable ,
280+ when : context . when
279281 } ,
280282 process . env . NODE_ENV === 'production'
281283 ? context . value
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ export type IContext<ThisConfig extends IConfiguration> = {
3737 state : ResolveState < ThisConfig [ 'state' ] >
3838 actions : ResolveActions < ThisConfig [ 'actions' ] >
3939 effects : ThisConfig [ 'effects' ]
40+ when : ( cb : ( ) => boolean ) => Promise < void >
4041 revertable : ( mutationsCallback : ( ) => any ) => ( ) => void
4142}
4243
You can’t perform that action at this time.
0 commit comments