tinybase 0.9.0 → 0.9.4

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.
Files changed (69) hide show
  1. package/lib/checkpoints.d.ts +35 -20
  2. package/lib/checkpoints.js +1 -1
  3. package/lib/checkpoints.js.gz +0 -0
  4. package/lib/common.d.ts +56 -0
  5. package/lib/common.js +1 -0
  6. package/lib/common.js.gz +0 -0
  7. package/lib/debug/checkpoints.d.ts +35 -20
  8. package/lib/debug/checkpoints.js +1 -1
  9. package/lib/debug/common.d.ts +56 -0
  10. package/lib/debug/common.js +3 -0
  11. package/lib/debug/indexes.d.ts +35 -76
  12. package/lib/debug/indexes.js +18 -10
  13. package/lib/debug/metrics.d.ts +45 -20
  14. package/lib/debug/metrics.js +1 -1
  15. package/lib/debug/persisters.d.ts +203 -17
  16. package/lib/debug/persisters.js +1 -1
  17. package/lib/debug/relationships.d.ts +46 -23
  18. package/lib/debug/relationships.js +1 -1
  19. package/lib/debug/store.d.ts +179 -102
  20. package/lib/debug/store.js +1 -1
  21. package/lib/debug/tinybase.d.ts +1 -2
  22. package/lib/debug/tinybase.js +18 -1029
  23. package/lib/debug/{react.d.ts → ui-react.d.ts} +262 -162
  24. package/lib/debug/{react.js → ui-react.js} +24 -15
  25. package/lib/indexes.d.ts +35 -76
  26. package/lib/indexes.js +1 -1
  27. package/lib/indexes.js.gz +0 -0
  28. package/lib/metrics.d.ts +45 -20
  29. package/lib/metrics.js +1 -1
  30. package/lib/metrics.js.gz +0 -0
  31. package/lib/persisters.d.ts +203 -17
  32. package/lib/persisters.js +1 -1
  33. package/lib/persisters.js.gz +0 -0
  34. package/lib/relationships.d.ts +46 -23
  35. package/lib/relationships.js +1 -1
  36. package/lib/relationships.js.gz +0 -0
  37. package/lib/store.d.ts +179 -102
  38. package/lib/store.js +1 -1
  39. package/lib/store.js.gz +0 -0
  40. package/lib/tinybase.d.ts +1 -2
  41. package/lib/tinybase.js +1 -1
  42. package/lib/tinybase.js.gz +0 -0
  43. package/lib/{react.d.ts → ui-react.d.ts} +262 -162
  44. package/lib/ui-react.js +1 -0
  45. package/lib/ui-react.js.gz +0 -0
  46. package/lib/umd/checkpoints.js +1 -1
  47. package/lib/umd/checkpoints.js.gz +0 -0
  48. package/lib/umd/common.js +1 -0
  49. package/lib/umd/common.js.gz +0 -0
  50. package/lib/umd/indexes.js +1 -1
  51. package/lib/umd/indexes.js.gz +0 -0
  52. package/lib/umd/metrics.js +1 -1
  53. package/lib/umd/metrics.js.gz +0 -0
  54. package/lib/umd/persisters.js +1 -1
  55. package/lib/umd/persisters.js.gz +0 -0
  56. package/lib/umd/relationships.js +1 -1
  57. package/lib/umd/relationships.js.gz +0 -0
  58. package/lib/umd/store.js +1 -1
  59. package/lib/umd/store.js.gz +0 -0
  60. package/lib/umd/tinybase.js +1 -1
  61. package/lib/umd/tinybase.js.gz +0 -0
  62. package/lib/umd/ui-react.js +1 -0
  63. package/lib/umd/ui-react.js.gz +0 -0
  64. package/package.json +21 -16
  65. package/readme.md +13 -13
  66. package/lib/react.js +0 -1
  67. package/lib/react.js.gz +0 -0
  68. package/lib/umd/react.js +0 -1
  69. package/lib/umd/react.js.gz +0 -0
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, {useContext as useContext$1} from 'react';
2
2
 
3
3
  const getTypeOf = (thing) => typeof thing;
4
4
  const EMPTY_STRING = '';
@@ -48,8 +48,11 @@ const useCheckpointsOrCheckpointsId = (checkpointsOrCheckpointsId) =>
48
48
  useThingOrThingId(checkpointsOrCheckpointsId, 8);
49
49
 
50
50
  const {useCallback, useEffect, useMemo: useMemo$1, useState} = React;
51
- const useCreate = (store, create, createDeps = []) =>
52
- useMemo$1(() => create(store), [store, ...createDeps]);
51
+ const useCreate = (store, create, createDeps = []) => {
52
+ const thing = useMemo$1(() => create(store), [store, ...createDeps]);
53
+ useEffect(() => () => thing.destroy(), [thing]);
54
+ return thing;
55
+ };
53
56
  const useListenable = (listenable, thing, defaulted, ...args) => {
54
57
  const getListenable = thing?.['get' + listenable] ?? (() => defaulted);
55
58
  const immediateListenable = getListenable(...args);
@@ -654,6 +657,9 @@ const useCreatePersister = (
654
657
  setDone(1);
655
658
  return;
656
659
  })();
660
+ return () => {
661
+ persister.destroy();
662
+ };
657
663
  }, [persister, ...thenDeps]);
658
664
  return persister;
659
665
  };
