as-model 0.1.24 → 0.1.26

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/README.md CHANGED
@@ -73,7 +73,7 @@ store0?.getInstance().increase();
73
73
  console.log(store0?.getInstance().count); // 1
74
74
  ```
75
75
 
76
- Model key is a template for creating multiple stores, and it is also an identifier to find the rightstore from multiple stores.
76
+ Model key is a template for creating multiple stores, and it is also an identifier to find the right store from multiple stores.
77
77
 
78
78
  Use **model** API to create store or key.
79
79
 
@@ -86,7 +86,7 @@ const key = model(counting).createKey(0);
86
86
  ......
87
87
  ```
88
88
 
89
- In typescript develop environment, `model` API can do a type check for making sure the model action method returns a correct type.
89
+ In typescript develop environment, `model` API can do a type check for making sure every model action method returns a correct type.
90
90
 
91
91
  ```js
92
92
  // ts
@@ -111,7 +111,7 @@ const key = counting.createKey(0);
111
111
  ......
112
112
  ```
113
113
 
114
- Sync store
114
+ Subscribe store
115
115
 
116
116
  ```js
117
117
  import {counting} from './model';
@@ -150,6 +150,7 @@ const store = model(counting).select((getInstance)=>{
150
150
  }
151
151
  }
152
152
  }).createStore(0);
153
+
153
154
  const {subscribe, select} = createSelector(store);
154
155
  const unsubscribe = subscribe();
155
156
  // When use select with no parameter,
@@ -159,7 +160,7 @@ await delayIncrease();
159
160
  select().count // 1
160
161
  ```
161
162
 
162
- Sync store with state in react hooks:
163
+ Subscribe store in react hooks:
163
164
 
164
165
  ```js
165
166
  import {model, createStores} from 'as-model';
@@ -304,13 +305,15 @@ A store object with model key and methods. The store object has `getInstance` me
304
305
  getInstance: ()=>instance,
305
306
  subscribe: (listener)=>unsubscribe,
306
307
  key: modelKey,
307
- destroy: ()=>void
308
+ destroy: ()=>void,
308
309
  update: (
309
310
  data:{
310
311
  model?:modelFn,
312
+ key?: modelKey,
311
313
  initialState?: any
314
+ state?: any;
312
315
  }
313
- )=>void
316
+ )=>void,
314
317
  }
315
318
  ```
316
319
 
@@ -333,14 +336,14 @@ A model key function with `createStore` method to create a store with the model
333
336
 
334
337
  ```js
335
338
  {
336
- createStore: (initialState?)=>store
339
+ createStore: (initialState?)=>Store
337
340
  }
338
341
  ```
339
342
 
340
343
  ### createStores
341
344
 
342
345
  ```js
343
- function createStores(...keys):stores
346
+ function createStores(...keys):StoreCollection
344
347
  ```
345
348
 
346
349
  #### parameters
@@ -349,9 +352,9 @@ function createStores(...keys):stores
349
352
 
350
353
  #### return
351
354
 
352
- Multiple stores created by the model keys.
355
+ StoreCollection created by the model keys.
353
356
 
354
- **stores** structure:
357
+ **StoreCollection** structure:
355
358
 
356
359
  ```js
357
360
  {
@@ -372,19 +375,28 @@ function createSignal(store):SignalStore
372
375
 
373
376
  #### return
374
377
 
375
- Signal store object with `subscribe` method to subscribe the state changes, and `getSignal` method to get the signal callback function.
378
+ Signal store object with `subscribe` method to subscribe the state changes, and `getSignal` method to get the Signal callback function.
376
379
 
377
- **signalAPI** structure:
380
+ **SignalStore** structure:
378
381
 
379
382
  ```js
380
383
  {
381
384
  subscribe: (listener)=>unsubscribe,
382
- getSignal: ()=>signal,
385
+ getSignal: ()=>Signal,
383
386
  key: modelKey,
384
387
  }
