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