tinybase 1.0.2 → 1.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/lib/checkpoints.d.ts +945 -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.d.ts +66 -0
  8. package/lib/debug/checkpoints.js +332 -0
  9. package/lib/debug/common.js +3 -0
  10. package/lib/debug/indexes.d.ts +167 -6
  11. package/lib/debug/indexes.js +431 -0
  12. package/lib/debug/metrics.d.ts +72 -0
  13. package/lib/debug/metrics.js +401 -0
  14. package/lib/debug/persisters.js +191 -0
  15. package/lib/debug/relationships.d.ts +86 -1
  16. package/lib/debug/relationships.js +434 -0
  17. package/lib/debug/store.d.ts +187 -5
  18. package/lib/debug/store.js +783 -0
  19. package/lib/debug/tinybase.js +144 -39
  20. package/lib/indexes.d.ts +939 -0
  21. package/lib/indexes.js +1 -0
  22. package/lib/indexes.js.gz +0 -0
  23. package/lib/metrics.d.ts +829 -0
  24. package/lib/metrics.js +1 -0
  25. package/lib/metrics.js.gz +0 -0
  26. package/lib/persisters.d.ts +711 -0
  27. package/lib/persisters.js +1 -0
  28. package/lib/persisters.js.gz +0 -0
  29. package/lib/relationships.d.ts +1201 -0
  30. package/lib/relationships.js +1 -0
  31. package/lib/relationships.js.gz +0 -0
  32. package/lib/store.d.ts +2688 -0
  33. package/lib/store.js +1 -0
  34. package/lib/store.js.gz +0 -0
  35. package/lib/tinybase.d.ts +13 -0
  36. package/lib/tinybase.js +1 -0
  37. package/lib/tinybase.js.gz +0 -0
  38. package/lib/ui-react.d.ts +7185 -0
  39. package/lib/ui-react.js +1 -0
  40. package/lib/ui-react.js.gz +0 -0
  41. package/lib/umd/checkpoints.js +1 -0
  42. package/lib/umd/checkpoints.js.gz +0 -0
  43. package/lib/umd/common.js +1 -0
  44. package/lib/umd/common.js.gz +0 -0
  45. package/lib/umd/indexes.js +1 -0
  46. package/lib/umd/indexes.js.gz +0 -0
  47. package/lib/umd/metrics.js +1 -0
  48. package/lib/umd/metrics.js.gz +0 -0
  49. package/lib/umd/persisters.js +1 -0
  50. package/lib/umd/persisters.js.gz +0 -0
  51. package/lib/umd/relationships.js +1 -0
  52. package/lib/umd/relationships.js.gz +0 -0
  53. package/lib/umd/store.js +1 -0
  54. package/lib/umd/store.js.gz +0 -0
  55. package/lib/umd/tinybase.js +1 -0
  56. package/lib/umd/tinybase.js.gz +0 -0
  57. package/lib/umd/ui-react.js +1 -0
  58. package/lib/umd/ui-react.js.gz +0 -0
  59. package/package.json +14 -14
  60. package/readme.md +2 -2
@@ -11,7 +11,7 @@
11
11
  * @module indexes
12
12
  */
13
13
 
14
- import {GetCell, Store} from './store.d';
14
+ import {GetCell, RowCallback, Store} from './store.d';
15
15
  import {Id, IdOrNull, Ids, SortKey} from './common.d';
16
16
 
17
17
  /**
@@ -43,6 +43,42 @@ export type Index = {[sliceId: Id]: Slice};
43
43
  */
44
44
  export type Slice = Ids;
45
45
 
