-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththunk.ts
More file actions
46 lines (44 loc) · 1.74 KB
/
thunk.ts
File metadata and controls
46 lines (44 loc) · 1.74 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
import { connect, MapStateToPropsParam, MapDispatchToProps, MapStateToProps } from 'react-redux';
import { exportMapState } from "./commons";
export * from "./commons";
interface ActionCall {
propertyKey: string;
callback: Function
}
const connectActionKey = Symbol("connectActionKey");
export function ConnectAction(callback: Function) {
return <any>function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
let funcs: ActionCall[] = [];
if (Reflect.hasMetadata(connectActionKey, target)) {
funcs = Reflect.getMetadata(connectActionKey, target)
}
funcs.push({ callback, propertyKey })
Reflect.defineMetadata(connectActionKey, funcs, target);
};
}
export function exportConnect(propType: { new(): any }, compType: { new(): any }) {
let example = new propType();
let proto = Object.getPrototypeOf(example);
let actionCalls: ActionCall[] = Reflect.getOwnMetadata(connectActionKey, proto) || [];
let mapDispatchToProps: MapDispatchToProps<any, any> = (dispatch, ownProps) => {
let actions = {};
for (let actCall of actionCalls) {
actions[actCall.propertyKey] = function () {
let old = Array.prototype.slice.call(arguments);
let args = [dispatch].concat(old);
actCall.callback.apply(this, args);
};
}
return actions;
}
let mapStateToProps = exportMapState(propType)
//
proto = Object.getPrototypeOf(compType);
proto.defaultProps = example;
return connect(mapStateToProps, mapDispatchToProps)(compType);
}
export function ReduxConnect(propType: { new(): any }) {
return function (target) {
return <any>exportConnect(propType, target)
}
}