11import { EventEmitter } from 'betsy'
2- import * as isPlainObject from 'is-plain-obj'
2+ import isPlainObject from 'is-plain-obj'
33import { IS_PROXY , ProxyStateTree , TTree , IMutation } from 'proxy-state-tree'
44import { Derived } from './derived'
55import { Devtools , Message , safeValue , safeValues } from './Devtools'
@@ -179,10 +179,11 @@ export class Overmind<Config extends Configuration> implements Configuration {
179179 actionId : this . actionReferences . indexOf ( action ) ,
180180 executionId : this . nextExecutionId ++ ,
181181 actionName : name ,
182- operatorId : - 1 ,
182+ operatorId : 0 ,
183183 path : [ ] ,
184184 emit : this . eventHub . emit . bind ( this . eventHub ) ,
185185 send : this . devtools ? this . devtools . send . bind ( this . devtools ) : ( ) => { } ,
186+ trackEffects : this . trackEffects . bind ( this , this . effects ) ,
186187 }
187188 }
188189 private createContext ( value , execution , tree ) {
@@ -224,11 +225,22 @@ export class Overmind<Config extends Configuration> implements Configuration {
224225 action [ IS_OPERATOR ]
225226 ? action (
226227 null ,
227- this . createContext ( value , execution , this . proxyStateTree ) ,
228- ( err , val ) => {
229- this . eventHub . emit ( EventType . ACTION_END , execution )
228+ Object . assign (
229+ {
230+ value,
231+ state : this . proxyStateTree . state ,
232+ execution,
233+ proxyStateTree : this . proxyStateTree ,
234+ } ,
235+ this . trackEffects ( this . effects , execution )
236+ ) ,
237+ ( err , finalContext ) => {
238+ this . eventHub . emit (
239+ EventType . ACTION_END ,
240+ finalContext . execution
241+ )
230242 if ( err ) reject ( err )
231- else resolve ( val )
243+ else resolve ( finalContext . value )
232244 }
233245 )
234246 : action (
@@ -476,7 +488,7 @@ export function pipe(...operators) {
476488 const run = ( runErr , runContext ) => {
477489 asyncTimeout = setTimeout ( ( ) => {
478490 context . execution . emit ( EventType . OPERATOR_ASYNC , {
479- ...context . execution ,
491+ ...runContext . execution ,
480492 isAsync : true ,
481493 } )
482494 } )
@@ -496,7 +508,7 @@ export function pipe(...operators) {
496508
497509 if ( operatorContext . value instanceof Promise ) {
498510 context . execution . emit ( EventType . OPERATOR_ASYNC , {
499- ...context . execution ,
511+ ...operatorContext . execution ,
500512 isAsync : true ,
501513 } )
502514 operatorContext . value
@@ -533,22 +545,31 @@ function startDebugOperator(type, arg, context) {
533545
534546 context . execution . emit ( EventType . OPERATOR_START , {
535547 ...context . execution ,
536- operatorId : context . execution . operatorId + 1 ,
537548 name,
538549 type,
539550 } )
540551}
541552
542- function stopDebugOperator ( context ) {
553+ function stopDebugOperator ( context , value ) {
543554 if ( IS_PRODUCTION ) {
544555 return
545556 }
546557
547- context . execution . emit ( EventType . OPERATOR_END , {
548- ...context . execution ,
549- result : safeValue ( context . value ) ,
550- isAsync : context . value instanceof Promise ,
551- } )
558+ if ( value instanceof Promise ) {
559+ value . then ( ( promiseValue ) => {
560+ context . execution . emit ( EventType . OPERATOR_END , {
561+ ...context . execution ,
562+ result : promiseValue ,
563+ isAsync : true ,
564+ } )
565+ } )
566+ } else {
567+ context . execution . emit ( EventType . OPERATOR_END , {
568+ ...context . execution ,
569+ result : value ,
570+ isAsync : false ,
571+ } )
572+ }
552573}
553574
554575function createContext ( context , value , path ?) {
@@ -559,15 +580,20 @@ function createContext(context, value, path?) {
559580 }
560581 }
561582
562- return {
563- ...context ,
564- value,
565- execution : {
566- ...context . execution ,
567- operatorId : context . execution . operatorId + 1 ,
568- path : path || context . execution . path ,
569- } ,
583+ const newExecution = {
584+ ...context . execution ,
585+ operatorId : context . execution . operatorId + 1 ,
586+ path : path || context . execution . path ,
570587 }
588+
589+ return Object . assign (
590+ {
591+ ...context ,
592+ value,
593+ execution : newExecution ,
594+ } ,
595+ context . execution . trackEffects ( newExecution )
596+ )
571597}
572598
573599function createNextPath ( next ) {
@@ -599,9 +625,8 @@ export function map<Input, Output, Config extends Configuration = TheConfig>(
599625 else {
600626 startDebugOperator ( 'map' , operation , context )
601627 const value = operation ( context )
602- const newContext = createContext ( context , value )
603- stopDebugOperator ( newContext )
604- next ( null , newContext )
628+ stopDebugOperator ( context , value )
629+ next ( null , createContext ( context , value ) )
605630 }
606631 }
607632 instance [ IS_OPERATOR ] = true
@@ -617,13 +642,14 @@ export function run<Input, Config extends Configuration = TheConfig>(
617642 else {
618643 startDebugOperator ( 'run' , operation , context )
619644 const result = operation ( context )
645+ stopDebugOperator ( context , context . value )
646+
620647 const newContext = createContext (
621648 context ,
622649 result instanceof Promise
623650 ? result . then ( ( ) => context . value )
624651 : context . value
625652 )
626- stopDebugOperator ( newContext )
627653 next ( null , newContext )
628654 }
629655 }
@@ -656,7 +682,7 @@ export function forEach<
656682 evaluatingCount --
657683
658684 if ( ! evaluatingCount ) {
659- stopDebugOperator ( lastContext )
685+ stopDebugOperator ( lastContext , lastContext . value )
660686 next ( null , lastContext )
661687 }
662688 }
@@ -698,7 +724,7 @@ export function parallel<Input, Config extends Configuration = TheConfig>(
698724 evaluatingCount --
699725
700726 if ( ! evaluatingCount ) {
701- stopDebugOperator ( lastContext )
727+ stopDebugOperator ( lastContext , lastContext . value )
702728 next ( null , lastContext )
703729 }
704730 }
@@ -728,13 +754,11 @@ export function filter<Input, Config extends Configuration = TheConfig>(
728754 else {
729755 startDebugOperator ( 'filter' , operation , context )
730756 if ( operation ( context ) ) {
731- const newContext = createContext ( context , context . value )
732- stopDebugOperator ( newContext )
733- next ( null , newContext )
757+ stopDebugOperator ( context , context . value )
758+ next ( null , createContext ( context , context . value ) )
734759 } else {
735- const newContext = createContext ( context , context . value )
736- stopDebugOperator ( newContext )
737- final ( null , newContext )
760+ stopDebugOperator ( context , context . value )
761+ final ( null , createContext ( context , context . value ) )
738762 }
739763 }
740764 }
@@ -755,7 +779,7 @@ export function mutate<Input, Config extends Configuration = TheConfig>(
755779 mutationTree . onMutation ( ( mutation ) => {
756780 context . execution . emit ( EventType . MUTATIONS , {
757781 ...context . execution ,
758- operatorId : context . execution . operatorId + 1 ,
782+ operatorId : context . execution . operatorId ,
759783 mutations : makeStringifySafeMutations ( [ mutation ] ) ,
760784 } )
761785 } )
@@ -764,15 +788,18 @@ export function mutate<Input, Config extends Configuration = TheConfig>(
764788 ...context ,
765789 state : mutationTree . state ,
766790 } )
767- const newContext = createContext (
768- context ,
769- maybePromise instanceof Promise
770- ? maybePromise . then ( ( ) => context . value )
771- : context . value
772- )
773791
774- stopDebugOperator ( newContext )
775- next ( null , newContext )
792+ stopDebugOperator ( context , maybePromise )
793+
794+ next (
795+ null ,
796+ createContext (
797+ context ,
798+ maybePromise instanceof Promise
799+ ? maybePromise . then ( ( ) => context . value )
800+ : context . value
801+ )
802+ )
776803 }
777804 }
778805 instance [ IS_OPERATOR ] = true
@@ -801,7 +828,7 @@ export function fork<
801828 const nextWithPaths = createNextPath ( ( err , returnedContext ) => {
802829 if ( err ) next ( err )
803830 else {
804- stopDebugOperator ( newContext )
831+ stopDebugOperator ( context , context . value )
805832 next ( null , { ...returnedContext , value : newContext . value } )
806833 }
807834 } )
@@ -829,14 +856,14 @@ export function when<
829856 if ( err ) next ( err )
830857 else {
831858 startDebugOperator ( 'when' , operation , context )
859+ const newContext = createContext (
860+ context ,
861+ context . value ,
862+ context . execution . path . concat ( 'true' )
863+ )
832864 if ( operation ( context ) ) {
833- const newContext = createContext (
834- context ,
835- context . value ,
836- context . execution . path . concat ( 'true' )
837- )
838865 const nextWithPath = createNextPath ( next )
839- stopDebugOperator ( newContext )
866+ stopDebugOperator ( context , context . value )
840867 paths . true ( null , newContext , nextWithPath )
841868 } else {
842869 const newContext = createContext (
@@ -845,7 +872,7 @@ export function when<
845872 context . execution . path . concat ( 'false' )
846873 )
847874 const nextWithPath = createNextPath ( next )
848- stopDebugOperator ( newContext )
875+ stopDebugOperator ( context , context . value )
849876 paths . false ( null , newContext , nextWithPath )
850877 }
851878 }
@@ -862,8 +889,10 @@ export function wait<Input, Config extends Configuration = TheConfig>(
862889 if ( err ) next ( err )
863890 else {
864891 startDebugOperator ( 'wait' , ms , context )
865- const newContext = createContext ( context , context . value )
866- setTimeout ( ( ) => next ( null , newContext ) , ms )
892+ setTimeout ( ( ) => {
893+ stopDebugOperator ( context , context . value )
894+ next ( null , createContext ( context , context . value ) )
895+ } , ms )
867896 }
868897 }
869898 instance [ IS_OPERATOR ] = true
@@ -888,9 +917,8 @@ export function debounce<Input, Config extends Configuration = TheConfig>(
888917 previousFinal = final
889918 timeout = setTimeout ( ( ) => {
890919 timeout = null
891- const newContext = createContext ( context , context . value )
892- stopDebugOperator ( newContext )
893- next ( null , newContext )
920+ stopDebugOperator ( context , context . value )
921+ next ( null , createContext ( context , context . value ) )
894922 } , ms )
895923 }
896924 instance [ IS_OPERATOR ] = true
0 commit comments