tinybase 0.0.0 → 0.9.3

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 (61) hide show
  1. package/LICENSE +21 -0
  2. package/lib/checkpoints.d.ts +876 -0
  3. package/lib/checkpoints.js +1 -0
  4. package/lib/checkpoints.js.gz +0 -0
  5. package/lib/common.d.ts +59 -0
  6. package/lib/debug/checkpoints.d.ts +876 -0
  7. package/lib/debug/checkpoints.js +326 -0
  8. package/lib/debug/common.d.ts +59 -0
  9. package/lib/debug/indexes.d.ts +829 -0
  10. package/lib/debug/indexes.js +390 -0
  11. package/lib/debug/metrics.d.ts +753 -0
  12. package/lib/debug/metrics.js +391 -0
  13. package/lib/debug/persisters.d.ts +704 -0
  14. package/lib/debug/persisters.js +191 -0
  15. package/lib/debug/relationships.d.ts +1114 -0
  16. package/lib/debug/relationships.js +418 -0
  17. package/lib/debug/store.d.ts +2503 -0
  18. package/lib/debug/store.js +725 -0
  19. package/lib/debug/tinybase.d.ts +13 -0
  20. package/lib/debug/tinybase.js +1708 -0
  21. package/lib/debug/ui-react.d.ts +7158 -0
  22. package/lib/debug/ui-react.js +1040 -0
  23. package/lib/indexes.d.ts +829 -0
  24. package/lib/indexes.js +1 -0
  25. package/lib/indexes.js.gz +0 -0
  26. package/lib/metrics.d.ts +753 -0
  27. package/lib/metrics.js +1 -0
  28. package/lib/metrics.js.gz +0 -0
  29. package/lib/persisters.d.ts +704 -0
  30. package/lib/persisters.js +1 -0
  31. package/lib/persisters.js.gz +0 -0
  32. package/lib/relationships.d.ts +1114 -0
  33. package/lib/relationships.js +1 -0
  34. package/lib/relationships.js.gz +0 -0
  35. package/lib/store.d.ts +2503 -0
  36. package/lib/store.js +1 -0
  37. package/lib/store.js.gz +0 -0
  38. package/lib/tinybase.d.ts +13 -0
  39. package/lib/tinybase.js +1 -0
  40. package/lib/tinybase.js.gz +0 -0
  41. package/lib/ui-react.d.ts +7158 -0
  42. package/lib/ui-react.js +1 -0
  43. package/lib/ui-react.js.gz +0 -0
  44. package/lib/umd/checkpoints.js +1 -0
  45. package/lib/umd/checkpoints.js.gz +0 -0
  46. package/lib/umd/indexes.js +1 -0
  47. package/lib/umd/indexes.js.gz +0 -0
  48. package/lib/umd/metrics.js +1 -0
  49. package/lib/umd/metrics.js.gz +0 -0
  50. package/lib/umd/persisters.js +1 -0
  51. package/lib/umd/persisters.js.gz +0 -0
  52. package/lib/umd/relationships.js +1 -0
  53. package/lib/umd/relationships.js.gz +0 -0
  54. package/lib/umd/store.js +1 -0
  55. package/lib/umd/store.js.gz +0 -0
  56. package/lib/umd/tinybase.js +1 -0
  57. package/lib/umd/tinybase.js.gz +0 -0
  58. package/lib/umd/ui-react.js +1 -0
  59. package/lib/umd/ui-react.js.gz +0 -0
  60. package/package.json +98 -2
  61. package/readme.md +195 -0
