Skip to content

Commit d3f167c

Browse files
committed
refactor(action-chain): move mutation events declaration to overmind
1 parent 02cafd1 commit d3f167c

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

packages/node_modules/action-chain/src/ActionBase.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ActionChain, Execution, ExecutionContext } from './ActionChain'
1+
import { ActionChain, ExecutionContext } from './ActionChain'
22

33
export class StopExecution {
4-
constructor(public value) {}
4+
constructor(public value: any) {}
55
}
66
export class ActionBase<Effects> {
77
static nextActionId: number = 0
@@ -11,17 +11,15 @@ export class ActionBase<Effects> {
1111
private initialActionId: number = ActionBase.nextActionId++,
1212
private runOperators?: (
1313
value: any,
14-
executionContext: {
15-
__execution: Execution
16-
__path: string[]
17-
},
14+
executionContext: ExecutionContext,
1815
newPath?: string
1916
) => any | Promise<any>
2017
) {
2118
interface I extends ActionBase<Effects> {
22-
(value: string): string
19+
(value: any): I
2320
displayName: string
2421
}
22+
this.getActionChain = this.getActionChain.bind(this)
2523

2624
const instance: I = Object.assign(function(value) {
2725
const initialOperator = typeof arguments[1] === 'undefined'
@@ -69,8 +67,8 @@ export class ActionBase<Effects> {
6967

7068
return instance
7169
}
72-
getActionChain = () => {
73-
return this.actionChain
70+
getActionChain<ExtraEvents = {}>() {
71+
return this.actionChain as ActionChain<Effects, ExtraEvents>
7472
}
7573
createOperatorResult = (
7674
type: string,

packages/node_modules/action-chain/src/ActionChain.ts

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
11
import { EventEmitter } from 'betsy'
2-
import { Mutation } from 'proxy-state-tree'
32

43
const 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-
165
export 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+
2216
export 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

6051
function 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)
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
export { ActionBase, StopExecution } from './ActionBase'
2-
export { ActionChain, Execution, ExecutionContext } from './ActionChain'
2+
export {
3+
ActionChain,
4+
ActionExecution,
5+
Execution,
6+
ExecutionContext,
7+
} from './ActionChain'

packages/node_modules/overmind/src/Action.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import ProxyStateTree from 'proxy-state-tree'
2-
import { ActionBase, StopExecution, ActionChain } from 'action-chain'
1+
import ProxyStateTree, { Mutation } from 'proxy-state-tree'
2+
import {
3+
ActionBase,
4+
ActionExecution,
5+
StopExecution,
6+
ActionChain,
7+
} from 'action-chain'
38

49
type OperatorCallback<Effects, Value, NewValue = Value> = (
510
effects: Effects,
@@ -23,7 +28,11 @@ export interface INoValueAction<
2328
> extends Action<State, Effects, InitialValue, Value> {
2429
(): Value
2530
}
26-
31+
interface MutationsEvents {
32+
mutations: ActionExecution & {
33+
mutations: Mutation[]
34+
}
35+
}
2736
export default class Action<
2837
State,
2938
Effects,
@@ -71,7 +80,7 @@ export default class Action<
7180
this.proxyStateTree.startMutationTracking()
7281
cb(effects.state, value)
7382
const mutations = this.proxyStateTree.clearMutationTracking()
74-
this.getActionChain().emitAsync('mutations', {
83+
this.getActionChain<MutationsEvents>().emitAsync('mutations', {
7584
mutations,
7685
...effects.__execution,
7786
})

0 commit comments

Comments
 (0)