11import { EventEmitter } from 'betsy'
2+ import { proxifyEffects } from './utils'
3+ import { ActionChainEvents , Execution } from './types'
24
35const IS_DEVELOPMENT = process . env . NODE_ENV !== 'production'
46
5- export type Execution = {
6- operatorId : number
7- actionId : number
8- executionId : number
9- }
10-
11- export type ExecutionContext = {
12- __execution : Execution
13- __path : string [ ]
14- }
15-
16- export type ActionExecution = {
17- actionId : number
18- executionId : number
19- actionName : string
20- value ?: any
21- }
22-
23- type OperatorExecution = {
24- actionId : number
25- executionId : number
26- operatorId : number
27- type : string
28- name : string
29- path : string
30- }
31-
32- interface ActionChainEvents {
33- effect : Execution & {
34- name : string
35- method : string | number | symbol
36- result : any
37- args : any [ ]
38- }
39- 'action:start' : ActionExecution
40- 'action:end' : ActionExecution
41- 'operator:start' : OperatorExecution
42- 'operator:async' : OperatorExecution & {
43- isAsync : boolean
44- }
45- 'operator:end' : OperatorExecution & {
46- isAsync : boolean
47- result : any
48- }
49- }
50-
51- function isObject ( value ) {
52- return typeof value === 'object' && ! Array . isArray ( value ) && value !== null
53- }
54-
557export class ActionChain < Effects , ExtraEvents = { } > extends EventEmitter <
568 ActionChainEvents & ExtraEvents
579> {
@@ -60,68 +12,17 @@ export class ActionChain<Effects, ExtraEvents = {}> extends EventEmitter<
6012 private options : { providerExceptions ?: string [ ] } = { }
6113 ) {
6214 super ( )
63- this . options . providerExceptions = options . providerExceptions || [ ]
64- }
65- private createGetHandler ( execution : Execution , path : string ) {
66- return ( target , prop ) => {
67- if ( typeof target [ prop ] === 'function' ) {
68- return ( ...args ) => {
69- const result = target [ prop ] ( ...args )
70- if ( result instanceof Promise ) {
71- result . then ( ( promisedResult ) => {
72- this . emitAsync ( 'effect' , {
73- ...execution ,
74- name : path ,
75- method : prop ,
76- args,
77- result : promisedResult ,
78- } )
79- } )
80- } else {
81- this . emitAsync ( 'effect' , {
82- ...execution ,
83- name : path ,
84- method : prop ,
85- args,
86- result,
87- } )
88- }
89-
90- return result
91- }
92- } else if ( isObject ( target [ prop ] ) ) {
93- return new Proxy ( target [ prop ] , {
94- get : this . createGetHandler ( execution , path + '.' + prop ) ,
95- } )
96- }
97-
98- return target [ prop ]
99- }
10015 }
10116
102- getEffects ( thisExecution : Execution , executionContext : ExecutionContext ) {
103- let effects = this . effects
104-
17+ getEffects ( thisExecution : Execution ) {
10518 if ( IS_DEVELOPMENT ) {
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- }
118-
119- return currentEffects
120- } ,
121- { } as Effects
19+ return proxifyEffects (
20+ this . effects ,
21+ this . options . providerExceptions ,
22+ ( effect ) => this . emitAsync ( 'effect' , { ...thisExecution , ...effect } )
12223 )
12324 }
12425
125- return Object . assign ( { } , effects , executionContext )
26+ return this . effects
12627 }
12728}
0 commit comments