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