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.
- package/lib/checkpoints.d.ts +35 -20
- package/lib/checkpoints.js +1 -1
- package/lib/checkpoints.js.gz +0 -0
- 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 +35 -20
- package/lib/debug/checkpoints.js +1 -1
- package/lib/debug/common.d.ts +56 -0
- package/lib/debug/common.js +3 -0
- package/lib/debug/indexes.d.ts +35 -76
- package/lib/debug/indexes.js +18 -10
- package/lib/debug/metrics.d.ts +45 -20
- package/lib/debug/metrics.js +1 -1
- package/lib/debug/persisters.d.ts +203 -17
- package/lib/debug/persisters.js +1 -1
- package/lib/debug/relationships.d.ts +46 -23
- package/lib/debug/relationships.js +1 -1
- package/lib/debug/store.d.ts +179 -102
- package/lib/debug/store.js +1 -1
- package/lib/debug/tinybase.d.ts +1 -2
- package/lib/debug/tinybase.js +18 -1029
- package/lib/debug/{react.d.ts → ui-react.d.ts} +262 -162
- package/lib/debug/{react.js → ui-react.js} +24 -15
- package/lib/indexes.d.ts +35 -76
- package/lib/indexes.js +1 -1
- package/lib/indexes.js.gz +0 -0
- package/lib/metrics.d.ts +45 -20
- package/lib/metrics.js +1 -1
- package/lib/metrics.js.gz +0 -0
- package/lib/persisters.d.ts +203 -17
- package/lib/persisters.js +1 -1
- package/lib/persisters.js.gz +0 -0
- package/lib/relationships.d.ts +46 -23
- package/lib/relationships.js +1 -1
- package/lib/relationships.js.gz +0 -0
- package/lib/store.d.ts +179 -102
- package/lib/store.js +1 -1
- package/lib/store.js.gz +0 -0
- package/lib/tinybase.d.ts +1 -2
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/{react.d.ts → ui-react.d.ts} +262 -162
- package/lib/ui-react.js +1 -0
- package/lib/ui-react.js.gz +0 -0
- package/lib/umd/checkpoints.js +1 -1
- package/lib/umd/checkpoints.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/metrics.js +1 -1
- package/lib/umd/metrics.js.gz +0 -0
- package/lib/umd/persisters.js +1 -1
- package/lib/umd/persisters.js.gz +0 -0
- package/lib/umd/relationships.js +1 -1
- package/lib/umd/relationships.js.gz +0 -0
- package/lib/umd/store.js +1 -1
- package/lib/umd/store.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 -0
- package/lib/umd/ui-react.js.gz +0 -0
- package/package.json +21 -16
- package/readme.md +13 -13
- package/lib/react.js +0 -1
- package/lib/react.js.gz +0 -0
- package/lib/umd/react.js +0 -1
- package/lib/umd/react.js.gz +0 -0
package/lib/debug/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,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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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/debug/indexes.js
CHANGED
|
@@ -168,6 +168,8 @@ const getCreateFunction = (getFunction) => {
|
|
|
168
168
|
};
|
|
169
169
|
};
|
|
170
170
|
|
|
171
|
+
const defaultSorter = (sortKey1, sortKey2) => (sortKey1 < sortKey2 ? -1 : 1);
|
|
172
|
+
|
|
171
173
|
const addDeepSet = (deepSet, value, ids) =>
|
|
172
174
|
arrayLength(ids) < 2
|
|
173
175
|
? setAdd(
|
|
@@ -196,7 +198,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
196
198
|
const listenerPool = [];
|
|
197
199
|
const allListeners = mapNew();
|
|
198
200
|
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
199
|
-
thing
|
|
201
|
+
thing ??= getThing();
|
|
200
202
|
const id = listenerPool.pop() ?? '' + nextId++;
|
|
201
203
|
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
202
204
|
addDeepSet(deepSet, id, idOrNulls);
|
|
@@ -276,7 +278,7 @@ const createIndexes = getCreateFunction((store) => {
|
|
|
276
278
|
setDefinition(
|
|
277
279
|
indexId,
|
|
278
280
|
tableId,
|
|
279
|
-
(change, changedSliceIds, changedSortKeys, sliceIds, sortKeys) => {
|
|
281
|
+
(change, changedSliceIds, changedSortKeys, sliceIds, sortKeys, force) => {
|
|
280
282
|
let sliceIdsChanged = 0;
|
|
281
283
|
const changedSlices = setNew();
|
|
282
284
|
const unsortedSlices = setNew();
|
|
@@ -305,12 +307,16 @@ const createIndexes = getCreateFunction((store) => {
|
|
|
305
307
|
}
|
|
306
308
|
});
|
|
307
309
|
change();
|
|
308
|
-
mapForEach(changedSortKeys, (rowId) =>
|
|
309
|
-
ifNotUndefined(mapGet(sliceIds, rowId), (sliceId) =>
|
|
310
|
-
setAdd(unsortedSlices, sliceId),
|
|
311
|
-
),
|
|
312
|
-
);
|
|
313
310
|
if (!collIsEmpty(sortKeys)) {
|
|
311
|
+
if (force) {
|
|
312
|
+
mapForEach(index, (sliceId) => setAdd(unsortedSlices, sliceId));
|
|
313
|
+
} else {
|
|
314
|
+
mapForEach(changedSortKeys, (rowId) =>
|
|
315
|
+
ifNotUndefined(mapGet(sliceIds, rowId), (sliceId) =>
|
|
316
|
+
setAdd(unsortedSlices, sliceId),
|
|
317
|
+
),
|
|
318
|
+
);
|
|
319
|
+
}
|
|
314
320
|
collForEach(unsortedSlices, (sliceId) => {
|
|
315
321
|
const rowIdArraySorter = (rowId1, rowId2) =>
|
|
316
322
|
rowIdSorter(
|
|
@@ -329,7 +335,7 @@ const createIndexes = getCreateFunction((store) => {
|
|
|
329
335
|
}
|
|
330
336
|
});
|
|
331
337
|
}
|
|
332
|
-
if (sliceIdsChanged) {
|
|
338
|
+
if (sliceIdsChanged || force) {
|
|
333
339
|
if (!isUndefined(sliceIdArraySorter)) {
|
|
334
340
|
const indexArray = [...index];
|
|
335
341
|
if (!arrayIsSorted(indexArray, sliceIdArraySorter)) {
|
|
@@ -337,8 +343,11 @@ const createIndexes = getCreateFunction((store) => {
|
|
|
337
343
|
indexId,
|
|
338
344
|
mapNew(arraySort(indexArray, sliceIdArraySorter)),
|
|
339
345
|
);
|
|
346
|
+
sliceIdsChanged = 1;
|
|
340
347
|
}
|
|
341
348
|
}
|
|
349
|
+
}
|
|
350
|
+
if (sliceIdsChanged) {
|
|
342
351
|
callListeners(sliceIdsListeners, [indexId]);
|
|
343
352
|
}
|
|
344
353
|
collForEach(changedSlices, (sliceId) =>
|
|
@@ -385,6 +394,5 @@ const createIndexes = getCreateFunction((store) => {
|
|
|
385
394
|
};
|
|
386
395
|
return objFreeze(indexes);
|
|
387
396
|
});
|
|
388
|
-
const defaultSorter = (sortKey1, sortKey2) => (sortKey1 < sortKey2 ? -1 : 1);
|
|
389
397
|
|
|
390
|
-
export {createIndexes
|
|
398
|
+
export {createIndexes};
|
package/lib/debug/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,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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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
|
-
* ```
|
|
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/debug/metrics.js
CHANGED
|
@@ -201,7 +201,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
201
201
|
const listenerPool = [];
|
|
202
202
|
const allListeners = mapNew();
|
|
203
203
|
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
204
|
-
thing
|
|
204
|
+
thing ??= getThing();
|
|
205
205
|
const id = listenerPool.pop() ?? '' + nextId++;
|
|
206
206
|
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
207
207
|
addDeepSet(deepSet, id, idOrNulls);
|