tinybase 0.9.2 → 1.0.1
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/LICENSE +1 -1
- package/lib/checkpoints.d.ts +38 -20
- package/lib/common.d.ts +56 -0
- package/lib/common.js +1 -0
- package/lib/common.js.gz +0 -0
- package/lib/debug/checkpoints.d.ts +38 -20
- package/lib/debug/common.d.ts +56 -0
- package/lib/debug/common.js +3 -0
- package/lib/debug/indexes.d.ts +39 -76
- package/lib/debug/indexes.js +17 -9
- package/lib/debug/metrics.d.ts +49 -20
- package/lib/debug/persisters.d.ts +207 -17
- package/lib/debug/relationships.d.ts +48 -23
- package/lib/debug/store.d.ts +184 -102
- package/lib/debug/tinybase.d.ts +1 -2
- package/lib/debug/tinybase.js +16 -8
- package/lib/debug/ui-react.d.ts +271 -163
- package/lib/debug/ui-react.js +24 -15
- package/lib/indexes.d.ts +39 -76
- package/lib/indexes.js +1 -1
- package/lib/indexes.js.gz +0 -0
- package/lib/metrics.d.ts +49 -20
- package/lib/persisters.d.ts +207 -17
- package/lib/relationships.d.ts +48 -23
- package/lib/store.d.ts +184 -102
- package/lib/tinybase.d.ts +1 -2
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/ui-react.d.ts +271 -163
- package/lib/ui-react.js +1 -1
- package/lib/ui-react.js.gz +0 -0
- package/lib/umd/common.js +1 -0
- package/lib/umd/common.js.gz +0 -0
- package/lib/umd/indexes.js +1 -1
- package/lib/umd/indexes.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -1
- package/lib/umd/tinybase.js.gz +0 -0
- package/lib/umd/ui-react.js +1 -1
- package/lib/umd/ui-react.js.gz +0 -0
- package/package.json +24 -15
- package/readme.md +13 -13
package/lib/debug/ui-react.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
7
|
-
* definitions, access the contents of those
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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),
|
|
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
|
|
7
|
-
* definitions, access the values of those
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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;
|