as-model 0.1.17 → 0.1.19

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 CHANGED
@@ -141,6 +141,9 @@
141
141
  function modelStoreIdentifier() {
142
142
  return true;
143
143
  }
144
+ function tokenIdentifier() {
145
+ return true;
146
+ }
144
147
 
145
148
  // src/validation/index.ts
146
149
  var noStateAModelKey = "no-state-a-model-key";
@@ -177,13 +180,124 @@
177
180
  }
178
181
  return data.modelStoreIdentifier === modelStoreIdentifier && data.modelStoreIdentifier();
179
182
  }
183
+ function isToken(data) {
184
+ if (!data) {
185
+ return false;
186
+ }
187
+ return data.tokenIdentifier === tokenIdentifier && data.tokenIdentifier();
188
+ }
180
189
  var validations = {
181
190
  isInstanceFromNoStateModel,
182
191
  isModelKey,
183
192
  isModelStore,
184
- isModelUsage
193
+ isModelUsage,
194
+ isToken
195
+ };
196
+
197
+ // src/tools/proxy.ts
198
+ function getDescriptors(target, receiver, ownOrPrototype, handler) {
199
+ var it = Object.keys(ownOrPrototype);
200
+ var result = {};
201
+ it.forEach(function(key) {
202
+ result[key] = {
203
+ get: function() {
204
+ if (!handler.get) {
205
+ return target[key];
206
+ }
207
+ return handler.get(target, key, receiver);
208
+ },
209
+ set: function(v) {
210
+ if (!handler.set) {
211
+ target[key] = v;
212
+ return;
213
+ }
214
+ var valid = handler.set(target, key, v, receiver);
215
+ if (!valid) {
216
+ throw new Error("".concat(key, " in proxy target is not mutable"));
217
+ }
218
+ }
219
+ };
220
+ });
221
+ return result;
222
+ }
223
+ var createSimpleProxy = function(target, handler) {
224
+ var proxy = {};
225
+ var own = getDescriptors(target, proxy, target, handler);
226
+ var prototype = getDescriptors(target, proxy, Object.getPrototypeOf(target), handler);
227
+ Object.defineProperties(proxy, _object_spread({}, prototype, own));
228
+ return proxy;
229
+ };
230
+ var createProxy = function(target, handler) {
231
+ if (typeof Proxy !== "function") {
232
+ return createSimpleProxy(target, handler);
233
+ }
234
+ return new Proxy(target, handler);
185
235
  };
186
236
 
237
+ // src/tools/shallowEqual.ts
238
+ function isObject(data) {
239
+ return data && (typeof data === "undefined" ? "undefined" : _type_of(data)) === "object";
240
+ }
241
+ function shallowEqual(prev, current) {
242
+ if (Object.is(prev, current)) {
243
+ return true;
244
+ }
245
+ if (!isObject(prev) || !isObject(current)) {
246
+ return false;
247
+ }
248
+ var prevKeys = Object.keys(prev);
249
+ var currentKeys = Object.keys(current);
250
+ if (prevKeys.length !== currentKeys.length) {
251
+ return false;
252
+ }
253
+ var pre = prev;
254
+ var curr = current;
255
+ var hasDiffKey = prevKeys.some(function(key) {
256
+ return !Object.prototype.hasOwnProperty.call(curr, key);
257
+ });
258
+ if (hasDiffKey) {
259
+ return false;
260
+ }
261
+ var hasDiffValue = currentKeys.some(function(key) {
262
+ var currentValue = curr[key];
263
+ var prevValue = pre[key];
264
+ return !Object.is(currentValue, prevValue);
265
+ });
266
+ return !hasDiffValue;
267
+ }
268
+
269
+ // src/tools/index.ts
270
+ function noop() {
271
+ }
272
+ function simpleErrorProcess(errors) {
273
+ return function wrap(callback) {
274
+ return function replaced() {
275
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
276
+ args[_key] = arguments[_key];
277
+ }
278
+ try {
279
+ return callback.apply(void 0, _to_consumable_array(args));
280
+ } catch (e) {
281
+ errors.push(e);
282
+ }
283
+ };
284
+ };
285
+ }
286
+
287
+ // src/updater/token.ts
288
+ function createToken() {
289
+ var value = {};
290
+ return {
291
+ isDifferent: function isDifferent(token) {
292
+ if (!isToken(token)) {
293
+ return true;
294
+ }
295
+ return token.value !== value;
296
+ },
297
+ value
298
+ };
299
+ }
300
+
187
301
  // src/updater/notifier.ts
