@cclr/model 0.1.9 → 0.1.11
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/lib/cjs/index.js +216 -123
- package/lib/esm/index.js +211 -124
- package/lib/type/index.d.ts +61 -34
- package/package.json +4 -4
package/lib/cjs/index.js
CHANGED
|
@@ -1,10 +1,114 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var immer = require('immer');
|
|
4
3
|
var lang = require('@cclr/lang');
|
|
4
|
+
var immer = require('immer');
|
|
5
|
+
|
|
6
|
+
var middlewareConfig = {
|
|
7
|
+
showLog: true
|
|
8
|
+
};
|
|
9
|
+
var handleModelMiddlewareConfig = function handleModelMiddlewareConfig(config) {
|
|
10
|
+
Reflect.ownKeys(config).forEach(function (key) {
|
|
11
|
+
if (lang.isString(key)) {
|
|
12
|
+
lang.set(middlewareConfig, key, config[key]);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
var gatter = function gatter(ctx) {
|
|
18
|
+
return function (next) {
|
|
19
|
+
return function (params) {
|
|
20
|
+
return new Promise(function ($return, $error) {
|
|
21
|
+
var action, payload, state, actions;
|
|
22
|
+
action = ctx.action, payload = ctx.payload, state = ctx.state, actions = ctx.actions;
|
|
23
|
+
return Promise.resolve(action(payload, {
|
|
24
|
+
state: state,
|
|
25
|
+
actions: actions
|
|
26
|
+
})).then(function ($await_1) {
|
|
27
|
+
try {
|
|
28
|
+
ctx.newState = $await_1;
|
|
29
|
+
return $return(next(params));
|
|
30
|
+
} catch ($boundEx) {
|
|
31
|
+
return $error($boundEx);
|
|
32
|
+
}
|
|
33
|
+
}, $error);
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
var gatterSync = function gatterSync(ctx) {
|
|
39
|
+
return function (next) {
|
|
40
|
+
return function (params) {
|
|
41
|
+
var action = ctx.action,
|
|
42
|
+
payload = ctx.payload,
|
|
43
|
+
state = ctx.state,
|
|
44
|
+
actions = ctx.actions;
|
|
45
|
+
ctx.newState = action(payload, {
|
|
46
|
+
state: state,
|
|
47
|
+
actions: actions
|
|
48
|
+
});
|
|
49
|
+
return next(params);
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
var logger = function logger(ctx) {
|
|
55
|
+
return function (next) {
|
|
56
|
+
return function (params) {
|
|
57
|
+
return new Promise(function ($return, $error) {
|
|
58
|
+
var showLog, r;
|
|
59
|
+
showLog = middlewareConfig.showLog;
|
|
60
|
+
if (!showLog) {
|
|
61
|
+
return $return(next(params));
|
|
62
|
+
}
|
|
63
|
+
console.log(ctx.modelName, '- action -start: ', ctx.action.name);
|
|
64
|
+
return Promise.resolve(next(params)).then(function ($await_1) {
|
|
65
|
+
try {
|
|
66
|
+
r = $await_1;
|
|
67
|
+
console.log(ctx.modelName, '- action -end: ', ctx.action.name);
|
|
68
|
+
return $return(r);
|
|
69
|
+
} catch ($boundEx) {
|
|
70
|
+
return $error($boundEx);
|
|
71
|
+
}
|
|
72
|
+
}, $error);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
var loggerSync = function loggerSync(ctx) {
|
|
78
|
+
return function (next) {
|
|
79
|
+
return function (params) {
|
|
80
|
+
var showLog = middlewareConfig.showLog;
|
|
81
|
+
if (!showLog) {
|
|
82
|
+
return next(params);
|
|
83
|
+
}
|
|
84
|
+
console.log(ctx.modelName, '- action -start: ', ctx.action.name);
|
|
85
|
+
var r = next(params);
|
|
86
|
+
console.log(ctx.modelName, '- action -end: ', ctx.action.name);
|
|
87
|
+
return r;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 发布函数
|
|
94
|
+
* @param ctx
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
var publish = function publish(ctx) {
|
|
98
|
+
return function (next) {
|
|
99
|
+
return function (params) {
|
|
100
|
+
var subscribes = ctx.subscribes;
|
|
101
|
+
subscribes === null || subscribes === void 0 || subscribes.forEach(function (callback) {
|
|
102
|
+
callback();
|
|
103
|
+
});
|
|
104
|
+
return next(params);
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
};
|
|
5
108
|
|
|
6
109
|
var State = {};
|
|
7
110
|
var Actions = {};
|
|
111
|
+
var Middlewares = {};
|
|
8
112
|
var Subscribes = {
|
|
9
113
|
/*******
|
|
10
114
|
[modelName]: [
|
|
@@ -16,14 +120,16 @@ var GlobalModel = {
|
|
|
16
120
|
__MId: 1,
|
|
17
121
|
State: State,
|
|
18
122
|
Actions: Actions,
|
|
19
|
-
Subscribes: Subscribes
|
|
123
|
+
Subscribes: Subscribes,
|
|
124
|
+
Middlewares: Middlewares
|
|
20
125
|
};
|
|
21
126
|
|
|
22
127
|
var getCtx = function getCtx(modelName) {
|
|
23
128
|
return {
|
|
24
129
|
state: GlobalModel.State[modelName],
|
|
25
130
|
actions: GlobalModel.Actions[modelName],
|
|
26
|
-
subscribes: GlobalModel.Subscribes[modelName]
|
|
131
|
+
subscribes: GlobalModel.Subscribes[modelName],
|
|
132
|
+
middlewares: GlobalModel.Middlewares[modelName]
|
|
27
133
|
};
|
|
28
134
|
};
|
|
29
135
|
var getState = function getState(modelName) {
|
|
@@ -33,6 +139,69 @@ var getActions = function getActions(modelName) {
|
|
|
33
139
|
return GlobalModel.Actions[modelName];
|
|
34
140
|
};
|
|
35
141
|
|
|
142
|
+
/**
|
|
143
|
+
* action返回
|
|
144
|
+
* @param ctx
|
|
145
|
+
* @returns
|
|
146
|
+
*/
|
|
147
|
+
var returnState = function returnState(ctx) {
|
|
148
|
+
return function (_next) {
|
|
149
|
+
return function (_params) {
|
|
150
|
+
return getState(ctx.modelName);
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
var produceState = function produceState(base, recipe) {
|
|
156
|
+
return immer.produce(base, recipe);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
var setter = function setter(ctx) {
|
|
160
|
+
return function (next) {
|
|
161
|
+
return function (params) {
|
|
162
|
+
var modelName = ctx.modelName;
|
|
163
|
+
var newState = ctx.newState;
|
|
164
|
+
if (!newState) {
|
|
165
|
+
return getState(modelName);
|
|
166
|
+
}
|
|
167
|
+
if (lang.isFunction(newState)) {
|
|
168
|
+
newState = produceState(getState(modelName), newState);
|
|
169
|
+
}
|
|
170
|
+
GlobalModel.State = immer.produce(GlobalModel.State, function (draft) {
|
|
171
|
+
draft[modelName] = newState;
|
|
172
|
+
});
|
|
173
|
+
return next(params);
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
var tryCatch = function tryCatch() {
|
|
179
|
+
return function (next) {
|
|
180
|
+
return function (params) {
|
|
181
|
+
return new Promise(function ($return, $error) {
|
|
182
|
+
var _next;
|
|
183
|
+
return Promise.resolve((_next = next(params)) === null || _next === void 0 ? void 0 : _next.catch(function (e) {
|
|
184
|
+
return console.log(e);
|
|
185
|
+
})).then($return, $error);
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
var tryCatchSync = function tryCatchSync() {
|
|
191
|
+
return function (next) {
|
|
192
|
+
return function (params) {
|
|
193
|
+
try {
|
|
194
|
+
return next(params);
|
|
195
|
+
} catch (e) {
|
|
196
|
+
console.log(e);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
var presetMiddleware = [tryCatch, logger, gatter, setter, publish, returnState];
|
|
203
|
+
var presetMiddlewareSync = [tryCatchSync, loggerSync, gatterSync, setter, publish, returnState];
|
|
204
|
+
|
|
36
205
|
var applyMiddleware = function applyMiddleware() {};
|
|
37
206
|
|
|
38
207
|
function _arrayLikeToArray(r, a) {
|
|
@@ -136,118 +305,6 @@ function _unsupportedIterableToArray(r, a) {
|
|
|
136
305
|
}
|
|
137
306
|
}
|
|
138
307
|
|
|
139
|
-
var gatter = function gatter(ctx) {
|
|
140
|
-
return function (next) {
|
|
141
|
-
return function (params) {
|
|
142
|
-
return new Promise(function ($return, $error) {
|
|
143
|
-
var action, payload, state, actions;
|
|
144
|
-
action = ctx.action, payload = ctx.payload, state = ctx.state, actions = ctx.actions;
|
|
145
|
-
return Promise.resolve(action(payload, {
|
|
146
|
-
state: state,
|
|
147
|
-
actions: actions
|
|
148
|
-
})).then(function ($await_1) {
|
|
149
|
-
try {
|
|
150
|
-
ctx.newState = $await_1;
|
|
151
|
-
return $return(next(params));
|
|
152
|
-
} catch ($boundEx) {
|
|
153
|
-
return $error($boundEx);
|
|
154
|
-
}
|
|
155
|
-
}, $error);
|
|
156
|
-
});
|
|
157
|
-
};
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
var logger = function logger(ctx) {
|
|
162
|
-
return function (next) {
|
|
163
|
-
return function (params) {
|
|
164
|
-
return new Promise(function ($return, $error) {
|
|
165
|
-
var r;
|
|
166
|
-
console.log(ctx.modelName, '- action -start: ', ctx.action.name);
|
|
167
|
-
return Promise.resolve(next(params)).then(function ($await_1) {
|
|
168
|
-
try {
|
|
169
|
-
r = $await_1;
|
|
170
|
-
console.log(ctx.modelName, '- action -end: ', ctx.action.name);
|
|
171
|
-
return $return(r);
|
|
172
|
-
} catch ($boundEx) {
|
|
173
|
-
return $error($boundEx);
|
|
174
|
-
}
|
|
175
|
-
}, $error);
|
|
176
|
-
});
|
|
177
|
-
};
|
|
178
|
-
};
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 发布函数
|
|
183
|
-
* @param ctx
|
|
184
|
-
* @returns
|
|
185
|
-
*/
|
|
186
|
-
var publish = function publish(ctx) {
|
|
187
|
-
return function (next) {
|
|
188
|
-
return function (params) {
|
|
189
|
-
var subscribes = ctx.subscribes;
|
|
190
|
-
subscribes === null || subscribes === void 0 || subscribes.forEach(function (callback) {
|
|
191
|
-
callback();
|
|
192
|
-
});
|
|
193
|
-
return next(params);
|
|
194
|
-
};
|
|
195
|
-
};
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* action返回
|
|
200
|
-
* @param ctx
|
|
201
|
-
* @returns
|
|
202
|
-
*/
|
|
203
|
-
var returnState = function returnState(ctx) {
|
|
204
|
-
return function (_next) {
|
|
205
|
-
return function (_params) {
|
|
206
|
-
return getState(ctx.modelName);
|
|
207
|
-
};
|
|
208
|
-
};
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
var produceState = function produceState(base, recipe) {
|
|
212
|
-
return immer.produce(base, recipe);
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
var setter = function setter(ctx) {
|
|
216
|
-
return function (next) {
|
|
217
|
-
return function (params) {
|
|
218
|
-
return new Promise(function ($return, $error) {
|
|
219
|
-
var modelName = ctx.modelName;
|
|
220
|
-
var newState = ctx.newState;
|
|
221
|
-
if (!newState) {
|
|
222
|
-
return $return(getState(modelName));
|
|
223
|
-
}
|
|
224
|
-
if (lang.isFunction(newState)) {
|
|
225
|
-
newState = produceState(getState(modelName), newState);
|
|
226
|
-
}
|
|
227
|
-
GlobalModel.State = immer.produce(GlobalModel.State, function (draft) {
|
|
228
|
-
draft[modelName] = newState;
|
|
229
|
-
});
|
|
230
|
-
return $return(next(params));
|
|
231
|
-
});
|
|
232
|
-
};
|
|
233
|
-
};
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
var tryCatch = function tryCatch() {
|
|
237
|
-
return function (next) {
|
|
238
|
-
return function (params) {
|
|
239
|
-
return new Promise(function ($return, $error) {
|
|
240
|
-
var _next;
|
|
241
|
-
return Promise.resolve((_next = next(params)) === null || _next === void 0 ? void 0 : _next.catch(function (e) {
|
|
242
|
-
return console.log(e);
|
|
243
|
-
})).then($return, $error);
|
|
244
|
-
});
|
|
245
|
-
};
|
|
246
|
-
};
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
var presetMiddleware = [tryCatch, logger, gatter, setter, publish, returnState];
|
|
250
|
-
|
|
251
308
|
function compose() {
|
|
252
309
|
for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
253
310
|
funcs[_key] = arguments[_key];
|
|
@@ -267,7 +324,7 @@ function compose() {
|
|
|
267
324
|
});
|
|
268
325
|
}
|
|
269
326
|
|
|
270
|
-
var rebuildActions = function rebuildActions(
|
|
327
|
+
var rebuildActions = function rebuildActions(modelName, actions) {
|
|
271
328
|
var updateActions = {};
|
|
272
329
|
Object.entries(actions).forEach(function (_ref) {
|
|
273
330
|
var _ref2 = _slicedToArray(_ref, 2),
|
|
@@ -282,7 +339,8 @@ var resetAction = function resetAction(action, modelName) {
|
|
|
282
339
|
var _getCtx = getCtx(modelName),
|
|
283
340
|
state = _getCtx.state,
|
|
284
341
|
actions = _getCtx.actions,
|
|
285
|
-
subscribes = _getCtx.subscribes
|
|
342
|
+
subscribes = _getCtx.subscribes,
|
|
343
|
+
middlewares = _getCtx.middlewares;
|
|
286
344
|
var ctx = {
|
|
287
345
|
payload: payload,
|
|
288
346
|
state: state,
|
|
@@ -293,10 +351,10 @@ var resetAction = function resetAction(action, modelName) {
|
|
|
293
351
|
newState: null,
|
|
294
352
|
setterFun: undefined
|
|
295
353
|
};
|
|
296
|
-
var
|
|
354
|
+
var middlewaresContent = middlewares.map(function (middleware) {
|
|
297
355
|
return middleware(ctx);
|
|
298
356
|
});
|
|
299
|
-
var composeFun = compose.apply(void 0, _toConsumableArray(
|
|
357
|
+
var composeFun = compose.apply(void 0, _toConsumableArray(middlewaresContent));
|
|
300
358
|
return composeFun(function () {
|
|
301
359
|
return getState(modelName);
|
|
302
360
|
})();
|
|
@@ -309,11 +367,15 @@ var initModel = function initModel(models) {
|
|
|
309
367
|
modelName = _ref2[0],
|
|
310
368
|
model = _ref2[1];
|
|
311
369
|
GlobalModel.__MId++;
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
370
|
+
initSingleModel(modelName, model);
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
var initSingleModel = function initSingleModel(modelName, model) {
|
|
374
|
+
GlobalModel.Middlewares[modelName] = model.middleware || presetMiddleware;
|
|
375
|
+
GlobalModel.State = produceState(GlobalModel.State, function (draft) {
|
|
376
|
+
draft[modelName] = model.state;
|
|
316
377
|
});
|
|
378
|
+
GlobalModel.Actions[modelName] = _objectSpread2({}, rebuildActions(modelName, model.actions));
|
|
317
379
|
};
|
|
318
380
|
|
|
319
381
|
/**
|
|
@@ -362,11 +424,42 @@ var Model = function Model(models) {
|
|
|
362
424
|
};
|
|
363
425
|
};
|
|
364
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Model 类
|
|
429
|
+
*/
|
|
430
|
+
var ModelSingle = function ModelSingle(model) {
|
|
431
|
+
/** 初始化 */
|
|
432
|
+
var mId = GlobalModel.__MId++;
|
|
433
|
+
var modelName = "modelsName-".concat(mId);
|
|
434
|
+
initSingleModel(modelName, model);
|
|
435
|
+
return {
|
|
436
|
+
getState: function getState$1() {
|
|
437
|
+
return getState(modelName);
|
|
438
|
+
},
|
|
439
|
+
getActions: function getActions$1() {
|
|
440
|
+
return getActions(modelName);
|
|
441
|
+
},
|
|
442
|
+
subscribe: function subscribe$1(callback) {
|
|
443
|
+
return subscribe(modelName, callback);
|
|
444
|
+
},
|
|
445
|
+
unsubscribe: function unsubscribe$1(callback) {
|
|
446
|
+
return unsubscribe(modelName, callback);
|
|
447
|
+
},
|
|
448
|
+
applyMiddleware: applyMiddleware
|
|
449
|
+
};
|
|
450
|
+
};
|
|
451
|
+
|
|
365
452
|
exports.Model = Model;
|
|
453
|
+
exports.ModelSingle = ModelSingle;
|
|
366
454
|
exports.gatter = gatter;
|
|
455
|
+
exports.gatterSync = gatterSync;
|
|
456
|
+
exports.handleModelMiddlewareConfig = handleModelMiddlewareConfig;
|
|
367
457
|
exports.logger = logger;
|
|
458
|
+
exports.loggerSync = loggerSync;
|
|
368
459
|
exports.presetMiddleware = presetMiddleware;
|
|
460
|
+
exports.presetMiddlewareSync = presetMiddlewareSync;
|
|
369
461
|
exports.publish = publish;
|
|
370
462
|
exports.returnState = returnState;
|
|
371
463
|
exports.setter = setter;
|
|
372
464
|
exports.tryCatch = tryCatch;
|
|
465
|
+
exports.tryCatchSync = tryCatchSync;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,8 +1,112 @@
|
|
|
1
|
+
import { isString, set, isFunction } from '@cclr/lang';
|
|
1
2
|
import { produce } from 'immer';
|
|
2
|
-
|
|
3
|
+
|
|
4
|
+
var middlewareConfig = {
|
|
5
|
+
showLog: true
|
|
6
|
+
};
|
|
7
|
+
var handleModelMiddlewareConfig = function handleModelMiddlewareConfig(config) {
|
|
8
|
+
Reflect.ownKeys(config).forEach(function (key) {
|
|
9
|
+
if (isString(key)) {
|
|
10
|
+
set(middlewareConfig, key, config[key]);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
var gatter = function gatter(ctx) {
|
|
16
|
+
return function (next) {
|
|
17
|
+
return function (params) {
|
|
18
|
+
return new Promise(function ($return, $error) {
|
|
19
|
+
var action, payload, state, actions;
|
|
20
|
+
action = ctx.action, payload = ctx.payload, state = ctx.state, actions = ctx.actions;
|
|
21
|
+
return Promise.resolve(action(payload, {
|
|
22
|
+
state: state,
|
|
23
|
+
actions: actions
|
|
24
|
+
})).then(function ($await_1) {
|
|
25
|
+
try {
|
|
26
|
+
ctx.newState = $await_1;
|
|
27
|
+
return $return(next(params));
|
|
28
|
+
} catch ($boundEx) {
|
|
29
|
+
return $error($boundEx);
|
|
30
|
+
}
|
|
31
|
+
}, $error);
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
var gatterSync = function gatterSync(ctx) {
|
|
37
|
+
return function (next) {
|
|
38
|
+
return function (params) {
|
|
39
|
+
var action = ctx.action,
|
|
40
|
+
payload = ctx.payload,
|
|
41
|
+
state = ctx.state,
|
|
42
|
+
actions = ctx.actions;
|
|
43
|
+
ctx.newState = action(payload, {
|
|
44
|
+
state: state,
|
|
45
|
+
actions: actions
|
|
46
|
+
});
|
|
47
|
+
return next(params);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
var logger = function logger(ctx) {
|
|
53
|
+
return function (next) {
|
|
54
|
+
return function (params) {
|
|
55
|
+
return new Promise(function ($return, $error) {
|
|
56
|
+
var showLog, r;
|
|
57
|
+
showLog = middlewareConfig.showLog;
|
|
58
|
+
if (!showLog) {
|
|
59
|
+
return $return(next(params));
|
|
60
|
+
}
|
|
61
|
+
console.log(ctx.modelName, '- action -start: ', ctx.action.name);
|
|
62
|
+
return Promise.resolve(next(params)).then(function ($await_1) {
|
|
63
|
+
try {
|
|
64
|
+
r = $await_1;
|
|
65
|
+
console.log(ctx.modelName, '- action -end: ', ctx.action.name);
|
|
66
|
+
return $return(r);
|
|
67
|
+
} catch ($boundEx) {
|
|
68
|
+
return $error($boundEx);
|
|
69
|
+
}
|
|
70
|
+
}, $error);
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
var loggerSync = function loggerSync(ctx) {
|
|
76
|
+
return function (next) {
|
|
77
|
+
return function (params) {
|
|
78
|
+
var showLog = middlewareConfig.showLog;
|
|
79
|
+
if (!showLog) {
|
|
80
|
+
return next(params);
|
|
81
|
+
}
|
|
82
|
+
console.log(ctx.modelName, '- action -start: ', ctx.action.name);
|
|
83
|
+
var r = next(params);
|
|
84
|
+
console.log(ctx.modelName, '- action -end: ', ctx.action.name);
|
|
85
|
+
return r;
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 发布函数
|
|
92
|
+
* @param ctx
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
var publish = function publish(ctx) {
|
|
96
|
+
return function (next) {
|
|
97
|
+
return function (params) {
|
|
98
|
+
var subscribes = ctx.subscribes;
|
|
99
|
+
subscribes === null || subscribes === void 0 || subscribes.forEach(function (callback) {
|
|
100
|
+
callback();
|
|
101
|
+
});
|
|
102
|
+
return next(params);
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
};
|
|
3
106
|
|
|
4
107
|
var State = {};
|
|
5
108
|
var Actions = {};
|
|
109
|
+
var Middlewares = {};
|
|
6
110
|
var Subscribes = {
|
|
7
111
|
/*******
|
|
8
112
|
[modelName]: [
|
|
@@ -14,14 +118,16 @@ var GlobalModel = {
|
|
|
14
118
|
__MId: 1,
|
|
15
119
|
State: State,
|
|
16
120
|
Actions: Actions,
|
|
17
|
-
Subscribes: Subscribes
|
|
121
|
+
Subscribes: Subscribes,
|
|
122
|
+
Middlewares: Middlewares
|
|
18
123
|
};
|
|
19
124
|
|
|
20
125
|
var getCtx = function getCtx(modelName) {
|
|
21
126
|
return {
|
|
22
127
|
state: GlobalModel.State[modelName],
|
|
23
128
|
actions: GlobalModel.Actions[modelName],
|
|
24
|
-
subscribes: GlobalModel.Subscribes[modelName]
|
|
129
|
+
subscribes: GlobalModel.Subscribes[modelName],
|
|
130
|
+
middlewares: GlobalModel.Middlewares[modelName]
|
|
25
131
|
};
|
|
26
132
|
};
|
|
27
133
|
var getState = function getState(modelName) {
|
|
@@ -31,6 +137,69 @@ var getActions = function getActions(modelName) {
|
|
|
31
137
|
return GlobalModel.Actions[modelName];
|
|
32
138
|
};
|
|
33
139
|
|
|
140
|
+
/**
|
|
141
|
+
* action返回
|
|
142
|
+
* @param ctx
|
|
143
|
+
* @returns
|
|
144
|
+
*/
|
|
145
|
+
var returnState = function returnState(ctx) {
|
|
146
|
+
return function (_next) {
|
|
147
|
+
return function (_params) {
|
|
148
|
+
return getState(ctx.modelName);
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
var produceState = function produceState(base, recipe) {
|
|
154
|
+
return produce(base, recipe);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
var setter = function setter(ctx) {
|
|
158
|
+
return function (next) {
|
|
159
|
+
return function (params) {
|
|
160
|
+
var modelName = ctx.modelName;
|
|
161
|
+
var newState = ctx.newState;
|
|
162
|
+
if (!newState) {
|
|
163
|
+
return getState(modelName);
|
|
164
|
+
}
|
|
165
|
+
if (isFunction(newState)) {
|
|
166
|
+
newState = produceState(getState(modelName), newState);
|
|
167
|
+
}
|
|
168
|
+
GlobalModel.State = produce(GlobalModel.State, function (draft) {
|
|
169
|
+
draft[modelName] = newState;
|
|
170
|
+
});
|
|
171
|
+
return next(params);
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
var tryCatch = function tryCatch() {
|
|
177
|
+
return function (next) {
|
|
178
|
+
return function (params) {
|
|
179
|
+
return new Promise(function ($return, $error) {
|
|
180
|
+
var _next;
|
|
181
|
+
return Promise.resolve((_next = next(params)) === null || _next === void 0 ? void 0 : _next.catch(function (e) {
|
|
182
|
+
return console.log(e);
|
|
183
|
+
})).then($return, $error);
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
var tryCatchSync = function tryCatchSync() {
|
|
189
|
+
return function (next) {
|
|
190
|
+
return function (params) {
|
|
191
|
+
try {
|
|
192
|
+
return next(params);
|
|
193
|
+
} catch (e) {
|
|
194
|
+
console.log(e);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
var presetMiddleware = [tryCatch, logger, gatter, setter, publish, returnState];
|
|
201
|
+
var presetMiddlewareSync = [tryCatchSync, loggerSync, gatterSync, setter, publish, returnState];
|
|
202
|
+
|
|
34
203
|
var applyMiddleware = function applyMiddleware() {};
|
|
35
204
|
|
|
36
205
|
function _arrayLikeToArray(r, a) {
|
|
@@ -134,118 +303,6 @@ function _unsupportedIterableToArray(r, a) {
|
|
|
134
303
|
}
|
|
135
304
|
}
|
|
136
305
|
|
|
137
|
-
var gatter = function gatter(ctx) {
|
|
138
|
-
return function (next) {
|
|
139
|
-
return function (params) {
|
|
140
|
-
return new Promise(function ($return, $error) {
|
|
141
|
-
var action, payload, state, actions;
|
|
142
|
-
action = ctx.action, payload = ctx.payload, state = ctx.state, actions = ctx.actions;
|
|
143
|
-
return Promise.resolve(action(payload, {
|
|
144
|
-
state: state,
|
|
145
|
-
actions: actions
|
|
146
|
-
})).then(function ($await_1) {
|
|
147
|
-
try {
|
|
148
|
-
ctx.newState = $await_1;
|
|
149
|
-
return $return(next(params));
|
|
150
|
-
} catch ($boundEx) {
|
|
151
|
-
return $error($boundEx);
|
|
152
|
-
}
|
|
153
|
-
}, $error);
|
|
154
|
-
});
|
|
155
|
-
};
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
var logger = function logger(ctx) {
|
|
160
|
-
return function (next) {
|
|
161
|
-
return function (params) {
|
|
162
|
-
return new Promise(function ($return, $error) {
|
|
163
|
-
var r;
|
|
164
|
-
console.log(ctx.modelName, '- action -start: ', ctx.action.name);
|
|
165
|
-
return Promise.resolve(next(params)).then(function ($await_1) {
|
|
166
|
-
try {
|
|
167
|
-
r = $await_1;
|
|
168
|
-
console.log(ctx.modelName, '- action -end: ', ctx.action.name);
|
|
169
|
-
return $return(r);
|
|
170
|
-
} catch ($boundEx) {
|
|
171
|
-
return $error($boundEx);
|
|
172
|
-
}
|
|
173
|
-
}, $error);
|
|
174
|
-
});
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 发布函数
|
|
181
|
-
* @param ctx
|
|
182
|
-
* @returns
|
|
183
|
-
*/
|
|
184
|
-
var publish = function publish(ctx) {
|
|
185
|
-
return function (next) {
|
|
186
|
-
return function (params) {
|
|
187
|
-
var subscribes = ctx.subscribes;
|
|
188
|
-
subscribes === null || subscribes === void 0 || subscribes.forEach(function (callback) {
|
|
189
|
-
callback();
|
|
190
|
-
});
|
|
191
|
-
return next(params);
|
|
192
|
-
};
|
|
193
|
-
};
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* action返回
|
|
198
|
-
* @param ctx
|
|
199
|
-
* @returns
|
|
200
|
-
*/
|
|
201
|
-
var returnState = function returnState(ctx) {
|
|
202
|
-
return function (_next) {
|
|
203
|
-
return function (_params) {
|
|
204
|
-
return getState(ctx.modelName);
|
|
205
|
-
};
|
|
206
|
-
};
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
var produceState = function produceState(base, recipe) {
|
|
210
|
-
return produce(base, recipe);
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
var setter = function setter(ctx) {
|
|
214
|
-
return function (next) {
|
|
215
|
-
return function (params) {
|
|
216
|
-
return new Promise(function ($return, $error) {
|
|
217
|
-
var modelName = ctx.modelName;
|
|
218
|
-
var newState = ctx.newState;
|
|
219
|
-
if (!newState) {
|
|
220
|
-
return $return(getState(modelName));
|
|
221
|
-
}
|
|
222
|
-
if (isFunction(newState)) {
|
|
223
|
-
newState = produceState(getState(modelName), newState);
|
|
224
|
-
}
|
|
225
|
-
GlobalModel.State = produce(GlobalModel.State, function (draft) {
|
|
226
|
-
draft[modelName] = newState;
|
|
227
|
-
});
|
|
228
|
-
return $return(next(params));
|
|
229
|
-
});
|
|
230
|
-
};
|
|
231
|
-
};
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
var tryCatch = function tryCatch() {
|
|
235
|
-
return function (next) {
|
|
236
|
-
return function (params) {
|
|
237
|
-
return new Promise(function ($return, $error) {
|
|
238
|
-
var _next;
|
|
239
|
-
return Promise.resolve((_next = next(params)) === null || _next === void 0 ? void 0 : _next.catch(function (e) {
|
|
240
|
-
return console.log(e);
|
|
241
|
-
})).then($return, $error);
|
|
242
|
-
});
|
|
243
|
-
};
|
|
244
|
-
};
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
var presetMiddleware = [tryCatch, logger, gatter, setter, publish, returnState];
|
|
248
|
-
|
|
249
306
|
function compose() {
|
|
250
307
|
for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
251
308
|
funcs[_key] = arguments[_key];
|
|
@@ -265,7 +322,7 @@ function compose() {
|
|
|
265
322
|
});
|
|
266
323
|
}
|
|
267
324
|
|
|
268
|
-
var rebuildActions = function rebuildActions(
|
|
325
|
+
var rebuildActions = function rebuildActions(modelName, actions) {
|
|
269
326
|
var updateActions = {};
|
|
270
327
|
Object.entries(actions).forEach(function (_ref) {
|
|
271
328
|
var _ref2 = _slicedToArray(_ref, 2),
|
|
@@ -280,7 +337,8 @@ var resetAction = function resetAction(action, modelName) {
|
|
|
280
337
|
var _getCtx = getCtx(modelName),
|
|
281
338
|
state = _getCtx.state,
|
|
282
339
|
actions = _getCtx.actions,
|
|
283
|
-
subscribes = _getCtx.subscribes
|
|
340
|
+
subscribes = _getCtx.subscribes,
|
|
341
|
+
middlewares = _getCtx.middlewares;
|
|
284
342
|
var ctx = {
|
|
285
343
|
payload: payload,
|
|
286
344
|
state: state,
|
|
@@ -291,10 +349,10 @@ var resetAction = function resetAction(action, modelName) {
|
|
|
291
349
|
newState: null,
|
|
292
350
|
setterFun: undefined
|
|
293
351
|
};
|
|
294
|
-
var
|
|
352
|
+
var middlewaresContent = middlewares.map(function (middleware) {
|
|
295
353
|
return middleware(ctx);
|
|
296
354
|
});
|
|
297
|
-
var composeFun = compose.apply(void 0, _toConsumableArray(
|
|
355
|
+
var composeFun = compose.apply(void 0, _toConsumableArray(middlewaresContent));
|
|
298
356
|
return composeFun(function () {
|
|
299
357
|
return getState(modelName);
|
|
300
358
|
})();
|
|
@@ -307,11 +365,15 @@ var initModel = function initModel(models) {
|
|
|
307
365
|
modelName = _ref2[0],
|
|
308
366
|
model = _ref2[1];
|
|
309
367
|
GlobalModel.__MId++;
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
368
|
+
initSingleModel(modelName, model);
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
var initSingleModel = function initSingleModel(modelName, model) {
|
|
372
|
+
GlobalModel.Middlewares[modelName] = model.middleware || presetMiddleware;
|
|
373
|
+
GlobalModel.State = produceState(GlobalModel.State, function (draft) {
|
|
374
|
+
draft[modelName] = model.state;
|
|
314
375
|
});
|
|
376
|
+
GlobalModel.Actions[modelName] = _objectSpread2({}, rebuildActions(modelName, model.actions));
|
|
315
377
|
};
|
|
316
378
|
|
|
317
379
|
/**
|
|
@@ -360,4 +422,29 @@ var Model = function Model(models) {
|
|
|
360
422
|
};
|
|
361
423
|
};
|
|
362
424
|
|
|
363
|
-
|
|
425
|
+
/**
|
|
426
|
+
* Model 类
|
|
427
|
+
*/
|
|
428
|
+
var ModelSingle = function ModelSingle(model) {
|
|
429
|
+
/** 初始化 */
|
|
430
|
+
var mId = GlobalModel.__MId++;
|
|
431
|
+
var modelName = "modelsName-".concat(mId);
|
|
432
|
+
initSingleModel(modelName, model);
|
|
433
|
+
return {
|
|
434
|
+
getState: function getState$1() {
|
|
435
|
+
return getState(modelName);
|
|
436
|
+
},
|
|
437
|
+
getActions: function getActions$1() {
|
|
438
|
+
return getActions(modelName);
|
|
439
|
+
},
|
|
440
|
+
subscribe: function subscribe$1(callback) {
|
|
441
|
+
return subscribe(modelName, callback);
|
|
442
|
+
},
|
|
443
|
+
unsubscribe: function unsubscribe$1(callback) {
|
|
444
|
+
return unsubscribe(modelName, callback);
|
|
445
|
+
},
|
|
446
|
+
applyMiddleware: applyMiddleware
|
|
447
|
+
};
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
export { Model, ModelSingle, gatter, gatterSync, handleModelMiddlewareConfig, logger, loggerSync, presetMiddleware, presetMiddlewareSync, publish, returnState, setter, tryCatch, tryCatchSync };
|
package/lib/type/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TAny, TPlainObject } from '@cclr/lang';
|
|
1
|
+
import { TAny, TPlainObject, TNoop } from '@cclr/lang';
|
|
2
2
|
|
|
3
3
|
type IsOptional<T> = Extract<T, undefined> extends never ? false : true;
|
|
4
4
|
type FirstArg<F> = F extends (arg1: infer A, ...args: any[]) => any ? A : never;
|
|
@@ -35,38 +35,6 @@ type TActionApi<A extends (...args: any[]) => any, S extends TState> = (arg: Fir
|
|
|
35
35
|
|
|
36
36
|
type TSubscribesFun = () => any;
|
|
37
37
|
|
|
38
|
-
/**
|
|
39
|
-
* 获取model的api
|
|
40
|
-
*/
|
|
41
|
-
type ModelsApi<MS extends ModelsType> = {
|
|
42
|
-
/** 获取state的值 */
|
|
43
|
-
getState<K extends keyof MS>(params: K): MS[K]['state'];
|
|
44
|
-
/** 获取actions */
|
|
45
|
-
getActions<K extends keyof MS>(params: K): TActionMapApi<MS[K]['actions'], MS[K]['state']>;
|
|
46
|
-
/** 订阅模块更新 */
|
|
47
|
-
subscribe<K extends keyof MS>(params: K, callback: TSubscribesFun): () => void;
|
|
48
|
-
/** 取消订阅 */
|
|
49
|
-
unsubscribe<K extends keyof MS>(params: K, callback: TSubscribesFun): void;
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* models的类型
|
|
53
|
-
*/
|
|
54
|
-
type ModelsType<S extends TState = TState, PM extends TPlainObject = TPlainObject> = {
|
|
55
|
-
[modelName: string]: ModelType<S, PM>;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* 单个model的类型
|
|
59
|
-
*/
|
|
60
|
-
type ModelType<S extends TState = TState, PM extends TPlainObject = TPlainObject> = {
|
|
61
|
-
state: S;
|
|
62
|
-
actions: TActionMap<S, PM>;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Model 类
|
|
67
|
-
*/
|
|
68
|
-
declare const Model: <MS extends ModelsType>(models: MS) => ModelsApi<MS>;
|
|
69
|
-
|
|
70
38
|
/** 中间件上下文 */
|
|
71
39
|
interface IActionCtx {
|
|
72
40
|
/** model的唯一索引 */
|
|
@@ -94,9 +62,57 @@ type TNextFun<P extends TActionParams = TActionParams, S extends TState = TState
|
|
|
94
62
|
/** 中间件 */
|
|
95
63
|
type TMiddleware<P extends TActionParams = TActionParams, S extends TState = TState, C extends IActionCtx = IActionCtx> = (ctx: C) => TNextFun<P, S>;
|
|
96
64
|
|
|
65
|
+
/**
|
|
66
|
+
* 获取model的api
|
|
67
|
+
*/
|
|
68
|
+
type ModelsApi<MS extends ModelsType> = {
|
|
69
|
+
/** 获取state的值 */
|
|
70
|
+
getState<K extends keyof MS>(params: K): MS[K]['state'];
|
|
71
|
+
/** 获取actions */
|
|
72
|
+
getActions<K extends keyof MS>(params: K): TActionMapApi<MS[K]['actions'], MS[K]['state']>;
|
|
73
|
+
/** 订阅模块更新 */
|
|
74
|
+
subscribe<K extends keyof MS>(params: K, callback: TSubscribesFun): TNoop;
|
|
75
|
+
/** 取消订阅 */
|
|
76
|
+
unsubscribe<K extends keyof MS>(params: K, callback: TSubscribesFun): void;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* 获取model的api
|
|
80
|
+
*/
|
|
81
|
+
type ModelsSingleApi<MT extends ModelType> = {
|
|
82
|
+
/** 获取state的值 */
|
|
83
|
+
getState(): MT['state'];
|
|
84
|
+
/** 获取actions */
|
|
85
|
+
getActions(): TActionMapApi<MT['actions'], MT['state']>;
|
|
86
|
+
/** 订阅模块更新 */
|
|
87
|
+
subscribe(callback: TSubscribesFun): TNoop;
|
|
88
|
+
/** 取消订阅 */
|
|
89
|
+
unsubscribe(callback: TSubscribesFun): void;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* models的类型
|
|
93
|
+
*/
|
|
94
|
+
type ModelsType<S extends TState = TState, PM extends TPlainObject = TPlainObject> = {
|
|
95
|
+
[modelName: string]: ModelType<S, PM>;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* 单个model的类型
|
|
99
|
+
*/
|
|
100
|
+
type ModelType<S extends TState = TState, PM extends TPlainObject = TPlainObject> = {
|
|
101
|
+
state: S;
|
|
102
|
+
actions: TActionMap<S, PM>;
|
|
103
|
+
middleware?: TMiddleware[];
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
declare const middlewareConfig: {
|
|
107
|
+
showLog: boolean;
|
|
108
|
+
};
|
|
109
|
+
declare const handleModelMiddlewareConfig: (config: Partial<typeof middlewareConfig>) => void;
|
|
110
|
+
|
|
97
111
|
declare const gatter: TMiddleware;
|
|
112
|
+
declare const gatterSync: TMiddleware;
|
|
98
113
|
|
|
99
114
|
declare const logger: TMiddleware;
|
|
115
|
+
declare const loggerSync: TMiddleware;
|
|
100
116
|
|
|
101
117
|
/**
|
|
102
118
|
* 发布函数
|
|
@@ -115,7 +131,18 @@ declare const returnState: TMiddleware;
|
|
|
115
131
|
declare const setter: TMiddleware;
|
|
116
132
|
|
|
117
133
|
declare const tryCatch: () => (next: any) => (params: any) => Promise<any>;
|
|
134
|
+
declare const tryCatchSync: () => (next: any) => (params: any) => any;
|
|
118
135
|
|
|
119
136
|
declare const presetMiddleware: TMiddleware[];
|
|
137
|
+
declare const presetMiddlewareSync: TMiddleware[];
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Model 类
|
|
141
|
+
*/
|
|
142
|
+
declare const Model: <MS extends ModelsType>(models: MS) => ModelsApi<MS>;
|
|
143
|
+
/**
|
|
144
|
+
* Model 类
|
|
145
|
+
*/
|
|
146
|
+
declare const ModelSingle: <MT extends ModelType>(model: MT) => ModelsSingleApi<MT>;
|
|
120
147
|
|
|
121
|
-
export { Model, type ModelType, type ModelsApi, type ModelsType, type TAction, type TActionMapApi, type TMiddleware, type TSubscribesFun, gatter, logger, presetMiddleware, publish, returnState, setter, tryCatch };
|
|
148
|
+
export { Model, ModelSingle, type ModelType, type ModelsApi, type ModelsSingleApi, type ModelsType, type TAction, type TActionMapApi, type TMiddleware, type TSubscribesFun, gatter, gatterSync, handleModelMiddlewareConfig, logger, loggerSync, presetMiddleware, presetMiddlewareSync, publish, returnState, setter, tryCatch, tryCatchSync };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cclr/model",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "前端开发状态管理",
|
|
5
5
|
"author": "cclr <18843152354@163.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"g:test": "vitest run",
|
|
28
28
|
"g:build": "ccf build"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "e03c9c40bc86083ba49fe9f372375a50223191cc",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@cclr/lang": "
|
|
33
|
-
"@cclr/utils": "
|
|
32
|
+
"@cclr/lang": "0.1.11",
|
|
33
|
+
"@cclr/utils": "0.1.11",
|
|
34
34
|
"immer": "^10.1.1"
|
|
35
35
|
}
|
|
36
36
|
}
|