ez-saga 0.1.5 → 17.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ez-saga",
3
- "version": "0.1.5",
3
+ "version": "17.0.0",
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": "^2.0.1",
30
- "redux": "^5.0.1",
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
  },
@@ -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
4
  import { call, put, select, takeEvery, putResolve } from 'redux-saga/effects';
5
5
  import win from 'global/window';
6
6
  import createPromiseMiddleware from './createPromiseMiddleware';
7
+ import { Reducer, AnyAction } from 'redux';
7
8
 
8
- function saveState(state, action) {
9
+ const saveState: Reducer<any, AnyAction> = (state: any, action: AnyAction) => {
9
10
  if (!action.payload) {
10
11
  return state;
11
12
  }
@@ -16,6 +17,49 @@ 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
+ export interface EffectTool {
26
+ call: any,
27
+ put: any,
28
+ select: any
29
+ }
30
+
31
+ /** Effect函数类型定义 */
32
+ export interface Effect {
33
+ (action: PayloadAction, tool: EffectTool): Promise<any>;
34
+ }
35
+
36
+ /** ModelReducer定义 */
37
+ export interface ModelReducer {
38
+ [key: string]: Reducer<any, AnyAction>;
39
+ }
40
+
41
+
42
+ /** ModelEffect定义 */
43
+ export interface ModelEffect {
44
+ [key: string]: Effect;
45
+ }
46
+
47
+ export interface ReduxModel {
48
+ name: string,
49
+ state: any;
50
+ initialState: any,
51
+ reducers: ModelReducer
52
+ effects: ModelEffect;
53
+ }
54
+
55
+ /** app */
56
+ export interface ReduxApp {
57
+ store: Store<any, ReduxAction>,
58
+ sagaMiddleware: SagaMiddleware<object>;
59
+ regist: (model: ReduxModel) => void
60
+ }
61
+
62
+
19
63
  /**
20
64
  * 获取注册model函数
21
65
  * @param store redux store
@@ -24,7 +68,9 @@ function saveState(state, action) {
24
68
  * @param sagaMiddleware saga中间件
25
69
  * @returns 返回function regist(model)
26
70
  */
27
- function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
71
+ function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: { [x: string]: ReduxModel; },
72
+ allReducers: { [x: string]: Reducer<any, AnyAction>; },
73
+ sagaMiddleware: SagaMiddleware<object>): (model: ReduxModel) => void {
28
74
  /** model函数注册函数
29
75
  * @param model 模块, 其格式为
30
76
  * {
@@ -34,7 +80,7 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
34
80
  * effects: {}
35
81
  * }
36
82
  */
37
- return function regist(model) {
83
+ return function regist(model: ReduxModel): void {
38
84
  if (registedModel[model.name]) {
39
85
  return;
40
86
  }
@@ -46,8 +92,11 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
46
92
  if (!model.reducers) {
47
93
  model.reducers = {};
48
94
  }
49
- if (!model.effect) {
50
- model.effect = {};
95
+ if (!model.reducers.saveState) {
96
+ model.reducers.saveState = saveState;
97
+ }
98
+ if (!model.effects) {
99
+ model.effects = {};
51
100
  }
52
101
  const modelSlice = createSlice(model);
53
102
  const reducer = modelSlice.reducer;
@@ -58,10 +107,9 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
58
107
  store.replaceReducer(newReducer);
59
108
  //注册effects
60
109
  for (let effect in model.effects) {
61
- let type = `${model.name}/${effect}`;
110
+ let type: string = `${model.name}/${effect}`;
62
111
  let execFun = model.effects[effect];
63
-
64
- function* loading(opFun, action) {
112
+ function* loading(opFun: any, action: any) {
65
113
  // 开始异步任务设置loading状态
66
114
  yield putResolve({ type: `${model.name}/saveState`, payload: { loading: true } });
67
115
  let ret = yield call(execFun, action, opFun);
@@ -83,7 +131,7 @@ function getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware) {
83
131
  }
84
132
 
85
133
  /** 创建store */
86
- export default function create() {
134
+ export default function create(): ReduxApp {
87
135
  //已经注册的reducer, key是名字, value是reducer
88
136
  const allReducers = {};
89
137
  //已注册model
@@ -113,7 +161,8 @@ export default function create() {
113
161
  );
114
162
 
115
163
  const regist = getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware);
116
- return {
164
+
165
+ let app: ReduxApp = {
117
166
  /** redux store */
118
167
  store: store,
119
168
  /** saga中间件 */
@@ -121,4 +170,5 @@ export default function create() {
121
170
  /** model注册函数 */
122
171
  regist: regist
123
172
  };
173
+ return app;
124
174
  }