188
302
  function defaultNotifyImplement(dispatches, action) {
189
303
  dispatches.forEach(function(callback) {
@@ -261,7 +375,6 @@
261
375
  }
262
376
  var dispatch = function dispatch2(action) {
263
377
  var dispatches = updater.dispatches, controlled = updater.controlled, model2 = updater.model, config2 = updater.config;
264
- var dispatchCallbacks = _to_consumable_array(dispatches);
265
378
  var state = action.state;
266
379
  var nextInstance = model2(state);
267
380
  var nextAction = _object_spread_props(_object_spread({}, action), {
@@ -272,15 +385,36 @@
272
385
  return _object_spread_props(_object_spread({}, u), {
273
386
  state,
274
387
  instance: nextInstance,
275
- version: u.version + 1
388
+ version: u.version + 1,
389
+ token: createToken()
276
390
  });
277
391
  });
278
392
  }
393
+ var notifyAction = function notifyAction2(act) {
394
+ var errors = [];
395
+ var dispatchCallbacks = dispatches.map(simpleErrorProcess(errors));
396
+ defaultNotifyImplement(dispatchCallbacks, act);
397
+ if (!errors.length) {
398
+ return {
399
+ errors: void 0
400
+ };
401
+ }
402
+ return {
403
+ errors
404
+ };
405
+ };
406
+ var notifyActionWithErrorThrow = function notifyActionWithErrorThrow2(act) {
407
+ var errors = notifyAction(act).errors;
408
+ if (!errors || !errors.length) {
409
+ return;
410
+ }
411
+ throw errors[0];
412
+ };
279
413
  try {
280
- if (typeof config2.batchNotify === "function") {
281
- config2.batchNotify(dispatchCallbacks, nextAction);
414
+ if (typeof config2.notify === "function") {
415
+ config2.notify(notifyAction, nextAction);
282
416
  } else {
283
- defaultNotifyImplement(dispatchCallbacks, nextAction);
417
+ notifyActionWithErrorThrow(nextAction);
284
418
  }
285
419
  } catch (e) {
286
420
  updater.mutate(function(u) {
@@ -325,82 +459,6 @@
325
459
  };
326
460
  }
327
461
 
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
462
  // src/updater/tunnel.ts
405
463
  function createUnInitializedUpdater() {
406
464
  return {
@@ -546,8 +604,8 @@
546
604
  updater.mutate(function(u, effect) {
547
605
  var _args_model;
548
606
  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
607
  var state = "state" in args ? args.state : u.state;
608
+ var token = createToken();
551
609
  if (u.controlled) {
552
610
  var instance = model2(state);
553
611
  return _object_spread_props(_object_spread({}, u), {
@@ -559,17 +617,18 @@
559
617
  if (u.isDestroyed) {
560
618
  return u;
561
619
  }
562
- if (!u.initialized && !("initialState" in args)) {
563
- throw new Error("Should update initialState first.");
620
+ if (!u.initialized && !("state" in args)) {
621
+ throw new Error("The updater has not been initialized, it should be updated with a state for initializing.");
564
622
  }
565
623
  if (!u.initialized) {
566
- var instance1 = model2(initialState);
624
+ var instance1 = model2(state);
567
625
  var initializedUpdater = createInitializedUpdater(u, middleWare);
568
626
  return _object_spread(_object_spread_props(_object_spread({}, u), {
569
627
  model: model2,
570
- state: initialState,
628
+ state,
571
629
  instance: instance1,
572
- initialized: true
630
+ initialized: true,
631
+ token
573
632
  }), initializedUpdater);
574
633
  }
575
634
  if (Object.is(u.model, model2) && Object.is(u.state, state)) {
@@ -590,6 +649,7 @@
590
649
  state,
591
650
  model: model2,
592
651
  instance: instance2,
652
+ token,
593
653
  initialized: true,
594
654
  cacheFields: {},
595
655
  cacheMethods: {}
@@ -607,6 +667,7 @@
607
667
  var updater = _object_spread({
608
668
  sidePayload: void 0,
609
669
  version: 0,
670
+ token: createToken(),
610
671
  isDestroyed: false,
611
672
  model: model2,
612
673
  instance: defaultInstance,
@@ -863,6 +924,9 @@
863
924
  var storeKey = store.key;
864
925
  return {
865
926
  key: storeKey,
927
+ getToken: function getToken() {
928
+ return store.getToken();
929
+ },
866
930
  subscribe: function subscribe(dispatcher) {
867
931
  return store.subscribe(enhance(dispatcher));
868
932
  },
@@ -1053,6 +1117,9 @@
1053
1117
  }
1054
1118
  return {
1055
1119
  key: store.key,
1120
+ getToken: function getToken() {
1121
+ return store.getToken();
1122
+ },
1056
1123
  subscribe: function subscribe(dispatcher) {
1057
1124
  return store.subscribe(enhance(dispatcher));
1058
1125
  },
@@ -1120,6 +1187,9 @@
1120
1187
  };
1121
1188
  var store = {
1122
1189
  key,
1190
+ getToken: function getToken() {
1191
+ return updater.token;
1192
+ },
1123
1193
  subscribe: function subscribe(dispatcher) {
1124
1194
  var _updater_createTunnel = updater.createTunnel(dispatcher), connect = _updater_createTunnel.connect, disconnect = _updater_createTunnel.disconnect;
1125
1195
  connect();
@@ -7,8 +7,12 @@ function modelUsageIdentifier() {
7
7
  function modelStoreIdentifier() {
8
8
  return true;
9
9
  }
10
+ function tokenIdentifier() {
11
+ return true;
12
+ }
10
13
  export {
11
14
  modelKeyIdentifier,
12
15
  modelStoreIdentifier,
13
- modelUsageIdentifier
16
+ modelUsageIdentifier,
17
+ tokenIdentifier
14
18
  };
@@ -150,6 +150,9 @@ function createSelector(store, opts) {
150
150
  }
151
151
  return {
152
152
  key: store.key,
153
+ getToken() {
154
+ return store.getToken();
155
+ },
153
156
  subscribe(dispatcher) {
154
157
  return store.subscribe(enhance(dispatcher));
155
158
  },
@@ -35,6 +35,9 @@ function createSignal(store) {
35
35
  const { key: storeKey } = store;
36
36
  return {
37
37
  key: storeKey,
38
+ getToken() {
39
+ return store.getToken();
40
+ },
38
41
  subscribe(dispatcher) {
39
42
  return store.subscribe(enhance(dispatcher));
40
43
  },
@@ -92,6 +92,9 @@ function createStore(modelLike, config = {}) {
92
92
  };
93
93
  const store = {
94
94
  key,
95
+ getToken() {
96
+ return updater.token;
97
+ },
95
98
  subscribe(dispatcher) {
96
99
  const { connect, disconnect } = updater.createTunnel(dispatcher);
97
100
  connect();
@@ -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
  };
@@ -24,6 +24,7 @@ import {
24
24
  createUnInitializedUpdater,
25
25
  destroy
26
26
  } from "./tunnel";
27
+ import { createToken } from "./token";
27
28
  function createInitializedUpdater(updater, middleWare) {
28
29
  const createTunnel = generateTunnelCreator(updater);
29
30
  return {
@@ -36,8 +37,8 @@ function createUpdateFn(updater, middleWare) {
36
37
  updater.mutate((u, effect) => {
37
38
  var _a;
38
39
  const model = (_a = args.model) != null ? _a : u.model;
39
- const initialState = "initialState" in args ? args.initialState : u.state;
40
40
  const state = "state" in args ? args.state : u.state;
41
+ const token = createToken();
41
42
  if (u.controlled) {
42
43
  const instance2 = model(state);
43
44
  return __spreadProps(__spreadValues({}, u), { state, instance: instance2, model });
@@ -45,17 +46,20 @@ function createUpdateFn(updater, middleWare) {
45
46
  if (u.isDestroyed) {
46
47
  return u;
47
48
  }
48
- if (!u.initialized && !("initialState" in args)) {
49
- throw new Error("Should update initialState first.");
49
+ if (!u.initialized && !("state" in args)) {
50
+ throw new Error(
51
+ "The updater has not been initialized, it should be updated with a state for initializing."
52
+ );
50
53
  }
51
54
  if (!u.initialized) {
52
- const instance2 = model(initialState);
55
+ const instance2 = model(state);
53
56
  const initializedUpdater = createInitializedUpdater(u, middleWare);
54
57
  return __spreadValues(__spreadProps(__spreadValues({}, u), {
55
58
  model,
56
- state: initialState,
59
+ state,
57
60
  instance: instance2,
58
- initialized: true
61
+ initialized: true,
62
+ token
59
63
  }), initializedUpdater);
60
64
  }
61
65
  if (Object.is(u.model, model) && Object.is(u.state, state)) {
@@ -76,6 +80,7 @@ function createUpdateFn(updater, middleWare) {
76
80
  state,
77
81
  model,
78
82
  instance,
83
+ token,
79
84
  initialized: true,
80
85
  cacheFields: {},
81
86
  cacheMethods: {}
@@ -92,6 +97,7 @@ function createUpdater(model, middleWare, config = {}) {
92
97
  const updater = __spreadValues({
93
98
  sidePayload: void 0,
94
99
  version: 0,
100
+ token: createToken(),
95
101
  isDestroyed: false,
96
102
  model,
97
103
  instance: defaultInstance,
@@ -17,6 +17,8 @@ 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";
21
+ import { createToken } from "./token";
20
22
  function defaultNotifyImplement(dispatches, action) {
21
23
  dispatches.forEach((callback) => {
22
24
  callback(action);
@@ -80,7 +82,6 @@ function generateNotifier(updater, middleWare) {
80
82
  }
81
83
  const dispatch = function dispatch2(action) {
82
84
  const { dispatches, controlled, model, config } = updater;
83
- const dispatchCallbacks = [...dispatches];
84
85
  const { state } = action;
85
86
  const nextInstance = model(state);
86
87
  const nextAction = __spreadProps(__spreadValues({}, action), { instance: nextInstance });
@@ -88,14 +89,31 @@ function generateNotifier(updater, middleWare) {
88
89
  updater.mutate((u) => __spreadProps(__spreadValues({}, u), {
89
90
  state,
90
91
  instance: nextInstance,
91
- version: u.version + 1
92
+ version: u.version + 1,
93
+ token: createToken()
92
94
  }));
93
95
  }
96
+ const notifyAction = function notifyAction2(act) {
97
+ const errors = [];
98
+ const dispatchCallbacks = dispatches.map(simpleErrorProcess(errors));
99
+ defaultNotifyImplement(dispatchCallbacks, act);
100
+ if (!errors.length) {
101
+ return { errors: void 0 };
102
+ }
103
+ return { errors };
104
+ };
105
+ const notifyActionWithErrorThrow = function notifyActionWithErrorThrow2(act) {
106
+ const { errors } = notifyAction(act);
107
+ if (!errors || !errors.length) {
108
+ return;
109
+ }
110
+ throw errors[0];
111
+ };
94
112
  try {
95
- if (typeof config.batchNotify === "function") {
96
- config.batchNotify(dispatchCallbacks, nextAction);
113
+ if (typeof config.notify === "function") {
114
+ config.notify(notifyAction, nextAction);
97
115
  } else {
98
- defaultNotifyImplement(dispatchCallbacks, nextAction);
116
+ notifyActionWithErrorThrow(nextAction);
99
117
  }
100
118
  } catch (e) {
101
119
  updater.mutate((u) => __spreadProps(__spreadValues({}, u), { dispatching: void 0 }));
@@ -0,0 +1,16 @@
1
+ import { isToken } from "../validation";
2
+ function createToken() {
3
+ const value = {};
4
+ return {
5
+ isDifferent(token) {
6
+ if (!isToken(token)) {
7
+ return true;
8
+ }
9
+ return token.value !== value;
10
+ },
11
+ value
12
+ };
13
+ }
14
+ export {
15
+ createToken
16
+ };
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  modelKeyIdentifier,
3
3
  modelStoreIdentifier,
4
- modelUsageIdentifier
4
+ modelUsageIdentifier,
5
+ tokenIdentifier
5
6
  } from "../identifiers";
6
7
  const noStateAModelKey = "no-state-a-model-key";
7
8
  function createNoStateModel() {
@@ -39,16 +40,24 @@ function isModelStore(data) {
39
40
  }
40
41
  return data.modelStoreIdentifier === modelStoreIdentifier && data.modelStoreIdentifier();
41
42
  }
43
+ function isToken(data) {
44
+ if (!data) {
45
+ return false;
46
+ }
47
+ return data.tokenIdentifier === tokenIdentifier && data.tokenIdentifier();
48
+ }
42
49
  const validations = {
43
50
  isInstanceFromNoStateModel,
44
51
  isModelKey,
45
52
  isModelStore,
46
- isModelUsage
53
+ isModelUsage,
54
+ isToken
47
55
  };
48
56
  export {
49
57
  createNoStateModel,
50
58
  isModelKey,
51
59
  isModelStore,
52
60
  isModelUsage,
61
+ isToken,
53
62
  validations
54
63
  };
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,12 @@ 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 interface Token {
45
+ isDifferent: (token: Token) => boolean;
46
+ value: unknown;
47
+ }
48
+
49
+ export declare type Dispatch = (action: Action) => any;
45
50
 
46
51
  export declare interface Key<
47
52
  S = any,
@@ -71,10 +76,10 @@ export declare type MiddleWare = (
71
76
  export declare interface Config {
72
77
  middleWares?: MiddleWare[];
73
78
  controlled?: boolean;
74
- batchNotify?: (
75
- listeners: ((action: Action) => void)[],
79
+ notify?: (
80
+ notifier: (action: Action) => { errors: any[] | undefined },
76
81
  action: Action
77
- ) => void;
82
+ ) => any;
78
83
  }
79
84
 
80
85
  /** createStore * */
@@ -93,6 +98,7 @@ export declare interface Store<
93
98
  R extends (instance: () => T) => any = (instance: () => T) => T
94
99
  > extends StoreIndex<S, T, R> {
95
100
  subscribe: (dispatcher: Dispatch) => () => void;
101
+ getToken: () => Token;
96
102
  getInstance: () => T;
97
103
  update: (args?: { model?: Model<S, T>; initialState?: S; state?: S }) => void;
98
104
  destroy: () => void;
@@ -195,6 +201,7 @@ declare interface SignalStore<
195
201
  T extends ModelInstance = any,
196
202
  R extends (instance: () => T) => any = (instance: () => T) => any
197
203
  > extends StoreIndex<S, T, R> {
204
+ getToken: () => Token;
198
205
  subscribe: (dispatcher: Dispatch) => () => void;
199
206
  getSignal: () => {
200
207
  (): T;
@@ -230,6 +237,7 @@ declare interface SelectorStore<
230
237
  T extends ModelInstance = any,
231
238
  R extends (instance: () => T) => any = (instance: () => T) => any
232
239
  > extends StoreIndex<S, T, R> {
240
+ getToken: () => Token;
233
241
  subscribe: (dispatcher: Dispatch) => () => void;
234
242
  select: SelectMethod<T, R>;
235
243
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "as-model",
4
- "version": "0.1.17",
4
+ "version": "0.1.19",
5
5
  "description": "This is a model state management tool",
6
6
  "license": "MIT",
7
7
  "author": "Jimmy.Harding",