ez-saga 17.0.0 → 17.0.2

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": "17.0.0",
3
+ "version": "17.0.2",
4
4
  "description": "The ez-saga project is a project that imitates dva-js",
5
5
  "main": "src/index.js",
6
6
  "sideEffects": false,
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,9 +1,9 @@
1
1
  import { legacy_createStore as createStore, applyMiddleware, compose, Action as ReduxAction, Store } from 'redux';
2
2
  import { createSlice, combineReducers } from '@reduxjs/toolkit';
3
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 createPromiseMiddleware from './createPromiseMiddleware';
7
7
  import { Reducer, AnyAction } from 'redux';
8
8
 
9
9
  const saveState: Reducer<any, AnyAction> = (state: any, action: AnyAction) => {
@@ -17,20 +17,21 @@ const saveState: Reducer<any, AnyAction> = (state: any, action: AnyAction) => {
17
17
  return newStat;
18
18
  }
19
19
 
20
- /** payload action定义 */
20
+ /** payload action类型 */
21
21
  export interface PayloadAction extends Action<string>, ReduxAction<string> {
22
22
  payload: any
23
23
  }
24
24
 
25
+ /** 工具 */
25
26
  export interface EffectTool {
26
- call: any,
27
- put: any,
28
- select: any
27
+ call: (...args: any[]) => any,
28
+ put: <A extends Action>(action: Action) => PutEffect<A>,
29
+ select: (selectFunc: (state: any) => any) => SelectEffect
29
30
  }
30
31
 
31
- /** Effect函数类型定义 */
32
+ /** Effect函数类型 */
32
33
  export interface Effect {
33
- (action: PayloadAction, tool: EffectTool): Promise<any>;
34
+ (action: PayloadAction, tool: EffectTool): Generator;
34
35
  }
35
36
 
36
37
  /** ModelReducer定义 */
@@ -52,6 +53,10 @@ export interface ReduxModel {
52
53
  effects: ModelEffect;
53
54
  }
54
55
 
56
+ export interface RegistedModel {
57
+ [key: string]: ReduxModel;
58
+ }
59
+
55
60
  /** app */
56
61
  export interface ReduxApp {
57
62
  store: Store<any, ReduxAction>,
@@ -59,6 +64,39 @@ export interface ReduxApp {
59
64
  regist: (model: ReduxModel) => void
60
65
  }
61
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
+
62
100
 
63
101
  /**
64
102
  * 获取注册model函数
@@ -68,7 +106,7 @@ export interface ReduxApp {
68
106
  * @param sagaMiddleware saga中间件
69
107
  * @returns 返回function regist(model)
70
108
  */
71
- function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: { [x: string]: ReduxModel; },
109
+ function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: RegistedModel,
72
110
  allReducers: { [x: string]: Reducer<any, AnyAction>; },
73
111
  sagaMiddleware: SagaMiddleware<object>): (model: ReduxModel) => void {
74
112
  /** model函数注册函数
@@ -130,12 +168,13 @@ function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: { [x:
130
168
  };
131
169
  }
132
170
 
171
+
133
172
  /** 创建store */
134
173
  export default function create(): ReduxApp {
135
174
  //已经注册的reducer, key是名字, value是reducer
136
175
  const allReducers = {};
137
176
  //已注册model
138
- const registedModel = {};
177
+ const registedModel: RegistedModel = {};
139
178
 
140
179
  const sagaMiddleware = createSagaMiddleware();
141
180
 
@@ -161,7 +200,7 @@ export default function create(): ReduxApp {
161
200
  );
162
201
 
163
202
  const regist = getRegistModelFunc(store, registedModel, allReducers, sagaMiddleware);
164
-
203
+
165
204
  let app: ReduxApp = {
166
205
  /** redux store */
167
206
  store: store,
package/src/index.js DELETED
@@ -1,4 +0,0 @@
1
- import createApp from './redux/createApp';
2
- export default {
3
- createApp: createApp
4
- };
@@ -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
- }