as-model 0.1.17 → 0.1.18
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/dist/index.js +117 -85
- package/esm/tools/index.js +13 -1
- package/esm/updater/index.js +6 -5
- package/esm/updater/notifier.js +20 -4
- package/index.d.ts +6 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -184,6 +184,96 @@
|
|
|
184
184
|
isModelUsage
|
|
185
185
|
};
|
|
186
186
|
|
|
187
|
+
// src/tools/proxy.ts
|
|
188
|
+
function getDescriptors(target, receiver, ownOrPrototype, handler) {
|
|
189
|
+
var it = Object.keys(ownOrPrototype);
|
|
190
|
+
var result = {};
|
|
191
|
+
it.forEach(function(key) {
|
|
192
|
+
result[key] = {
|
|
193
|
+
get: function() {
|
|
194
|
+
if (!handler.get) {
|
|
195
|
+
return target[key];
|
|
196
|
+
}
|
|
197
|
+
return handler.get(target, key, receiver);
|
|
198
|
+
},
|
|
199
|
+
set: function(v) {
|
|
200
|
+
if (!handler.set) {
|
|
201
|
+
target[key] = v;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
var valid = handler.set(target, key, v, receiver);
|
|
205
|
+
if (!valid) {
|
|
206
|
+
throw new Error("".concat(key, " in proxy target is not mutable"));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
});
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
var createSimpleProxy = function(target, handler) {
|
|
214
|
+
var proxy = {};
|
|
215
|
+
var own = getDescriptors(target, proxy, target, handler);
|
|
216
|
+
var prototype = getDescriptors(target, proxy, Object.getPrototypeOf(target), handler);
|
|
217
|
+
Object.defineProperties(proxy, _object_spread({}, prototype, own));
|
|
218
|
+
return proxy;
|
|
219
|
+
};
|
|
220
|
+
var createProxy = function(target, handler) {
|
|
221
|
+
if (typeof Proxy !== "function") {
|
|
222
|
+
return createSimpleProxy(target, handler);
|
|
223
|
+
}
|
|
224
|
+
return new Proxy(target, handler);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// src/tools/shallowEqual.ts
|
|
228
|
+
function isObject(data) {
|
|
229
|
+
return data && (typeof data === "undefined" ? "undefined" : _type_of(data)) === "object";
|
|
230
|
+
}
|
|
231
|
+
function shallowEqual(prev, current) {
|
|
232
|
+
if (Object.is(prev, current)) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
if (!isObject(prev) || !isObject(current)) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
var prevKeys = Object.keys(prev);
|
|
239
|
+
var currentKeys = Object.keys(current);
|
|
240
|
+
if (prevKeys.length !== currentKeys.length) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
var pre = prev;
|
|
244
|
+
var curr = current;
|
|
245
|
+
var hasDiffKey = prevKeys.some(function(key) {
|
|
246
|
+
return !Object.prototype.hasOwnProperty.call(curr, key);
|
|
247
|
+
});
|
|
248
|
+
if (hasDiffKey) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
var hasDiffValue = currentKeys.some(function(key) {
|
|
252
|
+
var currentValue = curr[key];
|
|
253
|
+
var prevValue = pre[key];
|
|
254
|
+
return !Object.is(currentValue, prevValue);
|
|
255
|
+
});
|
|
256
|
+
return !hasDiffValue;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/tools/index.ts
|
|
260
|
+
function noop() {
|
|
261
|
+
}
|
|
262
|
+
function simpleErrorProcess(errors) {
|
|
263
|
+
return function wrap(callback) {
|
|
264
|
+
return function replaced() {
|
|
265
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
266
|
+
args[_key] = arguments[_key];
|
|
267
|
+
}
|
|
268
|
+
try {
|
|
269
|
+
return callback.apply(void 0, _to_consumable_array(args));
|
|
270
|
+
} catch (e) {
|
|
271
|
+
errors.push(e);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
187
277
|
// src/updater/notifier.ts
|
|
188
278
|
function defaultNotifyImplement(dispatches, action) {
|
|
189
279
|
dispatches.forEach(function(callback) {
|
|
@@ -261,7 +351,6 @@
|
|
|
261
351
|
}
|
|
262
352
|
var dispatch = function dispatch2(action) {
|
|
263
353
|
var dispatches = updater.dispatches, controlled = updater.controlled, model2 = updater.model, config2 = updater.config;
|
|
264
|
-
var dispatchCallbacks = _to_consumable_array(dispatches);
|
|
265
354
|
var state = action.state;
|
|
266
355
|
var nextInstance = model2(state);
|
|
267
356
|
var nextAction = _object_spread_props(_object_spread({}, action), {
|
|
@@ -276,11 +365,31 @@
|
|
|
276
365
|
});
|
|
277
366
|
});
|
|
278
367
|
}
|
|
368
|
+
var notifyAction = function notifyAction2(act) {
|
|
369
|
+
var errors = [];
|
|
370
|
+
var dispatchCallbacks = dispatches.map(simpleErrorProcess(errors));
|
|
371
|
+
defaultNotifyImplement(dispatchCallbacks, act);
|
|
372
|
+
if (!errors.length) {
|
|
373
|
+
return {
|
|
374
|
+
errors: void 0
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
errors
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
var notifyActionWithErrorThrow = function notifyActionWithErrorThrow2(act) {
|
|
382
|
+
var errors = notifyAction(act).errors;
|
|
383
|
+
if (!errors || !errors.length) {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
throw errors[0];
|
|
387
|
+
};
|
|
279
388
|
try {
|
|
280
|
-
if (typeof config2.
|
|
281
|
-
config2.
|
|
389
|
+
if (typeof config2.notify === "function") {
|
|
390
|
+
config2.notify(notifyAction, nextAction);
|
|
282
391
|
} else {
|
|
283
|
-
|
|
392
|
+
notifyActionWithErrorThrow(nextAction);
|
|
284
393
|
}
|
|
285
394
|
} catch (e) {
|
|
286
395
|
updater.mutate(function(u) {
|
|
@@ -325,82 +434,6 @@
|
|
|
325
434
|
};
|
|
326
435
|
}
|
|
327
436
|
|
|
328
|
-
// src/tools/proxy.ts
|
|
329
|
-
function getDescriptors(target, receiver, ownOrPrototype, handler) {
|
|
330
|
-
var it = Object.keys(ownOrPrototype);
|
|
331
|
-
var result = {};
|
|
332
|
-
it.forEach(function(key) {
|
|
333
|
-
result[key] = {
|
|
334
|
-
get: function() {
|
|
335
|
-
if (!handler.get) {
|
|
336
|
-
return target[key];
|
|
337
|
-
}
|
|
338
|
-
return handler.get(target, key, receiver);
|
|
339
|
-
},
|
|
340
|
-
set: function(v) {
|
|
341
|
-
if (!handler.set) {
|
|
342
|
-
target[key] = v;
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
var valid = handler.set(target, key, v, receiver);
|
|
346
|
-
if (!valid) {
|
|
347
|
-
throw new Error("".concat(key, " in proxy target is not mutable"));
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
};
|
|
351
|
-
});
|
|
352
|
-
return result;
|
|
353
|
-
}
|
|
354
|
-
var createSimpleProxy = function(target, handler) {
|
|
355
|
-
var proxy = {};
|
|
356
|
-
var own = getDescriptors(target, proxy, target, handler);
|
|
357
|
-
var prototype = getDescriptors(target, proxy, Object.getPrototypeOf(target), handler);
|
|
358
|
-
Object.defineProperties(proxy, _object_spread({}, prototype, own));
|
|
359
|
-
return proxy;
|
|
360
|
-
};
|
|
361
|
-
var createProxy = function(target, handler) {
|
|
362
|
-
if (typeof Proxy !== "function") {
|
|
363
|
-
return createSimpleProxy(target, handler);
|
|
364
|
-
}
|
|
365
|
-
return new Proxy(target, handler);
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
// src/tools/shallowEqual.ts
|
|
369
|
-
function isObject(data) {
|
|
370
|
-
return data && (typeof data === "undefined" ? "undefined" : _type_of(data)) === "object";
|
|
371
|
-
}
|
|
372
|
-
function shallowEqual(prev, current) {
|
|
373
|
-
if (Object.is(prev, current)) {
|
|
374
|
-
return true;
|
|
375
|
-
}
|
|
376
|
-
if (!isObject(prev) || !isObject(current)) {
|
|
377
|
-
return false;
|
|
378
|
-
}
|
|
379
|
-
var prevKeys = Object.keys(prev);
|
|
380
|
-
var currentKeys = Object.keys(current);
|
|
381
|
-
if (prevKeys.length !== currentKeys.length) {
|
|
382
|
-
return false;
|
|
383
|
-
}
|
|
384
|
-
var pre = prev;
|
|
385
|
-
var curr = current;
|
|
386
|
-
var hasDiffKey = prevKeys.some(function(key) {
|
|
387
|
-
return !Object.prototype.hasOwnProperty.call(curr, key);
|
|
388
|
-
});
|
|
389
|
-
if (hasDiffKey) {
|
|
390
|
-
return false;
|
|
391
|
-
}
|
|
392
|
-
var hasDiffValue = currentKeys.some(function(key) {
|
|
393
|
-
var currentValue = curr[key];
|
|
394
|
-
var prevValue = pre[key];
|
|
395
|
-
return !Object.is(currentValue, prevValue);
|
|
396
|
-
});
|
|
397
|
-
return !hasDiffValue;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// src/tools/index.ts
|
|
401
|
-
function noop() {
|
|
402
|
-
}
|
|
403
|
-
|
|
404
437
|
// src/updater/tunnel.ts
|
|
405
438
|
function createUnInitializedUpdater() {
|
|
406
439
|
return {
|
|
@@ -546,7 +579,6 @@
|
|
|
546
579
|
updater.mutate(function(u, effect) {
|
|
547
580
|
var _args_model;
|
|
548
581
|
var model2 = (_args_model = args.model) !== null && _args_model !== void 0 ? _args_model : u.model;
|
|
549
|
-
var initialState = "initialState" in args ? args.initialState : u.state;
|
|
550
582
|
var state = "state" in args ? args.state : u.state;
|
|
551
583
|
if (u.controlled) {
|
|
552
584
|
var instance = model2(state);
|
|
@@ -559,15 +591,15 @@
|
|
|
559
591
|
if (u.isDestroyed) {
|
|
560
592
|
return u;
|
|
561
593
|
}
|
|
562
|
-
if (!u.initialized && !("
|
|
563
|
-
throw new Error("
|
|
594
|
+
if (!u.initialized && !("state" in args)) {
|
|
595
|
+
throw new Error("The updater has not been initialized, it should be updated with a state for initializing.");
|
|
564
596
|
}
|
|
565
597
|
if (!u.initialized) {
|
|
566
|
-
var instance1 = model2(
|
|
598
|
+
var instance1 = model2(state);
|
|
567
599
|
var initializedUpdater = createInitializedUpdater(u, middleWare);
|
|
568
600
|
return _object_spread(_object_spread_props(_object_spread({}, u), {
|
|
569
601
|
model: model2,
|
|
570
|
-
state
|
|
602
|
+
state,
|
|
571
603
|
instance: instance1,
|
|
572
604
|
initialized: true
|
|
573
605
|
}), initializedUpdater);
|
package/esm/tools/index.js
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
1
|
function noop() {
|
|
2
2
|
}
|
|
3
|
+
function simpleErrorProcess(errors) {
|
|
4
|
+
return function wrap(callback) {
|
|
5
|
+
return function replaced(...args) {
|
|
6
|
+
try {
|
|
7
|
+
return callback(...args);
|
|
8
|
+
} catch (e) {
|
|
9
|
+
errors.push(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
3
14
|
import { createSimpleProxy, createProxy } from "./proxy";
|
|
4
15
|
import { shallowEqual } from "./shallowEqual";
|
|
5
16
|
export {
|
|
6
17
|
createProxy,
|
|
7
18
|
createSimpleProxy,
|
|
8
19
|
noop,
|
|
9
|
-
shallowEqual
|
|
20
|
+
shallowEqual,
|
|
21
|
+
simpleErrorProcess
|
|
10
22
|
};
|
package/esm/updater/index.js
CHANGED
|
@@ -36,7 +36,6 @@ function createUpdateFn(updater, middleWare) {
|
|
|
36
36
|
updater.mutate((u, effect) => {
|
|
37
37
|
var _a;
|
|
38
38
|
const model = (_a = args.model) != null ? _a : u.model;
|
|
39
|
-
const initialState = "initialState" in args ? args.initialState : u.state;
|
|
40
39
|
const state = "state" in args ? args.state : u.state;
|
|
41
40
|
if (u.controlled) {
|
|
42
41
|
const instance2 = model(state);
|
|
@@ -45,15 +44,17 @@ function createUpdateFn(updater, middleWare) {
|
|
|
45
44
|
if (u.isDestroyed) {
|
|
46
45
|
return u;
|
|
47
46
|
}
|
|
48
|
-
if (!u.initialized && !("
|
|
49
|
-
throw new Error(
|
|
47
|
+
if (!u.initialized && !("state" in args)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"The updater has not been initialized, it should be updated with a state for initializing."
|
|
50
|
+
);
|
|
50
51
|
}
|
|
51
52
|
if (!u.initialized) {
|
|
52
|
-
const instance2 = model(
|
|
53
|
+
const instance2 = model(state);
|
|
53
54
|
const initializedUpdater = createInitializedUpdater(u, middleWare);
|
|
54
55
|
return __spreadValues(__spreadProps(__spreadValues({}, u), {
|
|
55
56
|
model,
|
|
56
|
-
state
|
|
57
|
+
state,
|
|
57
58
|
instance: instance2,
|
|
58
59
|
initialized: true
|
|
59
60
|
}), initializedUpdater);
|
package/esm/updater/notifier.js
CHANGED
|
@@ -17,6 +17,7 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
19
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { simpleErrorProcess } from "../tools";
|
|
20
21
|
function defaultNotifyImplement(dispatches, action) {
|
|
21
22
|
dispatches.forEach((callback) => {
|
|
22
23
|
callback(action);
|
|
@@ -80,7 +81,6 @@ function generateNotifier(updater, middleWare) {
|
|
|
80
81
|
}
|
|
81
82
|
const dispatch = function dispatch2(action) {
|
|
82
83
|
const { dispatches, controlled, model, config } = updater;
|
|
83
|
-
const dispatchCallbacks = [...dispatches];
|
|
84
84
|
const { state } = action;
|
|
85
85
|
const nextInstance = model(state);
|
|
86
86
|
const nextAction = __spreadProps(__spreadValues({}, action), { instance: nextInstance });
|
|
@@ -91,11 +91,27 @@ function generateNotifier(updater, middleWare) {
|
|
|
91
91
|
version: u.version + 1
|
|
92
92
|
}));
|
|
93
93
|
}
|
|
94
|
+
const notifyAction = function notifyAction2(act) {
|
|
95
|
+
const errors = [];
|
|
96
|
+
const dispatchCallbacks = dispatches.map(simpleErrorProcess(errors));
|
|
97
|
+
defaultNotifyImplement(dispatchCallbacks, act);
|
|
98
|
+
if (!errors.length) {
|
|
99
|
+
return { errors: void 0 };
|
|
100
|
+
}
|
|
101
|
+
return { errors };
|
|
102
|
+
};
|
|
103
|
+
const notifyActionWithErrorThrow = function notifyActionWithErrorThrow2(act) {
|
|
104
|
+
const { errors } = notifyAction(act);
|
|
105
|
+
if (!errors || !errors.length) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
throw errors[0];
|
|
109
|
+
};
|
|
94
110
|
try {
|
|
95
|
-
if (typeof config.
|
|
96
|
-
config.
|
|
111
|
+
if (typeof config.notify === "function") {
|
|
112
|
+
config.notify(notifyAction, nextAction);
|
|
97
113
|
} else {
|
|
98
|
-
|
|
114
|
+
notifyActionWithErrorThrow(nextAction);
|
|
99
115
|
}
|
|
100
116
|
} catch (e) {
|
|
101
117
|
updater.mutate((u) => __spreadProps(__spreadValues({}, u), { dispatching: void 0 }));
|
package/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ declare interface ModelInstance {
|
|
|
3
3
|
[key: number]: any;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
declare type FieldStructure<R = any> = {
|
|
6
|
+
export declare type FieldStructure<R = any> = {
|
|
7
7
|
callback: () => R;
|
|
8
8
|
deps: any[] | undefined;
|
|
9
9
|
identifier: (d: any) => d is FieldStructure<R>;
|
|
@@ -11,7 +11,7 @@ declare type FieldStructure<R = any> = {
|
|
|
11
11
|
get: () => R;
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
declare type MethodStructure<
|
|
14
|
+
export declare type MethodStructure<
|
|
15
15
|
R extends (...args: any[]) => any = (...args: any[]) => any
|
|
16
16
|
> = R & {
|
|
17
17
|
identifier: (d: any) => d is MethodStructure;
|
|
@@ -41,7 +41,7 @@ export declare type Action<S = any, T extends ModelInstance = ModelInstance> = {
|
|
|
41
41
|
prevInstance?: T;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
declare type Dispatch = (action: Action) => any;
|
|
44
|
+
export declare type Dispatch = (action: Action) => any;
|
|
45
45
|
|
|
46
46
|
export declare interface Key<
|
|
47
47
|
S = any,
|
|
@@ -71,10 +71,10 @@ export declare type MiddleWare = (
|
|
|
71
71
|
export declare interface Config {
|
|
72
72
|
middleWares?: MiddleWare[];
|
|
73
73
|
controlled?: boolean;
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
notify?: (
|
|
75
|
+
notifier: (action: Action) => { errors: any[] | undefined },
|
|
76
76
|
action: Action
|
|
77
|
-
) =>
|
|
77
|
+
) => any;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/** createStore * */
|