forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionChain.ts
More file actions
132 lines (117 loc) · 3.1 KB
/
ActionChain.ts
File metadata and controls
132 lines (117 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { EventEmitter } from 'betsy'
import { Mutation } from 'proxy-state-tree'
const IS_DEVELOPMENT = process.env.NODE_ENV !== 'production'
export type ExecutionContext = {
__execution: Execution
__path: string[]
}
export type ActionChainOptions = {
actionWrapper?: any
providerExceptions?: string[]
}
export type Execution = {
operatorId: number
actionId: number
executionId: number
}
export type ActionExecution = {
actionId: number
executionId: number
actionName: string
value?: any
}
export type OperatorExecution = {
actionId: number
executionId: number
operatorId: number
type: string
name: string
path: string
}
export interface ActionChainEvents {
effect: Execution & {
name: string
method: string | number | symbol
result: any
args: any[]
}
'action:start': ActionExecution
'action:end': ActionExecution
'operator:start': OperatorExecution
'operator:async': OperatorExecution & {
isAsync: boolean
}
'operator:end': OperatorExecution & {
isAsync: boolean
result: any
}
mutations: ActionExecution & {
mutations: Mutation[]
}
}
function isObject(value) {
return typeof value === 'object' && !Array.isArray(value) && value !== null
}
export class ActionChain<Effects> extends EventEmitter<ActionChainEvents> {
constructor(
private effects: Effects,
private options: ActionChainOptions = {}
) {
super()
this.options.providerExceptions = options.providerExceptions || []
}
private createGetHandler(execution, path) {
const instance = this
return (target, prop) => {
if (typeof target[prop] === 'function') {
return (...args) => {
const result = target[prop](...args)
if (result instanceof Promise) {
result.then((promisedResult) => {
instance.emitAsync('effect', {
...execution,
name: path,
method: prop,
args,
result: promisedResult,
})
})
} else {
instance.emitAsync('effect', {
...execution,
name: path,
method: prop,
args,
result,
})
}
return result
}
} else if (isObject(target[prop])) {
return new Proxy(target[prop], {
get: instance.createGetHandler(execution, path + '.' + prop),
})
}
return target[prop]
}
}
getEffects(thisExecution: Execution, executionContext: ExecutionContext) {
let effects = this.effects
if (IS_DEVELOPMENT) {
effects = Object.keys(this.effects).reduce((currentEffects, key) => {
if (
this.options.providerExceptions.indexOf(key) === -1 &&
isObject(this.effects[key])
) {
currentEffects[key] = new Proxy(this.effects[key], {
get: this.createGetHandler(thisExecution, key),
})
} else {
currentEffects[key] = this.effects[key]
}
return currentEffects
}, {}) as Effects
}
return Object.assign({}, effects, executionContext)
}
}