-
Notifications
You must be signed in to change notification settings - Fork 5
/
dva-core.d.ts
executable file
·109 lines (93 loc) · 2.99 KB
/
dva-core.d.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
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
declare module 'dva-core' {
import {
Reducer,
Action,
AnyAction,
ReducersMapObject,
MiddlewareAPI,
StoreEnhancer,
Store,
Dispatch,
} from 'redux';
import {SagaMiddleware, Task} from 'redux-saga';
//#region dva model
export interface EffectsCommandMap {
put: <A extends Action>(action: A) => any;
call: Function;
select: Function;
take: Function;
cancel: Function;
[key: string]: any;
}
// export type ActionWithPayload = { action: Action; payload: any; callback: Function };
export type Effect = (action: AnyAction, effects: EffectsCommandMap) => void;
export type EffectType = 'takeEvery' | 'takeLatest' | 'watcher' | 'throttle';
export type EffectWithType = [Effect, { type: EffectType }];
export interface EffectsMapObject {
[key: string]: Effect | EffectWithType;
}
export interface ReducerEnhancer {
(reducer: Reducer<any>): void;
}
// 使用redux-v4.x的ReducerMapObject会导致dva reducer中的action payload等参数无法确定类型(dva中使用的是3.x的redux)
// 这里从redux v3.x中找到了对应的ReducersMapObject以解决上述问题
export interface ReduxV3ReducersMapObject {
[key: string]: Reducer<any>;
}
export type ReducersMapObjectWithEnhancer = [ReducersMapObject, ReducerEnhancer];
export interface SubscriptionAPI {
dispatch: Dispatch<any>;
}
export type Subscription = (api: SubscriptionAPI, done: Function) => void;
export interface SubscriptionsMapObject {
[key: string]: Subscription;
}
export default interface Model {
namespace: string;
state?: any;
// reducers?: ReducersMapObject | ReducersMapObjectWithEnhancer;
reducers?: ReduxV3ReducersMapObject | ReducersMapObjectWithEnhancer;
effects?: EffectsMapObject;
subscriptions?: SubscriptionsMapObject;
}
//#enregion
export interface CreateOpts {
initialReducer?: ReducersMapObject,
setupApp: (app:DvaInstance)=>void;
}
export interface onActionFunc {
(api: MiddlewareAPI<any>): void,
}
export interface Hooks {
onError?: (e: Error, dispatch: Dispatch<any>) => void,
onAction?: onActionFunc | onActionFunc[],
onStateChange?: () => void,
onReducer?: ReducerEnhancer,
onEffect?: () => void,
onHmr?: () => void,
extraReducers?: ReducersMapObject,
extraEnhancers?: StoreEnhancer<any>[],
_handleActions?: Partial<Plugin>,
}
type usePlugin = (plugin:Partial<Hooks>)=>void;
export class Plugin {
use: usePlugin;
apply: (key:keyof Hooks,defaultHandler:Function)=>void;
get: (key:keyof Hooks)=> Partial<Hooks>;
}
export type DvaStore = Store & {
asyncReducers: {
[key:string]: Reducer;
},
runSaga: (...param:any)=>Task;
};
export type DvaInstance = {
_models: Array<Model>,
_store: DvaStore,
_plugin: Plugin,
use: usePlugin;
model: (m:Model)=>Model,
start: ()=>void,
};
export function create(hooksAndOpts?:Partial<Hooks>,createOpts?:CreateOpts):DvaInstance;
}