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,418 @@
1
+ const getTypeOf = (thing) => typeof thing;
2
+ const EMPTY_STRING = '';
3
+ const STRING = getTypeOf(EMPTY_STRING);
4
+
5
+ const arrayForEach = (array, cb) => array.forEach(cb);
6
+ const arrayLength = (array) => array.length;
7
+ const arrayIsEmpty = (array) => arrayLength(array) == 0;
8
+ const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
9
+ const arrayFromSecond = (ids) => ids.slice(1);
10
+
11
+ const isUndefined = (thing) => thing == void 0;
12
+ const ifNotUndefined = (value, then, otherwise) =>
13
+ isUndefined(value) ? otherwise?.() : then(value);
14
+ const isString = (thing) => getTypeOf(thing) == STRING;
15
+
16
+ const collSizeN = (collSizer) => (coll) =>
17
+ arrayReduce(collValues(coll), (total, coll2) => total + collSizer(coll2), 0);
18
+ const collSize = (coll) => coll.size;
19
+ const collSize2 = collSizeN(collSize);
20
+ const collSize3 = collSizeN(collSize2);
21
+ const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
22
+ const collIsEmpty = (coll) => isUndefined(coll) || collSize(coll) == 0;
23
+ const collValues = (coll) => [...(coll?.values() ?? [])];
24
+ const collClear = (coll) => coll.clear();
25
+ const collForEach = (coll, cb) => coll?.forEach(cb);
26
+ const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
27
+
28
+ const mapNew = (entries) => new Map(entries);
29
+ const mapKeys = (map) => [...(map?.keys() ?? [])];
30
+ const mapGet = (map, key) => map?.get(key);
31
+ const mapForEach = (map, cb) =>
32
+ collForEach(map, (value, key) => cb(key, value));
33
+ const mapSet = (map, key, value) =>
34
+ isUndefined(value) ? (collDel(map, key), map) : map?.set(key, value);
35
+ const mapEnsure = (map, key, defaultValue, onWillAdd) => {
36
+ if (!collHas(map, key)) {
37
+ onWillAdd?.(defaultValue);
38
+ map.set(key, defaultValue);
39
+ }
40
+ return mapGet(map, key);
41
+ };
42
+
43
+ const setNew = (entries) => new Set(entries);
44
+ const setAdd = (set, value) => set?.add(value);
45
+
46
+ const getDefinableFunctions = (store, getDefaultThing, validateRowValue) => {
47
+ const hasRow = store.hasRow;
48
+ const tableIds = mapNew();
49
+ const things = mapNew();
50
+ const allRowValues = mapNew();
51
+ const allSortKeys = mapNew();
52
+ const storeListenerIds = mapNew();
53
+ const getStore = () => store;
54
+ const getThingIds = () => mapKeys(tableIds);
55
+ const getTableId = (id) => mapGet(tableIds, id);
56
+ const getThing = (id) => mapGet(things, id);
57
+ const setThing = (id, thing) => mapSet(things, id, thing);
58
+ const removeStoreListeners = (id) =>
59
+ ifNotUndefined(mapGet(storeListenerIds, id), (listenerIds) => {
60
+ collForEach(listenerIds, store.delListener);
61
+ mapSet(storeListenerIds, id);
62
+ });
63
+ const setDefinition = (id, tableId, onChanged, getRowValue, getSortKey) => {
64
+ const changedRowValues = mapNew();
65
+ const changedSortKeys = mapNew();
66
+ mapSet(tableIds, id, tableId);
67
+ if (!collHas(things, id)) {
68
+ mapSet(things, id, getDefaultThing());
69
+ mapSet(allRowValues, id, mapNew());
70
+ mapSet(allSortKeys, id, mapNew());
71
+ }
72
+ const rowValues = mapGet(allRowValues, id);
73
+ const sortKeys = mapGet(allSortKeys, id);
74
+ const processRow = (rowId) => {
75
+ const getCell = (cellId) => store.getCell(tableId, rowId, cellId);
76
+ const oldRowValue = mapGet(rowValues, rowId);
77
+ const newRowValue = hasRow(tableId, rowId)
78
+ ? validateRowValue(getRowValue(getCell, rowId))
79
+ : void 0;
80
+ if (oldRowValue != newRowValue) {
81
+ mapSet(changedRowValues, rowId, [oldRowValue, newRowValue]);
82
+ }
83
+ if (!isUndefined(getSortKey)) {
84
+ const oldSortKey = mapGet(sortKeys, rowId);
85
+ const newSortKey = hasRow(tableId, rowId)
86
+ ? getSortKey(getCell, rowId)
87
+ : void 0;
88
+ if (oldSortKey != newSortKey) {
89
+ mapSet(changedSortKeys, rowId, newSortKey);
90
+ }
91
+ }
92
+ };
93
+ const processTable = (force) => {
94
+ onChanged(
95
+ () => {
96
+ collForEach(changedRowValues, ([, newRowValue], rowId) =>
97
+ mapSet(rowValues, rowId, newRowValue),
98
+ );
99
+ collForEach(changedSortKeys, (newSortKey, rowId) =>
100
+ mapSet(sortKeys, rowId, newSortKey),
101
+ );
102
+ },
103
+ changedRowValues,
104
+ changedSortKeys,
105
+ rowValues,
106
+ sortKeys,
107
+ force,
108
+ );
109
+ collClear(changedRowValues);
110
+ collClear(changedSortKeys);
111
+ };
112
+ mapForEach(rowValues, processRow);
113
+ if (store.hasTable(tableId)) {
114
+ arrayForEach(store.getRowIds(tableId), (rowId) => {
115
+ if (!collHas(rowValues, rowId)) {
116
+ processRow(rowId);
117
+ }
118
+ });
119
+ }
120
+ processTable(true);
121
+ removeStoreListeners(id);
122
+ mapSet(
123
+ storeListenerIds,
124
+ id,
125
+ setNew([
126
+ store.addRowListener(tableId, null, (_store, _tableId, rowId) =>
127
+ processRow(rowId),
128
+ ),
129
+ store.addTableListener(tableId, () => processTable()),
130
+ ]),
131
+ );
132
+ };
133
+ const delDefinition = (id) => {
134
+ mapSet(tableIds, id);
135
+ mapSet(things, id);
136
+ mapSet(allRowValues, id);
137
+ mapSet(allSortKeys, id);
138
+ removeStoreListeners(id);
139
+ };
140
+ const destroy = () => mapForEach(storeListenerIds, delDefinition);
141
+ return [
142
+ getStore,
143
+ getThingIds,
144
+ getTableId,
145
+ getThing,
146
+ setThing,
147
+ setDefinition,
148
+ delDefinition,
149
+ destroy,
150
+ ];
151
+ };
152
+ const getRowCellFunction = (getRowCell, defaultCellValue) =>
153
+ isString(getRowCell)
154
+ ? (getCell) => getCell(getRowCell)
155
+ : getRowCell ?? (() => defaultCellValue ?? EMPTY_STRING);
156
+ const getCreateFunction = (getFunction) => {
157
+ const getFunctionsByStore = /* @__PURE__ */ new WeakMap();
158
+ return (store) => {
159
+ if (!getFunctionsByStore.has(store)) {
160
+ getFunctionsByStore.set(store, getFunction(store));
161
+ }
162
+ return getFunctionsByStore.get(store);
163
+ };
164
+ };
165
+
166
+ const addDeepSet = (deepSet, value, ids) =>
167
+ arrayLength(ids) < 2
168
+ ? setAdd(
169
+ arrayIsEmpty(ids) ? deepSet : mapEnsure(deepSet, ids[0], setNew()),
170
+ value,
171
+ )
172
+ : addDeepSet(
173
+ mapEnsure(deepSet, ids[0], mapNew()),
174
+ value,
175
+ arrayFromSecond(ids),
176
+ );
177
+ const forDeepSet = (valueDo) => {
178
+ const deep = (deepIdSet, arg, ...ids) =>
179
+ ifNotUndefined(deepIdSet, (deepIdSet2) =>
180
+ arrayIsEmpty(ids)
181
+ ? valueDo(deepIdSet2, arg)
182
+ : arrayForEach([ids[0], null], (id) =>
183
+ deep(mapGet(deepIdSet2, id), arg, ...arrayFromSecond(ids)),
184
+ ),
185
+ );
186
+ return deep;
187
+ };
188
+ const getListenerFunctions = (getThing) => {
189
+ let thing;
190
+ let nextId = 0;
191
+ const listenerPool = [];
192
+ const allListeners = mapNew();
193
+ const addListener = (listener, deepSet, idOrNulls = []) => {
194
+ thing ?? (thing = getThing());
195
+ const id = listenerPool.pop() ?? '' + nextId++;
196
+ mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
197
+ addDeepSet(deepSet, id, idOrNulls);
198
+ return id;
199
+ };
200
+ const callListeners = (deepSet, ids = [], ...extraArgs) =>
201
+ forDeepSet(collForEach)(
202
+ deepSet,
203
+ (id) =>
204
+ ifNotUndefined(mapGet(allListeners, id), ([listener]) =>
205
+ listener(thing, ...ids, ...extraArgs),
206
+ ),
207
+ ...ids,
208
+ );
209
+ const delListener = (id) =>
210
+ ifNotUndefined(
211
+ mapGet(allListeners, id),
212
+ ([, deepSet, idOrNulls]) => {
213
+ forDeepSet(collDel)(deepSet, id, ...idOrNulls);
214
+ mapSet(allListeners, id);
215
+ if (arrayLength(listenerPool) < 1e3) {
216
+ listenerPool.push(id);
217
+ }
218
+ return idOrNulls;
219
+ },
220
+ () => [],
221
+ );
222
+ const callListener = (id, idNullGetters, extraArgsGetter) =>
223
+ ifNotUndefined(mapGet(allListeners, id), ([listener, , idOrNulls]) => {
224
+ const callWithIds = (...ids) => {
225
+ const index = arrayLength(ids);
226
+ index == arrayLength(idOrNulls)
227
+ ? listener(thing, ...ids, ...extraArgsGetter(ids))
228
+ : isUndefined(idOrNulls[index])
229
+ ? arrayForEach(idNullGetters[index](...ids), (id2) =>
230
+ callWithIds(...ids, id2),
231
+ )
232
+ : callWithIds(...ids, idOrNulls[index]);
233
+ };
234
+ callWithIds();
235
+ });
236
+ return [addListener, callListeners, delListener, callListener];
237
+ };
238
+
239
+ const object = Object;
240
+ const objFreeze = object.freeze;
241
+
242
+ const createRelationships = getCreateFunction((store) => {
243
+ const remoteTableIds = mapNew();
244
+ const remoteRowIdListeners = mapNew();
245
+ const localRowIdsListeners = mapNew();
246
+ const linkedRowIdsListeners = mapNew();
247
+ const [
248
+ getStore,
249
+ getRelationshipIds,
250
+ getLocalTableId,
251
+ getRelationship,
252
+ _setRelationship,
253
+ setDefinition,
254
+ delDefinition,
255
+ destroy,
256
+ ] = getDefinableFunctions(
257
+ store,
258
+ () => [mapNew(), mapNew(), mapNew(), mapNew()],
259
+ (value) => (isUndefined(value) ? void 0 : value + EMPTY_STRING),
260
+ );
261
+ const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
262
+ () => relationships,
263
+ );
264
+ const getLinkedRowIdsCache = (relationshipId, firstRowId, skipCache) =>
265
+ ifNotUndefined(
266
+ getRelationship(relationshipId),
267
+ ([remoteRows, , linkedRowsCache]) => {
268
+ if (!collHas(linkedRowsCache, firstRowId)) {
269
+ const linkedRows = setNew();
270
+ if (
271
+ getLocalTableId(relationshipId) != getRemoteTableId(relationshipId)
272
+ ) {
273
+ setAdd(linkedRows, firstRowId);
274
+ } else {
275
+ let rowId = firstRowId;
276
+ while (!isUndefined(rowId) && !collHas(linkedRows, rowId)) {
277
+ setAdd(linkedRows, rowId);
278
+ rowId = mapGet(remoteRows, rowId);
279
+ }
280
+ }
281
+ if (skipCache) {
282
+ return linkedRows;
283
+ }
284
+ mapSet(linkedRowsCache, firstRowId, linkedRows);
285
+ }
286
+ return mapGet(linkedRowsCache, firstRowId);
287
+ },
288
+ );
289
+ const delLinkedRowIdsCache = (relationshipId, firstRowId) =>
290
+ ifNotUndefined(getRelationship(relationshipId), ([, , linkedRowsCache]) =>
291
+ mapSet(linkedRowsCache, firstRowId),
292
+ );
293
+ const setRelationshipDefinition = (
294
+ relationshipId,
295
+ localTableId,
296
+ remoteTableId,
297
+ getRemoteRowId2,
298
+ ) => {
299
+ mapSet(remoteTableIds, relationshipId, remoteTableId);
300
+ setDefinition(
301
+ relationshipId,
302
+ localTableId,
303
+ (change, changedRemoteRowIds) => {
304
+ const changedLocalRows = setNew();
305
+ const changedRemoteRows = setNew();
306
+ const changedLinkedRows = setNew();
307
+ const [localRows, remoteRows] = getRelationship(relationshipId);
308
+ collForEach(
309
+ changedRemoteRowIds,
310
+ ([oldRemoteRowId, newRemoteRowId], localRowId) => {
311
+ if (!isUndefined(oldRemoteRowId)) {
312
+ setAdd(changedRemoteRows, oldRemoteRowId);
313
+ ifNotUndefined(
314
+ mapGet(remoteRows, oldRemoteRowId),
315
+ (oldRemoteRow) => {
316
+ collDel(oldRemoteRow, localRowId);
317
+ if (collIsEmpty(oldRemoteRow)) {
318
+ mapSet(remoteRows, oldRemoteRowId);
319
+ }
320
+ },
321
+ );
322
+ }
323
+ if (!isUndefined(newRemoteRowId)) {
324
+ setAdd(changedRemoteRows, newRemoteRowId);
325
+ if (!collHas(remoteRows, newRemoteRowId)) {
326
+ mapSet(remoteRows, newRemoteRowId, setNew());
327
+ }
328
+ setAdd(mapGet(remoteRows, newRemoteRowId), localRowId);
329
+ }
330
+ setAdd(changedLocalRows, localRowId);
331
+ mapSet(localRows, localRowId, newRemoteRowId);
332
+ mapForEach(
333
+ mapGet(linkedRowIdsListeners, relationshipId),
334
+ (firstRowId) => {
335
+ if (
336
+ collHas(
337
+ getLinkedRowIdsCache(relationshipId, firstRowId),
338
+ localRowId,
339
+ )
340
+ ) {
341
+ setAdd(changedLinkedRows, firstRowId);
342
+ }
343
+ },
344
+ );
345
+ },
346
+ );
347
+ change();
348
+ collForEach(changedLocalRows, (localRowId) =>
349
+ callListeners(remoteRowIdListeners, [relationshipId, localRowId]),
350
+ );
351
+ collForEach(changedRemoteRows, (remoteRowId) =>
352
+ callListeners(localRowIdsListeners, [relationshipId, remoteRowId]),
353
+ );
354
+ collForEach(changedLinkedRows, (firstRowId) => {
355
+ delLinkedRowIdsCache(relationshipId, firstRowId);
356
+ callListeners(linkedRowIdsListeners, [relationshipId, firstRowId]);
357
+ });
358
+ },
359
+ getRowCellFunction(getRemoteRowId2),
360
+ );
361
+ return relationships;
362
+ };
363
+ const delRelationshipDefinition = (relationshipId) => {
364
+ mapSet(remoteTableIds, relationshipId);
365
+ delDefinition(relationshipId);
366
+ return relationships;
367
+ };
368
+ const getRemoteTableId = (relationshipId) =>
369
+ mapGet(remoteTableIds, relationshipId);
370
+ const getRemoteRowId = (relationshipId, localRowId) =>
371
+ mapGet(getRelationship(relationshipId)?.[0], localRowId);
372
+ const getLocalRowIds = (relationshipId, remoteRowId) =>
373
+ collValues(mapGet(getRelationship(relationshipId)?.[1], remoteRowId));
374
+ const getLinkedRowIds = (relationshipId, firstRowId) =>
375
+ isUndefined(getRelationship(relationshipId))
376
+ ? [firstRowId]
377
+ : collValues(getLinkedRowIdsCache(relationshipId, firstRowId, true));
378
+ const addRemoteRowIdListener = (relationshipId, localRowId, listener) =>
379
+ addListener(listener, remoteRowIdListeners, [relationshipId, localRowId]);
380
+ const addLocalRowIdsListener = (relationshipId, remoteRowId, listener) =>
381
+ addListener(listener, localRowIdsListeners, [relationshipId, remoteRowId]);
382
+ const addLinkedRowIdsListener = (relationshipId, firstRowId, listener) => {
383
+ getLinkedRowIdsCache(relationshipId, firstRowId);
384
+ return addListener(listener, linkedRowIdsListeners, [
385
+ relationshipId,
386
+ firstRowId,
387
+ ]);
388
+ };
389
+ const delListener = (listenerId) => {
390
+ delLinkedRowIdsCache(...delListenerImpl(listenerId));
391
+ return relationships;
392
+ };
393
+ const getListenerStats = () => ({
394
+ remoteRowId: collSize3(remoteRowIdListeners),
395
+ localRowIds: collSize3(localRowIdsListeners),
396
+ linkedRowIds: collSize3(linkedRowIdsListeners),
397
+ });
398
+ const relationships = {
399
+ setRelationshipDefinition,
400
+ delRelationshipDefinition,
401
+ getStore,
402
+ getRelationshipIds,
403
+ getLocalTableId,
404
+ getRemoteTableId,
405
+ getRemoteRowId,
406
+ getLocalRowIds,
407
+ getLinkedRowIds,
408
+ addRemoteRowIdListener,
409
+ addLocalRowIdsListener,
410
+ addLinkedRowIdsListener,
411
+ delListener,
412
+ destroy,
413
+ getListenerStats,
414
+ };
415
+ return objFreeze(relationships);
416
+ });
417
+
418
+ export {createRelationships};