11import { EventEmitter } from 'betsy'
2- import { Mutation } from 'proxy-state-tree'
32
43const IS_DEVELOPMENT = process . env . NODE_ENV !== 'production'
54
6- export type ExecutionContext = {
7- __execution : Execution
8- __path : string [ ]
9- }
10-
11- export type ActionChainOptions = {
12- actionWrapper ?: any
13- providerExceptions ?: string [ ]
14- }
15-
165export type Execution = {
176 operatorId : number
187 actionId : number
198 executionId : number
209}
2110
11+ export type ExecutionContext = {
12+ __execution : Execution
13+ __path : string [ ]
14+ }
15+
2216export type ActionExecution = {
2317 actionId : number
2418 executionId : number
2519 actionName : string
2620 value ?: any
2721}
2822
29- export type OperatorExecution = {
23+ type OperatorExecution = {
3024 actionId : number
3125 executionId : number
3226 operatorId : number
@@ -35,7 +29,7 @@ export type OperatorExecution = {
3529 path : string
3630}
3731
38- export interface ActionChainEvents {
32+ interface ActionChainEvents {
3933 effect : Execution & {
4034 name : string
4135 method : string | number | symbol
@@ -52,32 +46,30 @@ export interface ActionChainEvents {
5246 isAsync : boolean
5347 result : any
5448 }
55- mutations : ActionExecution & {
56- mutations : Mutation [ ]
57- }
5849}
5950
6051function isObject ( value ) {
6152 return typeof value === 'object' && ! Array . isArray ( value ) && value !== null
6253}
6354
64- export class ActionChain < Effects > extends EventEmitter < ActionChainEvents > {
55+ export class ActionChain < Effects , ExtraEvents = { } > extends EventEmitter <
56+ ActionChainEvents & ExtraEvents
57+ > {
6558 constructor (
6659 private effects : Effects ,
67- private options : ActionChainOptions = { }
60+ private options : { providerExceptions ?: string [ ] } = { }
6861 ) {
6962 super ( )
7063 this . options . providerExceptions = options . providerExceptions || [ ]
7164 }
72- private createGetHandler ( execution , path ) {
73- const instance = this
65+ private createGetHandler ( execution : Execution , path : string ) {
7466 return ( target , prop ) => {
7567 if ( typeof target [ prop ] === 'function' ) {
7668 return ( ...args ) => {
7769 const result = target [ prop ] ( ...args )
7870 if ( result instanceof Promise ) {
7971 result . then ( ( promisedResult ) => {
80- instance . emitAsync ( 'effect' , {
72+ this . emitAsync ( 'effect' , {
8173 ...execution ,
8274 name : path ,
8375 method : prop ,
@@ -86,7 +78,7 @@ export class ActionChain<Effects> extends EventEmitter<ActionChainEvents> {
8678 } )
8779 } )
8880 } else {
89- instance . emitAsync ( 'effect' , {
81+ this . emitAsync ( 'effect' , {
9082 ...execution ,
9183 name : path ,
9284 method : prop ,
@@ -99,7 +91,7 @@ export class ActionChain<Effects> extends EventEmitter<ActionChainEvents> {
9991 }
10092 } else if ( isObject ( target [ prop ] ) ) {
10193 return new Proxy ( target [ prop ] , {
102- get : instance . createGetHandler ( execution , path + '.' + prop ) ,
94+ get : this . createGetHandler ( execution , path + '.' + prop ) ,
10395 } )
10496 }
10597
@@ -111,20 +103,23 @@ export class ActionChain<Effects> extends EventEmitter<ActionChainEvents> {
111103 let effects = this . effects
112104
113105 if ( IS_DEVELOPMENT ) {
114- effects = Object . keys ( this . effects ) . reduce ( ( currentEffects , key ) => {
115- if (
116- this . options . providerExceptions . indexOf ( key ) === - 1 &&
117- isObject ( this . effects [ key ] )
118- ) {
119- currentEffects [ key ] = new Proxy ( this . effects [ key ] , {
120- get : this . createGetHandler ( thisExecution , key ) ,
121- } )
122- } else {
123- currentEffects [ key ] = this . effects [ key ]
124- }
106+ effects = Object . keys ( this . effects ) . reduce (
107+ ( currentEffects , key ) => {
108+ if (
109+ this . options . providerExceptions . indexOf ( key ) === - 1 &&
110+ isObject ( this . effects [ key ] )
111+ ) {
112+ currentEffects [ key ] = new Proxy ( this . effects [ key ] , {
113+ get : this . createGetHandler ( thisExecution , key ) ,
114+ } )
115+ } else {
116+ currentEffects [ key ] = this . effects [ key ]
117+ }
125118
126- return currentEffects
127- } , { } ) as Effects
119+ return currentEffects
120+ } ,
121+ { } as Effects
122+ )
128123 }
129124
130125 return Object . assign ( { } , effects , executionContext )
0 commit comments