tinybase 0.0.0 → 0.9.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 (61) hide show
  1. package/LICENSE +21 -0
  2. package/lib/checkpoints.d.ts +861 -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 +861 -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 +815 -0
  10. package/lib/debug/indexes.js +390 -0
  11. package/lib/debug/metrics.d.ts +728 -0
  12. package/lib/debug/metrics.js +391 -0
  13. package/lib/debug/persisters.d.ts +521 -0
  14. package/lib/debug/persisters.js +191 -0
  15. package/lib/debug/react.d.ts +7077 -0
  16. package/lib/debug/react.js +1037 -0
  17. package/lib/debug/relationships.d.ts +1091 -0
  18. package/lib/debug/relationships.js +418 -0
  19. package/lib/debug/store.d.ts +2424 -0
  20. package/lib/debug/store.js +725 -0
  21. package/lib/debug/tinybase.d.ts +14 -0
  22. package/lib/debug/tinybase.js +2727 -0
  23. package/lib/indexes.d.ts +815 -0
  24. package/lib/indexes.js +1 -0
  25. package/lib/indexes.js.gz +0 -0
  26. package/lib/metrics.d.ts +728 -0
  27. package/lib/metrics.js +1 -0
  28. package/lib/metrics.js.gz +0 -0
  29. package/lib/persisters.d.ts +521 -0
  30. package/lib/persisters.js +1 -0
  31. package/lib/persisters.js.gz +0 -0
  32. package/lib/react.d.ts +7077 -0
  33. package/lib/react.js +1 -0
  34. package/lib/react.js.gz +0 -0
  35. package/lib/relationships.d.ts +1091 -0
  36. package/lib/relationships.js +1 -0
  37. package/lib/relationships.js.gz +0 -0
  38. package/lib/store.d.ts +2424 -0
  39. package/lib/store.js +1 -0
  40. package/lib/store.js.gz +0 -0
  41. package/lib/tinybase.d.ts +14 -0
  42. package/lib/tinybase.js +1 -0
  43. package/lib/tinybase.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/react.js +1 -0
  53. package/lib/umd/react.js.gz +0 -0
  54. package/lib/umd/relationships.js +1 -0
  55. package/lib/umd/relationships.js.gz +0 -0
  56. package/lib/umd/store.js +1 -0
  57. package/lib/umd/store.js.gz +0 -0
  58. package/lib/umd/tinybase.js +1 -0
  59. package/lib/umd/tinybase.js.gz +0 -0
  60. package/package.json +93 -2
  61. package/readme.md +195 -0
