ez-saga 0.1.5 → 17.0.1
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 +3 -3
- package/src/index.ts +7 -0
- package/src/redux/{createApp.js → createApp.ts} +103 -14
- package/src/index.js +0 -4
- package/src/redux/createPromiseMiddleware.js +0 -31
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ez-saga",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "17.0.1",
|
|
4
4
|
"description": "The ez-saga project is a project that imitates dva-js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"lint": "eslint --ext .js src"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@reduxjs/toolkit": "^
|
|
30
|
-
"redux": "^
|
|
29
|
+
"@reduxjs/toolkit": "^1.9.7",
|
|
30
|
+
"redux": "^4.2.1",
|
|
31
31
|
"redux-saga": "^1.3.0",
|
|
32
32
|
"global": "^4.4.0"
|
|
33
33
|
},
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import createApp, { PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp } from './redux/createApp';
|
|
2
|
+
export {
|
|
3
|
+
PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp
|
|
4
|
+
};
|
|
5
|
+
export default {
|
|
6
|
+
createApp: createApp
|
|
7
|
+
};
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { legacy_createStore as createStore, applyMiddleware, compose } from 'redux';
|
|
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 from 'redux-saga';
|
|
3
|
+
import createSagaMiddleware, { Action, SagaMiddleware } from 'redux-saga';
|
|
4
|
+
import { PutEffect, SelectEffect } from 'redux-saga/effects';
|
|
4
5
|
import { call, put, select, takeEvery, putResolve } from 'redux-saga/effects';
|
|
5
6
|
import win from 'global/window';
|
|
6
|
-
import
|
|
7
|
+
import { Reducer, AnyAction } from 'redux';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
const saveState: Reducer<any, AnyAction> = (state: any, action: AnyAction) => {
|
|
9
10
|
if (!action.payload) {
|
|
10
11
|
return state;
|
|
11
12
|
}
|
|
@@ -16,6 +17,87 @@ function saveState(state, action) {
|
|
|
16
17
|
return newStat;
|
|
17
18
|
}
|
|
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: Action) => PutEffect<A>,
|
|
29
|
+
select: (selectFunc: (state: any) => any) => SelectEffect
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Effect函数类型 */
|
|
33
|
+
export interface Effect {
|
|
34
|
+
(action: PayloadAction, tool: EffectTool): Promise<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
|
+
initialState: any,
|
|
52
|
+
reducers: ModelReducer
|
|
53
|
+
effects: ModelEffect;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface RegistedModel {
|
|
57
|
+
[key: string]: ReduxModel;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** app */
|
|
61
|
+
export interface ReduxApp {
|
|
62
|
+
store: Store<any, ReduxAction>,
|
|
63
|
+
sagaMiddleware: SagaMiddleware<object>;
|
|
64
|
+
regist: (model: ReduxModel) => void
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 创建中间层
|
|
69
|
+
* @param registedModel 已注册model
|
|
70
|
+
*/
|
|
71
|
+
function createPromiseMiddleware(registedModel: RegistedModel) {
|
|
72
|
+
function isEffect(type: string) {
|
|
73
|
+
if (!type || typeof type !== 'string') return false;
|
|
74
|
+
const [modelName, effect] = type.split('/');
|
|
75
|
+
const model = registedModel[modelName];
|
|
76
|
+
if (model) {
|
|
77
|
+
if (model.effects && model.effects[effect]) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return () => (next: (arg: any) => any) => (action: ReduxAction) => {
|
|
85
|
+
const { type } = action;
|
|
86
|
+
if (isEffect(type)) {
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
next({
|
|
89
|
+
_dy_resolve: resolve,
|
|
90
|
+
_dy_reject: reject,
|
|
91
|
+
...action,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
return next(action);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
19
101
|
/**
|
|
20
102
|
* 获取注册model函数
|
|
21
103
|
* @param store redux store
|
|
@@ -24,7 +106,9 @@ function saveState(state, action) {
|
|
|
24
106
|
* @param sagaMiddleware saga中间件
|
|
25
107
|
* @returns 返回function regist(model)
|
|
26
108
|
*/
|
|
27
|
-
function getRegistModelFunc(store, registedModel
|
|
109
|
+
function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: RegistedModel,
|
|
110
|
+
allReducers: { [x: string]: Reducer<any, AnyAction>; },
|
|
111
|
+
sagaMiddleware: SagaMiddleware<object>): (model: ReduxModel) => void {
|
|
28
112
|
/** model函数注册函数
|
|
29
113
|
* @param model 模块, 其格式为
|
|
30
114
|
* {
|
|
@@ -34,7 +118,7 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
|
|
|
34
118
|
* effects: {}
|
|
35
119
|
* }
|
|
36
120
|
*/
|
|
37
|
-
return function regist(model) {
|
|
121
|
+
return function regist(model: ReduxModel): void {
|
|
38
122
|
if (registedModel[model.name]) {
|
|
39
123
|
return;
|
|
40
124
|
}
|
|
@@ -46,8 +130,11 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
|
|
|
46
130
|
if (!model.reducers) {
|
|
47
131
|
model.reducers = {};
|
|
48
132
|
}
|
|
49
|
-
if (!model.
|
|
50
|
-
model.
|
|
133
|
+
if (!model.reducers.saveState) {
|
|
134
|
+
model.reducers.saveState = saveState;
|
|
135
|
+
}
|
|
136
|
+
if (!model.effects) {
|
|
137
|
+
model.effects = {};
|
|
51
138
|
}
|
|
52
139
|
const modelSlice = createSlice(model);
|
|
53
140
|
const reducer = modelSlice.reducer;
|
|
@@ -58,10 +145,9 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
|
|
|
58
145
|
store.replaceReducer(newReducer);
|
|
59
146
|
//注册effects
|
|
60
147
|
for (let effect in model.effects) {
|
|
61
|
-
let type = `${model.name}/${effect}`;
|
|
148
|
+
let type: string = `${model.name}/${effect}`;
|
|
62
149
|
let execFun = model.effects[effect];
|
|
63
|
-
|
|
64
|
-
function* loading(opFun, action) {
|
|
150
|
+
function* loading(opFun: any, action: any) {
|
|
65
151
|
// 开始异步任务设置loading状态
|
|
66
152
|
yield putResolve({ type: `${model.name}/saveState`, payload: { loading: true } });
|
|
67
153
|
let ret = yield call(execFun, action, opFun);
|
|
@@ -82,12 +168,13 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
|
|
|
82
168
|
};
|
|
83
169
|
}
|
|
84
170
|
|
|
171
|
+
|
|
85
172
|
/** 创建store */
|
|
86
|
-
export default function create() {
|
|
173
|
+
export default function create(): ReduxApp {
|
|
87
174
|
//已经注册的reducer, key是名字, value是reducer
|
|
88
175
|
const allReducers = {};
|
|
89
176
|
//已注册model
|
|
90
|
-
const registedModel = {};
|
|
177
|
+
const registedModel: RegistedModel = {};
|
|
91
178
|
|
|
92
179
|
const sagaMiddleware = createSagaMiddleware();
|
|
93
180
|
|
|
@@ -113,7 +200,8 @@ export default function create() {
|
|
|
113
200
|
);
|
|
114
201
|
|
|
115
202
|
const regist = getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware);
|
|
116
|
-
|
|
203
|
+
|
|
204
|
+
let app: ReduxApp = {
|
|
117
205
|
/** redux store */
|
|
118
206
|
store: store,
|
|
119
207
|
/** saga中间件 */
|
|
@@ -121,4 +209,5 @@ export default function create() {
|
|
|
121
209
|
/** model注册函数 */
|
|
122
210
|
regist: regist
|
|
123
211
|
};
|
|
212
|
+
return app;
|
|
124
213
|
}
|
package/src/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param registedModel 已注册model
|
|
3
|
-
*/
|
|
4
|
-
export default function createPromiseMiddleware(registedModel) {
|
|
5
|
-
return () => next => action => {
|
|
6
|
-
const { type } = action;
|
|
7
|
-
if (isEffect(type)) {
|
|
8
|
-
return new Promise((resolve, reject) => {
|
|
9
|
-
next({
|
|
10
|
-
_dy_resolve: resolve,
|
|
11
|
-
_dy_reject: reject,
|
|
12
|
-
...action,
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
} else {
|
|
16
|
-
return next(action);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
function isEffect(type) {
|
|
21
|
-
if (!type || typeof type !== 'string') return false;
|
|
22
|
-
const [modelName, effect] = type.split('/');
|
|
23
|
-
const model = registedModel[modelName];
|
|
24
|
-
if (model) {
|
|
25
|
-
if (model.effects && model.effects[effect]) {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
}
|