385
388
  ```
386
389
 
387
- The signal function returns a real time instance from store. Only when the properties picked from real time instance are changed, the subscribed listener can receive an action notification.
390
+ The Signal function returns a real time instance from store. Only when the properties picked from real time instance are changed, the subscribed listener can receive an action notification.
391
+
392
+ ```js
393
+ // Signal
394
+ function signal():instance;
395
+ // to start statistic the picked properties change
396
+ signal.startStatistics():void;
397
+ // to end statistic the picked properties change
398
+ signal.stopStatistics():void;
399
+ ```
388
400
 
389
401
  ### createSelector
390
402
 
@@ -398,6 +410,7 @@ function createSelector(store, opts?:SelectorOptions):SelectorStore
398
410
  * opts - (Optional) an object config to optimize createSelector.
399
411
 
400
412
  ```js
413
+ // opts
401
414
  {
402
415
  // When the selector is drived to reproduce a new data,
403
416
  // it compares if the result is different with the previous one,
@@ -423,7 +436,7 @@ function createSelector(store, opts?:SelectorOptions):SelectorStore
423
436
  ### model
424
437
 
425
438
  ```js
426
- function model(modelFn):modelAPI
439
+ function model(modelFn):ModelUsage
427
440
  ```
428
441
 
429
442
  #### parameters
@@ -432,9 +445,9 @@ function model(modelFn):modelAPI
432
445
 
433
446
  #### return
434
447
 
435
- Model api object with `createStore`, `createKey` methods to create store, key for the model function, and `select` method to set a default selector function (Use `createSelector(store).select()` to select the default one).
448
+ ModelUsage object with `createStore`, `createKey` methods to create store, key for the model function, and `select` method to set a default selector function (Use `createSelector(store).select()` to select the default one).
436
449
 
437
- **modelAPI** structure:
450
+ **ModelUsage** structure:
438
451
 
439
452
  ```js
440
453
  {
@@ -442,7 +455,7 @@ Model api object with `createStore`, `createKey` methods to create store, key fo
442
455
  createKey: (initialState?)=> key,
443
456
  select: (
444
457
  selector:(getInstance:()=>Instance)=>Record<string, any>|Array<any>
445
- )=>modelApI
458
+ )=>ModelUsage
446
459
  }
447
460
  ```
448
461
 
@@ -454,10 +467,10 @@ function config(options):configAPI
454
467
 
455
468
  #### parameters
456
469
 
457
- * options - (Optional) an object with the following properties:
458
- * batchNotify - (Optional) a callback function to batch notify the listeners, for example: `unstable_batchedUpdates` from react-dom.
459
- * controlled - (Optional) a boolean state to tell as-model use controlled mode to output instance changes.
460
- * middleWares - (Optional) a middleWare array for reproducing state or ignore actions.
470
+ ##### options - (Optional) an object with the following properties:
471
+ * notify - (Optional) a callback function for noticing an action to every subscriber, it accepts a notifier function and an action as parameters.
472
+ * controlled - (Optional) a boolean state to tell as-model use controlled mode to output instance changes.
473
+ * middleWares - (Optional) a middleWare array for reproducing state or ignore actions.
461
474
 
462
475
  #### return
463
476
 
@@ -468,10 +481,72 @@ All apis above except `createSignal` and `createSelector` API.
468
481
  createStore: (modelFnOrKey, initialState?)=>store,
469
482
  createKey: (modelFn, initialState?)=>key,
470
483
  createStores: (...keys)=>stores,
471
- model: (modelFn)=>modelAPI
484
+ model: (modelFn)=>ModelUsage
472
485
  }