@@ -0,0 +1,829 @@
1
+ /**
2
+ * The indexes module of the TinyBase project provides the ability to create
3
+ * and track indexes of the data in Store objects.
4
+ *
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
8
+ * listeners for when they change.
9
+ *
10
+ * @packageDocumentation
11
+ * @module indexes
12
+ */
13
+
14
+ import {GetCell, Store} from './store.d';
15
+ import {Id, IdOrNull, Ids} from './common.d';
16
+
17
+ /**
18
+ * The Index type represents the concept of a map of Slice objects, keyed by Id.
19
+ *
20
+ * The Ids in a Slice represent Row objects from a Table that all have a derived
21
+ * string value in common, as described by the setIndexDefinition method. Those
22
+ * values are used as the key for each Slice in the overall Index object.
23
+ *
24
+ * Note that the Index type is not actually used in the API, and you instead
25
+ * enumerate and access its structure with the getSliceIds method and
26
+ * getSliceRowIds method.
27
+ *
28
+ * @category Concept
29
+ */
30
+ export type Index = {[sliceId: Id]: Slice};
31
+
32
+ /**
33
+ * The Slice type represents the concept of a set of Row objects that comprise
34
+ * part of an Index.
35
+ *
36
+ * The Ids in a Slice represent Row objects from a Table that all have a derived
37
+ * string value in common, as described by the setIndexDefinition method.
38
+ *
39
+ * Note that the Slice type is not actually used in the API, and you instead get
40
+ * Row Ids directly with the getSliceRowIds method.
41
+ *
42
+ * @category Concept
43
+ */
44
+ export type Slice = Ids;
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
+ /**
54
+ * The SliceIdsListener type describes a function that is used to listen to
55
+ * changes to the Slice Ids in an Index.
56
+ *
57
+ * A SliceIdsListener is provided when using the addSliceIdsListener method. See
58
+ * that method for specific examples.
59
+ *
60
+ * When called, a SliceIdsListener is given a reference to the Indexes object,
61
+ * and the Id of the Index that changed.
62
+ *
63
+ * @param indexes A reference to the Indexes object that changed.
64
+ * @param indexId The Id of the Index that changed.
65
+ * @category Listener
66
+ */
67
+ export type SliceIdsListener = (indexes: Indexes, indexId: Id) => void;
68
+
69
+ /**
70
+ * The SliceRowIdsListener type describes a function that is used to listen to
71
+ * changes to the Row Ids in a Slice.
72
+ *
73
+ * A SliceRowIdsListener is provided when using the addSliceRowIdsListener
74
+ * method. See that method for specific examples.
75
+ *
76
+ * When called, a SliceRowIdsListener is given a reference to the Indexes
77
+ * object, the Id of the Index that changed, and the Id of the Slice whose Row
78
+ * Ids changed.
79
+ *
80
+ * @param indexes A reference to the Indexes object that changed.
81
+ * @param indexId The Id of the Index that changed.
82
+ * @param sliceId The Id of the Slice that changed.
83
+ * @category Listener
84
+ */
85
+ export type SliceRowIdsListener = (
86
+ indexes: Indexes,
87
+ indexId: Id,
88
+ sliceId: Id,
89
+ ) => void;
90
+
91
+ /**
92
+ * The IndexesListenerStats type describes the number of listeners registered
93
+ * with the Indexes object, and can be used for debugging purposes.
94
+ *
95
+ * A IndexesListenerStats object is returned from the getListenerStats method,
96
+ * and is only populated in a debug build.
97
+ *
98
+ * @category Development
99
+ */
100
+ export type IndexesListenerStats = {
101
+ /**
102
+ * The number of SlideIdsListeners registered with the Indexes object.
103
+ */
104
+ sliceIds?: number;
105
+ /**
106
+ * The number of SliceRowIdsListeners registered with the Indexes object.
107
+ */
108
+ sliceRowIds?: number;
109
+ };
110
+
111
+ /**
112
+ * An Indexes object lets you look up all the Row objects in a Table that have a
113
+ * certain Cell value.
114
+ *
115
+ * This is useful for creating filtered views of a Table, or simple search
116
+ * functionality.
117
+ *
118
+ * Create an Indexes object easily with the createIndexes function. From there,
119
+ * you can add new Index definitions (with the setIndexDefinition method), query
120
+ * their contents (with the getSliceIds method and getSliceRowIds method), and
121
+ * add listeners for when they change (with the addSliceIdsListener method and
122
+ * addSliceRowIdsListener method).
123
+ *
124
+ * This module defaults to indexing Row objects by one of their Cell values.
125
+ * However, far more complex indexes can be configured with a custom function.
126
+ *
127
+ * @example
128
+ * This example shows a very simple lifecycle of an Indexes object: from
129
+ * creation, to adding a definition, getting its contents, and then registering
130
+ * and removing a listener for it.
131
+ *
132
+ * ```js
133
+ * const store = createStore().setTable('pets', {
134
+ * fido: {species: 'dog'},
135
+ * felix: {species: 'cat'},
136
+ * cujo: {species: 'dog'},
137
+ * });
138
+ *
139
+ * const indexes = createIndexes(store);
140
+ * indexes.setIndexDefinition(
141
+ * 'bySpecies', // indexId
142
+ * 'pets', // tableId to index
143
+ * 'species', // cellId to index
144
+ * );
145
+ *
146
+ * console.log(indexes.getSliceIds('bySpecies'));
147
+ * // -> ['dog', 'cat']
148
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
149
+ * // -> ['fido', 'cujo']
150
+ *
151
+ * const listenerId = indexes.addSliceIdsListener('bySpecies', () => {
152
+ * console.log(indexes.getSliceIds('bySpecies'));
153
+ * });
154
+ * store.setRow('pets', 'lowly', {species: 'worm'});
155
+ * // -> ['dog', 'cat', 'worm']
156
+ *
157
+ * indexes.delListener(listenerId);
158
+ * indexes.destroy();
159
+ * ```
160
+ * @category Indexes
161
+ */
162
+ export interface Indexes {
163
+ /**
164
+ * The setIndexDefinition method lets you set the definition of an Index.
165
+ *
166
+ * Every Index definition is identified by a unique Id, and if you re-use an
167
+ * existing Id with this method, the previous definition is overwritten.
168
+ *
169
+ * An Index is a keyed map of Slice objects, each of which is a list of Row
170
+ * Ids from a given Table. Therefore the definition must specify the Table (by
171
+ * its Id) to be indexed.
172
+ *
173
+ * The Ids in a Slice represent Row objects from a Table that all have a
174
+ * derived string value in common, as described by this method. Those values
175
+ * are used as the key for each Slice in the overall Index object.
176
+ *
177
+ * Without the third `getSliceId` parameter, the Index will simply have a
178
+ * single Slice, keyed by an empty string. But more often you will specify a
179
+ * Cell value containing the Slice Id that the Row should belong to.
180
+ * Alternatively, a custom function can be provided that produces your own
181
+ * Slice Id from the local Row as a whole.
182
+ *
183
+ * The fourth `getSortKey` parameter specifies a Cell Id to get a value (or a
184
+ * function that processes a whole Row to get a value) that is used to sort
185
+ * the Row Ids within each Slice in the Index.
186
+ *
187
+ * The fifth parameter, `sliceIdSorter`, lets you specify a way to sort the
188
+ * Slice Ids when you access the Index, which may be useful if you are trying
189
+ * to create an alphabetic Index of Row entries. If not specified, the order
190
+ * of the Slice Ids will match the order of Row insertion.
191
+ *
192
+ * The final parameter, `rowIdSorter`, lets you specify a way to sort the Row
193
+ * Ids within each Slice, based on the `getSortKey` parameter. This may be
194
+ * useful if you are trying to keep Rows in a determined order relative to
195
+ * each other in the Index. If omitted, the Row Ids are sorted alphabetically,
196
+ * based on the `getSortKey` parameter.
197
+ *
198
+ * The two 'sorter' parameters, `sliceIdSorter` and `rowIdSorter`, are
199
+ * functions that take two values and return a positive or negative number for
200
+ * when they are in the wrong or right order, respectively. This is exactly
201
+ * the same as the 'compareFunction' that is used in the standard JavaScript
202
+ * array `sort` method, with the addition that `rowIdSorter` also takes the
203
+ * Slice Id parameter, in case you want to sort Row Ids differently in each
204
+ * Slice. You can use the convenient defaultSorter function to default this to
205
+ * be alphanumeric.
206
+ *
207
+ * @param indexId The Id of the Index to define.
208
+ * @param tableId The Id of the Table the Index will be generated from.
209
+ * @param getSliceId Either the Id of the Cell containing, or a function that
210
+ * produces, the Id that is used to indicate which Slice in the Index the Row
211
+ * Id should be in. Defaults to a function that returns `''` (meaning that if
212
+ * this `getSliceId` parameter is omitted, the Index will simply contain a
213
+ * single Slice containing all the Row Ids in the Table).
214
+ * @param getSortKey Either the Id of the Cell containing, or a function that
215
+ * produces, the value that is used to sort the Row Ids in each Slice.
216
+ * @param sliceIdSorter A function that takes two Slice Id values and returns
217
+ * a positive or negative number to indicate how they should be sorted.
218
+ * @param rowIdSorter A function that takes two Row Id values (and a slice Id)
219
+ * and returns a positive or negative number to indicate how they should be
220
+ * sorted.
221
+ * @returns A reference to the Indexes object.
222
+ * @example
223
+ * This example creates a Store, creates an Indexes object, and defines a
224
+ * simple Index based on the values in the `species` Cell.
225
+ *
226
+ * ```js
227
+ * const store = createStore().setTable('pets', {
228
+ * fido: {species: 'dog'},
229
+ * felix: {species: 'cat'},
230
+ * cujo: {species: 'dog'},
231
+ * });
232
+ *
233
+ * const indexes = createIndexes(store);
234
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
235
+ *
236
+ * console.log(indexes.getSliceIds('bySpecies'));
237
+ * // -> ['dog', 'cat']
238
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
239
+ * // -> ['fido', 'cujo']
240
+ * ```
241
+ * @example
242
+ * This example creates a Store, creates an Indexes object, and defines an
243
+ * Index based on the first letter of the pets' names.
244
+ *
245
+ * ```js
246
+ * const store = createStore().setTable('pets', {
247
+ * fido: {species: 'dog'},
248
+ * felix: {species: 'cat'},
249
+ * cujo: {species: 'dog'},
250
+ * });
251
+ *
252
+ * const indexes = createIndexes(store);
253
+ * indexes.setIndexDefinition('byFirst', 'pets', (_, rowId) => rowId[0]);
254
+ *
255
+ * console.log(indexes.getSliceIds('byFirst'));
256
+ * // -> ['f', 'c']
257
+ * console.log(indexes.getSliceRowIds('byFirst', 'f'));
258
+ * // -> ['fido', 'felix']
259
+ * ```
260
+ * @example
261
+ * This example creates a Store, creates an Indexes object, and defines an
262
+ * Index based on the first letter of the pets' names. The Slice Ids (and Row
263
+ * Ids within them) are alphabetically sorted.
264
+ *
265
+ * ```js
266
+ * const store = createStore().setTable('pets', {
267
+ * fido: {species: 'dog'},
268
+ * felix: {species: 'cat'},
269
+ * cujo: {species: 'dog'},
270
+ * });
271
+ *
272
+ * const indexes = createIndexes(store);
273
+ * indexes.setIndexDefinition(
274
+ * 'byFirst', // indexId
275
+ * 'pets', // tableId
276
+ * (_, rowId) => rowId[0], // each Row's sliceId
277
+ * (_, rowId) => rowId, // each Row's sort key
278
+ * defaultSorter, // sort Slice Ids
279
+ * defaultSorter, // sort Row Ids
280
+ * );
281
+ *
282
+ * console.log(indexes.getSliceIds('byFirst'));
283
+ * // -> ['c', 'f']
284
+ * console.log(indexes.getSliceRowIds('byFirst', 'f'));
285
+ * // -> ['felix', 'fido']
286
+ * ```
287
+ * @category Configuration
288
+ */
289
+ setIndexDefinition(
290
+ indexId: Id,
291
+ tableId: Id,
292
+ getSliceId?: Id | ((getCell: GetCell, rowId: Id) => Id),
293
+ getSortKey?: Id | ((getCell: GetCell, rowId: Id) => SortKey),
294
+ sliceIdSorter?: (sliceId1: Id, sliceId2: Id) => number,
295
+ rowIdSorter?: (sortKey1: SortKey, sortKey2: SortKey, sliceId: Id) => number,
296
+ ): Indexes;
297
+
298
+ /**
299
+ * The delIndexDefinition method removes an existing Index definition.
300
+ *
301
+ * @param indexId The Id of the Index to remove.
302
+ * @returns A reference to the Indexes object.
303
+ * @example
304
+ * This example creates a Store, creates an Indexes object, defines a simple
305
+ * Index, and then removes it.
306
+ *
307
+ * ```js
308
+ * const store = createStore().setTable('pets', {
309
+ * fido: {species: 'dog'},
310
+ * felix: {species: 'cat'},
311
+ * cujo: {species: 'dog'},
312
+ * });
313
+ *
314
+ * const indexes = createIndexes(store);
315
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
316
+ * console.log(indexes.getIndexIds());
317
+ * // -> ['bySpecies']
318
+ *
319
+ * indexes.delIndexDefinition('bySpecies');
320
+ * console.log(indexes.getIndexIds());
321
+ * // -> []
322
+ * ```
323
+ * @category Configuration
324
+ */
325
+ delIndexDefinition(indexId: Id): Indexes;
326
+
327
+ /**
328
+ * The getStore method returns a reference to the underlying Store that is
329
+ * backing this Indexes object.
330
+ *
331
+ * @returns A reference to the Store.
332
+ * @example
333
+ * This example creates an Indexes object against a newly-created Store and
334
+ * then gets its reference in order to update its data.
335
+ *
336
+ * ```js
337
+ * const indexes = createIndexes(createStore());
338
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
339
+ * indexes.getStore().setCell('pets', 'fido', 'species', 'dog');
340
+ * console.log(indexes.getSliceIds('bySpecies'));
341
+ * // -> ['dog']
342
+ * ```
343
+ * @category Getter
344
+ */
345
+ getStore(): Store;
346
+
347
+ /**
348
+ * The getIndexIds method returns an array of the Index Ids registered with
349
+ * this Indexes object.
350
+ *
351
+ * @returns An array of Ids.
352
+ * @example
353
+ * This example creates an Indexes object with two definitions, and then gets
354
+ * the Ids of the definitions.
355
+ *
356
+ * ```js
357
+ * const indexes = createIndexes(createStore())
358
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
359
+ * .setIndexDefinition('byColor', 'pets', 'color');
360
+ *
361
+ * console.log(indexes.getIndexIds());
362
+ * // -> ['bySpecies', 'byColor']
363
+ * ```
364
+ * @category Getter
365
+ */
366
+ getIndexIds(): Ids;
367
+
368
+ /**
369
+ * The getTableId method returns the Id of the underlying Table that is
370
+ * backing an Index.
371
+ *
372
+ * If the Index Id is invalid, the method returns `undefined`.
373
+ *
374
+ * @param indexId The Id of an Index.
375
+ * @returns The Id of the Table backing the Index, or `undefined`.
376
+ * @example
377
+ * This example creates an Indexes object, a single Index definition, and then
378
+ * queries it (and a non-existent definition) to get the underlying Table Id.
379
+ *
380
+ * ```js
381
+ * const indexes = createIndexes(createStore());
382
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
383
+ *
384
+ * console.log(indexes.getTableId('bySpecies'));
385
+ * // -> 'pets'
386
+ * console.log(indexes.getTableId('byColor'));
387
+ * // -> undefined
388
+ * ```
389
+ * @category Getter
390
+ */
391
+ getTableId(indexId: Id): Id;
392
+
393
+ /**
394
+ * The getSliceIds method gets the list of Slice Ids in an Index.
395
+ *
396
+ * If the identified Index does not exist (or if the definition references a
397
+ * Table that does not exist) then an empty array is returned.
398
+ *
399
+ * @param indexId The Id of the Index.
400
+ * @returns The Slice Ids in the Index, or an empty array.
401
+ * @example
402
+ * This example creates a Store, creates an Indexes object, and defines a
403
+ * simple Index. It then uses getSliceIds to see the available Slice Ids in
404
+ * the Index (and also the Slice Ids in an Index that has not been defined).
405
+ *
406
+ * ```js
407
+ * const store = createStore().setTable('pets', {
408
+ * fido: {species: 'dog'},
409
+ * felix: {species: 'cat'},
410
+ * cujo: {species: 'dog'},
411
+ * });
412
+ *
413
+ * const indexes = createIndexes(store);
414
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
415
+ *
416
+ * console.log(indexes.getSliceIds('bySpecies'));
417
+ * // -> ['dog', 'cat']
418
+ * console.log(indexes.getSliceIds('byColor'));
419
+ * // -> []
420
+ * ```
421
+ * @category Getter
422
+ */
423
+ getSliceIds(indexId: Id): Ids;
424
+
425
+ /**
426
+ * The getSliceRowIds method gets the list of Row Ids in a given Slice, within
427
+ * a given Index.
428
+ *
429
+ * If the identified Index or Slice do not exist (or if the definition
430
+ * references a Table that does not exist) then an empty array is returned.
431
+ *
432
+ * @param indexId The Id of the Index.
433
+ * @param sliceId The Id of the Slice in the Index.
434
+ * @returns The Row Ids in the Slice, or an empty array.
435
+ * @example
436
+ * This example creates a Store, creates an Indexes object, and defines a
437
+ * simple Index. It then uses getSliceRowIds to see the Row Ids in the Slice
438
+ * (and also the Row Ids in Slices that do not exist).
439
+ *
440
+ * ```js
441
+ * const store = createStore().setTable('pets', {
442
+ * fido: {species: 'dog'},
443
+ * felix: {species: 'cat'},
444
+ * cujo: {species: 'dog'},
445
+ * });
446
+ *
447
+ * const indexes = createIndexes(store);
448
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
449
+ *
450
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
451
+ * // -> ['fido', 'cujo']
452
+ * console.log(indexes.getSliceRowIds('bySpecies', 'worm'));
453
+ * // -> []
454
+ * console.log(indexes.getSliceRowIds('byColor', 'brown'));
455
+ * // -> []
456
+ * ```
457
+ * @category Getter
458
+ */
459
+ getSliceRowIds(indexId: Id, sliceId: Id): Ids;
460
+
461
+ /**
462
+ * The addSliceIdsListener method registers a listener function with the
463
+ * Indexes object that will be called whenever the Slice Ids in an Index
464
+ * change.
465
+ *
466
+ * You can either listen to a single Index (by specifying the Index Id as the
467
+ * method's first parameter), or changes to any Index (by providing a `null`
468
+ * wildcard).
469
+ *
470
+ * The provided listener is a SliceIdsListener function, and will be called
471
+ * with a reference to the Indexes object, and the Id of the Index that
472
+ * changed.
473
+ *
474
+ * @param indexId The Id of the Index to listen to, or `null` as a wildcard.
475
+ * @param listener The function that will be called whenever the Slice Ids in
476
+ * the Index change.
477
+ * @returns A unique Id for the listener that can later be used to remove it.
478
+ * @example
479
+ * This example creates a Store, a Indexes object, and then registers a
480
+ * listener that responds to any changes to a specific Index.
481
+ *
482
+ * ```js
483
+ * const store = createStore().setTable('pets', {
484
+ * fido: {species: 'dog'},
485
+ * felix: {species: 'cat'},
486
+ * cujo: {species: 'dog'},
487
+ * });
488
+ *
489
+ * const indexes = createIndexes(store);
490
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
491
+ *
492
+ * const listenerId = indexes.addSliceIdsListener(
493
+ * 'bySpecies',
494
+ * (indexes, indexId) => {
495
+ * console.log('Slice Ids for bySpecies index changed');
496
+ * console.log(indexes.getSliceIds('bySpecies'));
497
+ * },
498
+ * );
499
+ *
500
+ * store.setRow('pets', 'lowly', {species: 'worm'});
501
+ * // -> 'Slice Ids for bySpecies index changed'
502
+ * // -> ['dog', 'cat', 'worm']
503
+ *
504
+ * indexes.delListener(listenerId);
505
+ * ```
506
+ * @example
507
+ * This example creates a Store, a Indexes object, and then registers a
508
+ * listener that responds to any changes to any Index.
509
+ *
510
+ * ```js
511
+ * const store = createStore().setTable('pets', {
512
+ * fido: {species: 'dog', color: 'brown'},
513
+ * felix: {species: 'cat', color: 'black'},
514
+ * cujo: {species: 'dog', color: 'brown'},
515
+ * });
516
+ *
517
+ * const indexes = createIndexes(store)
518
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
519
+ * .setIndexDefinition('byColor', 'pets', 'color');
520
+ *
521
+ * const listenerId = indexes.addSliceIdsListener(
522
+ * null,
523
+ * (indexes, indexId) => {
524
+ * console.log(`Slice Ids for ${indexId} index changed`);
525
+ * console.log(indexes.getSliceIds(indexId));
526
+ * },
527
+ * );
528
+ *
529
+ * store.setRow('pets', 'lowly', {species: 'worm', color: 'pink'});
530
+ * // -> 'Slice Ids for bySpecies index changed'
531
+ * // -> ['dog', 'cat', 'worm']
532
+ * // -> 'Slice Ids for byColor index changed'
533
+ * // -> ['brown', 'black', 'pink']
534
+ *
535
+ * indexes.delListener(listenerId);
536
+ * ```
537
+ * @category Listener
538
+ */
539
+ addSliceIdsListener(indexId: IdOrNull, listener: SliceIdsListener): Id;
540
+
541
+ /**
542
+ * The addSliceRowIdsListener method registers a listener function with the
543
+ * Indexes object that will be called whenever the Row Ids in a Slice change.
544
+ *
545
+ * You can either listen to a single Slice (by specifying the Index Id and
546
+ * Slice Id as the method's first two parameters), or changes to any Slice (by
547
+ * providing `null` wildcards).
548
+ *
549
+ * Both, either, or neither of the `indexId` and `sliceId` parameters can be
550
+ * wildcarded with `null`. You can listen to a specific Slice in a specific
551
+ * Index, any Slice in a specific Index, a specific Slice in any Index, or any
552
+ * Slice in any Index.
553
+ *
554
+ * The provided listener is a SliceRowIdsListener function, and will be called
555
+ * with a reference to the Indexes object, the Id of the Index, and the Id of
556
+ * the Slice that changed.
557
+ *
558
+ * @param indexId The Id of the Index to listen to, or `null` as a wildcard.
559
+ * @param sliceId The Id of the Slice to listen to, or `null` as a wildcard.
560
+ * @param listener The function that will be called whenever the Row Ids in
561
+ * the Slice change.
562
+ * @returns A unique Id for the listener that can later be used to remove it.
563
+ * @example
564
+ * This example creates a Store, a Indexes object, and then registers a
565
+ * listener that responds to any changes to a specific Slice.
566
+ *
567
+ * ```js
568
+ * const store = createStore().setTable('pets', {
569
+ * fido: {species: 'dog'},
570
+ * felix: {species: 'cat'},
571
+ * cujo: {species: 'dog'},
572
+ * });
573
+ *
574
+ * const indexes = createIndexes(store);
575
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
576
+ *
577
+ * const listenerId = indexes.addSliceRowIdsListener(
578
+ * 'bySpecies',
579
+ * 'dog',
580
+ * (indexes, indexId, sliceId) => {
581
+ * console.log('Row Ids for dog slice in bySpecies index changed');
582
+ * console.log(indexes.getSliceRowIds('bySpecies', 'dog'));
583
+ * },
584
+ * );
585
+ *
586
+ * store.setRow('pets', 'toto', {species: 'dog'});
587
+ * // -> 'Row Ids for dog slice in bySpecies index changed'
588
+ * // -> ['fido', 'cujo', 'toto']
589
+ *
590
+ * indexes.delListener(listenerId);
591
+ * ```
592
+ * @example
593
+ * This example creates a Store, a Indexes object, and then registers a
594
+ * listener that responds to any changes to any Slice.
595
+ *
596
+ * ```js
597
+ * const store = createStore().setTable('pets', {
598
+ * fido: {species: 'dog', color: 'brown'},
599
+ * felix: {species: 'cat', color: 'black'},
600
+ * cujo: {species: 'dog', color: 'black'},
601
+ * });
602
+ *
603
+ * const indexes = createIndexes(store)
604
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
605
+ * .setIndexDefinition('byColor', 'pets', 'color');
606
+ *
607
+ * const listenerId = indexes.addSliceRowIdsListener(
608
+ * null,
609
+ * null,
610
+ * (indexes, indexId, sliceId) => {
611
+ * console.log(
612
+ * `Row Ids for ${sliceId} slice in ${indexId} index changed`,
613
+ * );
614
+ * console.log(indexes.getSliceRowIds(indexId, sliceId));
615
+ * },
616
+ * );
617
+ *
618
+ * store.setRow('pets', 'toto', {species: 'dog', color: 'brown'});
619
+ * // -> 'Row Ids for dog slice in bySpecies index changed'
620
+ * // -> ['fido', 'cujo', 'toto']
621
+ * // -> 'Row Ids for brown slice in byColor index changed'
622
+ * // -> ['fido', 'toto']
623
+ *
624
+ * indexes.delListener(listenerId);
625
+ * ```
626
+ * @category Listener
627
+ */
628
+ addSliceRowIdsListener(
629
+ indexId: IdOrNull,
630
+ sliceId: IdOrNull,
631
+ listener: SliceRowIdsListener,
632
+ ): Id;
633
+
634
+ /**
635
+ * The delListener method removes a listener that was previously added to the
636
+ * Indexes object.
637
+ *
638
+ * Use the Id returned by whichever method was used to add the listener. Note
639
+ * that the Indexes object may re-use this Id for future listeners added to
640
+ * it.
641
+ *
642
+ * @param listenerId The Id of the listener to remove.
643
+ * @returns A reference to the Indexes object.
644
+ * @example
645
+ * This example creates a Store, a Indexes object, registers a listener, and
646
+ * then removes it.
647
+ *
648
+ * ```js
649
+ * const store = createStore().setTable('pets', {
650
+ * fido: {species: 'dog'},
651
+ * felix: {species: 'cat'},
652
+ * cujo: {species: 'dog'},
653
+ * });
654
+ *
655
+ * const indexes = createIndexes(store);
656
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
657
+ *
658
+ * const listenerId = indexes.addSliceIdsListener(
659
+ * 'bySpecies',
660
+ * (indexes, indexId) => {
661
+ * console.log('Slice Ids for bySpecies index changed');
662
+ * },
663
+ * );
664
+ *
665
+ * store.setRow('pets', 'lowly', {species: 'worm'});
666
+ * // -> 'Slice Ids for bySpecies index changed'
667
+ *
668
+ * indexes.delListener(listenerId);
669
+ *
670
+ * store.setRow('pets', 'toto', {species: 'dog'});
671
+ * // -> undefined
672
+ * // The listener is not called.
673
+ * ```
674
+ * @category Listener
675
+ */
676
+ delListener(listenerId: Id): Indexes;
677
+
678
+ /**
679
+ * The destroy method should be called when this Indexes object is no longer
680
+ * used.
681
+ *
682
+ * This guarantees that all of the listeners that the object registered with
683
+ * the underlying Store are removed and it can be correctly garbage collected.
684
+ *
685
+ * @example
686
+ * This example creates a Store, adds an Indexes object with a
687
+ * definition (that registers a RowListener with the underlying Store),
688
+ * and then destroys it again, removing the listener.
689
+ *
690
+ * ```js
691
+ * const store = createStore().setTable('pets', {
692
+ * fido: {species: 'dog'},
693
+ * felix: {species: 'cat'},
694
+ * cujo: {species: 'dog'},
695
+ * });
696
+ *
697
+ * const indexes = createIndexes(store);
698
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
699
+ * console.log(store.getListenerStats().row);
700
+ * // -> 1
701
+ *
702
+ * indexes.destroy();
703
+ *
704
+ * console.log(store.getListenerStats().row);
705
+ * // -> 0
706
+ * ```
707
+ * @category Lifecycle
708
+ */
709
+ destroy(): void;
710
+
711
+ /**
712
+ * The getListenerStats method provides a set of statistics about the
713
+ * listeners registered with the Indexes object, and is used for debugging
714
+ * purposes.
715
+ *
716
+ * The IndexesListenerStats object contains a breakdown of the different types
717
+ * of listener.
718
+ *
719
+ * The statistics are only populated in a debug build: production builds
720
+ * return an empty object. The method is intended to be used during
721
+ * development to ensure your application is not leaking listener
722
+ * registrations, for example.
723
+ *
724
+ * @returns A IndexesListenerStats object containing Indexes listener
725
+ * statistics.
726
+ * @example
727
+ * This example gets the listener statistics of an Indexes object.
728
+ *
729
+ * ```js
730
+ * const store = createStore();
731
+ * const indexes = createIndexes(store);
732
+ * indexes.addSliceIdsListener(null, () => {
733
+ * console.log('Slice Ids changed');
734
+ * });
735
+ * indexes.addSliceRowIdsListener(null, null, () => {
736
+ * console.log('Slice Row Ids changed');
737
+ * });
738
+ *
739
+ * console.log(indexes.getListenerStats());
740
+ * // -> {sliceIds: 1, sliceRowIds: 1}
741
+ * ```
742
+ * @category Development
743
+ */
744
+ getListenerStats(): IndexesListenerStats;
745
+ }
746
+
747
+ /**
748
+ * The createIndexes function creates an Indexes object, and is the main entry
749
+ * point into the indexes module.
750
+ *
751
+ * It is trivially simple.
752
+ *
753
+ * A given Store can only have one Indexes object associated with it. If you
754
+ * call this function twice on the same Store, your second call will return a
755
+ * reference to the Indexes object created by the first.
756
+ *
757
+ * @param store The Store for which to register Index definitions.
758
+ * @returns A reference to the new Indexes object.
759
+ * @example
760
+ * This example creates an Indexes object.
761
+ *
762
+ * ```js
763
+ * const store = createStore();
764
+ * const indexes = createIndexes(store);
765
+ * console.log(indexes.getIndexIds());
766
+ * // -> []
767
+ * ```
768
+ * @example
769
+ * This example creates an Indexes object, and calls the method a second time
770
+ * for the same Store to return the same object.
771
+ *
772
+ * ```js
773
+ * const store = createStore();
774
+ * const indexes1 = createIndexes(store);
775
+ * const indexes2 = createIndexes(store);
776
+ * console.log(indexes1 === indexes2);
777
+ * // -> true
778
+ * ```
779
+ * @category Creation
780
+ */
781
+ export function createIndexes(store: Store): Indexes;
782
+
783
+ /**
784
+ * The defaultSorter function is provided as a convenience to sort keys
785
+ * alphanumerically, and can be provided to the `sliceIdSorter` and
786
+ * `rowIdSorter` parameters of the setIndexDefinition method, for example.
787
+ *
788
+ * @param sortKey1 The first item of the pair to compare.
789
+ * @param sortKey2 The second item of the pair to compare.
790
+ * @returns A number indicating how to sort the pair.
791
+ * @example
792
+ * This example creates an Indexes object.
793
+ *
794
+ * ```js
795
+ * const store = createStore();
796
+ * const indexes = createIndexes(store);
797
+ * console.log(indexes.getIndexIds());
798
+ * // -> []
799
+ * ```
800
+ * @example
801
+ * This example creates a Store, creates an Indexes object, and defines an
802
+ * Index based on the first letter of the pets' names. The Slice Ids (and Row
803
+ * Ids within them) are alphabetically sorted using the defaultSorter function.
804
+ *
805
+ * ```js
806
+ * const store = createStore().setTable('pets', {
807
+ * fido: {species: 'dog'},
808
+ * felix: {species: 'cat'},
809
+ * cujo: {species: 'dog'},
810
+ * });
811
+ *
812
+ * const indexes = createIndexes(store);
813
+ * indexes.setIndexDefinition(
814
+ * 'byFirst', // indexId
815
+ * 'pets', // tableId
816
+ * (_, rowId) => rowId[0], // each Row's Slice Id
817
+ * (_, rowId) => rowId, // each Row's sort key
818
+ * defaultSorter, // sort Slice Ids
819
+ * defaultSorter, // sort Row Ids by sort key
820
+ * );
821
+ *
822
+ * console.log(indexes.getSliceIds('byFirst'));
823
+ * // -> ['c', 'f']
824
+ * console.log(indexes.getSliceRowIds('byFirst', 'f'));
825
+ * // -> ['felix', 'fido']
826
+ * ```
827
+ * @category Convenience
828
+ */
829
+ export function defaultSorter(sortKey1: SortKey, sortKey2: SortKey): number;