tinybase 0.9.1 → 1.0.0

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 +38 -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 +38 -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 +39 -76
  12. package/lib/debug/indexes.js +18 -10
  13. package/lib/debug/metrics.d.ts +49 -20
  14. package/lib/debug/metrics.js +1 -1
  15. package/lib/debug/persisters.d.ts +207 -17
  16. package/lib/debug/persisters.js +1 -1
  17. package/lib/debug/relationships.d.ts +48 -23
  18. package/lib/debug/relationships.js +1 -1
  19. package/lib/debug/store.d.ts +184 -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} +270 -162
  24. package/lib/debug/{react.js → ui-react.js} +28 -19
  25. package/lib/indexes.d.ts +39 -76
  26. package/lib/indexes.js +1 -1
  27. package/lib/indexes.js.gz +0 -0
  28. package/lib/metrics.d.ts +49 -20
  29. package/lib/metrics.js +1 -1
  30. package/lib/metrics.js.gz +0 -0
  31. package/lib/persisters.d.ts +207 -17
  32. package/lib/persisters.js +1 -1
  33. package/lib/persisters.js.gz +0 -0
  34. package/lib/relationships.d.ts +48 -23
  35. package/lib/relationships.js +1 -1
  36. package/lib/relationships.js.gz +0 -0
  37. package/lib/store.d.ts +184 -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} +270 -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 +28 -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 = '';
@@ -16,8 +16,8 @@ const getUndefined = () => void 0;
16
16
 
17
17
  const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
18
18
 
19
- const {createContext, useContext} = React ?? {};
20
- const Context = createContext?.([]);
19
+ const {createContext, useContext} = React;
20
+ const Context = createContext([]);
21
21
  const useThing = (id, offset) => {
22
22
  const thingsAndThingsById = useContext(Context);
23
23
  return isUndefined(id)
@@ -47,9 +47,12 @@ const useRelationshipsOrRelationshipsId = (relationshipsOrRelationshipsId) =>
47
47
  const useCheckpointsOrCheckpointsId = (checkpointsOrCheckpointsId) =>
48
48
  useThingOrThingId(checkpointsOrCheckpointsId, 8);
49
49
 
50
- const {useCallback, useEffect, useMemo: useMemo$1, useState} = React ?? {};
51
- const useCreate = (store, create, createDeps = []) =>
52
- useMemo$1(() => create(store), [store, ...createDeps]);
50
+ const {useCallback, useEffect, useMemo: useMemo$1, useState} = React;
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,11 +657,14 @@ 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
  };
660
666
 