473
486
  ```
474
487
 
488
+ ### validations
489
+
490
+ A validate callback collection object.
491
+
492
+ #### validations.isInstanceFromNoStateModel
493
+
494
+ ```js
495
+ function isInstanceFromNoStateModel(instance: any): boolean
496
+ ```
497
+
498
+ To validate if the parameter object is a uninitialized store instance.
499
+
500
+ #### validations.isModelKey
501
+
502
+ ```js
503
+ function isModelKey<
504
+ S,
505
+ T extends ModelInstance,
506
+ R extends (ins: () => T) => any = (ins: () => T) => T
507
+ >(
508
+ data: any
509
+ ): data is ModelKey<S, T, R>;
510
+ ```
511
+
512
+ To validate if the parameter object is a model key.
513
+
514
+ #### validations.isModelStore
515
+
516
+ ```js
517
+ function isModelStore<
518
+ S,
519
+ T extends ModelInstance,
520
+ R extends (ins: () => T) => any = (ins: () => T) => T
521
+ >(
522
+ data: any
523
+ ): data is Store<S, T, R>;
524
+ ```
525
+
526
+ To validate if the parameter object is a store.
527
+
528
+ #### validations.isModelUsage
529
+
530
+ ```js
531
+ function isModelUsage<
532
+ S,
533
+ T extends ModelInstance,
534
+ R extends (ins: () => T) => any = (ins: () => T) => T
535
+ >(
536
+ data: any
537
+ ): data is ModelUsage<S, T, R>;
538
+ ```
539
+
540
+ To validate if the parameter object is a model usage. A model usage is created by API **model**, it is a tool collection provides **createKey**, **createStore**, **select** APIs based on the model.
541
+
542
+ ### shallowEqual
543
+
544
+ ```js
545
+ function shallowEqual(prev: any, current: any): boolean
546
+ ```
547
+
548
+ To validate if the value of left is shallow equal with the value of right.
549
+
475
550
  ## Browser Support
476
551
 
477
552
  ```
package/dist/index.js CHANGED
@@ -186,12 +186,19 @@
186
186
  }
187
187
  return data.tokenIdentifier === tokenIdentifier && data.tokenIdentifier();
188
188
  }
189
+ function isStoreIndex(data) {
190
+ if (!data) {
191
+ return false;
192
+ }
193
+ return !!data.key && isModelKey(data.key);
194
+ }
189
195
  var validations = {
190
196
  isInstanceFromNoStateModel,
191
197
  isModelKey,
192
198
  isModelStore,
193
199
  isModelUsage,
194
- isToken
200
+ isToken,
201
+ isStoreIndex
195
202
  };
196
203
 
197
204
  // src/tools/proxy.ts
@@ -634,7 +641,7 @@
634
641
  var controlledState = hasInitialState && !hasState ? args.initialState : state;
635
642
  var instance = model2(controlledState);
636
643
  return _object_spread_props(_object_spread({}, u), {
637
- state,
644
+ state: controlledState,
638
645
  instance,
639
646
  model: model2
640
647
  });
@@ -651,7 +658,7 @@
651
658
  var initializedUpdater = createInitializedUpdater(u, middleWare);
