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.
- package/LICENSE +21 -0
- package/lib/checkpoints.d.ts +861 -0
- package/lib/checkpoints.js +1 -0
- package/lib/checkpoints.js.gz +0 -0
- package/lib/common.d.ts +59 -0
- package/lib/debug/checkpoints.d.ts +861 -0
- package/lib/debug/checkpoints.js +326 -0
- package/lib/debug/common.d.ts +59 -0
- package/lib/debug/indexes.d.ts +815 -0
- package/lib/debug/indexes.js +390 -0
- package/lib/debug/metrics.d.ts +728 -0
- package/lib/debug/metrics.js +391 -0
- package/lib/debug/persisters.d.ts +521 -0
- package/lib/debug/persisters.js +191 -0
- package/lib/debug/react.d.ts +7077 -0
- package/lib/debug/react.js +1037 -0
- package/lib/debug/relationships.d.ts +1091 -0
- package/lib/debug/relationships.js +418 -0
- package/lib/debug/store.d.ts +2424 -0
- package/lib/debug/store.js +725 -0
- package/lib/debug/tinybase.d.ts +14 -0
- package/lib/debug/tinybase.js +2727 -0
- package/lib/indexes.d.ts +815 -0
- package/lib/indexes.js +1 -0
- package/lib/indexes.js.gz +0 -0
- package/lib/metrics.d.ts +728 -0
- package/lib/metrics.js +1 -0
- package/lib/metrics.js.gz +0 -0
- package/lib/persisters.d.ts +521 -0
- package/lib/persisters.js +1 -0
- package/lib/persisters.js.gz +0 -0
- package/lib/react.d.ts +7077 -0
- package/lib/react.js +1 -0
- package/lib/react.js.gz +0 -0
- package/lib/relationships.d.ts +1091 -0
- package/lib/relationships.js +1 -0
- package/lib/relationships.js.gz +0 -0
- package/lib/store.d.ts +2424 -0
- package/lib/store.js +1 -0
- package/lib/store.js.gz +0 -0
- package/lib/tinybase.d.ts +14 -0
- package/lib/tinybase.js +1 -0
- package/lib/tinybase.js.gz +0 -0
- package/lib/umd/checkpoints.js +1 -0
- package/lib/umd/checkpoints.js.gz +0 -0
- package/lib/umd/indexes.js +1 -0
- package/lib/umd/indexes.js.gz +0 -0
- package/lib/umd/metrics.js +1 -0
- package/lib/umd/metrics.js.gz +0 -0
- package/lib/umd/persisters.js +1 -0
- package/lib/umd/persisters.js.gz +0 -0
- package/lib/umd/react.js +1 -0
- package/lib/umd/react.js.gz +0 -0
- package/lib/umd/relationships.js +1 -0
- package/lib/umd/relationships.js.gz +0 -0
- package/lib/umd/store.js +1 -0
- package/lib/umd/store.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -0
- package/lib/umd/tinybase.js.gz +0 -0
- package/package.json +93 -2
- package/readme.md +195 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
const arrayHas = (array, value) => array.includes(value);
|
|
2
|
+
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
3
|
+
const arrayLength = (array) => array.length;
|
|
4
|
+
const arrayIsEmpty = (array) => arrayLength(array) == 0;
|
|
5
|
+
const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
|
|
6
|
+
const arrayFromSecond = (ids) => ids.slice(1);
|
|
7
|
+
const arrayClear = (array, to) => array.splice(0, to);
|
|
8
|
+
|
|
9
|
+
const isUndefined = (thing) => thing == void 0;
|
|
10
|
+
const ifNotUndefined = (value, then, otherwise) =>
|
|
11
|
+
isUndefined(value) ? otherwise?.() : then(value);
|
|
12
|
+
|
|
13
|
+
const collSizeN = (collSizer) => (coll) =>
|
|
14
|
+
arrayReduce(collValues(coll), (total, coll2) => total + collSizer(coll2), 0);
|
|
15
|
+
const collSize = (coll) => coll.size;
|
|
16
|
+
const collSize2 = collSizeN(collSize);
|
|
17
|
+
const collHas = (coll, keyOrValue) => coll?.has(keyOrValue) ?? false;
|
|
18
|
+
const collIsEmpty = (coll) => isUndefined(coll) || collSize(coll) == 0;
|
|
19
|
+
const collValues = (coll) => [...(coll?.values() ?? [])];
|
|
20
|
+
const collForEach = (coll, cb) => coll?.forEach(cb);
|
|
21
|
+
const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
|
|
22
|
+
|
|
23
|
+
const mapNew = (entries) => new Map(entries);
|
|
24
|
+
const mapGet = (map, key) => map?.get(key);
|
|
25
|
+
const mapSet = (map, key, value) =>
|
|
26
|
+
isUndefined(value) ? (collDel(map, key), map) : map?.set(key, value);
|
|
27
|
+
const mapEnsure = (map, key, defaultValue, onWillAdd) => {
|
|
28
|
+
if (!collHas(map, key)) {
|
|
29
|
+
onWillAdd?.(defaultValue);
|
|
30
|
+
map.set(key, defaultValue);
|
|
31
|
+
}
|
|
32
|
+
return mapGet(map, key);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const setNew = (entries) => new Set(entries);
|
|
36
|
+
const setAdd = (set, value) => set?.add(value);
|
|
37
|
+
|
|
38
|
+
const getCreateFunction = (getFunction) => {
|
|
39
|
+
const getFunctionsByStore = /* @__PURE__ */ new WeakMap();
|
|
40
|
+
return (store) => {
|
|
41
|
+
if (!getFunctionsByStore.has(store)) {
|
|
42
|
+
getFunctionsByStore.set(store, getFunction(store));
|
|
43
|
+
}
|
|
44
|
+
return getFunctionsByStore.get(store);
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const addDeepSet = (deepSet, value, ids) =>
|
|
49
|
+
arrayLength(ids) < 2
|
|
50
|
+
? setAdd(
|
|
51
|
+
arrayIsEmpty(ids) ? deepSet : mapEnsure(deepSet, ids[0], setNew()),
|
|
52
|
+
value,
|
|
53
|
+
)
|
|
54
|
+
: addDeepSet(
|
|
55
|
+
mapEnsure(deepSet, ids[0], mapNew()),
|
|
56
|
+
value,
|
|
57
|
+
arrayFromSecond(ids),
|
|
58
|
+
);
|
|
59
|
+
const forDeepSet = (valueDo) => {
|
|
60
|
+
const deep = (deepIdSet, arg, ...ids) =>
|
|
61
|
+
ifNotUndefined(deepIdSet, (deepIdSet2) =>
|
|
62
|
+
arrayIsEmpty(ids)
|
|
63
|
+
? valueDo(deepIdSet2, arg)
|
|
64
|
+
: arrayForEach([ids[0], null], (id) =>
|
|
65
|
+
deep(mapGet(deepIdSet2, id), arg, ...arrayFromSecond(ids)),
|
|
66
|
+
),
|
|
67
|
+
);
|
|
68
|
+
return deep;
|
|
69
|
+
};
|
|
70
|
+
const getListenerFunctions = (getThing) => {
|
|
71
|
+
let thing;
|
|
72
|
+
let nextId = 0;
|
|
73
|
+
const listenerPool = [];
|
|
74
|
+
const allListeners = mapNew();
|
|
75
|
+
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
76
|
+
thing ?? (thing = getThing());
|
|
77
|
+
const id = listenerPool.pop() ?? '' + nextId++;
|
|
78
|
+
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
79
|
+
addDeepSet(deepSet, id, idOrNulls);
|
|
80
|
+
return id;
|
|
81
|
+
};
|
|
82
|
+
const callListeners = (deepSet, ids = [], ...extraArgs) =>
|
|
83
|
+
forDeepSet(collForEach)(
|
|
84
|
+
deepSet,
|
|
85
|
+
(id) =>
|
|
86
|
+
ifNotUndefined(mapGet(allListeners, id), ([listener]) =>
|
|
87
|
+
listener(thing, ...ids, ...extraArgs),
|
|
88
|
+
),
|
|
89
|
+
...ids,
|
|
90
|
+
);
|
|
91
|
+
const delListener = (id) =>
|
|
92
|
+
ifNotUndefined(
|
|
93
|
+
mapGet(allListeners, id),
|
|
94
|
+
([, deepSet, idOrNulls]) => {
|
|
95
|
+
forDeepSet(collDel)(deepSet, id, ...idOrNulls);
|
|
96
|
+
mapSet(allListeners, id);
|
|
97
|
+
if (arrayLength(listenerPool) < 1e3) {
|
|
98
|
+
listenerPool.push(id);
|
|
99
|
+
}
|
|
100
|
+
return idOrNulls;
|
|
101
|
+
},
|
|
102
|
+
() => [],
|
|
103
|
+
);
|
|
104
|
+
const callListener = (id, idNullGetters, extraArgsGetter) =>
|
|
105
|
+
ifNotUndefined(mapGet(allListeners, id), ([listener, , idOrNulls]) => {
|
|
106
|
+
const callWithIds = (...ids) => {
|
|
107
|
+
const index = arrayLength(ids);
|
|
108
|
+
index == arrayLength(idOrNulls)
|
|
109
|
+
? listener(thing, ...ids, ...extraArgsGetter(ids))
|
|
110
|
+
: isUndefined(idOrNulls[index])
|
|
111
|
+
? arrayForEach(idNullGetters[index](...ids), (id2) =>
|
|
112
|
+
callWithIds(...ids, id2),
|
|
113
|
+
)
|
|
114
|
+
: callWithIds(...ids, idOrNulls[index]);
|
|
115
|
+
};
|
|
116
|
+
callWithIds();
|
|
117
|
+
});
|
|
118
|
+
return [addListener, callListeners, delListener, callListener];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const object = Object;
|
|
122
|
+
const objFreeze = object.freeze;
|
|
123
|
+
|
|
124
|
+
const createCheckpoints = getCreateFunction((store) => {
|
|
125
|
+
let backwardIdsSize = 100;
|
|
126
|
+
let currentId;
|
|
127
|
+
let delta = mapNew();
|
|
128
|
+
let listening = 1;
|
|
129
|
+
let nextCheckpointId;
|
|
130
|
+
let checkpointsChanged;
|
|
131
|
+
const checkpointIdsListeners = setNew();
|
|
132
|
+
const checkpointListeners = mapNew();
|
|
133
|
+
const [addListener, callListeners, delListenerImpl] = getListenerFunctions(
|
|
134
|
+
() => checkpoints,
|
|
135
|
+
);
|
|
136
|
+
const deltas = mapNew();
|
|
137
|
+
const labels = mapNew();
|
|
138
|
+
const backwardIds = [];
|
|
139
|
+
const forwardIds = [];
|
|
140
|
+
const updateStore = (oldOrNew, checkpointId) => {
|
|
141
|
+
listening = 0;
|
|
142
|
+
store.transaction(() =>
|
|
143
|
+
collForEach(mapGet(deltas, checkpointId), (table, tableId) =>
|
|
144
|
+
collForEach(table, (row, rowId) =>
|
|
145
|
+
collForEach(row, (oldNew, cellId) =>
|
|
146
|
+
isUndefined(oldNew[oldOrNew])
|
|
147
|
+
? store.delCell(tableId, rowId, cellId, true)
|
|
148
|
+
: store.setCell(tableId, rowId, cellId, oldNew[oldOrNew]),
|
|
149
|
+
),
|
|
150
|
+
),
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
listening = 1;
|
|
154
|
+
};
|
|
155
|
+
const clearCheckpointId = (checkpointId) => {
|
|
156
|
+
mapSet(deltas, checkpointId);
|
|
157
|
+
mapSet(labels, checkpointId);
|
|
158
|
+
callListeners(checkpointListeners, [checkpointId]);
|
|
159
|
+
};
|
|
160
|
+
const clearCheckpointIds = (checkpointIds, to) =>
|
|
161
|
+
arrayForEach(
|
|
162
|
+
arrayClear(checkpointIds, to ?? arrayLength(checkpointIds)),
|
|
163
|
+
clearCheckpointId,
|
|
164
|
+
);
|
|
165
|
+
const trimBackwardsIds = () =>
|
|
166
|
+
clearCheckpointIds(backwardIds, arrayLength(backwardIds) - backwardIdsSize);
|
|
167
|
+
const listenerId = store.addCellListener(
|
|
168
|
+
null,
|
|
169
|
+
null,
|
|
170
|
+
null,
|
|
171
|
+
(_store, tableId, rowId, cellId, newCell, oldCell) => {
|
|
172
|
+
if (listening) {
|
|
173
|
+
ifNotUndefined(currentId, () => {
|
|
174
|
+
backwardIds.push(currentId);
|
|
175
|
+
trimBackwardsIds();
|
|
176
|
+
clearCheckpointIds(forwardIds);
|
|
177
|
+
currentId = void 0;
|
|
178
|
+
checkpointsChanged = 1;
|
|
179
|
+
});
|
|
180
|
+
const table = mapEnsure(delta, tableId, mapNew());
|
|
181
|
+
const row = mapEnsure(table, rowId, mapNew());
|
|
182
|
+
const oldNew = mapEnsure(
|
|
183
|
+
row,
|
|
184
|
+
cellId,
|
|
185
|
+
[void 0, void 0],
|
|
186
|
+
(addOldNew) => (addOldNew[0] = oldCell),
|
|
187
|
+
);
|
|
188
|
+
oldNew[1] = newCell;
|
|
189
|
+
if (oldNew[0] === oldNew[1]) {
|
|
190
|
+
if (collIsEmpty(mapSet(row, cellId))) {
|
|
191
|
+
if (collIsEmpty(mapSet(table, rowId))) {
|
|
192
|
+
if (collIsEmpty(mapSet(delta, tableId))) {
|
|
193
|
+
currentId = backwardIds.pop();
|
|
194
|
+
checkpointsChanged = 1;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
callListenersIfChanged();
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
);
|
|
203
|
+
const addCheckpointImpl = (label = '') => {
|
|
204
|
+
if (isUndefined(currentId)) {
|
|
205
|
+
currentId = '' + nextCheckpointId++;
|
|
206
|
+
mapSet(deltas, currentId, delta);
|
|
207
|
+
setCheckpoint(currentId, label);
|
|
208
|
+
delta = mapNew();
|
|
209
|
+
checkpointsChanged = 1;
|
|
210
|
+
}
|
|
211
|
+
return currentId;
|
|
212
|
+
};
|
|
213
|
+
const goBackwardImpl = () => {
|
|
214
|
+
if (!arrayIsEmpty(backwardIds)) {
|
|
215
|
+
forwardIds.unshift(addCheckpointImpl());
|
|
216
|
+
updateStore(0, currentId);
|
|
217
|
+
currentId = backwardIds.pop();
|
|
218
|
+
checkpointsChanged = 1;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const goForwardImpl = () => {
|
|
222
|
+
if (!arrayIsEmpty(forwardIds)) {
|
|
223
|
+
backwardIds.push(currentId);
|
|
224
|
+
currentId = forwardIds.shift();
|
|
225
|
+
updateStore(1, currentId);
|
|
226
|
+
checkpointsChanged = 1;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
const callListenersIfChanged = () => {
|
|
230
|
+
if (checkpointsChanged) {
|
|
231
|
+
callListeners(checkpointIdsListeners);
|
|
232
|
+
checkpointsChanged = 0;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const setSize = (size) => {
|
|
236
|
+
backwardIdsSize = size;
|
|
237
|
+
trimBackwardsIds();
|
|
238
|
+
return checkpoints;
|
|
239
|
+
};
|
|
240
|
+
const addCheckpoint = (label) => {
|
|
241
|
+
const id = addCheckpointImpl(label);
|
|
242
|
+
callListenersIfChanged();
|
|
243
|
+
return id;
|
|
244
|
+
};
|
|
245
|
+
const setCheckpoint = (checkpointId, label) => {
|
|
246
|
+
if (
|
|
247
|
+
collHas(deltas, checkpointId) &&
|
|
248
|
+
mapGet(labels, checkpointId) !== label
|
|
249
|
+
) {
|
|
250
|
+
mapSet(labels, checkpointId, label);
|
|
251
|
+
callListeners(checkpointListeners, [checkpointId]);
|
|
252
|
+
}
|
|
253
|
+
return checkpoints;
|
|
254
|
+
};
|
|
255
|
+
const getStore = () => store;
|
|
256
|
+
const getCheckpointIds = () => [[...backwardIds], currentId, [...forwardIds]];
|
|
257
|
+
const getCheckpoint = (checkpointId) => mapGet(labels, checkpointId);
|
|
258
|
+
const goBackward = () => {
|
|
259
|
+
goBackwardImpl();
|
|
260
|
+
callListenersIfChanged();
|
|
261
|
+
return checkpoints;
|
|
262
|
+
};
|
|
263
|
+
const goForward = () => {
|
|
264
|
+
goForwardImpl();
|
|
265
|
+
callListenersIfChanged();
|
|
266
|
+
return checkpoints;
|
|
267
|
+
};
|
|
268
|
+
const goTo = (checkpointId) => {
|
|
269
|
+
const action = arrayHas(backwardIds, checkpointId)
|
|
270
|
+
? goBackwardImpl
|
|
271
|
+
: arrayHas(forwardIds, checkpointId)
|
|
272
|
+
? goForwardImpl
|
|
273
|
+
: null;
|
|
274
|
+
while (!isUndefined(action) && checkpointId != currentId) {
|
|
275
|
+
action();
|
|
276
|
+
}
|
|
277
|
+
callListenersIfChanged();
|
|
278
|
+
return checkpoints;
|
|
279
|
+
};
|
|
280
|
+
const addCheckpointIdsListener = (listener) =>
|
|
281
|
+
addListener(listener, checkpointIdsListeners);
|
|
282
|
+
const addCheckpointListener = (checkpointId, listener) =>
|
|
283
|
+
addListener(listener, checkpointListeners, [checkpointId]);
|
|
284
|
+
const delListener = (listenerId2) => {
|
|
285
|
+
delListenerImpl(listenerId2);
|
|
286
|
+
return checkpoints;
|
|
287
|
+
};
|
|
288
|
+
const clear = () => {
|
|
289
|
+
clearCheckpointIds(backwardIds);
|
|
290
|
+
clearCheckpointIds(forwardIds);
|
|
291
|
+
if (!isUndefined(currentId)) {
|
|
292
|
+
clearCheckpointId(currentId);
|
|
293
|
+
}
|
|
294
|
+
currentId = void 0;
|
|
295
|
+
nextCheckpointId = 0;
|
|
296
|
+
addCheckpoint();
|
|
297
|
+
return checkpoints;
|
|
298
|
+
};
|
|
299
|
+
const destroy = () => {
|
|
300
|
+
store.delListener(listenerId);
|
|
301
|
+
};
|
|
302
|
+
const getListenerStats = () => ({
|
|
303
|
+
checkpointIds: collSize(checkpointIdsListeners),
|
|
304
|
+
checkpoint: collSize2(checkpointListeners),
|
|
305
|
+
});
|
|
306
|
+
const checkpoints = {
|
|
307
|
+
setSize,
|
|
308
|
+
addCheckpoint,
|
|
309
|
+
setCheckpoint,
|
|
310
|
+
getStore,
|
|
311
|
+
getCheckpointIds,
|
|
312
|
+
getCheckpoint,
|
|
313
|
+
goBackward,
|
|
314
|
+
goForward,
|
|
315
|
+
goTo,
|
|
316
|
+
addCheckpointIdsListener,
|
|
317
|
+
addCheckpointListener,
|
|
318
|
+
delListener,
|
|
319
|
+
clear,
|
|
320
|
+
destroy,
|
|
321
|
+
getListenerStats,
|
|
322
|
+
};
|
|
323
|
+
return objFreeze(checkpoints.clear());
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
export {createCheckpoints};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The common module of the TinyBase project provides a small collection of
|
|
3
|
+
* common types used across other modules.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
* @module common
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Json type is a simple alias for a string, but is used to indicate that
|
|
11
|
+
* the string should be considered to be a JSON serialization of an object.
|
|
12
|
+
*
|
|
13
|
+
* @category General
|
|
14
|
+
*/
|
|
15
|
+
export type Json = string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The Ids type is a simple alias for an array of strings, but is used to
|
|
19
|
+
* indicate that the strings should be considered to be the keys of objects
|
|
20
|
+
* (such as the Row Id strings used in a Table).
|
|
21
|
+
*
|
|
22
|
+
* @category Identity
|
|
23
|
+
*/
|
|
24
|
+
export type Ids = Id[];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The Id type is a simple alias for a string, but is used to indicate that the
|
|
28
|
+
* string should be considered to be the key of an object (such as a Row Id
|
|
29
|
+
* string used in a Table).
|
|
30
|
+
*
|
|
31
|
+
* @category Identity
|
|
32
|
+
*/
|
|
33
|
+
export type Id = string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The Id type is a simple alias for the union of a string or `null` value,
|
|
37
|
+
* where the string should be considered to be the key of an objects (such as a
|
|
38
|
+
* Row Id string used in a Table), and typically `null` indicates a wildcard -
|
|
39
|
+
* such as when used in the Store addRowListener method.
|
|
40
|
+
*
|
|
41
|
+
* @category Identity
|
|
42
|
+
*/
|
|
43
|
+
export type IdOrNull = Id | null;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The ParameterizedCallback type represents a generic function that will take
|
|
47
|
+
* an optional parameter - such as the handler of a DOM event.
|
|
48
|
+
*
|
|
49
|
+
* @category Callback
|
|
50
|
+
*/
|
|
51
|
+
export type ParameterizedCallback<Parameter> = (parameter?: Parameter) => void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The Callback type represents a function that is used as a callback and which
|
|
55
|
+
* does not take a parameter.
|
|
56
|
+
*
|
|
57
|
+
* @category Callback
|
|
58
|
+
*/
|
|
59
|
+
export type Callback = () => void;
|