tinybase 1.0.5 → 1.1.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/lib/checkpoints.js +1 -1
- package/lib/checkpoints.js.gz +0 -0
- package/lib/debug/checkpoints.js +8 -6
- package/lib/debug/indexes.js +4 -2
- package/lib/debug/metrics.d.ts +1 -1
- package/lib/debug/metrics.js +4 -2
- package/lib/debug/relationships.js +4 -2
- package/lib/debug/store.d.ts +261 -0
- package/lib/debug/store.js +243 -185
- package/lib/debug/tinybase.js +247 -189
- package/lib/metrics.d.ts +1 -1
- package/lib/store.d.ts +261 -0
- package/lib/store.js +1 -1
- package/lib/store.js.gz +0 -0
- package/lib/tinybase.js +1 -1
- package/lib/tinybase.js.gz +0 -0
- package/lib/umd/checkpoints.js +1 -1
- package/lib/umd/checkpoints.js.gz +0 -0
- package/lib/umd/store.js +1 -1
- package/lib/umd/store.js.gz +0 -0
- package/lib/umd/tinybase.js +1 -1
- package/lib/umd/tinybase.js.gz +0 -0
- package/package.json +18 -17
- package/readme.md +2 -2
package/lib/checkpoints.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,t)=>e.includes(t),t=(e,t)=>e.forEach(t),n=e=>e.length,s=e=>0==n(e),o=e=>e.slice(1),r=e=>null==e,
|
|
1
|
+
const e=(e,t)=>e.includes(t),t=(e,t)=>e.forEach(t),n=e=>e.length,s=e=>0==n(e),o=e=>e.slice(1),r=(e,t)=>e.push(t),c=e=>e.pop(),l=e=>null==e,i=(e,t,n)=>l(e)?n?.():t(e),d=(e,t)=>e?.has(t)??!1,a=e=>l(e)||0==(e=>e.size)(e),h=(e,t)=>e?.forEach(t),u=(e,t)=>e?.delete(t),p=e=>new Map(e),C=(e,t)=>e?.get(t),g=(e,t,n)=>l(n)?(u(e,t),e):e?.set(t,n),k=(e,t,n,s)=>(d(e,t)||(s?.(n),e.set(t,n)),C(e,t)),f=e=>new Set(e),L=(e,t,r)=>n(r)<2?((e,t)=>e?.add(t))(s(r)?e:k(e,r[0],f()),t):L(k(e,r[0],p()),t,o(r)),v=e=>{const n=(r,c,...l)=>i(r,(r=>s(l)?e(r,c):t([l[0],null],(e=>n(C(r,e),c,...o(l))))));return n},w=Object.freeze,S=(e=>{const t=new WeakMap;return n=>(t.has(n)||t.set(n,e(n)),t.get(n))})((o=>{let S,z,E,I=100,M=p(),b=1;const j=f(),x=p(),[y,B,F]=(e=>{let s,o=0;const d=[],a=p();return[(t,n,r=[])=>{s??=e();const l=c(d)??""+o++;return g(a,l,[t,n,r]),L(n,l,r),l},(e,t=[],...n)=>v(h)(e,(e=>i(C(a,e),(([e])=>e(s,...t,...n)))),...t),e=>i(C(a,e),(([,t,s])=>(v(u)(t,e,...s),g(a,e),n(d)<1e3&&r(d,e),s)),(()=>[])),(e,o,r)=>i(C(a,e),(([e,,c])=>{const i=(...d)=>{const a=n(d);a==n(c)?e(s,...d,...r(d)):l(c[a])?t(o[a](...d),(e=>i(...d,e))):i(...d,c[a])};i()}))]})((()=>V)),O=p(),T=p(),W=[],m=[],q=(e,t)=>{b=0,o.transaction((()=>h(C(O,t),((t,n)=>h(t,((t,s)=>h(t,((t,r)=>l(t[e])?o.delCell(n,s,r,!0):o.setCell(n,s,r,t[e]))))))))),b=1},A=e=>{g(O,e),g(T,e),B(x,[e])},D=(e,s)=>t(((e,t)=>e.splice(0,t))(e,s??n(e)),A),G=()=>D(W,n(W)-I),H=o.addCellListener(null,null,null,((e,t,n,s,o,l)=>{if(b){i(S,(()=>{r(W,S),G(),D(m),S=void 0,E=1}));const e=k(M,t,p()),d=k(e,n,p()),h=k(d,s,[void 0,void 0],(e=>e[0]=l));h[1]=o,h[0]===h[1]&&a(g(d,s))&&a(g(e,n))&&a(g(M,t))&&(S=c(W),E=1),P()}})),J=(e="")=>(l(S)&&(S=""+z++,g(O,S,M),R(S,e),M=p(),E=1),S),K=()=>{s(W)||(m.unshift(J()),q(0,S),S=c(W),E=1)},N=()=>{s(m)||(r(W,S),S=m.shift(),q(1,S),E=1)},P=()=>{E&&(B(j),E=0)},Q=e=>{const t=J(e);return P(),t},R=(e,t)=>(U(e)&&C(T,e)!==t&&(g(T,e,t),B(x,[e])),V),U=e=>d(O,e),V={setSize:e=>(I=e,G(),V),addCheckpoint:Q,setCheckpoint:R,getStore:()=>o,getCheckpointIds:()=>[[...W],S,[...m]],forEachCheckpoint:e=>{return t=e,h(T,((e,n)=>t(n,e)));var t},hasCheckpoint:U,getCheckpoint:e=>C(T,e),goBackward:()=>(K(),P(),V),goForward:()=>(N(),P(),V),goTo:t=>{const n=e(W,t)?K:e(m,t)?N:null;for(;!l(n)&&t!=S;)n();return P(),V},addCheckpointIdsListener:e=>y(e,j),addCheckpointListener:(e,t)=>y(t,x,[e]),delListener:e=>(F(e),V),clear:()=>(D(W),D(m),l(S)||A(S),S=void 0,z=0,Q(),V),destroy:()=>{o.delListener(H)},getListenerStats:()=>({})};return w(V.clear())}));export{S as createCheckpoints};
|
package/lib/checkpoints.js.gz
CHANGED
|
Binary file
|
package/lib/debug/checkpoints.js
CHANGED
|
@@ -5,6 +5,8 @@ const arrayIsEmpty = (array) => arrayLength(array) == 0;
|
|
|
5
5
|
const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
|
|
6
6
|
const arrayFromSecond = (ids) => ids.slice(1);
|
|
7
7
|
const arrayClear = (array, to) => array.splice(0, to);
|
|
8
|
+
const arrayPush = (array, value) => array.push(value);
|
|
9
|
+
const arrayPop = (array) => array.pop();
|
|
8
10
|
|
|
9
11
|
const isUndefined = (thing) => thing == void 0;
|
|
10
12
|
const ifNotUndefined = (value, then, otherwise) =>
|
|
@@ -76,7 +78,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
76
78
|
const allListeners = mapNew();
|
|
77
79
|
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
78
80
|
thing ??= getThing();
|
|
79
|
-
const id = listenerPool
|
|
81
|
+
const id = arrayPop(listenerPool) ?? '' + nextId++;
|
|
80
82
|
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
81
83
|
addDeepSet(deepSet, id, idOrNulls);
|
|
82
84
|
return id;
|
|
@@ -97,7 +99,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
97
99
|
forDeepSet(collDel)(deepSet, id, ...idOrNulls);
|
|
98
100
|
mapSet(allListeners, id);
|
|
99
101
|
if (arrayLength(listenerPool) < 1e3) {
|
|
100
|
-
listenerPool
|
|
102
|
+
arrayPush(listenerPool, id);
|
|
101
103
|
}
|
|
102
104
|
return idOrNulls;
|
|
103
105
|
},
|
|
@@ -173,7 +175,7 @@ const createCheckpoints = getCreateFunction((store) => {
|
|
|
173
175
|
(_store, tableId, rowId, cellId, newCell, oldCell) => {
|
|
174
176
|
if (listening) {
|
|
175
177
|
ifNotUndefined(currentId, () => {
|
|
176
|
-
backwardIds
|
|
178
|
+
arrayPush(backwardIds, currentId);
|
|
177
179
|
trimBackwardsIds();
|
|
178
180
|
clearCheckpointIds(forwardIds);
|
|
179
181
|
currentId = void 0;
|
|
@@ -192,7 +194,7 @@ const createCheckpoints = getCreateFunction((store) => {
|
|
|
192
194
|
if (collIsEmpty(mapSet(row, cellId))) {
|
|
193
195
|
if (collIsEmpty(mapSet(table, rowId))) {
|
|
194
196
|
if (collIsEmpty(mapSet(delta, tableId))) {
|
|
195
|
-
currentId = backwardIds
|
|
197
|
+
currentId = arrayPop(backwardIds);
|
|
196
198
|
checkpointsChanged = 1;
|
|
197
199
|
}
|
|
198
200
|
}
|
|
@@ -216,13 +218,13 @@ const createCheckpoints = getCreateFunction((store) => {
|
|
|
216
218
|
if (!arrayIsEmpty(backwardIds)) {
|
|
217
219
|
forwardIds.unshift(addCheckpointImpl());
|
|
218
220
|
updateStore(0, currentId);
|
|
219
|
-
currentId = backwardIds
|
|
221
|
+
currentId = arrayPop(backwardIds);
|
|
220
222
|
checkpointsChanged = 1;
|
|
221
223
|
}
|
|
222
224
|
};
|
|
223
225
|
const goForwardImpl = () => {
|
|
224
226
|
if (!arrayIsEmpty(forwardIds)) {
|
|
225
|
-
backwardIds
|
|
227
|
+
arrayPush(backwardIds, currentId);
|
|
226
228
|
currentId = forwardIds.shift();
|
|
227
229
|
updateStore(1, currentId);
|
|
228
230
|
checkpointsChanged = 1;
|
package/lib/debug/indexes.js
CHANGED
|
@@ -12,6 +12,8 @@ const arrayLength = (array) => array.length;
|
|
|
12
12
|
const arrayIsEmpty = (array) => arrayLength(array) == 0;
|
|
13
13
|
const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
|
|
14
14
|
const arrayFromSecond = (ids) => ids.slice(1);
|
|
15
|
+
const arrayPush = (array, value) => array.push(value);
|
|
16
|
+
const arrayPop = (array) => array.pop();
|
|
15
17
|
|
|
16
18
|
const isUndefined = (thing) => thing == void 0;
|
|
17
19
|
const ifNotUndefined = (value, then, otherwise) =>
|
|
@@ -203,7 +205,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
203
205
|
const allListeners = mapNew();
|
|
204
206
|
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
205
207
|
thing ??= getThing();
|
|
206
|
-
const id = listenerPool
|
|
208
|
+
const id = arrayPop(listenerPool) ?? '' + nextId++;
|
|
207
209
|
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
208
210
|
addDeepSet(deepSet, id, idOrNulls);
|
|
209
211
|
return id;
|
|
@@ -224,7 +226,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
224
226
|
forDeepSet(collDel)(deepSet, id, ...idOrNulls);
|
|
225
227
|
mapSet(allListeners, id);
|
|
226
228
|
if (arrayLength(listenerPool) < 1e3) {
|
|
227
|
-
listenerPool
|
|
229
|
+
arrayPush(listenerPool, id);
|
|
228
230
|
}
|
|
229
231
|
return idOrNulls;
|
|
230
232
|
},
|
package/lib/debug/metrics.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export type Metric = number;
|
|
|
37
37
|
export type MetricCallback = (metricId: Id, metric?: Metric) => void;
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* The Aggregate type describes a custom function that takes an array
|
|
40
|
+
* The Aggregate type describes a custom function that takes an array of numbers
|
|
41
41
|
* and returns an aggregate that is used as a Metric.
|
|
42
42
|
*
|
|
43
43
|
* There are a number of common predefined aggregators, such as for counting,
|
package/lib/debug/metrics.js
CHANGED
|
@@ -13,6 +13,8 @@ const arrayLength = (array) => array.length;
|
|
|
13
13
|
const arrayIsEmpty = (array) => arrayLength(array) == 0;
|
|
14
14
|
const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
|
|
15
15
|
const arrayFromSecond = (ids) => ids.slice(1);
|
|
16
|
+
const arrayPush = (array, value) => array.push(value);
|
|
17
|
+
const arrayPop = (array) => array.pop();
|
|
16
18
|
|
|
17
19
|
const mathMax = Math.max;
|
|
18
20
|
const mathMin = Math.min;
|
|
@@ -206,7 +208,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
206
208
|
const allListeners = mapNew();
|
|
207
209
|
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
208
210
|
thing ??= getThing();
|
|
209
|
-
const id = listenerPool
|
|
211
|
+
const id = arrayPop(listenerPool) ?? '' + nextId++;
|
|
210
212
|
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
211
213
|
addDeepSet(deepSet, id, idOrNulls);
|
|
212
214
|
return id;
|
|
@@ -227,7 +229,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
227
229
|
forDeepSet(collDel)(deepSet, id, ...idOrNulls);
|
|
228
230
|
mapSet(allListeners, id);
|
|
229
231
|
if (arrayLength(listenerPool) < 1e3) {
|
|
230
|
-
listenerPool
|
|
232
|
+
arrayPush(listenerPool, id);
|
|
231
233
|
}
|
|
232
234
|
return idOrNulls;
|
|
233
235
|
},
|
|
@@ -7,6 +7,8 @@ const arrayLength = (array) => array.length;
|
|
|
7
7
|
const arrayIsEmpty = (array) => arrayLength(array) == 0;
|
|
8
8
|
const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
|
|
9
9
|
const arrayFromSecond = (ids) => ids.slice(1);
|
|
10
|
+
const arrayPush = (array, value) => array.push(value);
|
|
11
|
+
const arrayPop = (array) => array.pop();
|
|
10
12
|
|
|
11
13
|
const isUndefined = (thing) => thing == void 0;
|
|
12
14
|
const ifNotUndefined = (value, then, otherwise) =>
|
|
@@ -196,7 +198,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
196
198
|
const allListeners = mapNew();
|
|
197
199
|
const addListener = (listener, deepSet, idOrNulls = []) => {
|
|
198
200
|
thing ??= getThing();
|
|
199
|
-
const id = listenerPool
|
|
201
|
+
const id = arrayPop(listenerPool) ?? '' + nextId++;
|
|
200
202
|
mapSet(allListeners, id, [listener, deepSet, idOrNulls]);
|
|
201
203
|
addDeepSet(deepSet, id, idOrNulls);
|
|
202
204
|
return id;
|
|
@@ -217,7 +219,7 @@ const getListenerFunctions = (getThing) => {
|
|
|
217
219
|
forDeepSet(collDel)(deepSet, id, ...idOrNulls);
|
|
218
220
|
mapSet(allListeners, id);
|
|
219
221
|
if (arrayLength(listenerPool) < 1e3) {
|
|
220
|
-
listenerPool
|
|
222
|
+
arrayPush(listenerPool, id);
|
|
221
223
|
}
|
|
222
224
|
return idOrNulls;
|
|
223
225
|
},
|
package/lib/debug/store.d.ts
CHANGED
|
@@ -332,6 +332,35 @@ export type CellListener = (
|
|
|
332
332
|
getCellChange: GetCellChange | undefined,
|
|
333
333
|
) => void;
|
|
334
334
|
|
|
335
|
+
/**
|
|
336
|
+
* The InvalidCellListener type describes a function that is used to listen to
|
|
337
|
+
* attempts to set invalid data to a Cell.
|
|
338
|
+
*
|
|
339
|
+
* A InvalidCellListener is provided when using the addInvalidCellListener
|
|
340
|
+
* method. See that method for specific examples.
|
|
341
|
+
*
|
|
342
|
+
* When called, a InvalidCellListener is given a reference to the Store, the Id
|
|
343
|
+
* of the Table, the Id of the Row, and the Id of Cell that were being attempted
|
|
344
|
+
* to be changed. It is also given the invalid value of the Cell, which could
|
|
345
|
+
* have been of absolutely any type. Since there could have been multiple failed
|
|
346
|
+
* attempts to set the Cell within a single transaction, this is an array
|
|
347
|
+
* containing each attempt, chronologically.
|
|
348
|
+
*
|
|
349
|
+
* @param store A reference to the Store that was being changed.
|
|
350
|
+
* @param tableId The Id of the Table that was being changed.
|
|
351
|
+
* @param rowId The Id of the Row that was being changed.
|
|
352
|
+
* @param cellId The Id of the Cell that was being changed.
|
|
353
|
+
* @param invalidCells An array of the values of the Cell that were invalid.
|
|
354
|
+
* @category Listener
|
|
355
|
+
*/
|
|
356
|
+
export type InvalidCellListener = (
|
|
357
|
+
store: Store,
|
|
358
|
+
tableId: Id,
|
|
359
|
+
rowId: Id,
|
|
360
|
+
cellId: Id,
|
|
361
|
+
invalidCells: any[],
|
|
362
|
+
) => void;
|
|
363
|
+
|
|
335
364
|
/**
|
|
336
365
|
* The GetCellChange type describes a function that returns information about
|
|
337
366
|
* any Cell's changes during a transaction.
|
|
@@ -545,6 +574,9 @@ export type StoreListenerStats = {
|
|
|
545
574
|
* unique Id. And the setPartialRow method lets you update multiple Cell values
|
|
546
575
|
* in a Row without affecting the others.
|
|
547
576
|
*
|
|
577
|
+
* You can listen to attempts to write invalid data to a Cell with the
|
|
578
|
+
* addInvalidCellListener method.
|
|
579
|
+
*
|
|
548
580
|
* The transaction method is used to wrap multiple changes to the Store so that
|
|
549
581
|
* the relevant listeners only fire once.
|
|
550
582
|
*
|
|
@@ -2371,6 +2403,235 @@ export interface Store {
|
|
|
2371
2403
|
mutator?: boolean,
|
|
2372
2404
|
): Id;
|
|
2373
2405
|
|
|
2406
|
+
/**
|
|
2407
|
+
* The addInvalidCellListener method registers a listener function with the
|
|
2408
|
+
* Store that will be called whenever invalid data was attempted to be written
|
|
2409
|
+
* to a Cell.
|
|
2410
|
+
*
|
|
2411
|
+
* You can either listen to a single Cell (by specifying the Table Id, Row Id,
|
|
2412
|
+
* and Cell Id as the method's first three parameters) or invalid attempts to
|
|
2413
|
+
* change any Cell (by providing `null` wildcards).
|
|
2414
|
+
*
|
|
2415
|
+
* All, some, or none of the `tableId`, `rowId`, and `cellId` parameters can
|
|
2416
|
+
* be wildcarded with `null`. You can listen to a specific Cell in a specific
|
|
2417
|
+
* Row in a specific Table, any Cell in any Row in any Table, for example - or
|
|
2418
|
+
* every other combination of wildcards.
|
|
2419
|
+
*
|
|
2420
|
+
* The provided listener is an InvalidCellListener function, and will be
|
|
2421
|
+
* called with a reference to the Store, the Id of the Table, the Id of the
|
|
2422
|
+
* Row, and the Id of Cell that were being attempted to be changed. It is also
|
|
2423
|
+
* given the invalid value of the Cell, which could have been of absolutely
|
|
2424
|
+
* any type. Since there could have been multiple failed attempts to set the
|
|
2425
|
+
* Cell within a single transaction, this is an array containing each attempt,
|
|
2426
|
+
* chronologically.
|
|
2427
|
+
*
|
|
2428
|
+
* Use the optional mutator parameter to indicate that there is code in the
|
|
2429
|
+
* listener that will mutate Store data. If set to `false` (or omitted), such
|
|
2430
|
+
* mutations will be silently ignored. All relevant mutator listeners (with
|
|
2431
|
+
* this flag set to `true`) are called _before_ any non-mutator listeners
|
|
2432
|
+
* (since the latter may become relevant due to changes made in the former).
|
|
2433
|
+
* The changes made by mutator listeners do not fire other mutating listeners,
|
|
2434
|
+
* though they will fire non-mutator listeners.
|
|
2435
|
+
*
|
|
2436
|
+
* Special note should be made for how the listener will be called when a
|
|
2437
|
+
* Schema is present. The listener will be called:
|
|
2438
|
+
*
|
|
2439
|
+
* - if a Table is being updated that is not specified in the Schema
|
|
2440
|
+
* - if a Cell is of the wrong type specified in the Schema
|
|
2441
|
+
* - if a Cell is omitted and is not defaulted in the Schema
|
|
2442
|
+
* - if an empty Row is provided and there are no Cell defaults in the Schema
|
|
2443
|
+
*
|
|
2444
|
+
* The listener will not be called if Cell that is defaulted in the Schema is
|
|
2445
|
+
* not provided, as long as all of the Cells that are _not_ defaulted _are_
|
|
2446
|
+
* provided.
|
|
2447
|
+
*
|
|
2448
|
+
* To help understand all of these schema-based conditions, please see the
|
|
2449
|
+
* Schema example below.
|
|
2450
|
+
*
|
|
2451
|
+
* @param tableId The Id of the Table to listen to, or `null` as a wildcard.
|
|
2452
|
+
* @param rowId The Id of the Row to listen to, or `null` as a wildcard.
|
|
2453
|
+
* @param cellId The Id of the Cell to listen to, or `null` as a wildcard.
|
|
2454
|
+
* @param listener The function that will be called whenever an attempt to
|
|
2455
|
+
* write invalid data to the matching Cell was made.
|
|
2456
|
+
* @param mutator An optional boolean that indicates that the listener mutates
|
|
2457
|
+
* Store data.
|
|
2458
|
+
* @returns A unique Id for the listener that can later be used to call it
|
|
2459
|
+
* explicitly, or to remove it.
|
|
2460
|
+
* @example
|
|
2461
|
+
* This example registers a listener that responds to any invalid changes to a
|
|
2462
|
+
* specific Cell.
|
|
2463
|
+
*
|
|
2464
|
+
* ```js
|
|
2465
|
+
* const store = createStore().setTables({
|
|
2466
|
+
* pets: {fido: {species: 'dog', color: 'brown'}},
|
|
2467
|
+
* });
|
|
2468
|
+
* const listenerId = store.addInvalidCellListener(
|
|
2469
|
+
* 'pets',
|
|
2470
|
+
* 'fido',
|
|
2471
|
+
* 'color',
|
|
2472
|
+
* (store, tableId, rowId, cellId, invalidCells) => {
|
|
2473
|
+
* console.log('Invalid color cell in fido row in pets table');
|
|
2474
|
+
* console.log(invalidCells);
|
|
2475
|
+
* },
|
|
2476
|
+
* );
|
|
2477
|
+
*
|
|
2478
|
+
* store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
|
|
2479
|
+
* // -> 'Invalid color cell in fido row in pets table'
|
|
2480
|
+
* // -> [{r: '96', g: '4B', b: '00'}]
|
|
2481
|
+
*
|
|
2482
|
+
* store.delListener(listenerId);
|
|
2483
|
+
* ```
|
|
2484
|
+
* @example
|
|
2485
|
+
* This example registers a listener that responds to any invalid changes to
|
|
2486
|
+
* any Cell - in a Store _without_ a Schema. Note also how it then responds to
|
|
2487
|
+
* cases where an empty or invalid Row objects, or Table objects, or Tables
|
|
2488
|
+
* objects are provided.
|
|
2489
|
+
*
|
|
2490
|
+
* ```js
|
|
2491
|
+
* const store = createStore().setTables({
|
|
2492
|
+
* pets: {fido: {species: 'dog', color: 'brown'}},
|
|
2493
|
+
* });
|
|
2494
|
+
* const listenerId = store.addInvalidCellListener(
|
|
2495
|
+
* null,
|
|
2496
|
+
* null,
|
|
2497
|
+
* null,
|
|
2498
|
+
* (store, tableId, rowId, cellId) => {
|
|
2499
|
+
* console.log(
|
|
2500
|
+
* `Invalid ${cellId} cell in ${rowId} row in ${tableId} table`,
|
|
2501
|
+
* );
|
|
2502
|
+
* },
|
|
2503
|
+
* );
|
|
2504
|
+
*
|
|
2505
|
+
* store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
|
|
2506
|
+
* // -> 'Invalid color cell in fido row in pets table'
|
|
2507
|
+
* store.setTable('sales', {fido: {date: new Date()}});
|
|
2508
|
+
* // -> 'Invalid date cell in fido row in sales table'
|
|
2509
|
+
*
|
|
2510
|
+
* store.setRow('pets', 'felix', {});
|
|
2511
|
+
* // -> 'Invalid undefined cell in felix row in pets table'
|
|
2512
|
+
*
|
|
2513
|
+
* store.setRow('filter', 'name', /[a-z]?/);
|
|
2514
|
+
* // -> 'Invalid undefined cell in name row in filter table'
|
|
2515
|
+
*
|
|
2516
|
+
* store.setRow('sales', '2021', {forecast: undefined});
|
|
2517
|
+
* // -> 'Invalid forecast cell in 2021 row in sales table'
|
|
2518
|
+
*
|
|
2519
|
+
* store.addRow('filter', /[0-9]?/);
|
|
2520
|
+
* // -> 'Invalid undefined cell in undefined row in filter table'
|
|
2521
|
+
*
|
|
2522
|
+
* store.setTable('raw', {});
|
|
2523
|
+
* // -> 'Invalid undefined cell in undefined row in raw table'
|
|
2524
|
+
*
|
|
2525
|
+
* store.setTable('raw', ['row1', 'row2']);
|
|
2526
|
+
* // -> 'Invalid undefined cell in undefined row in raw table'
|
|
2527
|
+
*
|
|
2528
|
+
* store.setTables(['table1', 'table2']);
|
|
2529
|
+
* // -> 'Invalid undefined cell in undefined row in undefined table'
|
|
2530
|
+
*
|
|
2531
|
+
* store.delListener(listenerId);
|
|
2532
|
+
* ```
|
|
2533
|
+
* @example
|
|
2534
|
+
* This example registers a listener that responds to any invalid changes to
|
|
2535
|
+
* any Cell - in a Store _with_ a Schema. Note how it responds to cases where
|
|
2536
|
+
* missing parameters are provided for optional, and defaulted Cell values in
|
|
2537
|
+
* a Row.
|
|
2538
|
+
*
|
|
2539
|
+
* ```js
|
|
2540
|
+
* const store = createStore().setSchema({
|
|
2541
|
+
* pets: {
|
|
2542
|
+
* species: {type: 'string'},
|
|
2543
|
+
* color: {type: 'string', default: 'unknown'},
|
|
2544
|
+
* },
|
|
2545
|
+
* });
|
|
2546
|
+
*
|
|
2547
|
+
* const listenerId = store.addInvalidCellListener(
|
|
2548
|
+
* null,
|
|
2549
|
+
* null,
|
|
2550
|
+
* null,
|
|
2551
|
+
* (store, tableId, rowId, cellId) => {
|
|
2552
|
+
* console.log(
|
|
2553
|
+
* `Invalid ${cellId} cell in ${rowId} row in ${tableId} table`,
|
|
2554
|
+
* );
|
|
2555
|
+
* },
|
|
2556
|
+
* );
|
|
2557
|
+
*
|
|
2558
|
+
* store.setRow('sales', 'fido', {price: 5});
|
|
2559
|
+
* // -> 'Invalid price cell in fido row in sales table'
|
|
2560
|
+
* // The listener is called, because the sales Table is not in the schema
|
|
2561
|
+
*
|
|
2562
|
+
* store.setRow('pets', 'felix', {species: true});
|
|
2563
|
+
* // -> 'Invalid species cell in felix row in pets table'
|
|
2564
|
+
* // The listener is called, because species is invalid...
|
|
2565
|
+
* console.log(store.getRow('pets', 'felix'));
|
|
2566
|
+
* // -> {color: 'unknown'}
|
|
2567
|
+
* // ...even though a Row was set with the default value
|
|
2568
|
+
*
|
|
2569
|
+
* store.setRow('pets', 'fido', {color: 'brown'});
|
|
2570
|
+
* // -> 'Invalid species cell in fido row in pets table'
|
|
2571
|
+
* // The listener is called, because species is missing and not defaulted...
|
|
2572
|
+
* console.log(store.getRow('pets', 'fido'));
|
|
2573
|
+
* // -> {color: 'brown'}
|
|
2574
|
+
* // ...even though a Row was set
|
|
2575
|
+
*
|
|
2576
|
+
* store.setRow('pets', 'rex', {species: 'dog'});
|
|
2577
|
+
* console.log(store.getRow('pets', 'rex'));
|
|
2578
|
+
* // -> {species: 'dog', color: 'unknown'}
|
|
2579
|
+
* // The listener is not called, because color is defaulted
|
|
2580
|
+
*
|
|
2581
|
+
* store.delTables().setSchema({
|
|
2582
|
+
* pets: {
|
|
2583
|
+
* species: {type: 'string'},
|
|
2584
|
+
* color: {type: 'string'},
|
|
2585
|
+
* },
|
|
2586
|
+
* });
|
|
2587
|
+
*
|
|
2588
|
+
* store.setRow('pets', 'cujo', {});
|
|
2589
|
+
* // -> 'Invalid species cell in cujo row in pets table'
|
|
2590
|
+
* // -> 'Invalid color cell in cujo row in pets table'
|
|
2591
|
+
* // -> 'Invalid undefined cell in cujo row in pets table'
|
|
2592
|
+
* // The listener is called multiple times, because neither Cell is defaulted
|
|
2593
|
+
* // and the Row as a whole is empty
|
|
2594
|
+
*
|
|
2595
|
+
* store.delListener(listenerId);
|
|
2596
|
+
* ```
|
|
2597
|
+
* @example
|
|
2598
|
+
* This example registers a listener that responds to any changes to a
|
|
2599
|
+
* specific Cell, and which also mutates the Store itself.
|
|
2600
|
+
*
|
|
2601
|
+
* ```js
|
|
2602
|
+
* const store = createStore().setTables({
|
|
2603
|
+
* pets: {fido: {species: 'dog', color: 'brown'}},
|
|
2604
|
+
* });
|
|
2605
|
+
* const listenerId = store.addInvalidCellListener(
|
|
2606
|
+
* 'pets',
|
|
2607
|
+
* 'fido',
|
|
2608
|
+
* 'color',
|
|
2609
|
+
* (store, tableId, rowId, cellId, invalidCells) =>
|
|
2610
|
+
* store.setCell(
|
|
2611
|
+
* 'meta',
|
|
2612
|
+
* 'invalid_updates',
|
|
2613
|
+
* `${tableId}_${rowId}_${cellId}`,
|
|
2614
|
+
* JSON.stringify(invalidCells[0]),
|
|
2615
|
+
* ),
|
|
2616
|
+
* true,
|
|
2617
|
+
* );
|
|
2618
|
+
*
|
|
2619
|
+
* store.setCell('pets', 'fido', 'color', {r: '96', g: '4B', b: '00'});
|
|
2620
|
+
* console.log(store.getRow('meta', 'invalid_updates'));
|
|
2621
|
+
* // -> {'pets_fido_color': '{"r":"96","g":"4B","b":"00"}'}
|
|
2622
|
+
*
|
|
2623
|
+
* store.delListener(listenerId);
|
|
2624
|
+
* ```
|
|
2625
|
+
* @category Listener
|
|
2626
|
+
*/
|
|
2627
|
+
addInvalidCellListener(
|
|
2628
|
+
tableId: IdOrNull,
|
|
2629
|
+
rowId: IdOrNull,
|
|
2630
|
+
cellId: IdOrNull,
|
|
2631
|
+
listener: InvalidCellListener,
|
|
2632
|
+
mutator?: boolean,
|
|
2633
|
+
): Id;
|
|
2634
|
+
|
|
2374
2635
|
/**
|
|
2375
2636
|
* The callListener method provides a way for you to manually provoke a
|
|
2376
2637
|
* listener to be called, even if the underlying data hasn't changed.
|