@@ -735,22 +741,23 @@ const Provider = ({
735
741
  checkpoints,
736
742
  checkpointsById,
737
743
  children,
738
- }) =>
739
- /* @__PURE__ */ createElement(
744
+ }) => {
745
+ const thingsOrThingsId = useContext$1(Context);
746
+ return /* @__PURE__ */ createElement(
740
747
  Context.Provider,
741
748
  {
742
749
  value: useMemo(
743
750
  () => [
744
- store,
745
- storesById,
746
- metrics,
747
- metricsById,
748
- indexes,
749
- indexesById,
750
- relationships,
751
- relationshipsById,
752
- checkpoints,
753
- checkpointsById,
751
+ store ?? thingsOrThingsId[0],
752
+ {...thingsOrThingsId[1], ...storesById},
753
+ metrics ?? thingsOrThingsId[2],
754
+ {...thingsOrThingsId[3], ...metricsById},
755
+ indexes ?? thingsOrThingsId[4],
756
+ {...thingsOrThingsId[5], ...indexesById},
757
+ relationships ?? thingsOrThingsId[6],
758
+ {...thingsOrThingsId[7], ...relationshipsById},
759
+ checkpoints ?? thingsOrThingsId[8],
760
+ {...thingsOrThingsId[9], ...checkpointsById},
754
761
  ],
755
762
  [
756
763
  store,
@@ -763,11 +770,13 @@ const Provider = ({
763
770
  relationshipsById,
764
771
  checkpoints,
765
772
  checkpointsById,
773
+ thingsOrThingsId,
766
774
  ],
767
775
  ),
768
776
  },
769
777
  children,
770
778
  );
779
+ };
771
780
  const wrap = (children, separator, encloseWithId, id) => {
772
781
  const separatedChildren =
773
782
  isUndefined(separator) || !Array.isArray(children)
package/lib/indexes.d.ts CHANGED
@@ -3,8 +3,8 @@
3
3
  * and track indexes of the data in Store objects.
4
4
  *
5
5
  * The main entry point to this module is the createIndexes function, which
6
- * returns a new Indexes object. From there, you can create new index
7
- * definitions, access the contents of those indexes directly, and register
6
+ * returns a new Indexes object. From there, you can create new Index
7
+ * definitions, access the contents of those Indexes directly, and register
8
8
  * listeners for when they change.
9
9
  *
10
10
  * @packageDocumentation
@@ -12,7 +12,7 @@
12
12
  */
13
13
 
14
14
  import {GetCell, Store} from './store.d';
15
- import {Id, IdOrNull, Ids} from './common.d';
15
+ import {Id, IdOrNull, Ids, SortKey} from './common.d';
16
16
 
17
17
  /**
18
18
  * The Index type represents the concept of a map of Slice objects, keyed by Id.
@@ -43,13 +43,6 @@ export type Index = {[sliceId: Id]: Slice};
43
43
  */
44
44
  export type Slice = Ids;
45
45
 
46
- /**
47
- * The SortKey type represents a value that can be used by a sort function.
48
- *
49
- * @category Parameter
50
- */
51
- export type SortKey = string | number | boolean;
52
-
53
46
  /**
54
47
  * The SliceIdsListener type describes a function that is used to listen to
55
48
  * changes to the Slice Ids in an Index.
@@ -60,6 +53,8 @@ export type SortKey = string | number | boolean;
60
53
  * When called, a SliceIdsListener is given a reference to the Indexes object,
61
54
  * and the Id of the Index that changed.
62
55
  *
56
+ * @param indexes A reference to the Indexes object that changed.
57
+ * @param indexId The Id of the Index that changed.
63
58
  * @category Listener
64
59
  */
65
60
  export type SliceIdsListener = (indexes: Indexes, indexId: Id) => void;
@@ -75,6 +70,9 @@ export type SliceIdsListener = (indexes: Indexes, indexId: Id) => void;
75
70
  * object, the Id of the Index that changed, and the Id of the Slice whose Row
76
71
  * Ids changed.
77
72
  *
73
+ * @param indexes A reference to the Indexes object that changed.
74
+ * @param indexId The Id of the Index that changed.
75
+ * @param sliceId The Id of the Slice that changed.
78
76
  * @category Listener
79
77
  */
80
78
  export type SliceRowIdsListener = (
@@ -93,7 +91,13 @@ export type SliceRowIdsListener = (
93
91
  * @category Development
94
92
  */
95
93
  export type IndexesListenerStats = {
94
+ /**
95
+ * The number of SlideIdsListeners registered with the Indexes object.
96
+ */
96
97
  sliceIds?: number;
98
+ /**
99
+ * The number of SliceRowIdsListeners registered with the Indexes object.
100
+ */
97
101
  sliceRowIds?: number;
98
102
  };
99
103
 
@@ -118,7 +122,7 @@ export type IndexesListenerStats = {
118
122
  * creation, to adding a definition, getting its contents, and then registering
119
123
  * and removing a listener for it.
120
124
  *
121
- * ```tsx
125
+ * ```js
122
126
  * const store = createStore().setTable('pets', {
123
127
  * fido: {species: 'dog'},
124
128
  * felix: {species: 'cat'},
@@ -146,6 +150,7 @@ export type IndexesListenerStats = {
146
150
  * indexes.delListener(listenerId);
147
151
  * indexes.destroy();
148
152
  * ```
153
+ * @category Indexes
149
154
  */
150
155
  export interface Indexes {
151
156
  /**
@@ -211,7 +216,7 @@ export interface Indexes {
211
216
  * This example creates a Store, creates an Indexes object, and defines a
212
217
  * simple Index based on the values in the `species` Cell.
213
218
  *
214
- * ```tsx
219
+ * ```js
215
220
  * const store = createStore().setTable('pets', {
216
221
  * fido: {species: 'dog'},
217
222
  * felix: {species: 'cat'},
@@ -230,7 +235,7 @@ export interface Indexes {
230
235
  * This example creates a Store, creates an Indexes object, and defines an
231
236
  * Index based on the first letter of the pets' names.
232
237
  *
233
- * ```tsx
238
+ * ```js
234
239
  * const store = createStore().setTable('pets', {
235
240
  * fido: {species: 'dog'},
236
241
  * felix: {species: 'cat'},
@@ -250,7 +255,7 @@ export interface Indexes {
250
255
  * Index based on the first letter of the pets' names. The Slice Ids (and Row
251
256
  * Ids within them) are alphabetically sorted.
252
257
  *
253
- * ```tsx
258
+ * ```js
254
259
  * const store = createStore().setTable('pets', {
255
260
  * fido: {species: 'dog'},
256
261
  * felix: {species: 'cat'},
@@ -292,7 +297,7 @@ export interface Indexes {
292
297
  * This example creates a Store, creates an Indexes object, defines a simple
293
298
  * Index, and then removes it.
294
299
  *
295
- * ```tsx
300
+ * ```js
296
301
  * const store = createStore().setTable('pets', {
297
302
  * fido: {species: 'dog'},
298
303
  * felix: {species: 'cat'},
@@ -321,7 +326,7 @@ export interface Indexes {
321
326
  * This example creates an Indexes object against a newly-created Store and
322
327
  * then gets its reference in order to update its data.
323
328
  *
324
- * ```tsx
329
+ * ```js
325
330
  * const indexes = createIndexes(createStore());
326
331
  * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
327
332
  * indexes.getStore().setCell('pets', 'fido', 'species', 'dog');
@@ -341,7 +346,7 @@ export interface Indexes {
341
346
  * This example creates an Indexes object with two definitions, and then gets
342
347
  * the Ids of the definitions.
343
348
  *
344
- * ```tsx
349
+ * ```js
345
350
  * const indexes = createIndexes(createStore())
346
351
  * .setIndexDefinition('bySpecies', 'pets', 'species')
347
352
  * .setIndexDefinition('byColor', 'pets', 'color');
@@ -365,7 +370,7 @@ export interface Indexes {
365
370
  * This example creates an Indexes object, a single Index definition, and then
366
371
  * queries it (and a non-existent definition) to get the underlying Table Id.
367
372
  *
368
- * ```tsx
373
+ * ```js
369
374
  * const indexes = createIndexes(createStore());
370
375
  * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
371
376
  *
@@ -391,7 +396,7 @@ export interface Indexes {
391
396
  * simple Index. It then uses getSliceIds to see the available Slice Ids in
392
397
  * the Index (and also the Slice Ids in an Index that has not been defined).
393
398
  *
394
- * ```tsx
399
+ * ```js
395
400
  * const store = createStore().setTable('pets', {
396
401
  * fido: {species: 'dog'},
397
402
  * felix: {species: 'cat'},
@@ -425,7 +430,7 @@ export interface Indexes {
425
430
  * simple Index. It then uses getSliceRowIds to see the Row Ids in the Slice
426
431
  * (and also the Row Ids in Slices that do not exist).
427
432
  *
428
- * ```tsx
433
+ * ```js
429
434
  * const store = createStore().setTable('pets', {
430
435
  * fido: {species: 'dog'},
431
436
  * felix: {species: 'cat'},
@@ -467,7 +472,7 @@ export interface Indexes {
467
472
  * This example creates a Store, a Indexes object, and then registers a
468
473
  * listener that responds to any changes to a specific Index.
469
474
  *
470
- * ```tsx
475
+ * ```js
471
476
  * const store = createStore().setTable('pets', {
472
477
  * fido: {species: 'dog'},
473
478
  * felix: {species: 'cat'},
@@ -495,7 +500,7 @@ export interface Indexes {
495
500
  * This example creates a Store, a Indexes object, and then registers a
496
501
  * listener that responds to any changes to any Index.
497
502
  *
498
- * ```tsx
503
+ * ```js
499
504
  * const store = createStore().setTable('pets', {
500
505
  * fido: {species: 'dog', color: 'brown'},
501
506
  * felix: {species: 'cat', color: 'black'},
@@ -552,7 +557,7 @@ export interface Indexes {
552
557
  * This example creates a Store, a Indexes object, and then registers a
553
558
  * listener that responds to any changes to a specific Slice.
554
559
  *
555
- * ```tsx
560
+ * ```js
556
561
  * const store = createStore().setTable('pets', {
557
562
  * fido: {species: 'dog'},
558
563
  * felix: {species: 'cat'},
@@ -581,7 +586,7 @@ export interface Indexes {
581
586
  * This example creates a Store, a Indexes object, and then registers a
582
587
  * listener that responds to any changes to any Slice.
583
588
  *
584
- * ```tsx
589
+ * ```js
585
590
  * const store = createStore().setTable('pets', {
586
591
  * fido: {species: 'dog', color: 'brown'},
587
592
  * felix: {species: 'cat', color: 'black'},
@@ -633,7 +638,7 @@ export interface Indexes {
633
638
  * This example creates a Store, a Indexes object, registers a listener, and
634
639
  * then removes it.
635
640
  *
636
- * ```tsx
641
+ * ```js
637
642
  * const store = createStore().setTable('pets', {
638
643
  * fido: {species: 'dog'},
639
644
  * felix: {species: 'cat'},
@@ -675,7 +680,7 @@ export interface Indexes {
675
680
  * definition (that registers a RowListener with the underlying Store),
676
681
  * and then destroys it again, removing the listener.
677
682
  *
678
- * ```tsx
683
+ * ```js
679
684
  * const store = createStore().setTable('pets', {
680
685
  * fido: {species: 'dog'},
681
686
  * felix: {species: 'cat'},
@@ -714,7 +719,7 @@ export interface Indexes {
714
719
  * @example
715
720
  * This example gets the listener statistics of an Indexes object.
716
721
  *
717
- * ```tsx
722
+ * ```js
718
723
  * const store = createStore();
719
724
  * const indexes = createIndexes(store);
720
725
  * indexes.addSliceIdsListener(null, () => {
@@ -747,7 +752,7 @@ export interface Indexes {
747
752
  * @example
748
753
  * This example creates an Indexes object.
749
754
  *
750
- * ```tsx
755
+ * ```js
751
756
  * const store = createStore();
752
757
  * const indexes = createIndexes(store);
753
758
  * console.log(indexes.getIndexIds());
@@ -757,59 +762,13 @@ export interface Indexes {
757
762
  * This example creates an Indexes object, and calls the method a second time
758
763
  * for the same Store to return the same object.
759
764
  *
760
- * ```tsx
765
+ * ```js
761
766
  * const store = createStore();
762
767
  * const indexes1 = createIndexes(store);
763
768
  * const indexes2 = createIndexes(store);
764
769
  * console.log(indexes1 === indexes2);
765
770
  * // -> true
766
771
  * ```
772
+ * @category Creation
767
773
  */
768
774
  export function createIndexes(store: Store): Indexes;
769
-
770
- /**
771
- * The defaultSorter function is provided as a convenience to sort keys
772
- * alphanumerically, and can be provided to the `sliceIdSorter` and
773
- * `rowIdSorter` parameters of the setIndexDefinition method, for example.
774
- *
775
- * @param sortKey1 The first item of the pair to compare.
776
- * @param sortKey2 The second item of the pair to compare.
777
- * @returns A number indicating how to sort the pair.
778
- * @example
779
- * This example creates an Indexes object.
780
- *
781
- * ```tsx
782
- * const store = createStore();
783
- * const indexes = createIndexes(store);
784
- * console.log(indexes.getIndexIds());
785
- * // -> []
786
- * ```
787
- * @example
788
- * This example creates a Store, creates an Indexes object, and defines an
789
- * Index based on the first letter of the pets' names. The Slice Ids (and Row
790
- * Ids within them) are alphabetically sorted using the defaultSorter function.
791
- *
792
- * ```tsx
793
- * const store = createStore().setTable('pets', {
794
- * fido: {species: 'dog'},
795
- * felix: {species: 'cat'},
796
- * cujo: {species: 'dog'},
797
- * });
798
- *
799
- * const indexes = createIndexes(store);
800
- * indexes.setIndexDefinition(
801
- * 'byFirst', // indexId
802
- * 'pets', // tableId
803
- * (_, rowId) => rowId[0], // each Row's Slice Id
804
- * (_, rowId) => rowId, // each Row's sort key
805
- * defaultSorter, // sort Slice Ids
806
- * defaultSorter, // sort Row Ids by sort key
807
- * );
808
- *
809
- * console.log(indexes.getSliceIds('byFirst'));
810
- * // -> ['c', 'f']
811
- * console.log(indexes.getSliceRowIds('byFirst', 'f'));
812
- * // -> ['felix', 'fido']
813
- * ```
814
- */
815
- export function defaultSorter(sortKey1: SortKey, sortKey2: SortKey): number;
package/lib/indexes.js CHANGED
@@ -1 +1 @@
1
- const e=e=>typeof e,t=e(""),s=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),n=(e,t)=>e.sort(t),o=(e,t)=>e.forEach(t),r=e=>e.length,d=e=>0==r(e),c=e=>e.slice(1),i=e=>null==e,l=(e,t,s)=>i(e)?s?.():t(e),a=(e,t)=>e?.has(t)??!1,u=e=>i(e)||0==(e=>e.size)(e),g=e=>e.clear(),I=(e,t)=>e?.forEach(t),f=(e,t)=>e?.delete(t),h=e=>new Map(e),w=e=>[...e?.keys()??[]],p=(e,t)=>e?.get(t),L=(e,t)=>I(e,((e,s)=>t(s,e))),S=(e,t,s)=>i(s)?(f(e,t),e):e?.set(t,s),v=(e,t,s,n)=>(a(e,t)||(n?.(s),e.set(t,s)),p(e,t)),R=e=>new Set(e),b=(e,t)=>e?.add(t),x=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),y=(e,t,s)=>r(s)<2?b(d(s)?e:v(e,s[0],R()),t):y(v(e,s[0],h()),t,c(s)),T=e=>{const t=(s,n,...r)=>l(s,(s=>d(r)?e(s,n):o([r[0],null],(e=>t(p(s,e),n,...c(r))))));return t},k=Object.freeze,z=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((e=>{const t=h(),d=h(),[c,v,z,E,M,j,C,O]=((e,t,s)=>{const n=e.hasRow,r=h(),d=h(),c=h(),u=h(),f=h(),v=t=>l(p(f,t),(s=>{I(s,e.delListener),S(f,t)})),b=e=>{S(r,e),S(d,e),S(c,e),S(u,e),v(e)};return[()=>e,()=>w(r),e=>p(r,e),e=>p(d,e),(e,t)=>S(d,e,t),(l,w,b,x,y)=>{const T=h(),k=h();S(r,l,w),a(d,l)||(S(d,l,t()),S(c,l,h()),S(u,l,h()));const z=p(c,l),D=p(u,l),E=t=>{const o=s=>e.getCell(w,t,s),r=p(z,t),d=n(w,t)?s(x(o,t)):void 0;if(r!=d&&S(T,t,[r,d]),!i(y)){const e=p(D,t),s=n(w,t)?y(o,t):void 0;e!=s&&S(k,t,s)}},M=e=>{b((()=>{I(T,(([,e],t)=>S(z,t,e))),I(k,((e,t)=>S(D,t,e)))}),T,k,z,D,e),g(T),g(k)};L(z,E),e.hasTable(w)&&o(e.getRowIds(w),(e=>{a(z,e)||E(e)})),M(!0),v(l),S(f,l,R([e.addRowListener(w,null,((e,t,s)=>E(s))),e.addTableListener(w,(()=>M()))]))},b,()=>L(f,b)]})(e,h,(e=>i(e)?"":e+"")),[W,m,q]=(e=>{let t,s=0;const n=[],d=h();return[(o,r,c=[])=>{t??(t=e());const i=n.pop()??""+s++;return S(d,i,[o,r,c]),y(r,i,c),i},(e,s=[],...n)=>T(I)(e,(e=>l(p(d,e),(([e])=>e(t,...s,...n)))),...s),e=>l(p(d,e),(([,t,s])=>(T(f)(t,e,...s),S(d,e),r(n)<1e3&&n.push(e),s)),(()=>[])),(e,s,n)=>l(p(d,e),(([e,,d])=>{const c=(...l)=>{const a=r(l);a==r(d)?e(t,...l,...n(l)):i(d[a])?o(s[a](...l),(e=>c(...l,e))):c(...l,d[a])};c()}))]})((()=>A)),A={setIndexDefinition:(e,o,r,c,g,w=D)=>{const v=i(g)?void 0:([e],[t])=>g(e,t);return j(e,o,((o,r,g,x,y)=>{let T=0;const k=R(),z=R(),D=E(e);if(I(r,(([e,t],s)=>{i(e)||(b(k,e),l(p(D,e),(t=>{f(t,s),u(t)&&(S(D,e),T=1)}))),i(t)||(b(k,t),a(D,t)||(S(D,t,R()),T=1),b(p(D,t),s),i(c)||b(z,t))})),o(),L(g,(e=>l(p(x,e),(e=>b(z,e))))),u(y)||I(z,(e=>{const t=(t,s)=>w(p(y,t),p(y,s),e),o=[...p(D,e)];s(o,t)||(S(D,e,R(n(o,t))),b(k,e))})),T){if(!i(v)){const t=[...D];s(t,v)||M(e,h(n(t,v)))}m(t,[e])}I(k,(t=>m(d,[e,t])))}),x(r),l(c,x)),A},delIndexDefinition:e=>(C(e),A),getStore:c,getIndexIds:v,getTableId:z,getSliceIds:e=>w(E(e)),getSliceRowIds:(e,t)=>[...p(E(e),t)?.values()??[]],addSliceIdsListener:(e,s)=>W(s,t,[e]),addSliceRowIdsListener:(e,t,s)=>W(s,d,[e,t]),delListener:e=>(q(e),A),destroy:O,getListenerStats:()=>({})};return k(A)})),D=(e,t)=>e<t?-1:1;export{z as createIndexes,D as defaultSorter};
1
+ const e=e=>typeof e,t=e(""),s=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),n=(e,t)=>e.sort(t),o=(e,t)=>e.forEach(t),r=e=>e.length,d=e=>0==r(e),c=e=>e.slice(1),l=e=>null==e,i=(e,t,s)=>l(e)?s?.():t(e),a=(e,t)=>e?.has(t)??!1,u=e=>l(e)||0==(e=>e.size)(e),g=e=>e.clear(),I=(e,t)=>e?.forEach(t),f=(e,t)=>e?.delete(t),h=e=>new Map(e),w=e=>[...e?.keys()??[]],p=(e,t)=>e?.get(t),L=(e,t)=>I(e,((e,s)=>t(s,e))),S=(e,t,s)=>l(s)?(f(e,t),e):e?.set(t,s),v=(e,t,s,n)=>(a(e,t)||(n?.(s),e.set(t,s)),p(e,t)),R=e=>new Set(e),b=(e,t)=>e?.add(t),x=(s,n)=>e(s)==t?e=>e(s):s??(()=>n??""),y=(e,t)=>e<t?-1:1,T=(e,t,s)=>r(s)<2?b(d(s)?e:v(e,s[0],R()),t):T(v(e,s[0],h()),t,c(s)),k=e=>{const t=(s,n,...r)=>i(s,(s=>d(r)?e(s,n):o([r[0],null],(e=>t(p(s,e),n,...c(r))))));return t},z=Object.freeze,D=(e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))})((e=>{const t=h(),d=h(),[c,v,D,E,M,j,C,O]=((e,t,s)=>{const n=e.hasRow,r=h(),d=h(),c=h(),u=h(),f=h(),v=t=>i(p(f,t),(s=>{I(s,e.delListener),S(f,t)})),b=e=>{S(r,e),S(d,e),S(c,e),S(u,e),v(e)};return[()=>e,()=>w(r),e=>p(r,e),e=>p(d,e),(e,t)=>S(d,e,t),(i,w,b,x,y)=>{const T=h(),k=h();S(r,i,w),a(d,i)||(S(d,i,t()),S(c,i,h()),S(u,i,h()));const z=p(c,i),D=p(u,i),E=t=>{const o=s=>e.getCell(w,t,s),r=p(z,t),d=n(w,t)?s(x(o,t)):void 0;if(r!=d&&S(T,t,[r,d]),!l(y)){const e=p(D,t),s=n(w,t)?y(o,t):void 0;e!=s&&S(k,t,s)}},M=e=>{b((()=>{I(T,(([,e],t)=>S(z,t,e))),I(k,((e,t)=>S(D,t,e)))}),T,k,z,D,e),g(T),g(k)};L(z,E),e.hasTable(w)&&o(e.getRowIds(w),(e=>{a(z,e)||E(e)})),M(!0),v(i),S(f,i,R([e.addRowListener(w,null,((e,t,s)=>E(s))),e.addTableListener(w,(()=>M()))]))},b,()=>L(f,b)]})(e,h,(e=>l(e)?"":e+"")),[W,m,q]=(e=>{let t,s=0;const n=[],d=h();return[(o,r,c=[])=>{t??=e();const l=n.pop()??""+s++;return S(d,l,[o,r,c]),T(r,l,c),l},(e,s=[],...n)=>k(I)(e,(e=>i(p(d,e),(([e])=>e(t,...s,...n)))),...s),e=>i(p(d,e),(([,t,s])=>(k(f)(t,e,...s),S(d,e),r(n)<1e3&&n.push(e),s)),(()=>[])),(e,s,n)=>i(p(d,e),(([e,,d])=>{const c=(...i)=>{const a=r(i);a==r(d)?e(t,...i,...n(i)):l(d[a])?o(s[a](...i),(e=>c(...i,e))):c(...i,d[a])};c()}))]})((()=>A)),A={setIndexDefinition:(e,o,r,c,g,w=y)=>{const v=l(g)?void 0:([e],[t])=>g(e,t);return j(e,o,((o,r,g,x,y,T)=>{let k=0;const z=R(),D=R(),j=E(e);if(I(r,(([e,t],s)=>{l(e)||(b(z,e),i(p(j,e),(t=>{f(t,s),u(t)&&(S(j,e),k=1)}))),l(t)||(b(z,t),a(j,t)||(S(j,t,R()),k=1),b(p(j,t),s),l(c)||b(D,t))})),o(),u(y)||(T?L(j,(e=>b(D,e))):L(g,(e=>i(p(x,e),(e=>b(D,e))))),I(D,(e=>{const t=(t,s)=>w(p(y,t),p(y,s),e),o=[...p(j,e)];s(o,t)||(S(j,e,R(n(o,t))),b(z,e))}))),(k||T)&&!l(v)){const t=[...j];s(t,v)||(M(e,h(n(t,v))),k=1)}k&&m(t,[e]),I(z,(t=>m(d,[e,t])))}),x(r),i(c,x)),A},delIndexDefinition:e=>(C(e),A),getStore:c,getIndexIds:v,getTableId:D,getSliceIds:e=>w(E(e)),getSliceRowIds:(e,t)=>[...p(E(e),t)?.values()??[]],addSliceIdsListener:(e,s)=>W(s,t,[e]),addSliceRowIdsListener:(e,t,s)=>W(s,d,[e,t]),delListener:e=>(q(e),A),destroy:O,getListenerStats:()=>({})};return z(A)}));export{D as createIndexes};
package/lib/indexes.js.gz CHANGED
Binary file
package/lib/metrics.d.ts CHANGED
@@ -3,8 +3,8 @@
3
3
  * and track metrics and aggregates of the data in Store objects.
4
4
  *
5
5
  * The main entry point to this module is the createMetrics function, which
6
- * returns a new Metrics object. From there, you can create new metric
7
- * definitions, access the values of those metrics directly, and register
6
+ * returns a new Metrics object. From there, you can create new Metric
7
+ * definitions, access the values of those Metrics directly, and register
8
8
  * listeners for when they change.
9
9
  *
10
10
  * @packageDocumentation
@@ -31,6 +31,9 @@ export type Metric = number;
31
31
  * use a more complex aggregation of your own devising. See the
32
32
  * setMetricDefinition method for more examples.
33
33
  *
34
+ * @param numbers The array of numbers in the Metric's aggregation.
35
+ * @param length The length of the array of numbers in the Metric's aggregation.
36
+ * @returns The value of the Metric.
34
37
  * @category Aggregators
35
38
  */
36
39
  export type Aggregate = (numbers: number[], length: number) => Metric;
@@ -53,6 +56,10 @@ export type Aggregate = (numbers: number[], length: number) => Metric;
53
56
  * cost of growing the input data set. See the setMetricDefinition method for
54
57
  * more examples.
55
58
  *
59
+ * @param metric The current value of the Metric.
60
+ * @param add The number being added to the Metric's aggregation.
61
+ * @param length The length of the array of numbers in the Metric's aggregation.
62
+ * @returns The new value of the Metric.
56
63
  * @category Aggregators
57
64
  */
58
65
  export type AggregateAdd = (
@@ -82,6 +89,10 @@ export type AggregateAdd = (
82
89
  * cost of shrinking the input data set. See the setMetricDefinition method for
83
90
  * more examples.
84
91
  *
92
+ * @param metric The current value of the Metric.
93
+ * @param remove The number being removed from the Metric's aggregation.
94
+ * @param length The length of the array of numbers in the Metric's aggregation.
95
+ * @returns The new value of the Metric.
85
96
  * @category Aggregators
86
97
  */
87
98
  export type AggregateRemove = (
@@ -109,6 +120,11 @@ export type AggregateRemove = (
109
120
  * cost of changing the input data set in place. See the setMetricDefinition
110
121
  * method for more examples.
111
122
  *
123
+ * @param metric The current value of the Metric.
124
+ * @param add The number being added to the Metric's aggregation.
125
+ * @param remove The number being removed from the Metric's aggregation.
126
+ * @param length The length of the array of numbers in the Metric's aggregation.
127
+ * @returns The new value of the Metric.
112
128
  * @category Aggregators
113
129
  */
114
130
  export type AggregateReplace = (
@@ -132,6 +148,10 @@ export type AggregateReplace = (
132
148
  * has gained its first row), the old value will be `undefined`. If a Metric now
133
149
  * no longer has a value, the new value will be `undefined`.
134
150
  *
151
+ * @param metrics A reference to the Metrics object that changed.
152
+ * @param metricId The Id of the Metric that changed.
153
+ * @param newMetric The new value of the Metric that changed.
154
+ * @param oldMetric The old value of the Metric that changed.
135
155
  * @category Listener
136
156
  */
137
157
  export type MetricListener = (
@@ -151,6 +171,9 @@ export type MetricListener = (
151
171
  * @category Development
152
172
  */
153
173
  export type MetricsListenerStats = {
174
+ /**
175
+ * The number of MetricListeners registered with the Metrics object.
176
+ */
154
177
  metric?: number;
155
178
  };
156
179
 
@@ -176,7 +199,7 @@ export type MetricsListenerStats = {
176
199
  * creation, to adding a definition, getting an Metric, and then registering and
177
200
  * removing a listener for it.
178
201
  *
179
- * ```tsx
202
+ * ```js
180
203
  * const store = createStore().setTable('species', {
181
204
  * dog: {price: 5},
182
205
  * cat: {price: 4},
@@ -203,6 +226,7 @@ export type MetricsListenerStats = {
203
226
  * metrics.delListener(listenerId);
204
227
  * metrics.destroy();
205
228
  * ```
229
+ * @category Metrics
206
230
  */
207
231
  export interface Metrics {
208
232
  /**
@@ -258,7 +282,7 @@ export interface Metrics {
258
282
  * This example creates a Store, creates a Metrics object, and defines a
259
283
  * simple Metric to count the Row objects in the Table.
260
284
  *
261
- * ```tsx
285
+ * ```js
262
286
  * const store = createStore().setTable('species', {
263
287
  * dog: {price: 5},
264
288
  * cat: {price: 4},
@@ -276,7 +300,7 @@ export interface Metrics {
276
300
  * standard Metric to get the highest value of each `price` Cell in the Row
277
301
  * objects in the Table.
278
302
  *
279
- * ```tsx
303
+ * ```js
280
304
  * const store = createStore().setTable('species', {
281
305
  * dog: {price: 5},
282
306
  * cat: {price: 4},
@@ -293,7 +317,7 @@ export interface Metrics {
293
317
  * This example creates a Store, creates a Metrics object, and defines a
294
318
  * custom Metric to get the lowest value of each `price` Cell, greater than 2.
295
319
  *
296
- * ```tsx
320
+ * ```js
297
321
  * const store = createStore().setTable('species', {
298
322
  * dog: {price: 5},
299
323
  * cat: {price: 4},
@@ -317,7 +341,7 @@ export interface Metrics {
317
341
  * However, it also reduces algorithmic complexity with two shortcut
318
342
  * functions.
319
343
  *
320
- * ```tsx
344
+ * ```js
321
345
  * const store = createStore().setTable('species', {
322
346
  * dog: {price: 5},
323
347
  * cat: {price: 4},
@@ -350,7 +374,7 @@ export interface Metrics {
350
374
  * This example creates a Store, creates a Metrics object, and defines a
351
375
  * custom Metric to get the average value of a discounted price.
352
376
  *
353
- * ```tsx
377
+ * ```js
354
378
  * const store = createStore().setTable('species', {
355
379
  * dog: {price: 5, discount: 0.3},
356
380
  * cat: {price: 4, discount: 0.2},
@@ -389,7 +413,7 @@ export interface Metrics {
389
413
  * This example creates a Store, creates a Metrics object, defines a simple
390
414
  * Metric, and then removes it.
391
415
  *
392
- * ```tsx
416
+ * ```js
393
417
  * const store = createStore().setTable('species', {
394
418
  * dog: {price: 5},
395
419
  * cat: {price: 4},
@@ -418,7 +442,7 @@ export interface Metrics {
418
442
  * This example creates a Metrics object against a newly-created Store and
419
443
  * then gets its reference in order to update its data.
420
444
  *
421
- * ```tsx
445
+ * ```js
422
446
  * const metrics = createMetrics(createStore());
423
447
  * metrics.setMetricDefinition('speciesCount', 'species');
424
448
  * metrics.getStore().setCell('species', 'dog', 'price', 5);
@@ -438,7 +462,7 @@ export interface Metrics {
438
462
  * This example creates a Metrics object with two definitions, and then gets
439
463
  * the Ids of the definitions.
440
464
  *
441
- * ```tsx
465
+ * ```js
442
466
  * const metrics = createMetrics(createStore())
443
467
  * .setMetricDefinition('speciesCount', 'species')
444
468
  * .setMetricDefinition('petsCount', 'pets');
@@ -462,7 +486,7 @@ export interface Metrics {
462
486
  * This example creates a Metrics object, a single Metric definition, and then
463
487
  * queries it (and a non-existent definition) to get the underlying Table Id.
464
488
  *
465
- * ```tsx
489
+ * ```js
466
490
  * const metrics = createMetrics(createStore());
467
491
  * metrics.setMetricDefinition('speciesCount', 'species');
468
492
  *
@@ -489,7 +513,7 @@ export interface Metrics {
489
513
  * getMetric to access its value (and also the value of a Metric that has not
490
514
  * been defined).
491
515
  *
492
- * ```tsx
516
+ * ```js
493
517
  * const store = createStore().setTable('species', {
494
518
  * dog: {price: 5},
495
519
  * cat: {price: 4},
@@ -529,7 +553,7 @@ export interface Metrics {
529
553
  * This example creates a Store, a Metrics object, and then registers a
530
554
  * listener that responds to any changes to a specific Metric.
531
555
  *
532
- * ```tsx
556
+ * ```js
533
557
  * const store = createStore().setTable('species', {
534
558
  * dog: {price: 5},
535
559
  * cat: {price: 4},
@@ -557,7 +581,7 @@ export interface Metrics {
557
581
  * This example creates a Store, a Metrics object, and then registers a
558
582
  * listener that responds to any changes to any Metric.
559
583
  *
560
- * ```tsx
584
+ * ```js
561
585
  * const store = createStore().setTable('species', {
562
586
  * dog: {price: 5},
563
587
  * cat: {price: 4},
@@ -601,7 +625,7 @@ export interface Metrics {
601
625
  * This example creates a Store, a Metrics object, registers a listener, and
602
626
  * then removes it.
603
627
  *
604
- * ```tsx
628
+ * ```js
605
629
  * const store = createStore().setTable('species', {
606
630
  * dog: {price: 5},
607
631
  * cat: {price: 4},
@@ -643,7 +667,7 @@ export interface Metrics {
643
667
  * registers a RowListener with the underlying Store), and then destroys it
644
668
  * again, removing the listener.
645
669
  *
646
- * ```tsx
670
+ * ```js
647
671
  * const store = createStore().setTable('species', {
648
672
  * dog: {price: 5},
649
673
  * cat: {price: 4},
@@ -679,7 +703,7 @@ export interface Metrics {
679
703
  * @example
680
704
  * This example gets the listener statistics of a Metrics object.
681
705
  *
682
- * ```tsx
706
+ * ```js
683
707
  * const store = createStore();
684
708
  * const metrics = createMetrics(store);
685
709
  * metrics.addMetricListener(null, () => console.log('Metric changed'));
@@ -707,7 +731,7 @@ export interface Metrics {
707
731
  * @example
708
732
  * This example creates a Metrics object.
709
733
  *
710
- * ```tsx
734
+ * ```js
711
735
  * const store = createStore();
712
736
  * const metrics = createMetrics(store);
713
737
  * console.log(metrics.getMetricIds());
@@ -717,12 +741,13 @@ export interface Metrics {
717
741
  * This example creates a Metrics object, and calls the method a second time
718
742
  * for the same Store to return the same object.
719
743
  *
720
- * ```tsx
744
+ * ```js
721
745
  * const store = createStore();
722
746
  * const metrics1 = createMetrics(store);
723
747
  * const metrics2 = createMetrics(store);
724
748
  * console.log(metrics1 === metrics2);
725
749
  * // -> true
726
750
  * ```
751
+ * @category Creation
727
752
  */
728
753
  export function createMetrics(store: Store): Metrics;
package/lib/metrics.js CHANGED
@@ -1 +1 @@
1
- const e=e=>typeof e,t=e(""),n=e(e),s=(e,t)=>e.forEach(t),o=e=>c(e,((e,t)=>e+t),0),i=e=>e.length,r=e=>0==i(e),c=(e,t,n)=>e.reduce(t,n),a=e=>e.slice(1),d=Math.max,l=Math.min,u=isFinite,v=e=>null==e,g=(e,t,n)=>v(e)?n?.():t(e),h=()=>{},M=e=>e.size,f=(e,t)=>e?.has(t)??!1,p=e=>e.clear(),m=(e,t)=>e?.forEach(t),w=(e,t)=>e?.delete(t),L=e=>new Map(e),b=(e,t)=>e?.get(t),x=(e,t)=>m(e,((e,n)=>t(n,e))),y=(e,t,n)=>v(n)?(w(e,t),e):e?.set(t,n),I=(e,t,n,s)=>(f(e,t)||(s?.(n),e.set(t,n)),b(e,t)),R=e=>new Set(e),S=(n,s)=>e(n)==t?e=>e(n):n??(()=>s??""),T=(e,t,n)=>i(n)<2?((e,t)=>e?.add(t))(r(n)?e:I(e,n[0],R()),t):T(I(e,n[0],L()),t,a(n)),k=e=>{const t=(n,o,...i)=>g(n,(n=>r(i)?e(n,o):s([i[0],null],(e=>t(b(n,e),o,...a(i))))));return t},z=Object.freeze,D=L([["avg",[(e,t)=>o(e)/t,(e,t,n)=>e+(t-e)/(n+1),(e,t,n)=>e+(e-t)/(n-1),(e,t,n,s)=>e+(t-n)/s]],["max",[e=>d(...e),(e,t)=>d(t,e),(e,t)=>t==e?void 0:e,(e,t,n)=>n==e?void 0:d(t,e)]],["min",[e=>l(...e),(e,t)=>l(t,e),(e,t)=>t==e?void 0:e,(e,t,n)=>n==e?void 0:l(t,e)]],["sum",[e=>o(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,n)=>e-n+t]]]),E=(e=>{const t=new WeakMap;return n=>(t.has(n)||t.set(n,e(n)),t.get(n))})((t=>{const o=L(),[r,c,a,d,l,I,E,N]=((e,t,n)=>{const o=e.hasRow,i=L(),r=L(),c=L(),a=L(),d=L(),l=t=>g(b(d,t),(n=>{m(n,e.delListener),y(d,t)})),u=e=>{y(i,e),y(r,e),y(c,e),y(a,e),l(e)};return[()=>e,()=>[...i?.keys()??[]],e=>b(i,e),e=>b(r,e),(e,t)=>y(r,e,t),(u,g,h,M,w)=>{const I=L(),S=L();y(i,u,g),f(r,u)||(y(r,u,t()),y(c,u,L()),y(a,u,L()));const T=b(c,u),k=b(a,u),z=t=>{const s=n=>e.getCell(g,t,n),i=b(T,t),r=o(g,t)?n(M(s,t)):void 0;if(i!=r&&y(I,t,[i,r]),!v(w)){const e=b(k,t),n=o(g,t)?w(s,t):void 0;e!=n&&y(S,t,n)}},D=e=>{h((()=>{m(I,(([,e],t)=>y(T,t,e))),m(S,((e,t)=>y(k,t,e)))}),I,S,T,k,e),p(I),p(S)};x(T,z),e.hasTable(g)&&s(e.getRowIds(g),(e=>{f(T,e)||z(e)})),D(!0),l(u),y(d,u,R([e.addRowListener(g,null,((e,t,n)=>z(n))),e.addTableListener(g,(()=>D()))]))},u,()=>x(d,u)]})(t,h,(e=>isNaN(e)||v(e)||!0===e||!1===e||""===e?void 0:1*e)),[j,C,F]=(e=>{let t,n=0;const o=[],r=L();return[(s,i,c=[])=>{t??(t=e());const a=o.pop()??""+n++;return y(r,a,[s,i,c]),T(i,a,c),a},(e,n=[],...s)=>k(m)(e,(e=>g(b(r,e),(([e])=>e(t,...n,...s)))),...n),e=>g(b(r,e),(([,t,n])=>(k(w)(t,e,...n),y(r,e),i(o)<1e3&&o.push(e),n)),(()=>[])),(e,n,o)=>g(b(r,e),(([e,,r])=>{const c=(...a)=>{const d=i(a);d==i(r)?e(t,...a,...o(a)):v(r[d])?s(n[d](...a),(e=>c(...a,e))):c(...a,r[d])};c()}))]})((()=>O)),O={setMetricDefinition:(t,s,i,r,c,a,g)=>{const h=e(i)==n?[i,c,a,g]:b(D,i)??b(D,"sum");return I(t,s,((e,n,s,i,r,c)=>{let a=d(t),g=M(i);const[f,p,w,L]=h;var b;c=c||v(a),m(n,(([e,t])=>{c||(a=v(e)?p?.(a,t,g++):v(t)?w?.(a,e,g--):L?.(a,t,e,g)),c=c||v(a)})),e(),v(b=i)||0==M(b)?a=void 0:c&&(a=f((e=>[...e?.values()??[]])(i),M(i))),u(a)||(a=void 0);const x=d(t);a!=x&&(l(t,a),C(o,[t],a,x))}),S(r,1)),O},delMetricDefinition:e=>(E(e),O),getStore:r,getMetricIds:c,getTableId:a,getMetric:d,addMetricListener:(e,t)=>j(t,o,[e]),delListener:e=>(F(e),O),destroy:N,getListenerStats:()=>({})};return z(O)}));export{E as createMetrics};
1
+ const e=e=>typeof e,t=e(""),n=e(e),s=(e,t)=>e.forEach(t),o=e=>c(e,((e,t)=>e+t),0),i=e=>e.length,r=e=>0==i(e),c=(e,t,n)=>e.reduce(t,n),a=e=>e.slice(1),d=Math.max,l=Math.min,u=isFinite,v=e=>null==e,g=(e,t,n)=>v(e)?n?.():t(e),h=()=>{},M=e=>e.size,f=(e,t)=>e?.has(t)??!1,p=e=>e.clear(),m=(e,t)=>e?.forEach(t),w=(e,t)=>e?.delete(t),L=e=>new Map(e),b=(e,t)=>e?.get(t),x=(e,t)=>m(e,((e,n)=>t(n,e))),y=(e,t,n)=>v(n)?(w(e,t),e):e?.set(t,n),I=(e,t,n,s)=>(f(e,t)||(s?.(n),e.set(t,n)),b(e,t)),R=e=>new Set(e),S=(n,s)=>e(n)==t?e=>e(n):n??(()=>s??""),T=(e,t,n)=>i(n)<2?((e,t)=>e?.add(t))(r(n)?e:I(e,n[0],R()),t):T(I(e,n[0],L()),t,a(n)),k=e=>{const t=(n,o,...i)=>g(n,(n=>r(i)?e(n,o):s([i[0],null],(e=>t(b(n,e),o,...a(i))))));return t},z=Object.freeze,D=L([["avg",[(e,t)=>o(e)/t,(e,t,n)=>e+(t-e)/(n+1),(e,t,n)=>e+(e-t)/(n-1),(e,t,n,s)=>e+(t-n)/s]],["max",[e=>d(...e),(e,t)=>d(t,e),(e,t)=>t==e?void 0:e,(e,t,n)=>n==e?void 0:d(t,e)]],["min",[e=>l(...e),(e,t)=>l(t,e),(e,t)=>t==e?void 0:e,(e,t,n)=>n==e?void 0:l(t,e)]],["sum",[e=>o(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,n)=>e-n+t]]]),E=(e=>{const t=new WeakMap;return n=>(t.has(n)||t.set(n,e(n)),t.get(n))})((t=>{const o=L(),[r,c,a,d,l,I,E,N]=((e,t,n)=>{const o=e.hasRow,i=L(),r=L(),c=L(),a=L(),d=L(),l=t=>g(b(d,t),(n=>{m(n,e.delListener),y(d,t)})),u=e=>{y(i,e),y(r,e),y(c,e),y(a,e),l(e)};return[()=>e,()=>[...i?.keys()??[]],e=>b(i,e),e=>b(r,e),(e,t)=>y(r,e,t),(u,g,h,M,w)=>{const I=L(),S=L();y(i,u,g),f(r,u)||(y(r,u,t()),y(c,u,L()),y(a,u,L()));const T=b(c,u),k=b(a,u),z=t=>{const s=n=>e.getCell(g,t,n),i=b(T,t),r=o(g,t)?n(M(s,t)):void 0;if(i!=r&&y(I,t,[i,r]),!v(w)){const e=b(k,t),n=o(g,t)?w(s,t):void 0;e!=n&&y(S,t,n)}},D=e=>{h((()=>{m(I,(([,e],t)=>y(T,t,e))),m(S,((e,t)=>y(k,t,e)))}),I,S,T,k,e),p(I),p(S)};x(T,z),e.hasTable(g)&&s(e.getRowIds(g),(e=>{f(T,e)||z(e)})),D(!0),l(u),y(d,u,R([e.addRowListener(g,null,((e,t,n)=>z(n))),e.addTableListener(g,(()=>D()))]))},u,()=>x(d,u)]})(t,h,(e=>isNaN(e)||v(e)||!0===e||!1===e||""===e?void 0:1*e)),[j,C,F]=(e=>{let t,n=0;const o=[],r=L();return[(s,i,c=[])=>{t??=e();const a=o.pop()??""+n++;return y(r,a,[s,i,c]),T(i,a,c),a},(e,n=[],...s)=>k(m)(e,(e=>g(b(r,e),(([e])=>e(t,...n,...s)))),...n),e=>g(b(r,e),(([,t,n])=>(k(w)(t,e,...n),y(r,e),i(o)<1e3&&o.push(e),n)),(()=>[])),(e,n,o)=>g(b(r,e),(([e,,r])=>{const c=(...a)=>{const d=i(a);d==i(r)?e(t,...a,...o(a)):v(r[d])?s(n[d](...a),(e=>c(...a,e))):c(...a,r[d])};c()}))]})((()=>O)),O={setMetricDefinition:(t,s,i,r,c,a,g)=>{const h=e(i)==n?[i,c,a,g]:b(D,i)??b(D,"sum");return I(t,s,((e,n,s,i,r,c)=>{let a=d(t),g=M(i);const[f,p,w,L]=h;var b;c=c||v(a),m(n,(([e,t])=>{c||(a=v(e)?p?.(a,t,g++):v(t)?w?.(a,e,g--):L?.(a,t,e,g)),c=c||v(a)})),e(),v(b=i)||0==M(b)?a=void 0:c&&(a=f((e=>[...e?.values()??[]])(i),M(i))),u(a)||(a=void 0);const x=d(t);a!=x&&(l(t,a),C(o,[t],a,x))}),S(r,1)),O},delMetricDefinition:e=>(E(e),O),getStore:r,getMetricIds:c,getTableId:a,getMetric:d,addMetricListener:(e,t)=>j(t,o,[e]),delListener:e=>(F(e),O),destroy:N,getListenerStats:()=>({})};return z(O)}));export{E as createMetrics};
package/lib/metrics.js.gz CHANGED
Binary file