652
659
  return _object_spread(_object_spread_props(_object_spread({}, u), {
653
660
  model: model2,
654
- state,
661
+ state: initialState,
655
662
  instance: instance1,
656
663
  initialized: true,
657
664
  token
@@ -907,7 +914,7 @@
907
914
  // src/store/enhance/signal.ts
908
915
  function createSignal(store) {
909
916
  var signalStore = {
910
- collection: null,
917
+ collection: {},
911
918
  started: false,
912
919
  enabled: false
913
920
  };
@@ -919,26 +926,22 @@
919
926
  }
920
927
  var collection = signalStore.collection;
921
928
  if (collection == null) {
922
- dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher(action);
923
929
  return;
924
930
  }
925
- var current = extractInstance(store.updater);
931
+ var storeInstance = extractInstance(store.updater);
926
932
  var keys = Object.keys(collection);
927
- var currentCollectionEntries = keys.map(function(key) {
928
- var field2 = current[key];
929
- if (cacheIdentify.field(field2)) {
930
- return [
931
- key,
932
- field2.get()
933
- ];
933
+ if (!keys.length) {
934
+ return;
935
+ }
936
+ var hasChange = keys.some(function(key) {
937
+ var field2 = storeInstance[key];
938
+ var collectedField = collection[key];
939
+ if (cacheIdentify.field(field2) && cacheIdentify.field(collectedField)) {
940
+ return field2.get() !== collectedField.get();
934
941
  }
935
- return [
936
- key,
937
- field2
938
- ];
942
+ return field2 !== collectedField;
939
943
  });
940
- var currentCollection = Object.fromEntries(currentCollectionEntries);
941
- if (!shallowEqual(collection, currentCollection)) {
944
+ if (hasChange) {
942
945
  dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher(action);
943
946
  }
944
947
  };
@@ -957,20 +960,22 @@
957
960
  if (!signalStore.started) {
958
961
  return;
959
962
  }
960
- signalStore.collection = signalStore.collection || {};
961
963
  signalStore.collection[key] = val;
962
964
  };
963
- var getInstance = function getInstance2() {
964
- return extractInstance(store.updater, collectUsedFields);
965
+ var getInstance = function getInstance2(options) {
966
+ var cutOff = (options !== null && options !== void 0 ? options : {}).cutOff;
967
+ return extractInstance(store.updater, cutOff ? void 0 : collectUsedFields);
965
968
  };
966
- var signal = function signal2() {
967
- return getInstance();
969
+ var signal = function signal2(options) {
970
+ return getInstance(options);
968
971
  };
969
972
  signal.startStatistics = function startStatistics() {
970
973
  signalStore.started = true;
974
+ signalStore.collection = {};
971
975
  };
972
976
  signal.stopStatistics = function stopStatistics() {
973
977
  signalStore.started = false;
978
+ signalStore.collection = {};
974
979
  };
975
980
  signal.subscribe = function subscribe(dispatchCallback) {
976
981
  return store.subscribe(dispatchCallback);
@@ -1,8 +1,7 @@
1
1
  import { cacheIdentify, extractInstance } from "../instance";
2
- import { shallowEqual } from "../../tools";
3
2
  function createSignal(store) {
4
3
  const signalStore = {
5
- collection: null,
4
+ collection: {},
6
5
  started: false,
7
6
  enabled: false
8
7
  };
@@ -14,20 +13,22 @@ function createSignal(store) {
14
13
  }
15
14
  const { collection } = signalStore;
16
15
  if (collection == null) {
17
- dispatcher == null ? void 0 : dispatcher(action);
18
16
  return;
19
17
  }
20
- const current = extractInstance(store.updater);
18
+ const storeInstance = extractInstance(store.updater);
21
19
  const keys = Object.keys(collection);
22
- const currentCollectionEntries = keys.map((key) => {
23
- const field = current[key];
24
- if (cacheIdentify.field(field)) {
25
- return [key, field.get()];
20
+ if (!keys.length) {
21
+ return;
22
+ }
23
+ const hasChange = keys.some((key) => {
24
+ const field = storeInstance[key];
25
+ const collectedField = collection[key];
26
+ if (cacheIdentify.field(field) && cacheIdentify.field(collectedField)) {
27
+ return field.get() !== collectedField.get();
26
28
  }
27
- return [key, field];
29
+ return field !== collectedField;
28
30
  });
29
- const currentCollection = Object.fromEntries(currentCollectionEntries);
30
- if (!shallowEqual(collection, currentCollection)) {
31
+ if (hasChange) {
31
32
  dispatcher == null ? void 0 : dispatcher(action);
32
33
  }
33
34
  };
@@ -46,20 +47,25 @@ function createSignal(store) {
46
47
  if (!signalStore.started) {
47
48
  return;
48
49
  }
49
- signalStore.collection = signalStore.collection || {};
50
50
  signalStore.collection[key] = val;
51
51
  };
52
- const getInstance = function getInstance2() {
53
- return extractInstance(store.updater, collectUsedFields);
52
+ const getInstance = function getInstance2(options) {
53
+ const { cutOff } = options != null ? options : {};
54
+ return extractInstance(
55
+ store.updater,
56
+ cutOff ? void 0 : collectUsedFields
57
+ );
54
58
  };
55
- const signal = function signal2() {
56
- return getInstance();
59
+ const signal = function signal2(options) {
60
+ return getInstance(options);
57
61
  };
58
62
  signal.startStatistics = function startStatistics() {
59
63
  signalStore.started = true;
64
+ signalStore.collection = {};
60
65
  };
61
66
  signal.stopStatistics = function stopStatistics() {
62
67
  signalStore.started = false;
68
+ signalStore.collection = {};
63
69
  };
64
70
  signal.subscribe = function subscribe(dispatchCallback) {
65
71
  return store.subscribe(dispatchCallback);
@@ -48,7 +48,7 @@ function createUpdateFn(updater, middleWare) {
48
48
  if (u.controlled) {
49
49
  const controlledState = hasInitialState && !hasState ? args.initialState : state;
50
50
  const instance = model(controlledState);
51
- return __spreadProps(__spreadValues({}, u), { state, instance, model });
51
+ return __spreadProps(__spreadValues({}, u), { state: controlledState, instance, model });
52
52
  }
53
53
  if (u.isDestroyed) {
54
54
  return u;
@@ -64,7 +64,7 @@ function createUpdateFn(updater, middleWare) {
64
64
  const initializedUpdater = createInitializedUpdater(u, middleWare);
65
65
  return __spreadValues(__spreadProps(__spreadValues({}, u), {
66
66
  model,
67
- state,
67
+ state: initialState,
68
68
  instance,
69
69
  initialized: true,
70
70
  token
@@ -46,18 +46,26 @@ function isToken(data) {
46
46
  }
47
47
  return data.tokenIdentifier === tokenIdentifier && data.tokenIdentifier();
48
48
  }
49
+ function isStoreIndex(data) {
50
+ if (!data) {
51
+ return false;
52
+ }
53
+ return !!data.key && isModelKey(data.key);
54
+ }
49
55
  const validations = {
50
56
  isInstanceFromNoStateModel,
51
57
  isModelKey,
52
58
  isModelStore,
53
59
  isModelUsage,
54
- isToken
60
+ isToken,
61
+ isStoreIndex
55
62
  };
56
63
  export {
57
64
  createNoStateModel,
58
65
  isModelKey,
59
66
  isModelStore,
60
67
  isModelUsage,
68
+ isStoreIndex,
61
69
  isToken,
62
70
  validations
63
71
  };
package/index.d.ts CHANGED
@@ -200,6 +200,10 @@ export declare interface model {
200
200
 
201
201
  /** createSignal * */
202
202
 
203
+ export interface SignalOptions {
204
+ cutOff?: boolean;
205
+ }
206
+
203
207
  // eslint-disable-next-line @typescript-eslint/no-redeclare
204
208
  declare interface SignalStore<
205
209
  S = any,
@@ -209,7 +213,7 @@ declare interface SignalStore<
209
213
  getToken: () => Token;
210
214
  subscribe: (dispatcher: Dispatch) => () => void;
211
215
  getSignal: () => {
212
- (): T;
216
+ (options?: SignalOptions): T;
213
217
  startStatistics: () => void;
214
218
  stopStatistics: () => void;
215
219
  subscribe: (dispatcher: Dispatch) => () => void;
@@ -305,6 +309,13 @@ export declare const validations: {
305
309
  >(
306
310
  data: any
307
311
  ) => data is ModelUsage<S, T, R>;
312
+ isStoreIndex: <
313
+ S,
314
+ T extends ModelInstance,
315
+ R extends (ins: () => T) => any = (ins: () => T) => T
316
+ >(
317
+ data: any
318
+ ) => data is StoreIndex<S, T, R>;
308
319
  };
309
320
 
310
321
  /** tools * */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "as-model",
4
- "version": "0.1.24",
4
+ "version": "0.1.26",
5
5
  "description": "This is a model state management tool",
6
6
  "license": "MIT",
7
7
  "author": "Jimmy.Harding",