ez-saga 17.0.9 → 17.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.d.ts +1 -1
- package/src/index.ts +1 -1
- package/src/redux/PromiseMiddleware.ts +44 -0
- package/src/redux/createApp.ts +7 -89
- package/src/redux/defaultReducer.ts +17 -0
- package/src/redux/typeDeclare.ts +72 -0
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp } from './redux/
|
|
1
|
+
export { PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp } from './redux/typeDeclare';
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp } from './redux/
|
|
1
|
+
export { PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp } from './redux/typeDeclare';
|
|
2
2
|
import createApp from './redux/createApp';
|
|
3
3
|
export default {
|
|
4
4
|
createApp: createApp
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Dispatch, Middleware, MiddlewareAPI, Action, AnyAction } from 'redux';
|
|
2
|
+
import { RegistedModel } from './typeDeclare';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 创建中间层
|
|
6
|
+
* @param registedModel 已注册model
|
|
7
|
+
*/
|
|
8
|
+
function createPromiseMiddleware<D extends Dispatch>(registedModel: RegistedModel): Middleware<any, any, any> {
|
|
9
|
+
function isEffect(type: string) {
|
|
10
|
+
if (!type || typeof type !== 'string') return false;
|
|
11
|
+
const [modelName, effect] = type.split('/');
|
|
12
|
+
const model = registedModel[modelName];
|
|
13
|
+
if (model) {
|
|
14
|
+
if (model.effects && model.effects[effect]) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
// function isReduxAction(action: unknown): action is Action {
|
|
21
|
+
// return (action as Action)?.type !== undefined;
|
|
22
|
+
// }
|
|
23
|
+
|
|
24
|
+
return (api: MiddlewareAPI<D, any>) => (next: (action: AnyAction) => any) => (action: AnyAction) => {
|
|
25
|
+
let exeEffect = false;
|
|
26
|
+
if ((action as Action)?.type !== undefined) {
|
|
27
|
+
const { type } = action as Action;
|
|
28
|
+
exeEffect = isEffect(type);
|
|
29
|
+
}
|
|
30
|
+
if (exeEffect) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
next({
|
|
33
|
+
_dy_resolve: resolve,
|
|
34
|
+
_dy_reject: reject,
|
|
35
|
+
...action as Action,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
return next(action);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default createPromiseMiddleware;
|
package/src/redux/createApp.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { legacy_createStore as createStore, applyMiddleware, compose, Action as ReduxAction, Store } from 'redux';
|
|
2
2
|
import { createSlice, combineReducers } from '@reduxjs/toolkit';
|
|
3
|
-
import createSagaMiddleware, {
|
|
4
|
-
import { PutEffect, SelectEffect } from 'redux-saga/effects';
|
|
3
|
+
import createSagaMiddleware, { SagaMiddleware } from 'redux-saga';
|
|
5
4
|
import { call, put, select, takeEvery, putResolve } from 'redux-saga/effects';
|
|
6
5
|
import win from 'global/window';
|
|
7
6
|
import { Reducer, AnyAction } from 'redux';
|
|
7
|
+
import { ReduxModel, ReduxApp, RegistedModel, ReduxSagaModel, PayloadAction } from './typeDeclare';
|
|
8
|
+
//import saveState from './defaultReducer';
|
|
9
|
+
import createPromiseMiddleware from './PromiseMiddleware';
|
|
8
10
|
|
|
9
|
-
const saveState: Reducer<any,
|
|
11
|
+
const saveState: Reducer<any, PayloadAction> = (state: any, action: PayloadAction) => {
|
|
12
|
+
debugger;
|
|
10
13
|
if (!action.payload) {
|
|
11
14
|
return state;
|
|
12
15
|
}
|
|
@@ -15,92 +18,7 @@ const saveState: Reducer<any, AnyAction> = (state: any, action: AnyAction) => {
|
|
|
15
18
|
...action.payload
|
|
16
19
|
};
|
|
17
20
|
return newStat;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** payload action类型 */
|
|
21
|
-
export interface PayloadAction extends Action<string>, ReduxAction<string> {
|
|
22
|
-
payload: any
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** 工具 */
|
|
26
|
-
export interface EffectTool {
|
|
27
|
-
call: (...args: any[]) => any,
|
|
28
|
-
put: <A extends Action>(action: A) => PutEffect<A>,
|
|
29
|
-
select: ((selectFunc: (state: any) => any) => SelectEffect) | (() => SelectEffect)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Effect函数类型 */
|
|
33
|
-
export interface Effect {
|
|
34
|
-
(action: PayloadAction, tool: EffectTool): Generator | any;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** ModelReducer定义 */
|
|
38
|
-
export interface ModelReducer {
|
|
39
|
-
[key: string]: Reducer<any, AnyAction>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
/** ModelEffect定义 */
|
|
44
|
-
export interface ModelEffect {
|
|
45
|
-
[key: string]: Effect;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface ReduxModel {
|
|
49
|
-
name: string,
|
|
50
|
-
state?: any;
|
|
51
|
-
reducers?: ModelReducer
|
|
52
|
-
effects?: ModelEffect;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
interface ReduxSagaModel extends ReduxModel {
|
|
56
|
-
initialState: any,
|
|
57
|
-
reducers: ModelReducer
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface RegistedModel {
|
|
61
|
-
[key: string]: ReduxModel;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** app */
|
|
65
|
-
export interface ReduxApp {
|
|
66
|
-
store: Store<any, ReduxAction>,
|
|
67
|
-
sagaMiddleware: SagaMiddleware<object>;
|
|
68
|
-
regist: (model: ReduxModel) => void
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 创建中间层
|
|
73
|
-
* @param registedModel 已注册model
|
|
74
|
-
*/
|
|
75
|
-
function createPromiseMiddleware(registedModel: RegistedModel) {
|
|
76
|
-
function isEffect(type: string) {
|
|
77
|
-
if (!type || typeof type !== 'string') return false;
|
|
78
|
-
const [modelName, effect] = type.split('/');
|
|
79
|
-
const model = registedModel[modelName];
|
|
80
|
-
if (model) {
|
|
81
|
-
if (model.effects && model.effects[effect]) {
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return () => (next: (arg: any) => any) => (action: ReduxAction) => {
|
|
89
|
-
const { type } = action;
|
|
90
|
-
if (isEffect(type)) {
|
|
91
|
-
return new Promise((resolve, reject) => {
|
|
92
|
-
next({
|
|
93
|
-
_dy_resolve: resolve,
|
|
94
|
-
_dy_reject: reject,
|
|
95
|
-
...action,
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
return next(action);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
21
|
+
};
|
|
104
22
|
|
|
105
23
|
/**
|
|
106
24
|
* 获取注册model函数
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** 默认reducer */
|
|
2
|
+
import { Reducer } from 'redux';
|
|
3
|
+
import { PayloadAction } from './typeDeclare';
|
|
4
|
+
|
|
5
|
+
const saveState: Reducer<any, PayloadAction> = (state: any, action: PayloadAction) => {
|
|
6
|
+
debugger;
|
|
7
|
+
if (!action.payload) {
|
|
8
|
+
return state;
|
|
9
|
+
}
|
|
10
|
+
let newStat = {
|
|
11
|
+
...state,
|
|
12
|
+
...action.payload
|
|
13
|
+
};
|
|
14
|
+
return newStat;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default saveState;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** 类型申明 */
|
|
2
|
+
import {
|
|
3
|
+
Action as ReduxAction, Store, Reducer, AnyAction
|
|
4
|
+
} from 'redux';
|
|
5
|
+
import { Action, SagaMiddleware } from 'redux-saga';
|
|
6
|
+
import { PutEffect, SelectEffect } from 'redux-saga/effects';
|
|
7
|
+
|
|
8
|
+
/** payload action类型 */
|
|
9
|
+
export interface PayloadAction extends AnyAction, ReduxAction<string> {
|
|
10
|
+
/** 类型 */
|
|
11
|
+
type: string,
|
|
12
|
+
/** 载体 */
|
|
13
|
+
payload?: any
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** 工具 */
|
|
17
|
+
export interface EffectTool {
|
|
18
|
+
/** 调用异步函数, 并获得该异步函数的结果 */
|
|
19
|
+
call: (...args: any[]) => any,
|
|
20
|
+
/** 分发异步action */
|
|
21
|
+
put: <A extends Action>(action: A) => PutEffect<A>,
|
|
22
|
+
/** 分发同步action */
|
|
23
|
+
putResolve: <A extends Action>(action: A) => PutEffect<A>,
|
|
24
|
+
/** 从store从获取状态 */
|
|
25
|
+
select: ((selectFunc: (state: any) => any) => SelectEffect) | (() => SelectEffect)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Effect函数类型 */
|
|
29
|
+
export interface Effect {
|
|
30
|
+
(action: PayloadAction, tool: EffectTool): Generator | any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** ModelReducer定义 */
|
|
34
|
+
export interface ModelReducer {
|
|
35
|
+
[key: string]: Reducer<any, PayloadAction>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
/** ModelEffect定义 */
|
|
40
|
+
export interface ModelEffect {
|
|
41
|
+
[key: string]: Effect;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ReduxModel {
|
|
45
|
+
name: string,
|
|
46
|
+
state?: any;
|
|
47
|
+
reducers?: ModelReducer
|
|
48
|
+
effects?: ModelEffect;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ReduxSagaModel extends ReduxModel {
|
|
52
|
+
initialState: any,
|
|
53
|
+
reducerPath: string
|
|
54
|
+
reducers: ModelReducer
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface RegistedModel {
|
|
58
|
+
[key: string]: ReduxModel
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** app */
|
|
62
|
+
export interface ReduxApp {
|
|
63
|
+
store: Store<any, ReduxAction>,
|
|
64
|
+
sagaMiddleware: SagaMiddleware<object>;
|
|
65
|
+
regist: (model: ReduxModel) => void
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export interface ReduxSagaModel extends ReduxModel {
|
|
70
|
+
initialState: any,
|
|
71
|
+
reducers: ModelReducer
|
|
72
|
+
}
|