46
+ /**
47
+ * The IndexCallback type describes a function that takes an Index's Id and a
48
+ * callback to loop over each Slice within it.
49
+ *
50
+ * A IndexCallback is provided when using the forEachIndex method, so that you
51
+ * can do something based on every Index in the Indexes object. See that method
52
+ * for specific examples.
53
+ *
54
+ * @param indexId The Id of the Index that the callback can operate on.
55
+ * @param forEachRow A function that will let you iterate over the Slice objects
56
+ * in this Index.
57
+ * @category Callback
58
+ */
59
+ export type IndexCallback = (
60
+ indexId: Id,
61
+ forEachSlice: (sliceCallback: SliceCallback) => void,
62
+ ) => void;
63
+
64
+ /**
65
+ * The SliceCallback type describes a function that takes a Slice's Id and a
66
+ * callback to loop over each Row within it.
67
+ *
68
+ * A SliceCallback is provided when using the forEachSlice method, so that you
69
+ * can do something based on every Slice in an Index. See that method for
70
+ * specific examples.
71
+ *
72
+ * @param sliceId The Id of the Slice that the callback can operate on.
73
+ * @param forEachRow A function that will let you iterate over the Row objects
74
+ * in this Slice.
75
+ * @category Callback
76
+ */
77
+ export type SliceCallback = (
78
+ sliceId: Id,
79
+ forEachRow: (rowCallback: RowCallback) => void,
80
+ ) => void;
81
+
46
82
  /**
47
83
  * The SliceIdsListener type describes a function that is used to listen to
48
84
  * changes to the Slice Ids in an Index.
@@ -362,6 +398,131 @@ export interface Indexes {
362
398
  */
363
399
  getIndexIds(): Ids;
364
400
 
