-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathSequence.ts
48 lines (44 loc) · 1.24 KB
/
Sequence.ts
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
/**
* @author 定岳
* @description 处理Sequence
*/
import * as get from 'lodash.get';
export default class Sequence {
constructor(opts: { actions?: any; prefix: string }) {
this.actions = opts.actions;
this.prefix = opts.prefix;
}
actions: any;
prefix: string;
createSequenceAction = (callback): any => {
return (...seqArgs) => (dispatch, getState) => {
const proxy = new Proxy(this, {
get(target, prop) {
const state = get(getState(), target.prefix);
if (!state) {
throw new Error('invalid state, please check the [prefix]');
}
// State
if (Reflect.has(state, prop)) {
return Reflect.get(state, prop);
}
if (!target.actions) {
throw new Error('invalid actions, please check the [actions]');
}
// actions
if (Reflect.has(target.actions, prop)) {
const action = Reflect.get(target.actions, prop);
return (...actionArgs) => dispatch(action(...actionArgs));
}
},
set() {
return false;
}
});
return callback.call(proxy, ...seqArgs);
};
};
setActions = (actions: any) => {
this.actions = actions;
};
}