@@ -0,0 +1,2727 @@
1
+ import {promises, watch} from 'fs';
2
+ import React from 'react';
3
+
4
+ const getTypeOf = (thing) => typeof thing;
5
+ const EMPTY_OBJECT = '{}';
6
+ const EMPTY_STRING = '';
7
+ const STRING = getTypeOf(EMPTY_STRING);
8
+ const BOOLEAN = getTypeOf(true);
9
+ const NUMBER = getTypeOf(0);
10
+ const FUNCTION = getTypeOf(getTypeOf);
11
+ const TYPE = 'type';
12
+ const DEFAULT = 'default';
13
+ const UTF8 = 'utf8';
14
+ const SUM = 'sum';
15
+ const AVG = 'avg';
16
+ const MIN = 'min';
17
+ const MAX = 'max';
18
+
19
+ const arrayHas = (array, value) => array.includes(value);
20
+ const arrayIsSorted = (array, sorter) =>
21
+ array.every(
22
+ (value, index) => index == 0 || sorter(array[index - 1], value) <= 0,
23
+ );
24
+ const arraySort = (array, sorter) => array.sort(sorter);
25
+ const arrayForEach = (array, cb) => array.forEach(cb);
26
+ const arrayMap = (array, cb) => array.map(cb);
27
+ const arraySum = (array) => arrayReduce(array, (i, j) => i + j, 0);
28
+ const arrayLength = (array) => array.length;
29
+ const arrayIsEmpty = (array) => arrayLength(array) == 0;
30
+ const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
31
+ const arrayFilter = (array, cb) => array.filter(cb);
32
+ const arrayFromSecond = (ids) => ids.slice(1);
33
+ const arrayClear = (array, to) => array.splice(0, to);
34
+
35
+ const jsonString = (obj) =>
36
+ JSON.stringify(obj, (_key, value) =>
37
+ isInstanceOf(value, Map)
38
+ ? arrayReduce(
39
+ [...value],
40
+ (obj2, [key, value2]) => {
41
+ obj2[key] = value2;
42
+ return obj2;
43
+ },
44
+ {},
45
+ )
46
+ : value,
47
+ );
48
+ const jsonParse = JSON.parse;
49
+ const mathMax = Math.max;
50
+ const mathMin = Math.min;
51
+ const isFiniteNumber = isFinite;
52
+ const isInstanceOf = (thing, cls) => thing instanceof cls;
53
+ const isUndefined = (thing) => thing == void 0;
54
+ const ifNotUndefined = (value, then, otherwise) =>
55
+ isUndefined(value) ? otherwise?.() : then(value);
56
+ const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
57
+ const isString = (thing) => getTypeOf(thing) == STRING;
58
+ const isFunction = (thing) => getTypeOf(thing) == FUNCTION;
59
+ const getUndefined = () => void 0;
60
+
61
+ const collSizeN = (collSizer) => (coll) =>
62
+ arrayReduce(collValues(coll), (total, coll2) => total + collSizer(coll2), 0);
63
+ const collSize = (coll) => coll.size;
64
+ const collSize2 = collSizeN(collSize);
65
+ const collSize3 = collSizeN(collSize2);
66
+ const collSize4 = collSizeN(collSize3);
67
+ const collPairSize = (pair, func = collSize) => func(pair[0]) + func(pair[1]);
68
+ const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
69
+ const collIsEmpty = (coll) => isUndefined(coll) || collSize(coll) == 0;
70
+ const collValues = (coll) => [...(coll?.values() ?? [])];
71
+ const collClear = (coll) => coll.clear();
72
+ const collForEach = (coll, cb) => coll?.forEach(cb);
73
+ const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
74
+
75
+ const mapNew = (entries) => new Map(entries);
76
+ const mapNewPair = (newFunction = mapNew) => [newFunction(), newFunction()];
77
+ const mapKeys = (map) => [...(map?.keys() ?? [])];
78
+ const mapGet = (map, key) => map?.get(key);
79
+ const mapForEach = (map, cb) =>
80
+ collForEach(map, (value, key) => cb(key, value));
81
+ const mapSet = (map, key, value) =>
82
+ isUndefined(value) ? (collDel(map, key), map) : map?.set(key, value);
83
+ const mapEnsure = (map, key, defaultValue, onWillAdd) => {
84
+ if (!collHas(map, key)) {
85
+ onWillAdd?.(defaultValue);
86
+ map.set(key, defaultValue);
87
+ }
88
+ return mapGet(map, key);
89
+ };
90
+ const mapToObj = (map, childMapper) => {
91
+ const obj = {};
92
+ const mapper = childMapper ?? ((mapValue) => mapValue);
93
+ collForEach(map, (value, key) => (obj[key] = mapper(value)));
94
+ return obj;
95
+ };
96
+ const mapClone = (map, childMapper) => {
97
+ const map2 = mapNew();
98
+ const mapper = childMapper ?? ((mapValue) => mapValue);
99
+ collForEach(map, (value, key) => map2.set(key, mapper(value)));
100
+ return map2;
101
+ };
102
+
103
+ const setNew = (entries) => new Set(entries);
104
+ const setAdd = (set, value) => set?.add(value);
105
+
106
+ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
107
+ const hasRow = store.hasRow;
108
+ const tableIds = mapNew();
109
+ const things = mapNew();
110
+ const allRowValues = mapNew();
111
+ const allSortKeys = mapNew();
112
+ const storeListenerIds = mapNew();
113
+ const getStore = () => store;
114
+ const getThingIds = () => mapKeys(tableIds);
115
+ const getTableId = (id) => mapGet(tableIds, id);
116
+ const getThing = (id) => mapGet(things, id);
117
+ const setThing = (id, thing) => mapSet(things, id, thing);
118
+ const removeStoreListeners = (id) =>
119
+ ifNotUndefined(mapGet(storeListenerIds, id), (listenerIds) => {
120
+ collForEach(listenerIds, store.delListener);
121
+ mapSet(storeListenerIds, id);
122
+ });
123
+ const setDefinition = (id, tableId, onChanged, getRowValue, getSortKey) => {
124
+ const changedRowValues = mapNew();
125
+ const changedSortKeys = mapNew();
126
+ mapSet(tableIds, id, tableId);
127
+ if (!collHas(things, id)) {
128
+ mapSet(things, id, getDefaultThing());
129
+ mapSet(allRowValues, id, mapNew());
130
+ mapSet(allSortKeys, id, mapNew());
131
+ }
132
+ const rowValues = mapGet(allRowValues, id);
133
+ const sortKeys = mapGet(allSortKeys, id);
134
+ const processRow = (rowId) => {
135
+ const getCell = (cellId) => store.getCell(tableId, rowId, cellId);
136
+ const oldRowValue = mapGet(rowValues, rowId);
137
+ const newRowValue = hasRow(tableId, rowId)
138
+ ? validateRowValue(getRowValue(getCell, rowId))
139
+ : void 0;
140
+ if (oldRowValue != newRowValue) {
141
+ mapSet(changedRowValues, rowId, [oldRowValue, newRowValue]);
142
+ }
143
+ if (!isUndefined(getSortKey)) {
144
+ const oldSortKey = mapGet(sortKeys, rowId);
145
+ const newSortKey = hasRow(tableId, rowId)
146
+ ? getSortKey(getCell, rowId)
147
+ : void 0;
148
+ if (oldSortKey != newSortKey) {
149
+ mapSet(changedSortKeys, rowId, newSortKey);
150
+ }
151
+ }
152
+ };
153
+ const processTable = (force) => {
154
+ onChanged(
155
+ () => {
156
+ collForEach(changedRowValues, ([, newRowValue], rowId) =>
157
+ mapSet(rowValues, rowId, newRowValue),
158
+ );
159
+ collForEach(changedSortKeys, (newSortKey, rowId) =>
160
+ mapSet(sortKeys, rowId, newSortKey),
161
+ );
162
+ },
163
+ changedRowValues,
164
+ changedSortKeys,
165
+ rowValues,
166
+ sortKeys,
167
+ force,
168
+ );
169
+ collClear(changedRowValues);
170
+ collClear(changedSortKeys);
171
+ };
172
+ mapForEach(rowValues, processRow);
173
+ if (store.hasTable(tableId)) {
174
+ arrayForEach(store.getRowIds(tableId), (rowId) => {
175
+ if (!collHas(rowValues, rowId)) {
176
+ processRow(rowId);
177
+ }
178
+ });
179
+ }
180
+ processTable(true);
181
+ removeStoreListeners(id);
182
+ mapSet(
183
+ storeListenerIds,
184
+ id,
185
+ setNew([
186
+ store.addRowListener(tableId, null, (_store, _tableId, rowId) =>
187
+ processRow(rowId),
188
+ ),
189
+ store.addTableListener(tableId, () => processTable()),
190
+ ]),
191
+ );
192
+ };
193
+ const delDefinition = (id) => {
194
+ mapSet(tableIds, id);
195
+ mapSet(things, id);
196
+ mapSet(allRowValues, id);
197
+ mapSet(allSortKeys, id);
198
+ removeStoreListeners(id);
199
+ };
200
+ const destroy = () => mapForEach(storeListenerIds, delDefinition);
201
+ return [
202
+ getStore,
203
+ getThingIds,
204
+ getTableId,
205
+ getThing,
206
+ setThing,
207
+ setDefinition,
208
+ delDefinition,
209
+ destroy,
210
+ ];
211
+ };
212
+ const getRowCellFunction = (getRowCell, defaultCellValue) =>
213
+ isString(getRowCell)
214
+ ? (getCell) => getCell(getRowCell)
215
+ : getRowCell ?? (() => defaultCellValue ?? EMPTY_STRING);
216
+ const getCreateFunction = (getFunction) => {
217
+ const getFunctionsByStore = /* @__PURE__ */ new WeakMap();
218
+ return (store) => {
219
+ if (!getFunctionsByStore.has(store)) {
220
+ getFunctionsByStore.set(store, getFunction(store));
221
+ }
222
+ return getFunctionsByStore.get(store);
223
+ };
224
+ };
225
+
226
+ const addDeepSet = (deepSet, value, ids) =>
227
+ arrayLength(ids) < 2
228
+ ? setAdd(
229
+ arrayIsEmpty(ids) ? deepSet : mapEnsure(deepSet, ids[0], setNew()),
230
+ value,
231
+ )
232
+ : addDeepSet(
233
+ mapEnsure(deepSet, ids[0], mapNew()),
234
+ value,
235
+ arrayFromSecond(ids),
236
+ );
237
+ const forDeepSet = (valueDo) => {
238
+ const deep = (deepIdSet, arg, ...ids) =>
239
+ ifNotUndefined(deepIdSet, (deepIdSet2) =>
240
+ arrayIsEmpty(ids)
241
+ ? valueDo(deepIdSet2, arg)
242
+ : arrayForEach([ids[0], null], (id) =>
243
+ deep(mapGet(deepIdSet2, id), arg, ...arrayFromSecond(ids)),
244
+ ),
245
+ );
246
+ return deep;
247
+ };
248
+ const getListenerFunctions = (getThing) => {
249
+ let thing;
250
+ let nextId = 0;
251
+ const listenerPool = [];
252
+ const allListeners = mapNew();
253
+ const addListener = (listener, deepSet, idOrNulls = []) => {
254
+ thing ?? (thing = getThing());
255
+ const id = listenerPool.pop() ?? '' + nextId++;
256
+ mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
257
+ addDeepSet(deepSet, id, idOrNulls);
258
+ return id;
259
+ };
260
+ const callListeners = (deepSet, ids = [], ...extraArgs) =>
261
+ forDeepSet(collForEach)(
262
+ deepSet,
263
+ (id) =>
264
+ ifNotUndefined(mapGet(allListeners, id), ([listener]) =>
265
+ listener(thing, ...ids, ...extraArgs),
266
+ ),
267
+ ...ids,
268
+ );
269
+ const delListener = (id) =>
270
+ ifNotUndefined(
271
+ mapGet(allListeners, id),
272
+ ([, deepSet, idOrNulls]) => {
273
+ forDeepSet(collDel)(deepSet, id, ...idOrNulls);
274
+ mapSet(allListeners, id);
275
+ if (arrayLength(listenerPool) < 1e3) {
276
+ listenerPool.push(id);
277
+ }
278
+ return idOrNulls;
279
+ },
280
+ () => [],
281
+ );
282
+ const callListener = (id, idNullGetters, extraArgsGetter) =>
283
+ ifNotUndefined(mapGet(allListeners, id), ([listener, , idOrNulls]) => {
284
+ const callWithIds = (...ids) => {
285
+ const index = arrayLength(ids);
286
+ index == arrayLength(idOrNulls)
287
+ ? listener(thing, ...ids, ...extraArgsGetter(ids))
288
+ : isUndefined(idOrNulls[index])
289
+ ? arrayForEach(idNullGetters[index](...ids), (id2) =>
290
+ callWithIds(...ids, id2),
291
+ )
292
+ : callWithIds(...ids, idOrNulls[index]);
293
+ };
294
+ callWithIds();
295
+ });
296
+ return [addListener, callListeners, delListener, callListener];
297
+ };
298
+
299
+ const object = Object;
300
+ const objIds = object.keys;
301
+ const objFrozen = object.isFrozen;
302
+ const objFreeze = object.freeze;
303
+ const isObject = (obj) =>
304
+ isInstanceOf(obj, object) && obj.constructor == object;
305
+ const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
306
+ const objHas = (obj, id) => !isUndefined(objGet(obj, id));
307
+ const objDel = (obj, id) => delete obj[id];
308
+ const objForEach = (obj, cb) =>
309
+ arrayForEach(object.entries(obj), ([id, value]) => cb(value, id));
310
+ const objIsEmpty = (obj) => arrayIsEmpty(objIds(obj));
311
+
312
+ const createCheckpoints = getCreateFunction((store) => {
313
+ let backwardIdsSize = 100;
314
+ let currentId;
315
+ let delta = mapNew();
316
+ let listening = 1;
317
+ let nextCheckpointId;
318
+ let checkpointsChanged;
319
+ const checkpointIdsListeners = setNew();
320
+ const checkpointListeners = mapNew();
321
+ const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
322
+ () => checkpoints,
323
+ );
324
+ const deltas = mapNew();
325
+ const labels = mapNew();
326
+ const backwardIds = [];
327
+ const forwardIds = [];
328
+ const updateStore = (oldOrNew, checkpointId) => {
329
+ listening = 0;
330
+ store.transaction(() =>
331
+ collForEach(mapGet(deltas, checkpointId), (table, tableId) =>
332
+ collForEach(table, (row, rowId) =>
333
+ collForEach(row, (oldNew, cellId) =>
334
+ isUndefined(oldNew[oldOrNew])
335
+ ? store.delCell(tableId, rowId, cellId, true)
336
+ : store.setCell(tableId, rowId, cellId, oldNew[oldOrNew]),
337
+ ),
338
+ ),
339
+ ),
340
+ );
341
+ listening = 1;
342
+ };
343
+ const clearCheckpointId = (checkpointId) => {
344
+ mapSet(deltas, checkpointId);
345
+ mapSet(labels, checkpointId);
346
+ callListeners(checkpointListeners, [checkpointId]);
347
+ };
348
+ const clearCheckpointIds = (checkpointIds, to) =>
349
+ arrayForEach(
350
+ arrayClear(checkpointIds, to ?? arrayLength(checkpointIds)),
351
+ clearCheckpointId,
352
+ );
353
+ const trimBackwardsIds = () =>
354
+ clearCheckpointIds(backwardIds, arrayLength(backwardIds) - backwardIdsSize);
355
+ const listenerId = store.addCellListener(
356
+ null,
357
+ null,
358
+ null,
359
+ (_store, tableId, rowId, cellId, newCell, oldCell) => {
360
+ if (listening) {
361
+ ifNotUndefined(currentId, () => {
362
+ backwardIds.push(currentId);
363
+ trimBackwardsIds();
364
+ clearCheckpointIds(forwardIds);
365
+ currentId = void 0;
366
+ checkpointsChanged = 1;
367
+ });
368
+ const table = mapEnsure(delta, tableId, mapNew());
369
+ const row = mapEnsure(table, rowId, mapNew());
370
+ const oldNew = mapEnsure(
371
+ row,
372
+ cellId,
373
+ [void 0, void 0],
374
+ (addOldNew) => (addOldNew[0] = oldCell),
375
+ );
376
+ oldNew[1] = newCell;
377
+ if (oldNew[0] === oldNew[1]) {
378
+ if (collIsEmpty(mapSet(row, cellId))) {
379
+ if (collIsEmpty(mapSet(table, rowId))) {
380
+ if (collIsEmpty(mapSet(delta, tableId))) {
381
+ currentId = backwardIds.pop();
382
+ checkpointsChanged = 1;
383
+ }
384
+ }
385
+ }
386
+ }
387
+ callListenersIfChanged();
388
+ }
389
+ },
390
+ );
391
+ const addCheckpointImpl = (label = '') => {
392
+ if (isUndefined(currentId)) {
393
+ currentId = '' + nextCheckpointId++;
394
+ mapSet(deltas, currentId, delta);
395
+ setCheckpoint(currentId, label);
396
+ delta = mapNew();
397
+ checkpointsChanged = 1;
398
+ }
399
+ return currentId;
400
+ };
401
+ const goBackwardImpl = () => {
402
+ if (!arrayIsEmpty(backwardIds)) {
403
+ forwardIds.unshift(addCheckpointImpl());
404
+ updateStore(0, currentId);
405
+ currentId = backwardIds.pop();
406
+ checkpointsChanged = 1;
407
+ }
408
+ };
409
+ const goForwardImpl = () => {
410
+ if (!arrayIsEmpty(forwardIds)) {
411
+ backwardIds.push(currentId);
412
+ currentId = forwardIds.shift();
413
+ updateStore(1, currentId);
414
+ checkpointsChanged = 1;
415
+ }
416
+ };
417
+ const callListenersIfChanged = () => {
418
+ if (checkpointsChanged) {
419
+ callListeners(checkpointIdsListeners);
420
+ checkpointsChanged = 0;
421
+ }
422
+ };
423
+ const setSize = (size) => {
424
+ backwardIdsSize = size;
425
+ trimBackwardsIds();
426
+ return checkpoints;
427
+ };
428
+ const addCheckpoint = (label) => {
429
+ const id = addCheckpointImpl(label);
430
+ callListenersIfChanged();
431
+ return id;
432
+ };
433
+ const setCheckpoint = (checkpointId, label) => {
434
+ if (
435
+ collHas(deltas, checkpointId) &&
436
+ mapGet(labels, checkpointId) !== label
437
+ ) {
438
+ mapSet(labels, checkpointId, label);
439
+ callListeners(checkpointListeners, [checkpointId]);
440
+ }
441
+ return checkpoints;
442
+ };
443
+ const getStore = () => store;
444
+ const getCheckpointIds = () => [[...backwardIds], currentId, [...forwardIds]];
445
+ const getCheckpoint = (checkpointId) => mapGet(labels, checkpointId);
446
+ const goBackward = () => {
447
+ goBackwardImpl();
448
+ callListenersIfChanged();
449
+ return checkpoints;
450
+ };
451
+ const goForward = () => {
452
+ goForwardImpl();
453
+ callListenersIfChanged();
454
+ return checkpoints;
455
+ };
456
+ const goTo = (checkpointId) => {
457
+ const action = arrayHas(backwardIds, checkpointId)
458
+ ? goBackwardImpl
459
+ : arrayHas(forwardIds, checkpointId)
460
+ ? goForwardImpl
461
+ : null;
462
+ while (!isUndefined(action) && checkpointId != currentId) {
463
+ action();
464
+ }
465
+ callListenersIfChanged();
466
+ return checkpoints;
467
+ };
468
+ const addCheckpointIdsListener = (listener) =>
469
+ addListener(listener, checkpointIdsListeners);
470
+ const addCheckpointListener = (checkpointId, listener) =>
471
+ addListener(listener, checkpointListeners, [checkpointId]);
472
+ const delListener = (listenerId2) => {
473
+ delListenerImpl(listenerId2);
474
+ return checkpoints;
475
+ };
476
+ const clear = () => {
477
+ clearCheckpointIds(backwardIds);
478
+ clearCheckpointIds(forwardIds);
479
+ if (!isUndefined(currentId)) {
480
+ clearCheckpointId(currentId);
481
+ }
482
+ currentId = void 0;
483
+ nextCheckpointId = 0;
484
+ addCheckpoint();
485
+ return checkpoints;
486
+ };
487
+ const destroy = () => {
488
+ store.delListener(listenerId);
489
+ };
490
+ const getListenerStats = () => ({
491
+ checkpointIds: collSize(checkpointIdsListeners),
492
+ checkpoint: collSize2(checkpointListeners),
493
+ });
494
+ const checkpoints = {
495
+ setSize,
496
+ addCheckpoint,
497
+ setCheckpoint,
498
+ getStore,
499
+ getCheckpointIds,
500
+ getCheckpoint,
501
+ goBackward,
502
+ goForward,
503
+ goTo,
504
+ addCheckpointIdsListener,
505
+ addCheckpointListener,
506
+ delListener,
507
+ clear,
508
+ destroy,
509
+ getListenerStats,
510
+ };
511
+ return objFreeze(checkpoints.clear());
512
+ });
513
+
514
+ const createIndexes = getCreateFunction((store) => {
515
+ const sliceIdsListeners = mapNew();
516
+ const sliceRowIdsListeners = mapNew();
517
+ const [
518
+ getStore,
519
+ getIndexIds,
520
+ getTableId,
521
+ getIndex,
522
+ setIndex,
523
+ setDefinition,
524
+ delDefinition,
525
+ destroy,
526
+ ] = getDefinableFunctions(store, mapNew, (value) =>
527
+ isUndefined(value) ? EMPTY_STRING : value + EMPTY_STRING,
528
+ );
529
+ const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
530
+ () => indexes,
531
+ );
532
+ const setIndexDefinition = (
533
+ indexId,
534
+ tableId,
535
+ getSliceId,
536
+ getSortKey,
537
+ sliceIdSorter,
538
+ rowIdSorter = defaultSorter,
539
+ ) => {
540
+ const sliceIdArraySorter = isUndefined(sliceIdSorter)
541
+ ? void 0
542
+ : ([id1], [id2]) => sliceIdSorter(id1, id2);
543
+ setDefinition(
544
+ indexId,
545
+ tableId,
546
+ (change, changedSliceIds, changedSortKeys, sliceIds, sortKeys) => {
547
+ let sliceIdsChanged = 0;
548
+ const changedSlices = setNew();
549
+ const unsortedSlices = setNew();
550
+ const index = getIndex(indexId);
551
+ collForEach(changedSliceIds, ([oldSliceId, newSliceId], rowId) => {
552
+ if (!isUndefined(oldSliceId)) {
553
+ setAdd(changedSlices, oldSliceId);
554
+ ifNotUndefined(mapGet(index, oldSliceId), (oldSlice) => {
555
+ collDel(oldSlice, rowId);
556
+ if (collIsEmpty(oldSlice)) {
557
+ mapSet(index, oldSliceId);
558
+ sliceIdsChanged = 1;
559
+ }
560
+ });
561
+ }
562
+ if (!isUndefined(newSliceId)) {
563
+ setAdd(changedSlices, newSliceId);
564
+ if (!collHas(index, newSliceId)) {
565
+ mapSet(index, newSliceId, setNew());
566
+ sliceIdsChanged = 1;
567
+ }
568
+ setAdd(mapGet(index, newSliceId), rowId);
569
+ if (!isUndefined(getSortKey)) {
570
+ setAdd(unsortedSlices, newSliceId);
571
+ }
572
+ }
573
+ });
574
+ change();
575
+ mapForEach(changedSortKeys, (rowId) =>
576
+ ifNotUndefined(mapGet(sliceIds, rowId), (sliceId) =>
577
+ setAdd(unsortedSlices, sliceId),
578
+ ),
579
+ );
580
+ if (!collIsEmpty(sortKeys)) {
581
+ collForEach(unsortedSlices, (sliceId) => {
582
+ const rowIdArraySorter = (rowId1, rowId2) =>
583
+ rowIdSorter(
584
+ mapGet(sortKeys, rowId1),
585
+ mapGet(sortKeys, rowId2),
586
+ sliceId,
587
+ );
588
+ const sliceArray = [...mapGet(index, sliceId)];
589
+ if (!arrayIsSorted(sliceArray, rowIdArraySorter)) {
590
+ mapSet(
591
+ index,
592
+ sliceId,
593
+ setNew(arraySort(sliceArray, rowIdArraySorter)),
594
+ );
595
+ setAdd(changedSlices, sliceId);
596
+ }
597
+ });
598
+ }
599
+ if (sliceIdsChanged) {
600
+ if (!isUndefined(sliceIdArraySorter)) {
601
+ const indexArray = [...index];
602
+ if (!arrayIsSorted(indexArray, sliceIdArraySorter)) {
603
+ setIndex(
604
+ indexId,
605
+ mapNew(arraySort(indexArray, sliceIdArraySorter)),
606
+ );
607
+ }
608
+ }
609
+ callListeners(sliceIdsListeners, [indexId]);
610
+ }
611
+ collForEach(changedSlices, (sliceId) =>
612
+ callListeners(sliceRowIdsListeners, [indexId, sliceId]),
613
+ );
614
+ },
615
+ getRowCellFunction(getSliceId),
616
+ ifNotUndefined(getSortKey, getRowCellFunction),
617
+ );
618
+ return indexes;
619
+ };
620
+ const delIndexDefinition = (indexId) => {
621
+ delDefinition(indexId);
622
+ return indexes;
623
+ };
624
+ const getSliceIds = (indexId) => mapKeys(getIndex(indexId));
625
+ const getSliceRowIds = (indexId, sliceId) =>
626
+ collValues(mapGet(getIndex(indexId), sliceId));
627
+ const addSliceIdsListener = (indexId, listener) =>
628
+ addListener(listener, sliceIdsListeners, [indexId]);
629
+ const addSliceRowIdsListener = (indexId, sliceId, listener) =>
630
+ addListener(listener, sliceRowIdsListeners, [indexId, sliceId]);
631
+ const delListener = (listenerId) => {
632
+ delListenerImpl(listenerId);
633
+ return indexes;
634
+ };
635
+ const getListenerStats = () => ({
636
+ sliceIds: collSize2(sliceIdsListeners),
637
+ sliceRowIds: collSize3(sliceRowIdsListeners),
638
+ });
639
+ const indexes = {
640
+ setIndexDefinition,
641
+ delIndexDefinition,
642
+ getStore,
643
+ getIndexIds,
644
+ getTableId,
645
+ getSliceIds,
646
+ getSliceRowIds,
647
+ addSliceIdsListener,
648
+ addSliceRowIdsListener,
649
+ delListener,
650
+ destroy,
651
+ getListenerStats,
652
+ };
653
+ return objFreeze(indexes);
654
+ });
655
+ const defaultSorter = (sortKey1, sortKey2) => (sortKey1 < sortKey2 ? -1 : 1);
656
+
657
+ const aggregators = mapNew([
658
+ [
659
+ AVG,
660
+ [
661
+ (numbers, length) => arraySum(numbers) / length,
662
+ (metric, add, length) => metric + (add - metric) / (length + 1),
663
+ (metric, remove, length) => metric + (metric - remove) / (length - 1),
664
+ (metric, add, remove, length) => metric + (add - remove) / length,
665
+ ],
666
+ ],
667
+ [
668
+ MAX,
669
+ [
670
+ (numbers) => mathMax(...numbers),
671
+ (metric, add) => mathMax(add, metric),
672
+ (metric, remove) => (remove == metric ? void 0 : metric),
673
+ (metric, add, remove) =>
674
+ remove == metric ? void 0 : mathMax(add, metric),
675
+ ],
676
+ ],
677
+ [
678
+ MIN,
679
+ [
680
+ (numbers) => mathMin(...numbers),
681
+ (metric, add) => mathMin(add, metric),
682
+ (metric, remove) => (remove == metric ? void 0 : metric),
683
+ (metric, add, remove) =>
684
+ remove == metric ? void 0 : mathMin(add, metric),
685
+ ],
686
+ ],
687
+ [
688
+ SUM,
689
+ [
690
+ (numbers) => arraySum(numbers),
691
+ (metric, add) => metric + add,
692
+ (metric, remove) => metric - remove,
693
+ (metric, add, remove) => metric - remove + add,
694
+ ],
695
+ ],
696
+ ]);
697
+ const createMetrics = getCreateFunction((store) => {
698
+ const metricListeners = mapNew();
699
+ const [
700
+ getStore,
701
+ getMetricIds,
702
+ getTableId,
703
+ getMetric,
704
+ setMetric,
705
+ setDefinition,
706
+ delDefinition,
707
+ destroy,
708
+ ] = getDefinableFunctions(store, getUndefined, (value) =>
709
+ isNaN(value) ||
710
+ isUndefined(value) ||
711
+ value === true ||
712
+ value === false ||
713
+ value === EMPTY_STRING
714
+ ? void 0
715
+ : value * 1,
716
+ );
717
+ const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
718
+ () => metrics,
719
+ );
720
+ const setMetricDefinition = (
721
+ metricId,
722
+ tableId,
723
+ aggregate,
724
+ getNumber,
725
+ aggregateAdd,
726
+ aggregateRemove,
727
+ aggregateReplace,
728
+ ) => {
729
+ const metricAggregators = isFunction(aggregate)
730
+ ? [aggregate, aggregateAdd, aggregateRemove, aggregateReplace]
731
+ : mapGet(aggregators, aggregate) ?? mapGet(aggregators, SUM);
732
+ setDefinition(
733
+ metricId,
734
+ tableId,
735
+ (change, changedNumbers, _changedSortKeys, numbers, _sortKeys, force) => {
736
+ let newMetric = getMetric(metricId);
737
+ let length = collSize(numbers);
738
+ const [aggregate2, aggregateAdd2, aggregateRemove2, aggregateReplace2] =
739
+ metricAggregators;
740
+ force = force || isUndefined(newMetric);
741
+ collForEach(changedNumbers, ([oldNumber, newNumber]) => {
742
+ if (!force) {
743
+ newMetric = isUndefined(oldNumber)
744
+ ? aggregateAdd2?.(newMetric, newNumber, length++)
745
+ : isUndefined(newNumber)
746
+ ? aggregateRemove2?.(newMetric, oldNumber, length--)
747
+ : aggregateReplace2?.(newMetric, newNumber, oldNumber, length);
748
+ }
749
+ force = force || isUndefined(newMetric);
750
+ });
751
+ change();
752
+ if (collIsEmpty(numbers)) {
753
+ newMetric = void 0;
754
+ } else if (force) {
755
+ newMetric = aggregate2(collValues(numbers), collSize(numbers));
756
+ }
757
+ if (!isFiniteNumber(newMetric)) {
758
+ newMetric = void 0;
759
+ }
760
+ const oldMetric = getMetric(metricId);
761
+ if (newMetric != oldMetric) {
762
+ setMetric(metricId, newMetric);
763
+ callListeners(metricListeners, [metricId], newMetric, oldMetric);
764
+ }
765
+ },
766
+ getRowCellFunction(getNumber, 1),
767
+ );
768
+ return metrics;
769
+ };
770
+ const delMetricDefinition = (metricId) => {
771
+ delDefinition(metricId);
772
+ return metrics;
773
+ };
774
+ const addMetricListener = (metricId, listener) =>
775
+ addListener(listener, metricListeners, [metricId]);
776
+ const delListener = (listenerId) => {
777
+ delListenerImpl(listenerId);
778
+ return metrics;
779
+ };
780
+ const getListenerStats = () => ({metric: collSize2(metricListeners)});
781
+ const metrics = {
782
+ setMetricDefinition,
783
+ delMetricDefinition,
784
+ getStore,
785
+ getMetricIds,
786
+ getTableId,
787
+ getMetric,
788
+ addMetricListener,
789
+ delListener,
790
+ destroy,
791
+ getListenerStats,
792
+ };
793
+ return objFreeze(metrics);
794
+ });
795
+
796
+ const createCustomPersister = (
797
+ store,
798
+ getPersisted,
799
+ setPersisted,
800
+ startListeningToPersisted,
801
+ stopListeningToPersisted,
802
+ ) => {
803
+ let tablesListenerId;
804
+ let loadSave = 0;
805
+ let loads = 0;
806
+ let saves = 0;
807
+ const persister = {
808
+ load: async (initialTables) => {
809
+ /* istanbul ignore else */
810
+ if (loadSave != 2) {
811
+ loadSave = 1;
812
+ {
813
+ loads++;
814
+ }
815
+ const body = await getPersisted();
816
+ if (!isUndefined(body) && body != '') {
817
+ store.setJson(body);
818
+ } else {
819
+ store.setTables(initialTables);
820
+ }
821
+ loadSave = 0;
822
+ }
823
+ return persister;
824
+ },
825
+ startAutoLoad: async (initialTables) => {
826
+ persister.stopAutoLoad();
827
+ await persister.load(initialTables);
828
+ startListeningToPersisted(persister.load);
829
+ return persister;
830
+ },
831
+ stopAutoLoad: () => {
832
+ stopListeningToPersisted();
833
+ return persister;
834
+ },
835
+ save: async () => {
836
+ /* istanbul ignore else */
837
+ if (loadSave != 1) {
838
+ loadSave = 2;
839
+ {
840
+ saves++;
841
+ }
842
+ await setPersisted(store.getJson());
843
+ loadSave = 0;
844
+ }
845
+ return persister;
846
+ },
847
+ startAutoSave: async () => {
848
+ await persister.stopAutoSave().save();
849
+ tablesListenerId = store.addTablesListener(() => persister.save());
850
+ return persister;
851
+ },
852
+ stopAutoSave: () => {
853
+ ifNotUndefined(tablesListenerId, store.delListener);
854
+ return persister;
855
+ },
856
+ getStore: () => store,
857
+ destroy: () => persister.stopAutoLoad().stopAutoSave(),
858
+ getStats: () => ({loads, saves}),
859
+ };
860
+ return objFreeze(persister);
861
+ };
862
+
863
+ const STORAGE = 'storage';
864
+ const WINDOW = window;
865
+ const getStoragePersister = (store, storageName, storage) => {
866
+ let listener;
867
+ const getPersisted = async () => storage.getItem(storageName);
868
+ const setPersisted = async (json) => storage.setItem(storageName, json);
869
+ const startListeningToPersisted = (didChange) => {
870
+ listener = (event) => {
871
+ if (event.storageArea === storage && event.key === storageName) {
872
+ didChange();
873
+ }
874
+ };
875
+ WINDOW.addEventListener(STORAGE, listener);
876
+ };
877
+ const stopListeningToPersisted = () => {
878
+ WINDOW.removeEventListener(STORAGE, listener);
879
+ listener = void 0;
880
+ };
881
+ return createCustomPersister(
882
+ store,
883
+ getPersisted,
884
+ setPersisted,
885
+ startListeningToPersisted,
886
+ stopListeningToPersisted,
887
+ );
888
+ };
889
+ const createLocalPersister = (store, storageName) =>
890
+ getStoragePersister(store, storageName, localStorage);
891
+ const createSessionPersister = (store, storageName) =>
892
+ getStoragePersister(store, storageName, sessionStorage);
893
+
894
+ const createFilePersister = (store, filePath) => {
895
+ let watcher;
896
+ const getPersisted = async () => {
897
+ try {
898
+ return await promises.readFile(filePath, UTF8);
899
+ } catch {}
900
+ };
901
+ const setPersisted = async (json) => {
902
+ try {
903
+ await promises.writeFile(filePath, json, UTF8);
904
+ } catch {}
905
+ };
906
+ const startListeningToPersisted = (didChange) => {
907
+ watcher = watch(filePath, didChange);
908
+ };
909
+ const stopListeningToPersisted = () => {
910
+ watcher?.close();
911
+ watcher = void 0;
912
+ };
913
+ return createCustomPersister(
914
+ store,
915
+ getPersisted,
916
+ setPersisted,
917
+ startListeningToPersisted,
918
+ stopListeningToPersisted,
919
+ );
920
+ };
921
+
922
+ const getETag = (response) => response.headers.get('ETag');
923
+ const createRemotePersister = (
924
+ store,
925
+ loadUrl,
926
+ saveUrl,
927
+ autoLoadIntervalSeconds,
928
+ ) => {
929
+ let interval;
930
+ let lastEtag;
931
+ const getPersisted = async () => {
932
+ const response = await fetch(loadUrl);
933
+ lastEtag = getETag(response);
934
+ return response.text();
935
+ };
936
+ const setPersisted = async (json) =>
937
+ await fetch(saveUrl, {
938
+ method: 'POST',
939
+ headers: {'Content-Type': 'application/json'},
940
+ body: json,
941
+ });
942
+ const startListeningToPersisted = (didChange) => {
943
+ interval = setInterval(async () => {
944
+ const response = await fetch(loadUrl, {method: 'HEAD'});
945
+ const currentEtag = getETag(response);
946
+ if (
947
+ !isUndefined(lastEtag) &&
948
+ !isUndefined(currentEtag) &&
949
+ currentEtag != lastEtag
950
+ ) {
951
+ lastEtag = currentEtag;
952
+ didChange();
953
+ }
954
+ }, autoLoadIntervalSeconds * 1e3);
955
+ };
956
+ const stopListeningToPersisted = () => {
957
+ ifNotUndefined(interval, clearInterval);
958
+ interval = void 0;
959
+ };
960
+ return createCustomPersister(
961
+ store,
962
+ getPersisted,
963
+ setPersisted,
964
+ startListeningToPersisted,
965
+ stopListeningToPersisted,
966
+ );
967
+ };
968
+
969
+ const {createContext, useContext} = React;
970
+ const Context = createContext([]);
971
+ const useThing = (id, offset) => {
972
+ const thingsAndThingsById = useContext(Context);
973
+ return isUndefined(id)
974
+ ? thingsAndThingsById[offset]
975
+ : objGet(thingsAndThingsById[offset + 1], id);
976
+ };
977
+ const useThingOrThingId = (thingOrThingId, offset) => {
978
+ const thing = useThing(thingOrThingId, offset);
979
+ if (isUndefined(thingOrThingId) || isString(thingOrThingId)) {
980
+ return thing;
981
+ }
982
+ return thingOrThingId;
983
+ };
984
+ const useStore = (id) => useThing(id, 0);
985
+ const useMetrics = (id) => useThing(id, 2);
986
+ const useIndexes = (id) => useThing(id, 4);
987
+ const useRelationships = (id) => useThing(id, 6);
988
+ const useCheckpoints = (id) => useThing(id, 8);
989
+ const useStoreOrStoreId = (storeOrStoreId) =>
990
+ useThingOrThingId(storeOrStoreId, 0);
991
+ const useMetricsOrMetricsId = (metricsOrMetricsId) =>
992
+ useThingOrThingId(metricsOrMetricsId, 2);
993
+ const useIndexesOrIndexesId = (indexesOrIndexesId) =>
994
+ useThingOrThingId(indexesOrIndexesId, 4);
995
+ const useRelationshipsOrRelationshipsId = (relationshipsOrRelationshipsId) =>
996
+ useThingOrThingId(relationshipsOrRelationshipsId, 6);
997
+ const useCheckpointsOrCheckpointsId = (checkpointsOrCheckpointsId) =>
998
+ useThingOrThingId(checkpointsOrCheckpointsId, 8);
999
+
1000
+ const {useCallback, useEffect, useMemo: useMemo$1, useState} = React;
1001
+ const useCreate = (store, create, createDeps = []) =>
1002
+ useMemo$1(() => create(store), [store, ...createDeps]);
1003
+ const useListenable = (listenable, thing, defaulted, ...args) => {
1004
+ const getListenable = thing?.['get' + listenable] ?? (() => defaulted);
1005
+ const immediateListenable = getListenable(...args);
1006
+ const [, setListenable] = useState(immediateListenable);
1007
+ useEffect(() => {
1008
+ const listenerId = thing?.[`add${listenable}Listener`]?.(
1009
+ ...args,
1010
+ () => setListenable(getListenable(...args)),
1011
+ false,
1012
+ );
1013
+ return () => thing?.delListener(listenerId);
1014
+ }, [thing, listenable, setListenable, getListenable, ...args]);
1015
+ return immediateListenable;
1016
+ };
1017
+ const useListener = (
1018
+ listenable,
1019
+ thing,
1020
+ listener,
1021
+ listenerDeps = [],
1022
+ mutator,
1023
+ ...args
1024
+ ) => {
1025
+ useEffect(() => {
1026
+ const listenerId = thing?.[`add${listenable}Listener`]?.(
1027
+ ...args,
1028
+ listener,
1029
+ mutator,
1030
+ );
1031
+ return () => thing?.delListener(listenerId);
1032
+ }, [thing, listenable, ...listenerDeps, mutator, ...args]);
1033
+ };
1034
+ const useSetCallback = (
1035
+ storeOrStoreId,
1036
+ settable,
1037
+ get,
1038
+ getDeps = [],
1039
+ then = getUndefined,
1040
+ thenDeps = [],
1041
+ ...args
1042
+ ) => {
1043
+ const store = useStoreOrStoreId(storeOrStoreId);
1044
+ return useCallback(
1045
+ (parameter) =>
1046
+ ifNotUndefined(store, (store2) =>
1047
+ ifNotUndefined(get(parameter, store2), (value) =>
1048
+ then(store2['set' + settable](...args, value), value),
1049
+ ),
1050
+ ),
1051
+ [store, settable, ...getDeps, ...thenDeps, ...args],
1052
+ );
1053
+ };
1054
+ const useDel = (
1055
+ storeOrStoreId,
1056
+ deletable,
1057
+ then = getUndefined,
1058
+ thenDeps = [],
1059
+ ...args
1060
+ ) => {
1061
+ const store = useStoreOrStoreId(storeOrStoreId);
1062
+ return useCallback(
1063
+ () => then(store?.['del' + deletable](...args)),
1064
+ [store, deletable, ...thenDeps, ...args],
1065
+ );
1066
+ };
1067
+ const useCheckpointAction = (checkpointsOrCheckpointsId, action, arg) => {
1068
+ const checkpoints = useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId);
1069
+ return useCallback(
1070
+ () => checkpoints?.[action](arg),
1071
+ [checkpoints, action, arg],
1072
+ );
1073
+ };
1074
+ const useCreateStore = (create, createDeps = []) =>
1075
+ useMemo$1(create, createDeps);
1076
+ const useTables = (storeOrStoreId) =>
1077
+ useListenable('Tables', useStoreOrStoreId(storeOrStoreId), {});
1078
+ const useTableIds = (storeOrStoreId) =>
1079
+ useListenable('TableIds', useStoreOrStoreId(storeOrStoreId), []);
1080
+ const useTable = (tableId, storeOrStoreId) =>
1081
+ useListenable('Table', useStoreOrStoreId(storeOrStoreId), {}, tableId);
1082
+ const useRowIds = (tableId, storeOrStoreId) =>
1083
+ useListenable('RowIds', useStoreOrStoreId(storeOrStoreId), [], tableId);
1084
+ const useRow = (tableId, rowId, storeOrStoreId) =>
1085
+ useListenable('Row', useStoreOrStoreId(storeOrStoreId), {}, tableId, rowId);
1086
+ const useCellIds = (tableId, rowId, storeOrStoreId) =>
1087
+ useListenable(
1088
+ 'CellIds',
1089
+ useStoreOrStoreId(storeOrStoreId),
1090
+ [],
1091
+ tableId,
1092
+ rowId,
1093
+ );
1094
+ const useCell = (tableId, rowId, cellId, storeOrStoreId) =>
1095
+ useListenable(
1096
+ 'Cell',
1097
+ useStoreOrStoreId(storeOrStoreId),
1098
+ void 0,
1099
+ tableId,
1100
+ rowId,
1101
+ cellId,
1102
+ );
1103
+ const useSetTablesCallback = (
1104
+ getTables,
1105
+ getTablesDeps,
1106
+ storeOrStoreId,
1107
+ then,
1108
+ thenDeps,
1109
+ ) =>
1110
+ useSetCallback(
1111
+ storeOrStoreId,
1112
+ 'Tables',
1113
+ getTables,
1114
+ getTablesDeps,
1115
+ then,
1116
+ thenDeps,
1117
+ );
1118
+ const useSetTableCallback = (
1119
+ tableId,
1120
+ getTable,
1121
+ getTableDeps,
1122
+ storeOrStoreId,
1123
+ then,
1124
+ thenDeps,
1125
+ ) =>
1126
+ useSetCallback(
1127
+ storeOrStoreId,
1128
+ 'Table',
1129
+ getTable,
1130
+ getTableDeps,
1131
+ then,
1132
+ thenDeps,
1133
+ tableId,
1134
+ );
1135
+ const useSetRowCallback = (
1136
+ tableId,
1137
+ rowId,
1138
+ getRow,
1139
+ getRowDeps,
1140
+ storeOrStoreId,
1141
+ then,
1142
+ thenDeps,
1143
+ ) =>
1144
+ useSetCallback(
1145
+ storeOrStoreId,
1146
+ 'Row',
1147
+ getRow,
1148
+ getRowDeps,
1149
+ then,
1150
+ thenDeps,
1151
+ tableId,
1152
+ rowId,
1153
+ );
1154
+ const useAddRowCallback = (
1155
+ tableId,
1156
+ getRow,
1157
+ getRowDeps = [],
1158
+ storeOrStoreId,
1159
+ then = getUndefined,
1160
+ thenDeps = [],
1161
+ ) => {
1162
+ const store = useStoreOrStoreId(storeOrStoreId);
1163
+ return useCallback(
1164
+ (parameter) =>
1165
+ ifNotUndefined(store, (store2) =>
1166
+ ifNotUndefined(getRow(parameter, store2), (row) =>
1167
+ then(store2.addRow(tableId, row), store2, row),
1168
+ ),
1169
+ ),
1170
+ [store, tableId, ...getRowDeps, ...thenDeps],
1171
+ );
1172
+ };
1173
+ const useSetPartialRowCallback = (
1174
+ tableId,
1175
+ rowId,
1176
+ getPartialRow,
1177
+ getPartialRowDeps,
1178
+ storeOrStoreId,
1179
+ then,
1180
+ thenDeps,
1181
+ ) =>
1182
+ useSetCallback(
1183
+ storeOrStoreId,
1184
+ 'PartialRow',
1185
+ getPartialRow,
1186
+ getPartialRowDeps,
1187
+ then,
1188
+ thenDeps,
1189
+ tableId,
1190
+ rowId,
1191
+ );
1192
+ const useSetCellCallback = (
1193
+ tableId,
1194
+ rowId,
1195
+ cellId,
1196
+ getCell,
1197
+ getCellDeps,
1198
+ storeOrStoreId,
1199
+ then,
1200
+ thenDeps,
1201
+ ) =>
1202
+ useSetCallback(
1203
+ storeOrStoreId,
1204
+ 'Cell',
1205
+ getCell,
1206
+ getCellDeps,
1207
+ then,
1208
+ thenDeps,
1209
+ tableId,
1210
+ rowId,
1211
+ cellId,
1212
+ );
1213
+ const useDelTablesCallback = (storeOrStoreId, then, thenDeps) =>
1214
+ useDel(storeOrStoreId, 'Tables', then, thenDeps);
1215
+ const useDelTableCallback = (tableId, storeOrStoreId, then, thenDeps) =>
1216
+ useDel(storeOrStoreId, 'Table', then, thenDeps, tableId);
1217
+ const useDelRowCallback = (tableId, rowId, storeOrStoreId, then, thenDeps) =>
1218
+ useDel(storeOrStoreId, 'Row', then, thenDeps, tableId, rowId);
1219
+ const useDelCellCallback = (
1220
+ tableId,
1221
+ rowId,
1222
+ cellId,
1223
+ forceDel,
1224
+ storeOrStoreId,
1225
+ then,
1226
+ thenDeps,
1227
+ ) =>
1228
+ useDel(
1229
+ storeOrStoreId,
1230
+ 'Cell',
1231
+ then,
1232
+ thenDeps,
1233
+ tableId,
1234
+ rowId,
1235
+ cellId,
1236
+ forceDel,
1237
+ );
1238
+ const useTablesListener = (listener, listenerDeps, mutator, storeOrStoreId) =>
1239
+ useListener(
1240
+ 'Tables',
1241
+ useStoreOrStoreId(storeOrStoreId),
1242
+ listener,
1243
+ listenerDeps,
1244
+ mutator,
1245
+ );
1246
+ const useTableIdsListener = (listener, listenerDeps, mutator, storeOrStoreId) =>
1247
+ useListener(
1248
+ 'TableIds',
1249
+ useStoreOrStoreId(storeOrStoreId),
1250
+ listener,
1251
+ listenerDeps,
1252
+ mutator,
1253
+ );
1254
+ const useTableListener = (
1255
+ tableId,
1256
+ listener,
1257
+ listenerDeps,
1258
+ mutator,
1259
+ storeOrStoreId,
1260
+ ) =>
1261
+ useListener(
1262
+ 'Table',
1263
+ useStoreOrStoreId(storeOrStoreId),
1264
+ listener,
1265
+ listenerDeps,
1266
+ mutator,
1267
+ tableId,
1268
+ );
1269
+ const useRowIdsListener = (
1270
+ tableId,
1271
+ listener,
1272
+ listenerDeps,
1273
+ mutator,
1274
+ storeOrStoreId,
1275
+ ) =>
1276
+ useListener(
1277
+ 'RowIds',
1278
+ useStoreOrStoreId(storeOrStoreId),
1279
+ listener,
1280
+ listenerDeps,
1281
+ mutator,
1282
+ tableId,
1283
+ );
1284
+ const useRowListener = (
1285
+ tableId,
1286
+ rowId,
1287
+ listener,
1288
+ listenerDeps,
1289
+ mutator,
1290
+ storeOrStoreId,
1291
+ ) =>
1292
+ useListener(
1293
+ 'Row',
1294
+ useStoreOrStoreId(storeOrStoreId),
1295
+ listener,
1296
+ listenerDeps,
1297
+ mutator,
1298
+ tableId,
1299
+ rowId,
1300
+ );
1301
+ const useCellIdsListener = (
1302
+ tableId,
1303
+ rowId,
1304
+ listener,
1305
+ listenerDeps,
1306
+ mutator,
1307
+ storeOrStoreId,
1308
+ ) =>
1309
+ useListener(
1310
+ 'CellIds',
1311
+ useStoreOrStoreId(storeOrStoreId),
1312
+ listener,
1313
+ listenerDeps,
1314
+ mutator,
1315
+ tableId,
1316
+ rowId,
1317
+ );
1318
+ const useCellListener = (
1319
+ tableId,
1320
+ rowId,
1321
+ cellId,
1322
+ listener,
1323
+ listenerDeps,
1324
+ mutator,
1325
+ storeOrStoreId,
1326
+ ) =>
1327
+ useListener(
1328
+ 'Cell',
1329
+ useStoreOrStoreId(storeOrStoreId),
1330
+ listener,
1331
+ listenerDeps,
1332
+ mutator,
1333
+ tableId,
1334
+ rowId,
1335
+ cellId,
1336
+ );
1337
+ const useCreateMetrics = (store, create, createDeps) =>
1338
+ useCreate(store, create, createDeps);
1339
+ const useMetric = (metricId, metricsOrMetricsId) =>
1340
+ useListenable(
1341
+ 'Metric',
1342
+ useMetricsOrMetricsId(metricsOrMetricsId),
1343
+ void 0,
1344
+ metricId,
1345
+ );
1346
+ const useMetricListener = (
1347
+ metricId,
1348
+ listener,
1349
+ listenerDeps,
1350
+ metricsOrMetricsId,
1351
+ ) =>
1352
+ useListener(
1353
+ 'Metric',
1354
+ useMetricsOrMetricsId(metricsOrMetricsId),
1355
+ listener,
1356
+ listenerDeps,
1357
+ void 0,
1358
+ metricId,
1359
+ );
1360
+ const useCreateIndexes = (store, create, createDeps) =>
1361
+ useCreate(store, create, createDeps);
1362
+ const useSliceIds = (indexId, indexesOrIndexesId) =>
1363
+ useListenable(
1364
+ 'SliceIds',
1365
+ useIndexesOrIndexesId(indexesOrIndexesId),
1366
+ [],
1367
+ indexId,
1368
+ );
1369
+ const useSliceRowIds = (indexId, sliceId, indexesOrIndexesId) =>
1370
+ useListenable(
1371
+ 'SliceRowIds',
1372
+ useIndexesOrIndexesId(indexesOrIndexesId),
1373
+ [],
1374
+ indexId,
1375
+ sliceId,
1376
+ );
1377
+ const useSliceIdsListener = (
1378
+ indexId,
1379
+ listener,
1380
+ listenerDeps,
1381
+ indexesOrIndexesId,
1382
+ ) =>
1383
+ useListener(
1384
+ 'SliceIds',
1385
+ useIndexesOrIndexesId(indexesOrIndexesId),
1386
+ listener,
1387
+ listenerDeps,
1388
+ void 0,
1389
+ indexId,
1390
+ );
1391
+ const useSliceRowIdsListener = (
1392
+ indexId,
1393
+ sliceId,
1394
+ listener,
1395
+ listenerDeps,
1396
+ indexesOrIndexesId,
1397
+ ) =>
1398
+ useListener(
1399
+ 'SliceRowIds',
1400
+ useIndexesOrIndexesId(indexesOrIndexesId),
1401
+ listener,
1402
+ listenerDeps,
1403
+ void 0,
1404
+ indexId,
1405
+ sliceId,
1406
+ );
1407
+ const useCreateRelationships = (store, create, createDeps) =>
1408
+ useCreate(store, create, createDeps);
1409
+ const useRemoteRowId = (
1410
+ relationshipId,
1411
+ localRowId,
1412
+ relationshipsOrRelationshipsId,
1413
+ ) =>
1414
+ useListenable(
1415
+ 'RemoteRowId',
1416
+ useRelationshipsOrRelationshipsId(relationshipsOrRelationshipsId),
1417
+ void 0,
1418
+ relationshipId,
1419
+ localRowId,
1420
+ );
1421
+ const useLocalRowIds = (
1422
+ relationshipId,
1423
+ remoteRowId,
1424
+ relationshipsOrRelationshipsId,
1425
+ ) =>
1426
+ useListenable(
1427
+ 'LocalRowIds',
1428
+ useRelationshipsOrRelationshipsId(relationshipsOrRelationshipsId),
1429
+ [],
1430
+ relationshipId,
1431
+ remoteRowId,
1432
+ );
1433
+ const useLinkedRowIds = (
1434
+ relationshipId,
1435
+ firstRowId,
1436
+ relationshipsOrRelationshipsId,
1437
+ ) =>
1438
+ useListenable(
1439
+ 'LinkedRowIds',
1440
+ useRelationshipsOrRelationshipsId(relationshipsOrRelationshipsId),
1441
+ [],
1442
+ relationshipId,
1443
+ firstRowId,
1444
+ );
1445
+ const useRemoteRowIdListener = (
1446
+ relationshipId,
1447
+ localRowId,
1448
+ listener,
1449
+ listenerDeps,
1450
+ relationshipsOrRelationshipsId,
1451
+ ) =>
1452
+ useListener(
1453
+ 'RemoteRowId',
1454
+ useRelationshipsOrRelationshipsId(relationshipsOrRelationshipsId),
1455
+ listener,
1456
+ listenerDeps,
1457
+ void 0,
1458
+ relationshipId,
1459
+ localRowId,
1460
+ );
1461
+ const useLocalRowIdsListener = (
1462
+ relationshipId,
1463
+ remoteRowId,
1464
+ listener,
1465
+ listenerDeps,
1466
+ relationshipsOrRelationshipsId,
1467
+ ) =>
1468
+ useListener(
1469
+ 'LocalRowIds',
1470
+ useRelationshipsOrRelationshipsId(relationshipsOrRelationshipsId),
1471
+ listener,
1472
+ listenerDeps,
1473
+ void 0,
1474
+ relationshipId,
1475
+ remoteRowId,
1476
+ );
1477
+ const useLinkedRowIdsListener = (
1478
+ relationshipId,
1479
+ firstRowId,
1480
+ listener,
1481
+ listenerDeps,
1482
+ relationshipsOrRelationshipsId,
1483
+ ) =>
1484
+ useListener(
1485
+ 'LinkedRowIds',
1486
+ useRelationshipsOrRelationshipsId(relationshipsOrRelationshipsId),
1487
+ listener,
1488
+ listenerDeps,
1489
+ void 0,
1490
+ relationshipId,
1491
+ firstRowId,
1492
+ );
1493
+ const useCreateCheckpoints = (store, create, createDeps) =>
1494
+ useCreate(store, create, createDeps);
1495
+ const useCheckpointIds = (checkpointsOrCheckpointsId) =>
1496
+ useListenable(
1497
+ 'CheckpointIds',
1498
+ useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId),
1499
+ [[], void 0, []],
1500
+ );
1501
+ const useCheckpoint = (checkpointId, checkpointsOrCheckpointsId) =>
1502
+ useListenable(
1503
+ 'Checkpoint',
1504
+ useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId),
1505
+ void 0,
1506
+ checkpointId,
1507
+ );
1508
+ const useSetCheckpointCallback = (
1509
+ getCheckpoint = getUndefined,
1510
+ getCheckpointDeps = [],
1511
+ checkpointsOrCheckpointsId,
1512
+ then = getUndefined,
1513
+ thenDeps = [],
1514
+ ) => {
1515
+ const checkpoints = useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId);
1516
+ return useCallback(
1517
+ (parameter) =>
1518
+ ifNotUndefined(checkpoints, (checkpoints2) => {
1519
+ const label = getCheckpoint(parameter);
1520
+ then(checkpoints2.addCheckpoint(label), checkpoints2, label);
1521
+ }),
1522
+ [checkpoints, ...getCheckpointDeps, ...thenDeps],
1523
+ );
1524
+ };
1525
+ const useGoBackwardCallback = (checkpointsOrCheckpointsId) =>
1526
+ useCheckpointAction(checkpointsOrCheckpointsId, 'goBackward');
1527
+ const useGoForwardCallback = (checkpointsOrCheckpointsId) =>
1528
+ useCheckpointAction(checkpointsOrCheckpointsId, 'goForward');
1529
+ const useGoToCallback = (
1530
+ getCheckpointId,
1531
+ getCheckpointIdDeps = [],
1532
+ checkpointsOrCheckpointsId,
1533
+ then = getUndefined,
1534
+ thenDeps = [],
1535
+ ) => {
1536
+ const checkpoints = useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId);
1537
+ return useCallback(
1538
+ (parameter) =>
1539
+ ifNotUndefined(checkpoints, (checkpoints2) =>
1540
+ ifNotUndefined(getCheckpointId(parameter), (checkpointId) =>
1541
+ then(checkpoints2.goTo(checkpointId), checkpointId),
1542
+ ),
1543
+ ),
1544
+ [checkpoints, ...getCheckpointIdDeps, ...thenDeps],
1545
+ );
1546
+ };
1547
+ const useUndoInformation = (checkpointsOrCheckpointsId) => {
1548
+ const checkpoints = useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId);
1549
+ const [backwardIds, currentId] = useCheckpointIds(checkpoints);
1550
+ return [
1551
+ !arrayIsEmpty(backwardIds),
1552
+ useGoBackwardCallback(checkpoints),
1553
+ currentId,
1554
+ ifNotUndefined(currentId, (id) => checkpoints?.getCheckpoint(id)) ?? '',
1555
+ ];
1556
+ };
1557
+ const useRedoInformation = (checkpointsOrCheckpointsId) => {
1558
+ const checkpoints = useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId);
1559
+ const [, , [forwardId]] = useCheckpointIds(checkpoints);
1560
+ return [
1561
+ !isUndefined(forwardId),
1562
+ useGoForwardCallback(checkpoints),
1563
+ forwardId,
1564
+ ifNotUndefined(forwardId, (id) => checkpoints?.getCheckpoint(id)) ?? '',
1565
+ ];
1566
+ };
1567
+ const useCheckpointIdsListener = (
1568
+ listener,
1569
+ listenerDeps,
1570
+ checkpointsOrCheckpointsId,
1571
+ ) =>
1572
+ useListener(
1573
+ 'CheckpointIds',
1574
+ useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId),
1575
+ listener,
1576
+ listenerDeps,
1577
+ );
1578
+ const useCheckpointListener = (
1579
+ checkpointId,
1580
+ listener,
1581
+ listenerDeps,
1582
+ checkpointsOrCheckpointsId,
1583
+ ) =>
1584
+ useListener(
1585
+ 'Checkpoint',
1586
+ useCheckpointsOrCheckpointsId(checkpointsOrCheckpointsId),
1587
+ listener,
1588
+ listenerDeps,
1589
+ void 0,
1590
+ checkpointId,
1591
+ );
1592
+ const useCreatePersister = (
1593
+ store,
1594
+ create,
1595
+ createDeps = [],
1596
+ then,
1597
+ thenDeps = [],
1598
+ ) => {
1599
+ const [, setDone] = useState();
1600
+ const persister = useMemo$1(() => create(store), [store, ...createDeps]);
1601
+ useEffect(() => {
1602
+ (async () => {
1603
+ await then?.(persister);
1604
+ setDone(1);
1605
+ return;
1606
+ })();
1607
+ }, [persister, ...thenDeps]);
1608
+ return persister;
1609
+ };
1610
+
1611
+ const {createElement, useMemo} = React;
1612
+ const useRelationshipsStoreTableId = (relationships) => {
1613
+ const resolvedRelationships =
1614
+ useRelationshipsOrRelationshipsId(relationships);
1615
+ return [resolvedRelationships, resolvedRelationships?.getStore()];
1616
+ };
1617
+ const useComponentPerRow = (
1618
+ {
1619
+ relationshipId,
1620
+ relationships,
1621
+ rowComponent: Row = RowView,
1622
+ getRowComponentProps,
1623
+ separator,
1624
+ debugIds,
1625
+ },
1626
+ getRowIdsHook,
1627
+ rowId,
1628
+ ) => {
1629
+ const [resolvedRelationships, store] =
1630
+ useRelationshipsStoreTableId(relationships);
1631
+ const tableId = resolvedRelationships?.getLocalTableId(relationshipId);
1632
+ const rowIds = getRowIdsHook(relationshipId, rowId, resolvedRelationships);
1633
+ return wrap(
1634
+ arrayMap(rowIds, (rowId2) =>
1635
+ /* @__PURE__ */ createElement(Row, {
1636
+ ...getProps(getRowComponentProps, rowId2),
1637
+ key: rowId2,
1638
+ tableId,
1639
+ rowId: rowId2,
1640
+ store,
1641
+ debugIds,
1642
+ }),
1643
+ ),
1644
+ separator,
1645
+ debugIds,
1646
+ rowId,
1647
+ );
1648
+ };
1649
+ const getUseCheckpointView =
1650
+ (getCheckpoints) =>
1651
+ ({
1652
+ checkpoints,
1653
+ checkpointComponent: Checkpoint = CheckpointView,
1654
+ getCheckpointComponentProps,
1655
+ separator,
1656
+ debugIds,
1657
+ }) => {
1658
+ const resolvedCheckpoints = useCheckpointsOrCheckpointsId(checkpoints);
1659
+ return wrap(
1660
+ arrayMap(
1661
+ getCheckpoints(useCheckpointIds(resolvedCheckpoints)),
1662
+ (checkpointId) =>
1663
+ /* @__PURE__ */ createElement(Checkpoint, {
1664
+ ...getProps(getCheckpointComponentProps, checkpointId),
1665
+ key: checkpointId,
1666
+ checkpoints: resolvedCheckpoints,
1667
+ checkpointId,
1668
+ debugIds,
1669
+ }),
1670
+ ),
1671
+ separator,
1672
+ );
1673
+ };
1674
+ const getProps = (getProps2, id) =>
1675
+ isUndefined(getProps2) ? {} : getProps2(id);
1676
+ const Provider = ({
1677
+ store,
1678
+ storesById,
1679
+ metrics,
1680
+ metricsById,
1681
+ indexes,
1682
+ indexesById,
1683
+ relationships,
1684
+ relationshipsById,
1685
+ checkpoints,
1686
+ checkpointsById,
1687
+ children,
1688
+ }) =>
1689
+ /* @__PURE__ */ createElement(
1690
+ Context.Provider,
1691
+ {
1692
+ value: useMemo(
1693
+ () => [
1694
+ store,
1695
+ storesById,
1696
+ metrics,
1697
+ metricsById,
1698
+ indexes,
1699
+ indexesById,
1700
+ relationships,
1701
+ relationshipsById,
1702
+ checkpoints,
1703
+ checkpointsById,
1704
+ ],
1705
+ [
1706
+ store,
1707
+ storesById,
1708
+ metrics,
1709
+ metricsById,
1710
+ indexes,
1711
+ indexesById,
1712
+ relationships,
1713
+ relationshipsById,
1714
+ checkpoints,
1715
+ checkpointsById,
1716
+ ],
1717
+ ),
1718
+ },
1719
+ children,
1720
+ );
1721
+ const wrap = (children, separator, encloseWithId, id) => {
1722
+ const separatedChildren =
1723
+ isUndefined(separator) || !Array.isArray(children)
1724
+ ? children
1725
+ : arrayMap(children, (child, c) => (c > 0 ? [separator, child] : child));
1726
+ return encloseWithId ? [id, ':{', separatedChildren, '}'] : separatedChildren;
1727
+ };
1728
+ const CellView = ({tableId, rowId, cellId, store, debugIds}) =>
1729
+ wrap(
1730
+ EMPTY_STRING + (useCell(tableId, rowId, cellId, store) ?? EMPTY_STRING),
1731
+ void 0,
1732
+ debugIds,
1733
+ cellId,
1734
+ );
1735
+ const RowView = ({
1736
+ tableId,
1737
+ rowId,
1738
+ store,
1739
+ cellComponent: Cell = CellView,
1740
+ getCellComponentProps,
1741
+ separator,
1742
+ debugIds,
1743
+ }) =>
1744
+ wrap(
1745
+ arrayMap(useCellIds(tableId, rowId, store), (cellId) =>
1746
+ /* @__PURE__ */ createElement(Cell, {
1747
+ ...getProps(getCellComponentProps, cellId),
1748
+ key: cellId,
1749
+ tableId,
1750
+ rowId,
1751
+ cellId,
1752
+ store,
1753
+ debugIds,
1754
+ }),
1755
+ ),
1756
+ separator,
1757
+ debugIds,
1758
+ rowId,
1759
+ );
1760
+ const TableView = ({
1761
+ tableId,
1762
+ store,
1763
+ rowComponent: Row = RowView,
1764
+ getRowComponentProps,
1765
+ separator,
1766
+ debugIds,
1767
+ }) =>
1768
+ wrap(
1769
+ arrayMap(useRowIds(tableId, store), (rowId) =>
1770
+ /* @__PURE__ */ createElement(Row, {
1771
+ ...getProps(getRowComponentProps, rowId),
1772
+ key: rowId,
1773
+ tableId,
1774
+ rowId,
1775
+ store,
1776
+ debugIds,
1777
+ }),
1778
+ ),
1779
+ separator,
1780
+ debugIds,
1781
+ tableId,
1782
+ );
1783
+ const TablesView = ({
1784
+ store,
1785
+ tableComponent: Table = TableView,
1786
+ getTableComponentProps,
1787
+ separator,
1788
+ debugIds,
1789
+ }) =>
1790
+ wrap(
1791
+ arrayMap(useTableIds(store), (tableId) =>
1792
+ /* @__PURE__ */ createElement(Table, {
1793
+ ...getProps(getTableComponentProps, tableId),
1794
+ key: tableId,
1795
+ tableId,
1796
+ store,
1797
+ debugIds,
1798
+ }),
1799
+ ),
1800
+ separator,
1801
+ );
1802
+ const MetricView = ({metricId, metrics, debugIds}) =>
1803
+ wrap(
1804
+ useMetric(metricId, metrics) ?? EMPTY_STRING,
1805
+ void 0,
1806
+ debugIds,
1807
+ metricId,
1808
+ );
1809
+ const SliceView = ({
1810
+ indexId,
1811
+ sliceId,
1812
+ indexes,
1813
+ rowComponent: Row = RowView,
1814
+ getRowComponentProps,
1815
+ separator,
1816
+ debugIds,
1817
+ }) => {
1818
+ const resolvedIndexes = useIndexesOrIndexesId(indexes);
1819
+ const store = resolvedIndexes?.getStore();
1820
+ const tableId = resolvedIndexes?.getTableId(indexId);
1821
+ const rowIds = useSliceRowIds(indexId, sliceId, resolvedIndexes);
1822
+ return wrap(
1823
+ arrayMap(rowIds, (rowId) =>
1824
+ /* @__PURE__ */ createElement(Row, {
1825
+ ...getProps(getRowComponentProps, rowId),
1826
+ key: rowId,
1827
+ tableId,
1828
+ rowId,
1829
+ store,
1830
+ debugIds,
1831
+ }),
1832
+ ),
1833
+ separator,
1834
+ debugIds,
1835
+ sliceId,
1836
+ );
1837
+ };
1838
+ const IndexView = ({
1839
+ indexId,
1840
+ indexes,
1841
+ sliceComponent: Slice = SliceView,
1842
+ getSliceComponentProps,
1843
+ separator,
1844
+ debugIds,
1845
+ }) =>
1846
+ wrap(
1847
+ arrayMap(useSliceIds(indexId, indexes), (sliceId) =>
1848
+ /* @__PURE__ */ createElement(Slice, {
1849
+ ...getProps(getSliceComponentProps, sliceId),
1850
+ key: sliceId,
1851
+ indexId,
1852
+ sliceId,
1853
+ indexes,
1854
+ debugIds,
1855
+ }),
1856
+ ),
1857
+ separator,
1858
+ debugIds,
1859
+ indexId,
1860
+ );
1861
+ const RemoteRowView = ({
1862
+ relationshipId,
1863
+ localRowId,
1864
+ relationships,
1865
+ rowComponent: Row = RowView,
1866
+ getRowComponentProps,
1867
+ debugIds,
1868
+ }) => {
1869
+ const [resolvedRelationships, store] =
1870
+ useRelationshipsStoreTableId(relationships);
1871
+ const tableId = resolvedRelationships?.getRemoteTableId(relationshipId);
1872
+ const rowId = useRemoteRowId(
1873
+ relationshipId,
1874
+ localRowId,
1875
+ resolvedRelationships,
1876
+ );
1877
+ return wrap(
1878
+ isUndefined(tableId) || isUndefined(rowId)
1879
+ ? null
1880
+ : /* @__PURE__ */ createElement(Row, {
1881
+ ...getProps(getRowComponentProps, rowId),
1882
+ key: rowId,
1883
+ tableId,
1884
+ rowId,
1885
+ store,
1886
+ debugIds,
1887
+ }),
1888
+ void 0,
1889
+ debugIds,
1890
+ localRowId,
1891
+ );
1892
+ };
1893
+ const LocalRowsView = (props) =>
1894
+ useComponentPerRow(props, useLocalRowIds, props.remoteRowId);
1895
+ const LinkedRowsView = (props) =>
1896
+ useComponentPerRow(props, useLinkedRowIds, props.firstRowId);
1897
+ const CheckpointView = ({checkpoints, checkpointId, debugIds}) =>
1898
+ wrap(
1899
+ useCheckpoint(checkpointId, checkpoints) ?? '',
1900
+ void 0,
1901
+ debugIds,
1902
+ checkpointId,
1903
+ );
1904
+ const BackwardCheckpointsView = getUseCheckpointView(
1905
+ (checkpointIds) => checkpointIds[0],
1906
+ );
1907
+ const CurrentCheckpointView = getUseCheckpointView((checkpointIds) =>
1908
+ isUndefined(checkpointIds[1]) ? [] : [checkpointIds[1]],
1909
+ );
1910
+ const ForwardCheckpointsView = getUseCheckpointView(
1911
+ (checkpointIds) => checkpointIds[2],
1912
+ );
1913
+
1914
+ const createRelationships = getCreateFunction((store) => {
1915
+ const remoteTableIds = mapNew();
1916
+ const remoteRowIdListeners = mapNew();
1917
+ const localRowIdsListeners = mapNew();
1918
+ const linkedRowIdsListeners = mapNew();
1919
+ const [
1920
+ getStore,
1921
+ getRelationshipIds,
1922
+ getLocalTableId,
1923
+ getRelationship,
1924
+ _setRelationship,
1925
+ setDefinition,
1926
+ delDefinition,
1927
+ destroy,
1928
+ ] = getDefinableFunctions(
1929
+ store,
1930
+ () => [mapNew(), mapNew(), mapNew(), mapNew()],
1931
+ (value) => (isUndefined(value) ? void 0 : value + EMPTY_STRING),
1932
+ );
1933
+ const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
1934
+ () => relationships,
1935
+ );
1936
+ const getLinkedRowIdsCache = (relationshipId, firstRowId, skipCache) =>
1937
+ ifNotUndefined(
1938
+ getRelationship(relationshipId),
1939
+ ([remoteRows, , linkedRowsCache]) => {
1940
+ if (!collHas(linkedRowsCache, firstRowId)) {
1941
+ const linkedRows = setNew();
1942
+ if (
1943
+ getLocalTableId(relationshipId) != getRemoteTableId(relationshipId)
1944
+ ) {
1945
+ setAdd(linkedRows, firstRowId);
1946
+ } else {
1947
+ let rowId = firstRowId;
1948
+ while (!isUndefined(rowId) && !collHas(linkedRows, rowId)) {
1949
+ setAdd(linkedRows, rowId);
1950
+ rowId = mapGet(remoteRows, rowId);
1951
+ }
1952
+ }
1953
+ if (skipCache) {
1954
+ return linkedRows;
1955
+ }
1956
+ mapSet(linkedRowsCache, firstRowId, linkedRows);
1957
+ }
1958
+ return mapGet(linkedRowsCache, firstRowId);
1959
+ },
1960
+ );
1961
+ const delLinkedRowIdsCache = (relationshipId, firstRowId) =>
1962
+ ifNotUndefined(getRelationship(relationshipId), ([, , linkedRowsCache]) =>
1963
+ mapSet(linkedRowsCache, firstRowId),
1964
+ );
1965
+ const setRelationshipDefinition = (
1966
+ relationshipId,
1967
+ localTableId,
1968
+ remoteTableId,
1969
+ getRemoteRowId2,
1970
+ ) => {
1971
+ mapSet(remoteTableIds, relationshipId, remoteTableId);
1972
+ setDefinition(
1973
+ relationshipId,
1974
+ localTableId,
1975
+ (change, changedRemoteRowIds) => {
1976
+ const changedLocalRows = setNew();
1977
+ const changedRemoteRows = setNew();
1978
+ const changedLinkedRows = setNew();
1979
+ const [localRows, remoteRows] = getRelationship(relationshipId);
1980
+ collForEach(
1981
+ changedRemoteRowIds,
1982
+ ([oldRemoteRowId, newRemoteRowId], localRowId) => {
1983
+ if (!isUndefined(oldRemoteRowId)) {
1984
+ setAdd(changedRemoteRows, oldRemoteRowId);
1985
+ ifNotUndefined(
1986
+ mapGet(remoteRows, oldRemoteRowId),
1987
+ (oldRemoteRow) => {
1988
+ collDel(oldRemoteRow, localRowId);
1989
+ if (collIsEmpty(oldRemoteRow)) {
1990
+ mapSet(remoteRows, oldRemoteRowId);
1991
+ }
1992
+ },
1993
+ );
1994
+ }
1995
+ if (!isUndefined(newRemoteRowId)) {
1996
+ setAdd(changedRemoteRows, newRemoteRowId);
1997
+ if (!collHas(remoteRows, newRemoteRowId)) {
1998
+ mapSet(remoteRows, newRemoteRowId, setNew());
1999
+ }
2000
+ setAdd(mapGet(remoteRows, newRemoteRowId), localRowId);
2001
+ }
2002
+ setAdd(changedLocalRows, localRowId);
2003
+ mapSet(localRows, localRowId, newRemoteRowId);
2004
+ mapForEach(
2005
+ mapGet(linkedRowIdsListeners, relationshipId),
2006
+ (firstRowId) => {
2007
+ if (
2008
+ collHas(
2009
+ getLinkedRowIdsCache(relationshipId, firstRowId),
2010
+ localRowId,
2011
+ )
2012
+ ) {
2013
+ setAdd(changedLinkedRows, firstRowId);
2014
+ }
2015
+ },
2016
+ );
2017
+ },
2018
+ );
2019
+ change();
2020
+ collForEach(changedLocalRows, (localRowId) =>
2021
+ callListeners(remoteRowIdListeners, [relationshipId, localRowId]),
2022
+ );
2023
+ collForEach(changedRemoteRows, (remoteRowId) =>
2024
+ callListeners(localRowIdsListeners, [relationshipId, remoteRowId]),
2025
+ );
2026
+ collForEach(changedLinkedRows, (firstRowId) => {
2027
+ delLinkedRowIdsCache(relationshipId, firstRowId);
2028
+ callListeners(linkedRowIdsListeners, [relationshipId, firstRowId]);
2029
+ });
2030
+ },
2031
+ getRowCellFunction(getRemoteRowId2),
2032
+ );
2033
+ return relationships;
2034
+ };
2035
+ const delRelationshipDefinition = (relationshipId) => {
2036
+ mapSet(remoteTableIds, relationshipId);
2037
+ delDefinition(relationshipId);
2038
+ return relationships;
2039
+ };
2040
+ const getRemoteTableId = (relationshipId) =>
2041
+ mapGet(remoteTableIds, relationshipId);
2042
+ const getRemoteRowId = (relationshipId, localRowId) =>
2043
+ mapGet(getRelationship(relationshipId)?.[0], localRowId);
2044
+ const getLocalRowIds = (relationshipId, remoteRowId) =>
2045
+ collValues(mapGet(getRelationship(relationshipId)?.[1], remoteRowId));
2046
+ const getLinkedRowIds = (relationshipId, firstRowId) =>
2047
+ isUndefined(getRelationship(relationshipId))
2048
+ ? [firstRowId]
2049
+ : collValues(getLinkedRowIdsCache(relationshipId, firstRowId, true));
2050
+ const addRemoteRowIdListener = (relationshipId, localRowId, listener) =>
2051
+ addListener(listener, remoteRowIdListeners, [relationshipId, localRowId]);
2052
+ const addLocalRowIdsListener = (relationshipId, remoteRowId, listener) =>
2053
+ addListener(listener, localRowIdsListeners, [relationshipId, remoteRowId]);
2054
+ const addLinkedRowIdsListener = (relationshipId, firstRowId, listener) => {
2055
+ getLinkedRowIdsCache(relationshipId, firstRowId);
2056
+ return addListener(listener, linkedRowIdsListeners, [
2057
+ relationshipId,
2058
+ firstRowId,
2059
+ ]);
2060
+ };
2061
+ const delListener = (listenerId) => {
2062
+ delLinkedRowIdsCache(...delListenerImpl(listenerId));
2063
+ return relationships;
2064
+ };
2065
+ const getListenerStats = () => ({
2066
+ remoteRowId: collSize3(remoteRowIdListeners),
2067
+ localRowIds: collSize3(localRowIdsListeners),
2068
+ linkedRowIds: collSize3(linkedRowIdsListeners),
2069
+ });
2070
+ const relationships = {
2071
+ setRelationshipDefinition,
2072
+ delRelationshipDefinition,
2073
+ getStore,
2074
+ getRelationshipIds,
2075
+ getLocalTableId,
2076
+ getRemoteTableId,
2077
+ getRemoteRowId,
2078
+ getLocalRowIds,
2079
+ getLinkedRowIds,
2080
+ addRemoteRowIdListener,
2081
+ addLocalRowIdsListener,
2082
+ addLinkedRowIdsListener,
2083
+ delListener,
2084
+ destroy,
2085
+ getListenerStats,
2086
+ };
2087
+ return objFreeze(relationships);
2088
+ });
2089
+
2090
+ const transformMap = (map, toBeLikeObject, setId, delId = mapSet) => {
2091
+ const idsToDelete = arrayFilter(
2092
+ mapKeys(map),
2093
+ (id) => !objHas(toBeLikeObject, id),
2094
+ );
2095
+ arrayForEach(objIds(toBeLikeObject), (id) =>
2096
+ setId(map, id, toBeLikeObject[id]),
2097
+ );
2098
+ arrayForEach(idsToDelete, (id) => delId(map, id));
2099
+ return map;
2100
+ };
2101
+ const getCellType = (cell) => {
2102
+ const type = getTypeOf(cell);
2103
+ return isTypeStringOrBoolean(type) || (type == NUMBER && isFiniteNumber(cell))
2104
+ ? type
2105
+ : void 0;
2106
+ };
2107
+ const validate = (obj, validateChild) => {
2108
+ if (isUndefined(obj) || !isObject(obj) || objFrozen(obj)) {
2109
+ return false;
2110
+ }
2111
+ objForEach(obj, (child, id) => {
2112
+ if (!validateChild(child, id)) {
2113
+ objDel(obj, id);
2114
+ }
2115
+ });
2116
+ return !objIsEmpty(obj);
2117
+ };
2118
+ const idsChanged = (ids, id, added) =>
2119
+ mapSet(ids, id, mapGet(ids, id) == -added ? void 0 : added);
2120
+ const createStore = () => {
2121
+ let hasSchema;
2122
+ let nextRowId = 0;
2123
+ let transactions = 0;
2124
+ const changedTableIds = mapNew();
2125
+ const changedRowIds = mapNew();
2126
+ const changedCellIds = mapNew();
2127
+ const changedCells = mapNew();
2128
+ const schemaMap = mapNew();
2129
+ const schemaDefaultRows = mapNew();
2130
+ const tablesMap = mapNew();
2131
+ const tablesListeners = mapNewPair(setNew);
2132
+ const tableIdsListeners = mapNewPair(setNew);
2133
+ const tableListeners = mapNewPair();
2134
+ const rowIdsListeners = mapNewPair();
2135
+ const rowListeners = mapNewPair();
2136
+ const cellIdsListeners = mapNewPair();
2137
+ const cellListeners = mapNewPair();
2138
+ const [addListener, callListeners, delListenerImpl, callListenerImpl] =
2139
+ getListenerFunctions(() => store);
2140
+ const validateSchema = (schema) =>
2141
+ validate(schema, (tableSchema) =>
2142
+ validate(tableSchema, (cellSchema) => {
2143
+ if (
2144
+ !validate(cellSchema, (_child, id) => arrayHas([TYPE, DEFAULT], id))
2145
+ ) {
2146
+ return false;
2147
+ }
2148
+ const type = cellSchema[TYPE];
2149
+ if (!isTypeStringOrBoolean(type) && type != NUMBER) {
2150
+ return false;
2151
+ }
2152
+ if (getCellType(cellSchema[DEFAULT]) != type) {
2153
+ objDel(cellSchema, DEFAULT);
2154
+ }
2155
+ return true;
2156
+ }),
2157
+ );
2158
+ const validateTables = (tables) => validate(tables, validateTable);
2159
+ const validateTable = (table, tableId) =>
2160
+ (!hasSchema || collHas(schemaMap, tableId)) &&
2161
+ validate(table, (row) => validateRow(tableId, row));
2162
+ const validateRow = (tableId, row, skipDefaults) =>
2163
+ validate(
2164
+ skipDefaults ? row : addDefaultsToRow(row, tableId),
2165
+ (cell, cellId) =>
2166
+ ifNotUndefined(
2167
+ getValidatedCell(tableId, cellId, cell),
2168
+ (validCell) => {
2169
+ row[cellId] = validCell;
2170
+ return true;
2171
+ },
2172
+ () => false,
2173
+ ),
2174
+ );
2175
+ const getValidatedCell = (tableId, cellId, cell) =>
2176
+ hasSchema
2177
+ ? ifNotUndefined(
2178
+ mapGet(mapGet(schemaMap, tableId), cellId),
2179
+ (cellSchema) =>
2180
+ getCellType(cell) != cellSchema[TYPE] ? cellSchema[DEFAULT] : cell,
2181
+ )
2182
+ : isUndefined(getCellType(cell))
2183
+ ? void 0
2184
+ : cell;
2185
+ const addDefaultsToRow = (row, tableId) => {
2186
+ ifNotUndefined(mapGet(schemaDefaultRows, tableId), (defaultRow) =>
2187
+ objForEach(defaultRow, (cell, cellId) => {
2188
+ if (!objHas(row, cellId)) {
2189
+ row[cellId] = cell;
2190
+ }
2191
+ }),
2192
+ );
2193
+ return row;
2194
+ };
2195
+ const setValidSchema = (schema) =>
2196
+ transformMap(
2197
+ schemaMap,
2198
+ schema,
2199
+ (_schema, tableId, tableSchema) => {
2200
+ const defaultRow = {};
2201
+ transformMap(
2202
+ mapEnsure(schemaMap, tableId, mapNew()),
2203
+ tableSchema,
2204
+ (tableSchemaMap, cellId, cellSchema) => {
2205
+ mapSet(tableSchemaMap, cellId, cellSchema);
2206
+ ifNotUndefined(
2207
+ cellSchema[DEFAULT],
2208
+ (def) => (defaultRow[cellId] = def),
2209
+ );
2210
+ },
2211
+ );
2212
+ mapSet(schemaDefaultRows, tableId, defaultRow);
2213
+ },
2214
+ (_schema, tableId) => {
2215
+ mapSet(schemaMap, tableId);
2216
+ mapSet(schemaDefaultRows, tableId);
2217
+ },
2218
+ );
2219
+ const setValidTables = (tables) =>
2220
+ transformMap(
2221
+ tablesMap,
2222
+ tables,
2223
+ (_tables, tableId, table) => setValidTable(tableId, table),
2224
+ (_tables, tableId) => delValidTable(tableId),
2225
+ );
2226
+ const setValidTable = (tableId, table) =>
2227
+ transformMap(
2228
+ mapEnsure(tablesMap, tableId, mapNew(), () =>
2229
+ tableIdsChanged(tableId, 1),
2230
+ ),
2231
+ table,
2232
+ (tableMap, rowId, row) => setValidRow(tableId, tableMap, rowId, row),
2233
+ (tableMap, rowId) => delValidRow(tableId, tableMap, rowId),
2234
+ );
2235
+ const setValidRow = (tableId, tableMap, rowId, newRow, forceDel) =>
2236
+ transformMap(
2237
+ mapEnsure(tableMap, rowId, mapNew(), () =>
2238
+ rowIdsChanged(tableId, rowId, 1),
2239
+ ),
2240
+ newRow,
2241
+ (rowMap, cellId, cell) =>
2242
+ setValidCell(tableId, rowId, rowMap, cellId, cell),
2243
+ (rowMap, cellId) =>
2244
+ delValidCell(tableId, tableMap, rowId, rowMap, cellId, forceDel),
2245
+ );
2246
+ const setValidCell = (tableId, rowId, rowMap, cellId, newCell) => {
2247
+ if (!collHas(rowMap, cellId)) {
2248
+ cellIdsChanged(tableId, rowId, cellId, 1);
2249
+ }
2250
+ const oldCell = mapGet(rowMap, cellId);
2251
+ if (newCell !== oldCell) {
2252
+ cellChanged(tableId, rowId, cellId, oldCell);
2253
+ mapSet(rowMap, cellId, newCell);
2254
+ }
2255
+ };
2256
+ const setValidRowTransaction = (tableId, rowId, row) =>
2257
+ transaction(() =>
2258
+ setValidRow(tableId, getOrCreateTable(tableId), rowId, row),
2259
+ );
2260
+ const setCellIntoDefaultRow = (tableId, tableMap, rowId, cellId, validCell) =>
2261
+ ifNotUndefined(
2262
+ mapGet(tableMap, rowId),
2263
+ (rowMap) => setValidCell(tableId, rowId, rowMap, cellId, validCell),
2264
+ () =>
2265
+ setValidRow(
2266
+ tableId,
2267
+ tableMap,
2268
+ rowId,
2269
+ addDefaultsToRow({[cellId]: validCell}, tableId),
2270
+ ),
2271
+ );
2272
+ const getNewRowId = (tableMap) => {
2273
+ const rowId = '' + nextRowId++;
2274
+ if (!collHas(tableMap, rowId)) {
2275
+ return rowId;
2276
+ }
2277
+ return getNewRowId(tableMap);
2278
+ };
2279
+ const getOrCreateTable = (tableId) =>
2280
+ mapGet(tablesMap, tableId) ?? setValidTable(tableId, {});
2281
+ const delValidTable = (tableId) => setValidTable(tableId, {});
2282
+ const delValidRow = (tableId, tableMap, rowId) =>
2283
+ setValidRow(tableId, tableMap, rowId, {}, true);
2284
+ const delValidCell = (tableId, table, rowId, row, cellId, forceDel) => {
2285
+ const defaultCell = mapGet(schemaDefaultRows, tableId)?.[cellId];
2286
+ if (!isUndefined(defaultCell) && !forceDel) {
2287
+ return setValidCell(tableId, rowId, row, cellId, defaultCell);
2288
+ }
2289
+ const delCell2 = (cellId2) => {
2290
+ cellChanged(tableId, rowId, cellId2, mapGet(row, cellId2));
2291
+ cellIdsChanged(tableId, rowId, cellId2, -1);
2292
+ mapSet(row, cellId2);
2293
+ };
2294
+ isUndefined(defaultCell) ? delCell2(cellId) : mapForEach(row, delCell2);
2295
+ if (collIsEmpty(row)) {
2296
+ rowIdsChanged(tableId, rowId, -1);
2297
+ if (collIsEmpty(mapSet(table, rowId))) {
2298
+ tableIdsChanged(tableId, -1);
2299
+ mapSet(tablesMap, tableId);
2300
+ }
2301
+ }
2302
+ };
2303
+ const tableIdsChanged = (tableId, added) =>
2304
+ idsChanged(changedTableIds, tableId, added);
2305
+ const rowIdsChanged = (tableId, rowId, added) =>
2306
+ idsChanged(mapEnsure(changedRowIds, tableId, mapNew()), rowId, added);
2307
+ const cellIdsChanged = (tableId, rowId, cellId, added) =>
2308
+ idsChanged(
2309
+ mapEnsure(mapEnsure(changedCellIds, tableId, mapNew()), rowId, mapNew()),
2310
+ cellId,
2311
+ added,
2312
+ );
2313
+ const cellChanged = (tableId, rowId, cellId, oldCell) =>
2314
+ mapEnsure(
2315
+ mapEnsure(mapEnsure(changedCells, tableId, mapNew()), rowId, mapNew()),
2316
+ cellId,
2317
+ oldCell,
2318
+ );
2319
+ const getCellChange = (tableId, rowId, cellId) => {
2320
+ const changedRow = mapGet(mapGet(changedCells, tableId), rowId);
2321
+ const newCell = getCell(tableId, rowId, cellId);
2322
+ return collHas(changedRow, cellId)
2323
+ ? [true, mapGet(changedRow, cellId), newCell]
2324
+ : [false, newCell, newCell];
2325
+ };
2326
+ const callListenersForChanges = (mutator) => {
2327
+ const emptyIdListeners =
2328
+ collIsEmpty(cellIdsListeners[mutator]) &&
2329
+ collIsEmpty(rowIdsListeners[mutator]) &&
2330
+ collIsEmpty(tableIdsListeners[mutator]);
2331
+ const emptyOtherListeners =
2332
+ collIsEmpty(cellListeners[mutator]) &&
2333
+ collIsEmpty(rowListeners[mutator]) &&
2334
+ collIsEmpty(tableListeners[mutator]) &&
2335
+ collIsEmpty(tablesListeners[mutator]);
2336
+ if (emptyIdListeners && emptyOtherListeners) {
2337
+ return;
2338
+ }
2339
+ const changes = mutator
2340
+ ? [
2341
+ mapClone(changedTableIds),
2342
+ mapClone(changedRowIds, mapClone),
2343
+ mapClone(changedCellIds, (table) => mapClone(table, mapClone)),
2344
+ mapClone(changedCells, (table) => mapClone(table, mapClone)),
2345
+ ]
2346
+ : [changedTableIds, changedRowIds, changedCellIds, changedCells];
2347
+ if (!emptyIdListeners) {
2348
+ collForEach(changes[2], (rowCellIds, tableId) =>
2349
+ collForEach(rowCellIds, (changedIds, rowId) => {
2350
+ if (!collIsEmpty(changedIds)) {
2351
+ callListeners(cellIdsListeners[mutator], [tableId, rowId]);
2352
+ }
2353
+ }),
2354
+ );
2355
+ collForEach(changes[1], (changedIds, tableId) => {
2356
+ if (!collIsEmpty(changedIds)) {
2357
+ callListeners(rowIdsListeners[mutator], [tableId]);
2358
+ }
2359
+ });
2360
+ if (!collIsEmpty(changes[0])) {
2361
+ callListeners(tableIdsListeners[mutator]);
2362
+ }
2363
+ }
2364
+ if (!emptyOtherListeners) {
2365
+ let tablesChanged;
2366
+ collForEach(changes[3], (rows, tableId) => {
2367
+ let tableChanged;
2368
+ collForEach(rows, (cells, rowId) => {
2369
+ let rowChanged;
2370
+ collForEach(cells, (oldCell, cellId) => {
2371
+ const newCell = getCell(tableId, rowId, cellId);
2372
+ if (newCell !== oldCell) {
2373
+ callListeners(
2374
+ cellListeners[mutator],
2375
+ [tableId, rowId, cellId],
2376
+ newCell,
2377
+ oldCell,
2378
+ getCellChange,
2379
+ );
2380
+ tablesChanged = tableChanged = rowChanged = 1;
2381
+ }
2382
+ });
2383
+ if (rowChanged) {
2384
+ callListeners(
2385
+ rowListeners[mutator],
2386
+ [tableId, rowId],
2387
+ getCellChange,
2388
+ );
2389
+ }
2390
+ });
2391
+ if (tableChanged) {
2392
+ callListeners(tableListeners[mutator], [tableId], getCellChange);
2393
+ }
2394
+ });
2395
+ if (tablesChanged) {
2396
+ callListeners(tablesListeners[mutator], [], getCellChange);
2397
+ }
2398
+ }
2399
+ };
2400
+ const getTables = () =>
2401
+ mapToObj(tablesMap, (tableMap) => mapToObj(tableMap, mapToObj));
2402
+ const getTableIds = () => mapKeys(tablesMap);
2403
+ const getTable = (tableId) => mapToObj(mapGet(tablesMap, tableId), mapToObj);
2404
+ const getRowIds = (tableId) => mapKeys(mapGet(tablesMap, tableId));
2405
+ const getRow = (tableId, rowId) =>
2406
+ mapToObj(mapGet(mapGet(tablesMap, tableId), rowId));
2407
+ const getCellIds = (tableId, rowId) =>
2408
+ mapKeys(mapGet(mapGet(tablesMap, tableId), rowId));
2409
+ const getCell = (tableId, rowId, cellId) =>
2410
+ mapGet(mapGet(mapGet(tablesMap, tableId), rowId), cellId);
2411
+ const hasTable = (tableId) => collHas(tablesMap, tableId);
2412
+ const hasRow = (tableId, rowId) => collHas(mapGet(tablesMap, tableId), rowId);
2413
+ const hasCell = (tableId, rowId, cellId) =>
2414
+ collHas(mapGet(mapGet(tablesMap, tableId), rowId), cellId);
2415
+ const getJson = () => jsonString(tablesMap);
2416
+ const getSchemaJson = () => jsonString(schemaMap);
2417
+ const setTables = (tables) => {
2418
+ if (validateTables(tables)) {
2419
+ transaction(() => setValidTables(tables));
2420
+ }
2421
+ return store;
2422
+ };
2423
+ const setTable = (tableId, table) => {
2424
+ if (validateTable(table, tableId)) {
2425
+ transaction(() => setValidTable(tableId, table));
2426
+ }
2427
+ return store;
2428
+ };
2429
+ const setRow = (tableId, rowId, row) => {
2430
+ if (validateRow(tableId, row)) {
2431
+ setValidRowTransaction(tableId, rowId, row);
2432
+ }
2433
+ return store;
2434
+ };
2435
+ const addRow = (tableId, row) => {
2436
+ let rowId = void 0;
2437
+ if (validateRow(tableId, row)) {
2438
+ rowId = getNewRowId(mapGet(tablesMap, tableId));
2439
+ setValidRowTransaction(tableId, rowId, row);
2440
+ }
2441
+ return rowId;
2442
+ };
2443
+ const setPartialRow = (tableId, rowId, partialRow) => {
2444
+ if (validateRow(tableId, partialRow, 1)) {
2445
+ transaction(() => {
2446
+ const table = getOrCreateTable(tableId);
2447
+ objForEach(partialRow, (cell, cellId) =>
2448
+ setCellIntoDefaultRow(tableId, table, rowId, cellId, cell),
2449
+ );
2450
+ });
2451
+ }
2452
+ return store;
2453
+ };
2454
+ const setCell = (tableId, rowId, cellId, cell) => {
2455
+ ifNotUndefined(
2456
+ getValidatedCell(
2457
+ tableId,
2458
+ cellId,
2459
+ isFunction(cell) ? cell(getCell(tableId, rowId, cellId)) : cell,
2460
+ ),
2461
+ (validCell) =>
2462
+ transaction(() =>
2463
+ setCellIntoDefaultRow(
2464
+ tableId,
2465
+ getOrCreateTable(tableId),
2466
+ rowId,
2467
+ cellId,
2468
+ validCell,
2469
+ ),
2470
+ ),
2471
+ );
2472
+ return store;
2473
+ };
2474
+ const setJson = (json) => {
2475
+ try {
2476
+ json === EMPTY_OBJECT ? delTables() : setTables(jsonParse(json));
2477
+ } catch {}
2478
+ return store;
2479
+ };
2480
+ const setSchema = (schema) => {
2481
+ if ((hasSchema = validateSchema(schema))) {
2482
+ setValidSchema(schema);
2483
+ if (!collIsEmpty(tablesMap)) {
2484
+ const tables = getTables();
2485
+ delTables();
2486
+ setTables(tables);
2487
+ }
2488
+ }
2489
+ return store;
2490
+ };
2491
+ const delTables = () => {
2492
+ transaction(() => setValidTables({}));
2493
+ return store;
2494
+ };
2495
+ const delTable = (tableId) => {
2496
+ if (collHas(tablesMap, tableId)) {
2497
+ transaction(() => delValidTable(tableId));
2498
+ }
2499
+ return store;
2500
+ };
2501
+ const delRow = (tableId, rowId) => {
2502
+ ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) => {
2503
+ if (collHas(tableMap, rowId)) {
2504
+ transaction(() => delValidRow(tableId, tableMap, rowId));
2505
+ }
2506
+ });
2507
+ return store;
2508
+ };
2509
+ const delCell = (tableId, rowId, cellId, forceDel) => {
2510
+ ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) =>
2511
+ ifNotUndefined(mapGet(tableMap, rowId), (rowMap) => {
2512
+ if (collHas(rowMap, cellId)) {
2513
+ transaction(() =>
2514
+ delValidCell(tableId, tableMap, rowId, rowMap, cellId, forceDel),
2515
+ );
2516
+ }
2517
+ }),
2518
+ );
2519
+ return store;
2520
+ };
2521
+ const delSchema = () => {
2522
+ setValidSchema({});
2523
+ hasSchema = false;
2524
+ return store;
2525
+ };
2526
+ const transaction = (actions) => {
2527
+ if (transactions == -1) {
2528
+ return;
2529
+ }
2530
+ transactions++;
2531
+ const result = actions();
2532
+ transactions--;
2533
+ if (transactions == 0) {
2534
+ transactions = 1;
2535
+ callListenersForChanges(1);
2536
+ transactions = -1;
2537
+ callListenersForChanges(0);
2538
+ transactions = 0;
2539
+ arrayForEach(
2540
+ [changedCells, changedTableIds, changedRowIds, changedCellIds],
2541
+ collClear,
2542
+ );
2543
+ }
2544
+ return result;
2545
+ };
2546
+ const forEachTable = (tableCallback) =>
2547
+ collForEach(tablesMap, (tableMap, tableId) =>
2548
+ tableCallback(tableId, (rowCallback) =>
2549
+ collForEach(tableMap, (rowMap, rowId) =>
2550
+ rowCallback(rowId, (cellCallback) =>
2551
+ mapForEach(rowMap, cellCallback),
2552
+ ),
2553
+ ),
2554
+ ),
2555
+ );
2556
+ const forEachRow = (tableId, rowCallback) =>
2557
+ collForEach(mapGet(tablesMap, tableId), (rowMap, rowId) =>
2558
+ rowCallback(rowId, (cellCallback) => mapForEach(rowMap, cellCallback)),
2559
+ );
2560
+ const forEachCell = (tableId, rowId, cellCallback) =>
2561
+ mapForEach(mapGet(mapGet(tablesMap, tableId), rowId), cellCallback);
2562
+ const addTablesListener = (listener, mutator) =>
2563
+ addListener(listener, tablesListeners[mutator ? 1 : 0]);
2564
+ const addTableIdsListener = (listener, mutator) =>
2565
+ addListener(listener, tableIdsListeners[mutator ? 1 : 0]);
2566
+ const addTableListener = (tableId, listener, mutator) =>
2567
+ addListener(listener, tableListeners[mutator ? 1 : 0], [tableId]);
2568
+ const addRowIdsListener = (tableId, listener, mutator) =>
2569
+ addListener(listener, rowIdsListeners[mutator ? 1 : 0], [tableId]);
2570
+ const addRowListener = (tableId, rowId, listener, mutator) =>
2571
+ addListener(listener, rowListeners[mutator ? 1 : 0], [tableId, rowId]);
2572
+ const addCellIdsListener = (tableId, rowId, listener, mutator) =>
2573
+ addListener(listener, cellIdsListeners[mutator ? 1 : 0], [tableId, rowId]);
2574
+ const addCellListener = (tableId, rowId, cellId, listener, mutator) =>
2575
+ addListener(listener, cellListeners[mutator ? 1 : 0], [
2576
+ tableId,
2577
+ rowId,
2578
+ cellId,
2579
+ ]);
2580
+ const callListener = (listenerId) => {
2581
+ callListenerImpl(listenerId, [getTableIds, getRowIds, getCellIds], (ids) =>
2582
+ isUndefined(ids[2]) ? [] : Array(2).fill(getCell(...ids)),
2583
+ );
2584
+ return store;
2585
+ };
2586
+ const delListener = (listenerId) => {
2587
+ delListenerImpl(listenerId);
2588
+ return store;
2589
+ };
2590
+ const getListenerStats = () => ({
2591
+ tables: collPairSize(tablesListeners),
2592
+ tableIds: collPairSize(tableIdsListeners),
2593
+ table: collPairSize(tableListeners, collSize2),
2594
+ rowIds: collPairSize(rowIdsListeners, collSize2),
2595
+ row: collPairSize(rowListeners, collSize3),
2596
+ cellIds: collPairSize(cellIdsListeners, collSize3),
2597
+ cell: collPairSize(cellListeners, collSize4),
2598
+ });
2599
+ const store = {
2600
+ getTables,
2601
+ getTableIds,
2602
+ getTable,
2603
+ getRowIds,
2604
+ getRow,
2605
+ getCellIds,
2606
+ getCell,
2607
+ hasTable,
2608
+ hasRow,
2609
+ hasCell,
2610
+ getJson,
2611
+ getSchemaJson,
2612
+ setTables,
2613
+ setTable,
2614
+ setRow,
2615
+ addRow,
2616
+ setPartialRow,
2617
+ setCell,
2618
+ setJson,
2619
+ setSchema,
2620
+ delTables,
2621
+ delTable,
2622
+ delRow,
2623
+ delCell,
2624
+ delSchema,
2625
+ transaction,
2626
+ forEachTable,
2627
+ forEachRow,
2628
+ forEachCell,
2629
+ addTablesListener,
2630
+ addTableIdsListener,
2631
+ addTableListener,
2632
+ addRowIdsListener,
2633
+ addRowListener,
2634
+ addCellIdsListener,
2635
+ addCellListener,
2636
+ callListener,
2637
+ delListener,
2638
+ getListenerStats,
2639
+ };
2640
+ return objFreeze(store);
2641
+ };
2642
+
2643
+ export {
2644
+ BackwardCheckpointsView,
2645
+ CellView,
2646
+ CheckpointView,
2647
+ CurrentCheckpointView,
2648
+ ForwardCheckpointsView,
2649
+ IndexView,
2650
+ LinkedRowsView,
2651
+ LocalRowsView,
2652
+ MetricView,
2653
+ Provider,
2654
+ RemoteRowView,
2655
+ RowView,
2656
+ SliceView,
2657
+ TableView,
2658
+ TablesView,
2659
+ createCheckpoints,
2660
+ createCustomPersister,
2661
+ createFilePersister,
2662
+ createIndexes,
2663
+ createLocalPersister,
2664
+ createMetrics,
2665
+ createRelationships,
2666
+ createRemotePersister,
2667
+ createSessionPersister,
2668
+ createStore,
2669
+ defaultSorter,
2670
+ useAddRowCallback,
2671
+ useCell,
2672
+ useCellIds,
2673
+ useCellIdsListener,
2674
+ useCellListener,
2675
+ useCheckpoint,
2676
+ useCheckpointIds,
2677
+ useCheckpointIdsListener,
2678
+ useCheckpointListener,
2679
+ useCheckpoints,
2680
+ useCreateCheckpoints,
2681
+ useCreateIndexes,
2682
+ useCreateMetrics,
2683
+ useCreatePersister,
2684
+ useCreateRelationships,
2685
+ useCreateStore,
2686
+ useDelCellCallback,
2687
+ useDelRowCallback,
2688
+ useDelTableCallback,
2689
+ useDelTablesCallback,
2690
+ useGoBackwardCallback,
2691
+ useGoForwardCallback,
2692
+ useGoToCallback,
2693
+ useIndexes,
2694
+ useLinkedRowIds,
2695
+ useLinkedRowIdsListener,
2696
+ useLocalRowIds,
2697
+ useLocalRowIdsListener,
2698
+ useMetric,
2699
+ useMetricListener,
2700
+ useMetrics,
2701
+ useRedoInformation,
2702
+ useRelationships,
2703
+ useRemoteRowId,
2704
+ useRemoteRowIdListener,
2705
+ useRow,
2706
+ useRowIds,
2707
+ useRowIdsListener,
2708
+ useRowListener,
2709
+ useSetCellCallback,
2710
+ useSetCheckpointCallback,
2711
+ useSetPartialRowCallback,
2712
+ useSetRowCallback,
2713
+ useSetTableCallback,
2714
+ useSetTablesCallback,
2715
+ useSliceIds,
2716
+ useSliceIdsListener,
2717
+ useSliceRowIds,
2718
+ useSliceRowIdsListener,
2719
+ useStore,
2720
+ useTable,
2721
+ useTableIds,
2722
+ useTableIdsListener,
2723
+ useTableListener,
2724
+ useTables,
2725
+ useTablesListener,
2726
+ useUndoInformation,
2727
+ };