401
+ /**
402
+ * The forEachIndex method takes a function that it will then call for each
403
+ * Index in a specified Indexes object.
404
+ *
405
+ * This method is useful for iterating over the structure of the Indexes
406
+ * object in a functional style. The `indexCallback` parameter is a
407
+ * IndexCallback function that will be called with the Id of each Index, and
408
+ * with a function that can then be used to iterate over each Slice of the
409
+ * Index, should you wish.
410
+ *
411
+ * @param indexCallback The function that should be called for every Index.
412
+ * @example
413
+ * This example iterates over each Index in an Indexes object, and lists each
414
+ * Slice Id within them.
415
+ *
416
+ * ```js
417
+ * const store = createStore().setTable('pets', {
418
+ * fido: {species: 'dog', color: 'brown'},
419
+ * felix: {species: 'cat', color: 'black'},
420
+ * cujo: {species: 'dog', color: 'black'},
421
+ * });
422
+ * const indexes = createIndexes(store)
423
+ * .setIndexDefinition('bySpecies', 'pets', 'species')
424
+ * .setIndexDefinition('byColor', 'pets', 'color');
425
+ *
426
+ * indexes.forEachIndex((indexId, forEachSlice) => {
427
+ * console.log(indexId);
428
+ * forEachSlice((sliceId) => console.log(`- ${sliceId}`));
429
+ * });
430
+ * // -> 'bySpecies'
431
+ * // -> '- dog'
432
+ * // -> '- cat'
433
+ * // -> 'byColor'
434
+ * // -> '- brown'
435
+ * // -> '- black'
436
+ * ```
437
+ * @category Iterator
438
+ */
439
+ forEachIndex(indexCallback: IndexCallback): void;
440
+
441
+ /**
442
+ * The forEachSlice method takes a function that it will then call for each
443
+ * Slice in a specified Index.
444
+ *
445
+ * This method is useful for iterating over the Slice structure of the Index
446
+ * in a functional style. The `rowCallback` parameter is a RowCallback
447
+ * function that will be called with the Id and value of each Row in the
448
+ * Slice.
449
+ *
450
+ * @param indexId The Id of the Index to iterate over.
451
+ * @param sliceCallback The function that should be called for every Slice.
452
+ * @example
453
+ * This example iterates over each Row in a Slice, and lists its Id.
454
+ *
455
+ * ```js
456
+ * const store = createStore().setTable('pets', {
457
+ * fido: {species: 'dog'},
458
+ * felix: {species: 'cat'},
459
+ * cujo: {species: 'dog'},
460
+ * });
461
+ * const indexes = createIndexes(store);
462
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
463
+ *
464
+ * indexes.forEachSlice('bySpecies', (sliceId, forEachRow) => {
465
+ * console.log(sliceId);
466
+ * forEachRow((rowId) => console.log(`- ${rowId}`));
467
+ * });
468
+ * // -> 'dog'
469
+ * // -> '- fido'
470
+ * // -> '- cujo'
471
+ * // -> 'cat'
472
+ * // -> '- felix'
473
+ * ```
474
+ * @category Iterator
475
+ */
476
+ forEachSlice(indexId: Id, sliceCallback: SliceCallback): void;
477
+
478
+ /**
479
+ * The hasIndex method returns a boolean indicating whether a given Index
480
+ * exists in the Indexes object.
481
+ *
482
+ * @param indexId The Id of a possible Index in the Indexes object.
483
+ * @returns Whether an Index with that Id exists.
484
+ * @example
485
+ * This example shows two simple Index existence checks.
486
+ *
487
+ * ```js
488
+ * const indexes = createIndexes(createStore());
489
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
490
+ * console.log(indexes.hasIndex('bySpecies'));
491
+ * // -> true
492
+ * console.log(indexes.hasIndex('byColor'));
493
+ * // -> false
494
+ * ```
495
+ * @category Getter
496
+ */
497
+ hasIndex(indexId: Id): boolean;
498
+
499
+ /**
500
+ * The hasSlice method returns a boolean indicating whether a given Slice
501
+ * exists in the Indexes object.
502
+ *
503
+ * @param indexId The Id of a possible Index in the Indexes object.
504
+ * @param sliceId The Id of a possible Slice in the Index.
505
+ * @returns Whether a Slice with that Id exists.
506
+ * @example
507
+ * This example shows two simple Index existence checks.
508
+ *
509
+ * ```js
510
+ * const store = createStore().setTable('pets', {
511
+ * fido: {species: 'dog'},
512
+ * felix: {species: 'cat'},
513
+ * cujo: {species: 'dog'},
514
+ * });
515
+ * const indexes = createIndexes(store);
516
+ * indexes.setIndexDefinition('bySpecies', 'pets', 'species');
517
+ * console.log(indexes.hasSlice('bySpecies', 'dog'));
518
+ * // -> true
519
+ * console.log(indexes.hasSlice('bySpecies', 'worm'));
520
+ * // -> false
521
+ * ```
522
+ * @category Getter
523
+ */
524
+ hasSlice(indexId: Id, sliceId: Id): boolean;
525
+
365
526
  /**
366
527
  * The getTableId method returns the Id of the underlying Table that is
367
528
  * backing an Index.
@@ -473,7 +634,7 @@ export interface Indexes {
473
634
  * the Index change.
474
635
  * @returns A unique Id for the listener that can later be used to remove it.
475
636
  * @example
476
- * This example creates a Store, a Indexes object, and then registers a
637
+ * This example creates a Store, an Indexes object, and then registers a
477
638
  * listener that responds to any changes to a specific Index.
478
639
  *
479
640
  * ```js
@@ -501,7 +662,7 @@ export interface Indexes {
501
662
  * indexes.delListener(listenerId);
502
663
  * ```
503
664
  * @example
504
- * This example creates a Store, a Indexes object, and then registers a
665
+ * This example creates a Store, an Indexes object, and then registers a
505
666
  * listener that responds to any changes to any Index.
506
667
  *
507
668
  * ```js
@@ -558,7 +719,7 @@ export interface Indexes {
558
719
  * the Slice change.
559
720
  * @returns A unique Id for the listener that can later be used to remove it.
560
721
  * @example
561
- * This example creates a Store, a Indexes object, and then registers a
722
+ * This example creates a Store, an Indexes object, and then registers a
562
723
  * listener that responds to any changes to a specific Slice.
563
724
  *
564
725
  * ```js
@@ -587,7 +748,7 @@ export interface Indexes {
587
748
  * indexes.delListener(listenerId);
588
749
  * ```
589
750
  * @example
590
- * This example creates a Store, a Indexes object, and then registers a
751
+ * This example creates a Store, an Indexes object, and then registers a
591
752
  * listener that responds to any changes to any Slice.
592
753
  *
593
754
  * ```js
@@ -639,7 +800,7 @@ export interface Indexes {
639
800
  * @param listenerId The Id of the listener to remove.
640
801
  * @returns A reference to the Indexes object.
641
802
  * @example
642
- * This example creates a Store, a Indexes object, registers a listener, and
803
+ * This example creates a Store, an Indexes object, registers a listener, and
643
804
  * then removes it.
644
805
  *
645
806
  * ```js
@@ -0,0 +1,431 @@
1
+ const getTypeOf = (thing) => typeof thing;
2
+ const EMPTY_STRING = '';
3
+ const STRING = getTypeOf(EMPTY_STRING);
4
+
5
+ const arrayIsSorted = (array, sorter) =>
6
+ array.every(
7
+ (value, index) => index == 0 || sorter(array[index - 1], value) <= 0,
8
+ );
9
+ const arraySort = (array, sorter) => array.sort(sorter);
10
+ const arrayForEach = (array, cb) => array.forEach(cb);
11
+ const arrayLength = (array) => array.length;
12
+ const arrayIsEmpty = (array) => arrayLength(array) == 0;
13
+ const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
14
+ const arrayFromSecond = (ids) => ids.slice(1);
15
+ const arrayPush = (array, value) => array.push(value);
16
+ const arrayPop = (array) => array.pop();
17
+
18
+ const isUndefined = (thing) => thing == void 0;
19
+ const ifNotUndefined = (value, then, otherwise) =>
20
+ isUndefined(value) ? otherwise?.() : then(value);
21
+ const isString = (thing) => getTypeOf(thing) == STRING;
22
+
23
+ const collSizeN = (collSizer) => (coll) =>
24
+ arrayReduce(collValues(coll), (total, coll2) => total + collSizer(coll2), 0);
25
+ const collSize = (coll) => coll.size;
26
+ const collSize2 = collSizeN(collSize);
27
+ const collSize3 = collSizeN(collSize2);
28
+ const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
29
+ const collIsEmpty = (coll) => isUndefined(coll) || collSize(coll) == 0;
30
+ const collValues = (coll) => [...(coll?.values() ?? [])];
31
+ const collClear = (coll) => coll.clear();
32
+ const collForEach = (coll, cb) => coll?.forEach(cb);
33
+ const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
34
+
35
+ const mapNew = (entries) => new Map(entries);
36
+ const mapKeys = (map) => [...(map?.keys() ?? [])];
37
+ const mapGet = (map, key) => map?.get(key);
38
+ const mapForEach = (map, cb) =>
39
+ collForEach(map, (value, key) => cb(key, value));
40
+ const mapSet = (map, key, value) =>
41
+ isUndefined(value) ? (collDel(map, key), map) : map?.set(key, value);
42
+ const mapEnsure = (map, key, defaultValue, onWillAdd) => {
43
+ if (!collHas(map, key)) {
44
+ onWillAdd?.(defaultValue);
45
+ map.set(key, defaultValue);
46
+ }
47
+ return mapGet(map, key);
48
+ };
49
+
50
+ const setNew = (entries) => new Set(entries);
51
+ const setAdd = (set, value) => set?.add(value);
52
+
53
+ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
54
+ const hasRow = store.hasRow;
55
+ const tableIds = mapNew();
56
+ const things = mapNew();
57
+ const allRowValues = mapNew();
58
+ const allSortKeys = mapNew();
59
+ const storeListenerIds = mapNew();
60
+ const getStore = () => store;
61
+ const getThingIds = () => mapKeys(tableIds);
62
+ const forEachThing = (cb) => mapForEach(things, cb);
63
+ const hasThing = (id) => collHas(things, id);
64
+ const getTableId = (id) => mapGet(tableIds, id);
65
+ const getThing = (id) => mapGet(things, id);
66
+ const setThing = (id, thing) => mapSet(things, id, thing);
67
+ const removeStoreListeners = (id) =>
68
+ ifNotUndefined(mapGet(storeListenerIds, id), (listenerIds) => {
69
+ collForEach(listenerIds, store.delListener);
70
+ mapSet(storeListenerIds, id);
71
+ });
72
+ const setDefinition = (id, tableId, onChanged, getRowValue, getSortKey) => {
73
+ const changedRowValues = mapNew();
74
+ const changedSortKeys = mapNew();
75
+ mapSet(tableIds, id, tableId);
76
+ if (!collHas(things, id)) {
77
+ mapSet(things, id, getDefaultThing());
78
+ mapSet(allRowValues, id, mapNew());
79
+ mapSet(allSortKeys, id, mapNew());
80
+ }
81
+ const rowValues = mapGet(allRowValues, id);
82
+ const sortKeys = mapGet(allSortKeys, id);
83
+ const processRow = (rowId) => {
84
+ const getCell = (cellId) => store.getCell(tableId, rowId, cellId);
85
+ const oldRowValue = mapGet(rowValues, rowId);
86
+ const newRowValue = hasRow(tableId, rowId)
87
+ ? validateRowValue(getRowValue(getCell, rowId))
88
+ : void 0;
89
+ if (oldRowValue != newRowValue) {
90
+ mapSet(changedRowValues, rowId, [oldRowValue, newRowValue]);
91
+ }
92
+ if (!isUndefined(getSortKey)) {
93
+ const oldSortKey = mapGet(sortKeys, rowId);
94
+ const newSortKey = hasRow(tableId, rowId)
95
+ ? getSortKey(getCell, rowId)
96
+ : void 0;
97
+ if (oldSortKey != newSortKey) {
98
+ mapSet(changedSortKeys, rowId, newSortKey);
99
+ }
100
+ }
101
+ };
102
+ const processTable = (force) => {
103
+ onChanged(
104
+ () => {
105
+ collForEach(changedRowValues, ([, newRowValue], rowId) =>
106
+ mapSet(rowValues, rowId, newRowValue),
107
+ );
108
+ collForEach(changedSortKeys, (newSortKey, rowId) =>
109
+ mapSet(sortKeys, rowId, newSortKey),
110
+ );
111
+ },
112
+ changedRowValues,
113
+ changedSortKeys,
114
+ rowValues,
115
+ sortKeys,
116
+ force,
117
+ );
118
+ collClear(changedRowValues);
119
+ collClear(changedSortKeys);
120
+ };
121
+ mapForEach(rowValues, processRow);
122
+ if (store.hasTable(tableId)) {
123
+ arrayForEach(store.getRowIds(tableId), (rowId) => {
124
+ if (!collHas(rowValues, rowId)) {
125
+ processRow(rowId);
126
+ }
127
+ });
128
+ }
129
+ processTable(true);
130
+ removeStoreListeners(id);
131
+ mapSet(
132
+ storeListenerIds,
133
+ id,
134
+ setNew([
135
+ store.addRowListener(tableId, null, (_store, _tableId, rowId) =>
136
+ processRow(rowId),
137
+ ),
138
+ store.addTableListener(tableId, () => processTable()),
139
+ ]),
140
+ );
141
+ };
142
+ const delDefinition = (id) => {
143
+ mapSet(tableIds, id);
144
+ mapSet(things, id);
145
+ mapSet(allRowValues, id);
146
+ mapSet(allSortKeys, id);
147
+ removeStoreListeners(id);
148
+ };
149
+ const destroy = () => mapForEach(storeListenerIds, delDefinition);
150
+ return [
151
+ getStore,
152
+ getThingIds,
153
+ forEachThing,
154
+ hasThing,
155
+ getTableId,
156
+ getThing,
157
+ setThing,
158
+ setDefinition,
159
+ delDefinition,
160
+ destroy,
161
+ ];
162
+ };
163
+ const getRowCellFunction = (getRowCell, defaultCellValue) =>
164
+ isString(getRowCell)
165
+ ? (getCell) => getCell(getRowCell)
166
+ : getRowCell ?? (() => defaultCellValue ?? EMPTY_STRING);
167
+ const getCreateFunction = (getFunction) => {
168
+ const getFunctionsByStore = /* @__PURE__ */ new WeakMap();
169
+ return (store) => {
170
+ if (!getFunctionsByStore.has(store)) {
171
+ getFunctionsByStore.set(store, getFunction(store));
172
+ }
173
+ return getFunctionsByStore.get(store);
174
+ };
175
+ };
176
+
177
+ const defaultSorter = (sortKey1, sortKey2) => (sortKey1 < sortKey2 ? -1 : 1);
178
+
179
+ const addDeepSet = (deepSet, value, ids) =>
180
+ arrayLength(ids) < 2
181
+ ? setAdd(
182
+ arrayIsEmpty(ids) ? deepSet : mapEnsure(deepSet, ids[0], setNew()),
183
+ value,
184
+ )
185
+ : addDeepSet(
186
+ mapEnsure(deepSet, ids[0], mapNew()),
187
+ value,
188
+ arrayFromSecond(ids),
189
+ );
190
+ const forDeepSet = (valueDo) => {
191
+ const deep = (deepIdSet, arg, ...ids) =>
192
+ ifNotUndefined(deepIdSet, (deepIdSet2) =>
193
+ arrayIsEmpty(ids)
194
+ ? valueDo(deepIdSet2, arg)
195
+ : arrayForEach([ids[0], null], (id) =>
196
+ deep(mapGet(deepIdSet2, id), arg, ...arrayFromSecond(ids)),
197
+ ),
198
+ );
199
+ return deep;
200
+ };
201
+ const getListenerFunctions = (getThing) => {
202
+ let thing;
203
+ let nextId = 0;
204
+ const listenerPool = [];
205
+ const allListeners = mapNew();
206
+ const addListener = (listener, deepSet, idOrNulls = []) => {
207
+ thing ??= getThing();
208
+ const id = arrayPop(listenerPool) ?? '' + nextId++;
209
+ mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
210
+ addDeepSet(deepSet, id, idOrNulls);
211
+ return id;
212
+ };
213
+ const callListeners = (deepSet, ids = [], ...extraArgs) =>
214
+ forDeepSet(collForEach)(
215
+ deepSet,
216
+ (id) =>
217
+ ifNotUndefined(mapGet(allListeners, id), ([listener]) =>
218
+ listener(thing, ...ids, ...extraArgs),
219
+ ),
220
+ ...ids,
221
+ );
222
+ const delListener = (id) =>
223
+ ifNotUndefined(
224
+ mapGet(allListeners, id),
225
+ ([, deepSet, idOrNulls]) => {
226
+ forDeepSet(collDel)(deepSet, id, ...idOrNulls);
227
+ mapSet(allListeners, id);
228
+ if (arrayLength(listenerPool) < 1e3) {
229
+ arrayPush(listenerPool, id);
230
+ }
231
+ return idOrNulls;
232
+ },
233
+ () => [],
234
+ );
235
+ const callListener = (id, idNullGetters, extraArgsGetter) =>
236
+ ifNotUndefined(mapGet(allListeners, id), ([listener, , idOrNulls]) => {
237
+ const callWithIds = (...ids) => {
238
+ const index = arrayLength(ids);
239
+ index == arrayLength(idOrNulls)
240
+ ? listener(thing, ...ids, ...extraArgsGetter(ids))
241
+ : isUndefined(idOrNulls[index])
242
+ ? arrayForEach(idNullGetters[index](...ids), (id2) =>
243
+ callWithIds(...ids, id2),
244
+ )
245
+ : callWithIds(...ids, idOrNulls[index]);
246
+ };
247
+ callWithIds();
248
+ });
249
+ return [addListener, callListeners, delListener, callListener];
250
+ };
251
+
252
+ const object = Object;
253
+ const objFreeze = object.freeze;
254
+
255
+ const createIndexes = getCreateFunction((store) => {
256
+ const sliceIdsListeners = mapNew();
257
+ const sliceRowIdsListeners = mapNew();
258
+ const [
259
+ getStore,
260
+ getIndexIds,
261
+ forEachIndexImpl,
262
+ hasIndex,
263
+ getTableId,
264
+ getIndex,
265
+ setIndex,
266
+ setDefinition,
267
+ delDefinition,
268
+ destroy,
269
+ ] = getDefinableFunctions(store, mapNew, (value) =>
270
+ isUndefined(value) ? EMPTY_STRING : value + EMPTY_STRING,
271
+ );
272
+ const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
273
+ () => indexes,
274
+ );
275
+ const hasSlice = (indexId, sliceId) => collHas(getIndex(indexId), sliceId);
276
+ const setIndexDefinition = (
277
+ indexId,
278
+ tableId,
279
+ getSliceId,
280
+ getSortKey,
281
+ sliceIdSorter,
282
+ rowIdSorter = defaultSorter,
283
+ ) => {
284
+ const sliceIdArraySorter = isUndefined(sliceIdSorter)
285
+ ? void 0
286
+ : ([id1], [id2]) => sliceIdSorter(id1, id2);
287
+ setDefinition(
288
+ indexId,
289
+ tableId,
290
+ (change, changedSliceIds, changedSortKeys, sliceIds, sortKeys, force) => {
291
+ let sliceIdsChanged = 0;
292
+ const changedSlices = setNew();
293
+ const unsortedSlices = setNew();
294
+ const index = getIndex(indexId);
295
+ collForEach(changedSliceIds, ([oldSliceId, newSliceId], rowId) => {
296
+ if (!isUndefined(oldSliceId)) {
297
+ setAdd(changedSlices, oldSliceId);
298
+ ifNotUndefined(mapGet(index, oldSliceId), (oldSlice) => {
299
+ collDel(oldSlice, rowId);
300
+ if (collIsEmpty(oldSlice)) {
301
+ mapSet(index, oldSliceId);
302
+ sliceIdsChanged = 1;
303
+ }
304
+ });
305
+ }
306
+ if (!isUndefined(newSliceId)) {
307
+ setAdd(changedSlices, newSliceId);
308
+ if (!collHas(index, newSliceId)) {
309
+ mapSet(index, newSliceId, setNew());
310
+ sliceIdsChanged = 1;
311
+ }
312
+ setAdd(mapGet(index, newSliceId), rowId);
313
+ if (!isUndefined(getSortKey)) {
314
+ setAdd(unsortedSlices, newSliceId);
315
+ }
316
+ }
317
+ });
318
+ change();
319
+ if (!collIsEmpty(sortKeys)) {
320
+ if (force) {
321
+ mapForEach(index, (sliceId) => setAdd(unsortedSlices, sliceId));
322
+ } else {
323
+ mapForEach(changedSortKeys, (rowId) =>
324
+ ifNotUndefined(mapGet(sliceIds, rowId), (sliceId) =>
325
+ setAdd(unsortedSlices, sliceId),
326
+ ),
327
+ );
328
+ }
329
+ collForEach(unsortedSlices, (sliceId) => {
330
+ const rowIdArraySorter = (rowId1, rowId2) =>
331
+ rowIdSorter(
332
+ mapGet(sortKeys, rowId1),
333
+ mapGet(sortKeys, rowId2),
334
+ sliceId,
335
+ );
336
+ const sliceArray = [...mapGet(index, sliceId)];
337
+ if (!arrayIsSorted(sliceArray, rowIdArraySorter)) {
338
+ mapSet(
339
+ index,
340
+ sliceId,
341
+ setNew(arraySort(sliceArray, rowIdArraySorter)),
342
+ );
343
+ setAdd(changedSlices, sliceId);
344
+ }
345
+ });
346
+ }
347
+ if (sliceIdsChanged || force) {
348
+ if (!isUndefined(sliceIdArraySorter)) {
349
+ const indexArray = [...index];
350
+ if (!arrayIsSorted(indexArray, sliceIdArraySorter)) {
351
+ setIndex(
352
+ indexId,
353
+ mapNew(arraySort(indexArray, sliceIdArraySorter)),
354
+ );
355
+ sliceIdsChanged = 1;
356
+ }
357
+ }
358
+ }
359
+ if (sliceIdsChanged) {
360
+ callListeners(sliceIdsListeners, [indexId]);
361
+ }
362
+ collForEach(changedSlices, (sliceId) =>
363
+ callListeners(sliceRowIdsListeners, [indexId, sliceId]),
364
+ );
365
+ },
366
+ getRowCellFunction(getSliceId),
367
+ ifNotUndefined(getSortKey, getRowCellFunction),
368
+ );
369
+ return indexes;
370
+ };
371
+ const forEachIndex = (indexCallback) =>
372
+ forEachIndexImpl((indexId, slices) =>
373
+ indexCallback(indexId, (sliceCallback) =>
374
+ forEachSliceImpl(indexId, sliceCallback, slices),
375
+ ),
376
+ );
377
+ const forEachSlice = (indexId, sliceCallback) =>
378
+ forEachSliceImpl(indexId, sliceCallback, getIndex(indexId));
379
+ const forEachSliceImpl = (indexId, sliceCallback, slices) => {
380
+ const tableId = getTableId(indexId);
381
+ collForEach(slices, (rowIds, sliceId) =>
382
+ sliceCallback(sliceId, (rowCallback) =>
383
+ collForEach(rowIds, (rowId) =>
384
+ rowCallback(rowId, (cellCallback) =>
385
+ store.forEachCell(tableId, rowId, cellCallback),
386
+ ),
387
+ ),
388
+ ),
389
+ );
390
+ };
391
+ const delIndexDefinition = (indexId) => {
392
+ delDefinition(indexId);
393
+ return indexes;
394
+ };
395
+ const getSliceIds = (indexId) => mapKeys(getIndex(indexId));
396
+ const getSliceRowIds = (indexId, sliceId) =>
397
+ collValues(mapGet(getIndex(indexId), sliceId));
398
+ const addSliceIdsListener = (indexId, listener) =>
399
+ addListener(listener, sliceIdsListeners, [indexId]);
400
+ const addSliceRowIdsListener = (indexId, sliceId, listener) =>
401
+ addListener(listener, sliceRowIdsListeners, [indexId, sliceId]);
402
+ const delListener = (listenerId) => {
403
+ delListenerImpl(listenerId);
404
+ return indexes;
405
+ };
406
+ const getListenerStats = () => ({
407
+ sliceIds: collSize2(sliceIdsListeners),
408
+ sliceRowIds: collSize3(sliceRowIdsListeners),
409
+ });
410
+ const indexes = {
411
+ setIndexDefinition,
412
+ delIndexDefinition,
413
+ getStore,
414
+ getIndexIds,
415
+ forEachIndex,
416
+ forEachSlice,
417
+ hasIndex,
418
+ hasSlice,
419
+ getTableId,
420
+ getSliceIds,
421
+ getSliceRowIds,
422
+ addSliceIdsListener,
423
+ addSliceRowIdsListener,
424
+ delListener,
425
+ destroy,
426
+ getListenerStats,
427
+ };
428
+ return objFreeze(indexes);
429
+ });
430
+
431
+ export {createIndexes};