ez-saga 17.0.9 → 17.1.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ez-saga",
3
- "version": "17.0.9",
3
+ "version": "17.1.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,
@@ -49,7 +49,7 @@
49
49
  "babel-loader": "^9.1.3",
50
50
  "babel-plugin-dynamic-import-webpack": "^1.1.0",
51
51
  "babel-plugin-import": "^1.13.8",
52
- "chalk": "^5.3.0",
52
+ "chalk": "^4.1.2",
53
53
  "copy-webpack-plugin": "^12.0.2",
54
54
  "core-js": "^3.35.1",
55
55
  "cross-env": "^7.0.3",
@@ -67,7 +67,9 @@
67
67
  "webpack-bundle-analyzer": "^4.10.1",
68
68
  "webpack-cli": "^5.1.4",
69
69
  "webpack-dev-server": "^4.15.1",
70
- "webpack-merge": "^5.10.0"
70
+ "webpack-merge": "^5.10.0",
71
+ "ts-loader": "^9.5.1",
72
+ "typescript": "^5.3.3"
71
73
  },
72
74
  "engines": {
73
75
  "node": ">= 14.0.0",
@@ -83,6 +85,8 @@
83
85
  "dist",
84
86
  "lib",
85
87
  "src",
86
- "index.js"
88
+ "index.js",
89
+ "tysconfig.json",
90
+ "typing.d.ts"
87
91
  ]
88
92
  }
package/src/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { PayloadAction, EffectTool, Effect, ModelReducer, ModelEffect, ReduxModel, ReduxApp } from './redux/createApp';
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/createApp';
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,41 @@
1
+ import { Dispatch, Middleware, MiddlewareAPI, Action } from 'redux';
2
+ import { RegistedModel } from './typeDeclare';
3
+ import { PayloadAction } from './typeDeclare';
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
+
21
+ return (api: MiddlewareAPI<D, any>) => (next: (action: PayloadAction) => any) => (action: PayloadAction) => {
22
+ let exeEffect = false;
23
+ if ((action as Action)?.type !== undefined) {
24
+ const { type } = action as Action;
25
+ exeEffect = isEffect(type);
26
+ }
27
+ if (exeEffect) {
28
+ return new Promise((resolve, reject) => {
29
+ next({
30
+ _dy_resolve: resolve,
31
+ _dy_reject: reject,
32
+ ...action as Action,
33
+ });
34
+ });
35
+ } else {
36
+ return next(action);
37
+ }
38
+ };
39
+ }
40
+
41
+ export default createPromiseMiddleware;
@@ -1,106 +1,12 @@
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, { Action, SagaMiddleware } from 'redux-saga';
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
- import { Reducer, AnyAction } from 'redux';
8
-
9
- const saveState: Reducer<any, AnyAction> = (state: any, action: AnyAction) => {
10
- if (!action.payload) {
11
- return state;
12
- }
13
- let newStat = {
14
- ...state,
15
- ...action.payload
16
- };
17
- 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
-
6
+ import { Reducer, Action } from 'redux';
7
+ import { ReduxModel, ReduxApp, RegistedModel, ReduxSagaModel, EffectTool, PayloadAction } from './typeDeclare';
8
+ import saveState from './defaultReducer';
9
+ import createPromiseMiddleware from './PromiseMiddleware';
104
10
 
105
11
  /**
106
12
  * 获取注册model函数
@@ -111,7 +17,7 @@ function createPromiseMiddleware(registedModel: RegistedModel) {
111
17
  * @returns 返回function regist(model)
112
18
  */
113
19
  function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: RegistedModel,
114
- allReducers: { [x: string]: Reducer<any, AnyAction>; },
20
+ allReducers: { [x: string]: Reducer<any, Action>; },
115
21
  sagaMiddleware: SagaMiddleware<object>): (model: ReduxModel) => void {
116
22
  /** model函数注册函数
117
23
  * @param model 模块, 其格式为
@@ -154,7 +60,7 @@ function getRegistModelFunc(store: Store<any, ReduxAction>, registedModel: Regis
154
60
  for (let effect in model.effects) {
155
61
  let type: string = `${model.name}/${effect}`;
156
62
  let execFun = model.effects[effect];
157
- function* loading(opFun: any, action: any) {
63
+ function* loading(opFun: EffectTool, action: PayloadAction) {
158
64
  // 开始异步任务设置loading状态
159
65
  yield putResolve({ type: `${model.name}/saveState`, payload: { loading: true } });
160
66
  let ret = yield call(execFun, action, opFun);
@@ -0,0 +1,16 @@
1
+ /** 默认reducer */
2
+ import { Reducer } from 'redux';
3
+ import { PayloadAction } from './typeDeclare';
4
+
5
+ const saveState: Reducer<any, PayloadAction> = (state: any, action: PayloadAction) => {
6
+ if (!action.payload) {
7
+ return state;
8
+ }
9
+ let newStat = {
10
+ ...state,
11
+ ...action.payload
12
+ };
13
+ return newStat;
14
+ };
15
+
16
+ export default saveState;
@@ -0,0 +1,73 @@
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
+ [extraProps: string]: any;
15
+ }
16
+
17
+ /** 工具 */
18
+ export interface EffectTool {
19
+ /** 调用异步函数, 并获得该异步函数的结果 */
20
+ call: (...args: any[]) => any,
21
+ /** 分发异步action */
22
+ put: <A extends Action>(action: A) => PutEffect<A>,
23
+ /** 分发同步action */
24
+ putResolve: <A extends Action>(action: A) => PutEffect<A>,
25
+ /** 从store从获取状态 */
26
+ select: ((selectFunc: (state: any) => any) => SelectEffect) | (() => SelectEffect)
27
+ }
28
+
29
+ /** Effect函数类型 */
30
+ export interface Effect {
31
+ (action: PayloadAction, tool: EffectTool): Generator | any;
32
+ }
33
+
34
+ /** ModelReducer定义 */
35
+ export interface ModelReducer {
36
+ [key: string]: Reducer<any, PayloadAction>;
37
+ }
38
+
39
+
40
+ /** ModelEffect定义 */
41
+ export interface ModelEffect {
42
+ [key: string]: Effect;
43
+ }
44
+
45
+ export interface ReduxModel {
46
+ name: string,
47
+ state?: any;
48
+ reducers?: ModelReducer
49
+ effects?: ModelEffect;
50
+ }
51
+
52
+ export interface ReduxSagaModel extends ReduxModel {
53
+ initialState: any,
54
+ reducerPath: string
55
+ reducers: ModelReducer
56
+ }
57
+
58
+ export interface RegistedModel {
59
+ [key: string]: ReduxModel
60
+ }
61
+
62
+ /** app */
63
+ export interface ReduxApp {
64
+ store: Store<any, ReduxAction>,
65
+ sagaMiddleware: SagaMiddleware<object>;
66
+ regist: (model: ReduxModel) => void
67
+ }
68
+
69
+
70
+ export interface ReduxSagaModel extends ReduxModel {
71
+ initialState: any,
72
+ reducers: ModelReducer
73
+ }