661
- const {createElement, useMemo} = React ?? {};
667
+ const {createElement, useMemo} = React;
662
668
  const useRelationshipsStoreTableId = (relationships) => {
663
669
  const resolvedRelationships =
664
670
  useRelationshipsOrRelationshipsId(relationships);
@@ -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,11 @@ export type IndexesListenerStats = {
146
150
  * indexes.delListener(listenerId);
147
151
  * indexes.destroy();
148
152
  * ```
153
+ * @see Metrics And Indexes guides
154
+ * @see Rolling Dice demos
155
+ * @see Country demo
156
+ * @see Todo App demos
157
+ * @category Indexes
149
158
  */
150
159
  export interface Indexes {
151
160
  /**
@@ -211,7 +220,7 @@ export interface Indexes {
211
220
  * This example creates a Store, creates an Indexes object, and defines a
212
221
  * simple Index based on the values in the `species` Cell.
213
222
  *
214
- * ```tsx
223
+ * ```js
215
224
  * const store = createStore().setTable('pets', {
216
225
  * fido: {species: 'dog'},
217
226
  * felix: {species: 'cat'},
@@ -230,7 +239,7 @@ export interface Indexes {
230
239
  * This example creates a Store, creates an Indexes object, and defines an
231
240
  * Index based on the first letter of the pets' names.
232
241
  *
233
- * ```tsx
242
+ * ```js
234
243
  * const store = createStore().setTable('pets', {
235
244
  * fido: {species: 'dog'},
236
245
  * felix: {species: 'cat'},
@@ -250,7 +259,7 @@ export interface Indexes {
250
259
  * Index based on the first letter of the pets' names. The Slice Ids (and Row
251
260
  * Ids within them) are alphabetically sorted.
252
261
  *
253
- * ```tsx
262
+ * ```js
254
263
  * const store = createStore().setTable('pets', {
255
264
  * fido: {species: 'dog'},
256
265
  * felix: {species: 'cat'},
@@ -292,7 +301,7 @@ export interface Indexes {
292
301
  * This example creates a Store, creates an Indexes object, defines a simple
293
302
  * Index, and then removes it.
294
303
  *
295
- * ```tsx
304
+ * ```js
296
305
  * const store = createStore().setTable('pets', {
297
306
  * fido: {species: 'dog'},
298
307
  * felix: {species: 'cat'},
@@ -321,7 +330,7 @@ export interface Indexes {
321
330
  * This example creates an Indexes object against a newly-created Store and
322
331
  * then gets its reference in order to update its data.
323
332
  *
324
- * ```tsx
333
+ * ```js
325
334
  * const indexes = createIndexes(createStore());
326
335
  * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
327
336
  * indexes.getStore().setCell('pets', 'fido', 'species', 'dog');
@@ -341,7 +350,7 @@ export interface Indexes {
341
350
  * This example creates an Indexes object with two definitions, and then gets
342
351
  * the Ids of the definitions.
343
352
  *
344
- * ```tsx
353
+ * ```js
345
354
  * const indexes = createIndexes(createStore())
346
355
  * .setIndexDefinition('bySpecies', 'pets', 'species')
347
356
  * .setIndexDefinition('byColor', 'pets', 'color');
@@ -365,7 +374,7 @@ export interface Indexes {
365
374
  * This example creates an Indexes object, a single Index definition, and then
366
375
  * queries it (and a non-existent definition) to get the underlying Table Id.
367
376
  *
368
- * ```tsx
377
+ * ```js
369
378
  * const indexes = createIndexes(createStore());
370
379
  * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
371
380
  *
@@ -391,7 +400,7 @@ export interface Indexes {
391
400
  * simple Index. It then uses getSliceIds to see the available Slice Ids in
392
401
  * the Index (and also the Slice Ids in an Index that has not been defined).
393
402
  *
394
- * ```tsx
403
+ * ```js
395
404
  * const store = createStore().setTable('pets', {
396
405
  * fido: {species: 'dog'},
397
406
  * felix: {species: 'cat'},
@@ -425,7 +434,7 @@ export interface Indexes {
425
434
  * simple Index. It then uses getSliceRowIds to see the Row Ids in the Slice
426
435
  * (and also the Row Ids in Slices that do not exist).
427
436
  *
428
- * ```tsx
437
+ * ```js
429
438
  * const store = createStore().setTable('pets', {
430
439
  * fido: {species: 'dog'},
431
440
  * felix: {species: 'cat'},
@@ -467,7 +476,7 @@ export interface Indexes {
467
476
  * This example creates a Store, a Indexes object, and then registers a
468
477
  * listener that responds to any changes to a specific Index.
469
478
  *
470
- * ```tsx
479
+ * ```js
471
480
  * const store = createStore().setTable('pets', {
472
481
  * fido: {species: 'dog'},
473
482
  * felix: {species: 'cat'},
@@ -495,7 +504,7 @@ export interface Indexes {
495
504
  * This example creates a Store, a Indexes object, and then registers a
496
505
  * listener that responds to any changes to any Index.
497
506
  *
498
- * ```tsx
507
+ * ```js
499
508
  * const store = createStore().setTable('pets', {
500
509
  * fido: {species: 'dog', color: 'brown'},
501
510
  * felix: {species: 'cat', color: 'black'},
@@ -552,7 +561,7 @@ export interface Indexes {
552
561
  * This example creates a Store, a Indexes object, and then registers a
553
562
  * listener that responds to any changes to a specific Slice.
554
563
  *
555
- * ```tsx
564
+ * ```js
556
565
  * const store = createStore().setTable('pets', {
557
566
  * fido: {species: 'dog'},
558
567
  * felix: {species: 'cat'},
@@ -581,7 +590,7 @@ export interface Indexes {
581
590
  * This example creates a Store, a Indexes object, and then registers a
582
591
  * listener that responds to any changes to any Slice.
583
592
  *
584
- * ```tsx
593
+ * ```js
585
594
  * const store = createStore().setTable('pets', {
586
595
  * fido: {species: 'dog', color: 'brown'},
587
596
  * felix: {species: 'cat', color: 'black'},
@@ -633,7 +642,7 @@ export interface Indexes {
633
642
  * This example creates a Store, a Indexes object, registers a listener, and
634
643
  * then removes it.
635
644
  *
636
- * ```tsx
645
+ * ```js
637
646
  * const store = createStore().setTable('pets', {
638
647
  * fido: {species: 'dog'},
639
648
  * felix: {species: 'cat'},
@@ -675,7 +684,7 @@ export interface Indexes {
675
684
  * definition (that registers a RowListener with the underlying Store),
676
685
  * and then destroys it again, removing the listener.
677
686
  *
678
- * ```tsx
687
+ * ```js
679
688
  * const store = createStore().setTable('pets', {
680
689
  * fido: {species: 'dog'},
681
690
  * felix: {species: 'cat'},
@@ -714,7 +723,7 @@ export interface Indexes {
714
723
  * @example
715
724
  * This example gets the listener statistics of an Indexes object.
716
725
  *
717
- * ```tsx
726
+ * ```js
718
727
  * const store = createStore();
719
728
  * const indexes = createIndexes(store);
720
729
  * indexes.addSliceIdsListener(null, () => {
@@ -747,7 +756,7 @@ export interface Indexes {
747
756
  * @example
748
757
  * This example creates an Indexes object.
749
758
  *
750
- * ```tsx
759
+ * ```js
751
760
  * const store = createStore();
752
761
  * const indexes = createIndexes(store);
753
762
  * console.log(indexes.getIndexIds());
@@ -757,59 +766,13 @@ export interface Indexes {
757
766
  * This example creates an Indexes object, and calls the method a second time
758
767
  * for the same Store to return the same object.
759
768
  *
760
- * ```tsx
769
+ * ```js
761
770
  * const store = createStore();
762
771
  * const indexes1 = createIndexes(store);
763
772
  * const indexes2 = createIndexes(store);
764
773
  * console.log(indexes1 === indexes2);
765
774
  * // -> true
766
775
  * ```
776
+ * @category Creation
767
777
  */
768
778
  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,11 @@ export type MetricsListenerStats = {
203
226
  * metrics.delListener(listenerId);
204
227
  * metrics.destroy();
205
228
  * ```
229
+ * @see Metrics And Indexes guides
230
+ * @see Rolling Dice demos
231
+ * @see Country demo
232
+ * @see Todo App demos
233
+ * @category Metrics
206
234
  */
207
235
  export interface Metrics {
208
236
  /**
@@ -258,7 +286,7 @@ export interface Metrics {
258
286
  * This example creates a Store, creates a Metrics object, and defines a
259
287
  * simple Metric to count the Row objects in the Table.
260
288
  *
261
- * ```tsx
289
+ * ```js
262
290
  * const store = createStore().setTable('species', {
263
291
  * dog: {price: 5},
264
292
  * cat: {price: 4},
@@ -276,7 +304,7 @@ export interface Metrics {
276
304
  * standard Metric to get the highest value of each `price` Cell in the Row
277
305
  * objects in the Table.
278
306
  *
279
- * ```tsx
307
+ * ```js
280
308
  * const store = createStore().setTable('species', {
281
309
  * dog: {price: 5},
282
310
  * cat: {price: 4},
@@ -293,7 +321,7 @@ export interface Metrics {
293
321
  * This example creates a Store, creates a Metrics object, and defines a
294
322
  * custom Metric to get the lowest value of each `price` Cell, greater than 2.
295
323
  *
296
- * ```tsx
324
+ * ```js
297
325
  * const store = createStore().setTable('species', {
298
326
  * dog: {price: 5},
299
327
  * cat: {price: 4},
@@ -317,7 +345,7 @@ export interface Metrics {
317
345
  * However, it also reduces algorithmic complexity with two shortcut
318
346
  * functions.
319
347
  *
320
- * ```tsx
348
+ * ```js
321
349
  * const store = createStore().setTable('species', {
322
350
  * dog: {price: 5},
323
351
  * cat: {price: 4},
@@ -350,7 +378,7 @@ export interface Metrics {
350
378
  * This example creates a Store, creates a Metrics object, and defines a
351
379
  * custom Metric to get the average value of a discounted price.
352
380
  *
353
- * ```tsx
381
+ * ```js
354
382
  * const store = createStore().setTable('species', {
355
383
  * dog: {price: 5, discount: 0.3},
356
384
  * cat: {price: 4, discount: 0.2},
@@ -389,7 +417,7 @@ export interface Metrics {
389
417
  * This example creates a Store, creates a Metrics object, defines a simple
390
418
  * Metric, and then removes it.
391
419
  *
392
- * ```tsx
420
+ * ```js
393
421
  * const store = createStore().setTable('species', {
394
422
  * dog: {price: 5},
395
423
  * cat: {price: 4},
@@ -418,7 +446,7 @@ export interface Metrics {
418
446
  * This example creates a Metrics object against a newly-created Store and
419
447
  * then gets its reference in order to update its data.
420
448
  *
421
- * ```tsx
449
+ * ```js
422
450
  * const metrics = createMetrics(createStore());
423
451
  * metrics.setMetricDefinition('speciesCount', 'species');
424
452
  * metrics.getStore().setCell('species', 'dog', 'price', 5);
@@ -438,7 +466,7 @@ export interface Metrics {
438
466
  * This example creates a Metrics object with two definitions, and then gets
439
467
  * the Ids of the definitions.
440
468
  *
441
- * ```tsx
469
+ * ```js
442
470
  * const metrics = createMetrics(createStore())
443
471
  * .setMetricDefinition('speciesCount', 'species')
444
472
  * .setMetricDefinition('petsCount', 'pets');
@@ -462,7 +490,7 @@ export interface Metrics {
462
490
  * This example creates a Metrics object, a single Metric definition, and then
463
491
  * queries it (and a non-existent definition) to get the underlying Table Id.
464
492
  *
465
- * ```tsx
493
+ * ```js
466
494
  * const metrics = createMetrics(createStore());
467
495
  * metrics.setMetricDefinition('speciesCount', 'species');
468
496
  *
@@ -489,7 +517,7 @@ export interface Metrics {
489
517
  * getMetric to access its value (and also the value of a Metric that has not
490
518
  * been defined).
491
519
  *
492
- * ```tsx
520
+ * ```js
493
521
  * const store = createStore().setTable('species', {
494
522
  * dog: {price: 5},
495
523
  * cat: {price: 4},
@@ -529,7 +557,7 @@ export interface Metrics {
529
557
  * This example creates a Store, a Metrics object, and then registers a
530
558
  * listener that responds to any changes to a specific Metric.
531
559
  *
532
- * ```tsx
560
+ * ```js
533
561
  * const store = createStore().setTable('species', {
534
562
  * dog: {price: 5},
535
563
  * cat: {price: 4},
@@ -557,7 +585,7 @@ export interface Metrics {
557
585
  * This example creates a Store, a Metrics object, and then registers a
558
586
  * listener that responds to any changes to any Metric.
559
587
  *
560
- * ```tsx
588
+ * ```js
561
589
  * const store = createStore().setTable('species', {
562
590
  * dog: {price: 5},
563
591
  * cat: {price: 4},
@@ -601,7 +629,7 @@ export interface Metrics {
601
629
  * This example creates a Store, a Metrics object, registers a listener, and
602
630
  * then removes it.
603
631
  *
604
- * ```tsx
632
+ * ```js
605
633
  * const store = createStore().setTable('species', {
606
634
  * dog: {price: 5},
607
635
  * cat: {price: 4},
@@ -643,7 +671,7 @@ export interface Metrics {
643
671
  * registers a RowListener with the underlying Store), and then destroys it
644
672
  * again, removing the listener.
645
673
  *
646
- * ```tsx
674
+ * ```js
647
675
  * const store = createStore().setTable('species', {
648
676
  * dog: {price: 5},
649
677
  * cat: {price: 4},
@@ -679,7 +707,7 @@ export interface Metrics {
679
707
  * @example
680
708
  * This example gets the listener statistics of a Metrics object.
681
709
  *
682
- * ```tsx
710
+ * ```js
683
711
  * const store = createStore();
684
712
  * const metrics = createMetrics(store);
685
713
  * metrics.addMetricListener(null, () => console.log('Metric changed'));
@@ -707,7 +735,7 @@ export interface Metrics {
707
735
  * @example
708
736
  * This example creates a Metrics object.
709
737
  *
710
- * ```tsx
738
+ * ```js
711
739
  * const store = createStore();
712
740
  * const metrics = createMetrics(store);
713
741
  * console.log(metrics.getMetricIds());
@@ -717,12 +745,13 @@ export interface Metrics {
717
745
  * This example creates a Metrics object, and calls the method a second time
718
746
  * for the same Store to return the same object.
719
747
  *
720
- * ```tsx
748
+ * ```js
721
749
  * const store = createStore();
722
750
  * const metrics1 = createMetrics(store);
723
751
  * const metrics2 = createMetrics(store);
724
752
  * console.log(metrics1 === metrics2);
725
753
  * // -> true
726
754
  * ```
755
+ * @category Creation
727
756
  */
728
757
  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