tinybase 1.1.0-beta.2 → 1.2.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/debug/metrics.d.ts +1 -1
- package/lib/debug/store.d.ts +117 -7
- package/lib/debug/store.js +59 -25
- package/lib/debug/tinybase.js +59 -25
- package/lib/metrics.d.ts +1 -1
- package/lib/store.d.ts +117 -7
- 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/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 +14 -14
- package/readme.md +2 -2
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/store.d.ts
CHANGED
|
@@ -88,6 +88,18 @@ export type Row = {[cellId: Id]: Cell};
|
|
|
88
88
|
*/
|
|
89
89
|
export type Cell = string | number | boolean;
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* The CellOrUndefined type is a data structure representing the data in a
|
|
93
|
+
* single cell or the value `undefined`.
|
|
94
|
+
*
|
|
95
|
+
* This is used when describing a Cell that is present _or_ that is not present
|
|
96
|
+
* - such as when it has been deleted, or when describing a previous state where
|
|
97
|
+
* the Cell value has since been added.
|
|
98
|
+
*
|
|
99
|
+
* @category Store
|
|
100
|
+
*/
|
|
101
|
+
export type CellOrUndefined = Cell | undefined;
|
|
102
|
+
|
|
91
103
|
/**
|
|
92
104
|
* The TableCallback type describes a function that takes a Table's Id and a
|
|
93
105
|
* callback to loop over each Row within it.
|
|
@@ -149,7 +161,7 @@ export type CellCallback = (cellId: Id, cell: Cell) => void;
|
|
|
149
161
|
* @param cell The current value of the Cell to map to a new value.
|
|
150
162
|
* @category Callback
|
|
151
163
|
*/
|
|
152
|
-
export type MapCell = (cell:
|
|
164
|
+
export type MapCell = (cell: CellOrUndefined) => Cell;
|
|
153
165
|
|
|
154
166
|
/**
|
|
155
167
|
* The GetCell type describes a function that takes a Id and returns the Cell
|
|
@@ -162,7 +174,7 @@ export type MapCell = (cell: Cell | undefined) => Cell;
|
|
|
162
174
|
* @param cellId The Id of the Cell to fetch the value for.
|
|
163
175
|
* @category Callback
|
|
164
176
|
*/
|
|
165
|
-
export type GetCell = (cellId: Id) =>
|
|
177
|
+
export type GetCell = (cellId: Id) => CellOrUndefined;
|
|
166
178
|
|
|
167
179
|
/**
|
|
168
180
|
* The TablesListener type describes a function that is used to listen to
|
|
@@ -390,8 +402,8 @@ export type GetCellChange = (tableId: Id, rowId: Id, cellId: Id) => CellChange;
|
|
|
390
402
|
*/
|
|
391
403
|
export type CellChange = [
|
|
392
404
|
changed: boolean,
|
|
393
|
-
oldCell:
|
|
394
|
-
newCell:
|
|
405
|
+
oldCell: CellOrUndefined,
|
|
406
|
+
newCell: CellOrUndefined,
|
|
395
407
|
];
|
|
396
408
|
|
|
397
409
|
/**
|
|
@@ -461,6 +473,59 @@ export type CellSchema =
|
|
|
461
473
|
default?: boolean;
|
|
462
474
|
};
|
|
463
475
|
|
|
476
|
+
/**
|
|
477
|
+
* The ChangedCells type describes the Cell values that have been changed during
|
|
478
|
+
* a transaction, primarily used so that you can indicate whether the
|
|
479
|
+
* transaction should be rolled back.
|
|
480
|
+
*
|
|
481
|
+
* A ChangedCells object is provided to the `doRollback` callback when using the
|
|
482
|
+
* transaction method. See that method for specific examples.
|
|
483
|
+
*
|
|
484
|
+
* This type is a nested structure of Table, Row, and Cell objects, much like
|
|
485
|
+
* the Tables object, but one which provides both the old and new Cell values in
|
|
486
|
+
* a two-part array. These are describing the state of each changed Cell in
|
|
487
|
+
* Store at the _start_ of the transaction, and by the _end_ of the transaction.
|
|
488
|
+
*
|
|
489
|
+
* Hence, an `undefined` value for the first item in the array means that the
|
|
490
|
+
* Cell was added during the transaction. An `undefined` value for the second
|
|
491
|
+
* item in the array means that the Cell was removed during the transaction. An
|
|
492
|
+
* array with two different Cell values indicates that it was changed. The
|
|
493
|
+
* two-part array will never contain two items of the same value (including two
|
|
494
|
+
* `undefined` values), even if, during the transaction, a Cell was changed to a
|
|
495
|
+
* different value and then changed back.
|
|
496
|
+
*
|
|
497
|
+
* @category Transaction
|
|
498
|
+
*/
|
|
499
|
+
export type ChangedCells = {
|
|
500
|
+
[tableId: Id]: {
|
|
501
|
+
[rowId: Id]: {
|
|
502
|
+
[cellId: Id]: [CellOrUndefined, CellOrUndefined];
|
|
503
|
+
};
|
|
504
|
+
};
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* The InvalidCells type describes the invalid Cell values that have been
|
|
508
|
+
* attempted during a transaction, primarily used so that you can indicate
|
|
509
|
+
* whether the transaction should be rolled back.
|
|
510
|
+
*
|
|
511
|
+
* An InvalidCells object is provided to the `doRollback` callback when using
|
|
512
|
+
* the transaction method. See that method for specific examples.
|
|
513
|
+
*
|
|
514
|
+
* This type is a nested structure of Table, Row, and Cell objects, much like
|
|
515
|
+
* the Tables object, but one for which Cell values are listed in array
|
|
516
|
+
* (much like the InvalidCellListener type) so that multiple failed attempts to
|
|
517
|
+
* change a Cell during the transaction are described.
|
|
518
|
+
*
|
|
519
|
+
* @category Transaction
|
|
520
|
+
*/
|
|
521
|
+
export type InvalidCells = {
|
|
522
|
+
[tableId: Id]: {
|
|
523
|
+
[rowId: Id]: {
|
|
524
|
+
[cellId: Id]: any[];
|
|
525
|
+
};
|
|
526
|
+
};
|
|
527
|
+
};
|
|
528
|
+
|
|
464
529
|
/**
|
|
465
530
|
* The StoreListenerStats type describes the number of listeners registered with
|
|
466
531
|
* the Store, and can be used for debugging purposes.
|
|
@@ -897,7 +962,7 @@ export interface Store {
|
|
|
897
962
|
* ```
|
|
898
963
|
* @category Getter
|
|
899
964
|
*/
|
|
900
|
-
getCell(tableId: Id, rowId: Id, cellId: Id):
|
|
965
|
+
getCell(tableId: Id, rowId: Id, cellId: Id): CellOrUndefined;
|
|
901
966
|
|
|
902
967
|
/**
|
|
903
968
|
* The hasTables method returns a boolean indicating whether any Table objects
|
|
@@ -1603,7 +1668,15 @@ export interface Store {
|
|
|
1603
1668
|
* Transactions can be nested. Relevant listeners will be called only when the
|
|
1604
1669
|
* outermost one completes.
|
|
1605
1670
|
*
|
|
1671
|
+
* The second, optional parameter, `doRollback` is a callback that you can use
|
|
1672
|
+
* to rollback the transaction if it did not complete to your satisfaction. It
|
|
1673
|
+
* is called with `changedCells` and `invalidCells` parameters, which inform
|
|
1674
|
+
* you of the net changes that have been made during the transaction, and any
|
|
1675
|
+
* invalid attempts to do so, respectively.
|
|
1676
|
+
*
|
|
1606
1677
|
* @param actions The function to be executed as a transaction.
|
|
1678
|
+
* @param doRollback An optional callback that should return `true` if you
|
|
1679
|
+
* want to rollback the transaction at the end.
|
|
1607
1680
|
* @returns Whatever value the provided transaction function returns.
|
|
1608
1681
|
* @example
|
|
1609
1682
|
* This example makes changes to two Cells, first outside, and secondly
|
|
@@ -1653,9 +1726,46 @@ export interface Store {
|
|
|
1653
1726
|
* // -> undefined
|
|
1654
1727
|
* // No net change during the transaction, so the listener is not called.
|
|
1655
1728
|
* ```
|
|
1656
|
-
* @
|
|
1729
|
+
* @example
|
|
1730
|
+
* This example makes multiple changes to the Store, including some attempts
|
|
1731
|
+
* to update a Cell with invalid values. The `doRollback` callback receives
|
|
1732
|
+
* information about the changes and invalid attempts, and then judges that
|
|
1733
|
+
* the transaction should be rolled back to its original state.
|
|
1734
|
+
*
|
|
1735
|
+
* ```js
|
|
1736
|
+
* const store = createStore().setTables({
|
|
1737
|
+
* pets: {fido: {species: 'dog', color: 'brown'}},
|
|
1738
|
+
* });
|
|
1739
|
+
*
|
|
1740
|
+
* store.transaction(
|
|
1741
|
+
* () => {
|
|
1742
|
+
* store.setCell('pets', 'fido', 'color', 'black');
|
|
1743
|
+
* store.setCell('pets', 'fido', 'eyes', ['left', 'right']);
|
|
1744
|
+
* store.setCell('pets', 'fido', 'info', {sold: null});
|
|
1745
|
+
* },
|
|
1746
|
+
* (changedCells, invalidCells) => {
|
|
1747
|
+
* console.log(store.getTables());
|
|
1748
|
+
* // -> {pets: {fido: {species: 'dog', color: 'black'}}}
|
|
1749
|
+
* console.log(changedCells);
|
|
1750
|
+
* // -> {pets: {fido: {color: ['brown', 'black']}}}
|
|
1751
|
+
* console.log(invalidCells);
|
|
1752
|
+
* // -> {pets: {fido: {eyes: [['left', 'right']], info: [{sold: null}]}}}
|
|
1753
|
+
* return invalidCells['pets'] != null;
|
|
1754
|
+
* },
|
|
1755
|
+
* );
|
|
1756
|
+
*
|
|
1757
|
+
* console.log(store.getTables());
|
|
1758
|
+
* // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
|
|
1759
|
+
* ```
|
|
1760
|
+
* @category Transaction
|
|
1657
1761
|
*/
|
|
1658
|
-
transaction<Return>(
|
|
1762
|
+
transaction<Return>(
|
|
1763
|
+
actions: () => Return,
|
|
1764
|
+
doRollback?: (
|
|
1765
|
+
changedCells: ChangedCells,
|
|
1766
|
+
invalidCells: InvalidCells,
|
|
1767
|
+
) => boolean,
|
|
1768
|
+
): Return;
|
|
1659
1769
|
|
|
1660
1770
|
/**
|
|
1661
1771
|
* The forEachTable method takes a function that it will then call for each
|
package/lib/debug/store.js
CHANGED
|
@@ -8,6 +8,7 @@ const FUNCTION = getTypeOf(getTypeOf);
|
|
|
8
8
|
const TYPE = 'type';
|
|
9
9
|
const DEFAULT = 'default';
|
|
10
10
|
|
|
11
|
+
const arrayPair = (value) => [value, value];
|
|
11
12
|
const arrayHas = (array, value) => array.includes(value);
|
|
12
13
|
const arrayForEach = (array, cb) => array.forEach(cb);
|
|
13
14
|
const arrayLength = (array) => array.length;
|
|
@@ -69,10 +70,14 @@ const mapEnsure = (map, key, defaultValue, onWillAdd) => {
|
|
|
69
70
|
}
|
|
70
71
|
return mapGet(map, key);
|
|
71
72
|
};
|
|
72
|
-
const mapToObj = (map, childMapper) => {
|
|
73
|
+
const mapToObj = (map, childMapper, childExclude) => {
|
|
73
74
|
const obj = {};
|
|
74
75
|
const mapper = childMapper ?? ((mapValue) => mapValue);
|
|
75
|
-
collForEach(map, (value, key) =>
|
|
76
|
+
collForEach(map, (value, key) =>
|
|
77
|
+
ifNotUndefined(mapper(value), (mappedValue) =>
|
|
78
|
+
childExclude?.(mappedValue) ? 0 : (obj[key] = mappedValue),
|
|
79
|
+
),
|
|
80
|
+
);
|
|
76
81
|
return obj;
|
|
77
82
|
};
|
|
78
83
|
const mapClone = (map, childMapper) => {
|
|
@@ -81,6 +86,7 @@ const mapClone = (map, childMapper) => {
|
|
|
81
86
|
collForEach(map, (value, key) => map2.set(key, mapper(value)));
|
|
82
87
|
return map2;
|
|
83
88
|
};
|
|
89
|
+
const mapClone2 = (map) => mapClone(map, mapClone);
|
|
84
90
|
|
|
85
91
|
const object = Object;
|
|
86
92
|
const objIds = object.keys;
|
|
@@ -355,7 +361,7 @@ const createStore = () => {
|
|
|
355
361
|
}
|
|
356
362
|
const oldCell = mapGet(rowMap, cellId);
|
|
357
363
|
if (newCell !== oldCell) {
|
|
358
|
-
cellChanged(tableId, rowId, cellId, oldCell);
|
|
364
|
+
cellChanged(tableId, rowId, cellId, oldCell, newCell);
|
|
359
365
|
mapSet(rowMap, cellId, newCell);
|
|
360
366
|
}
|
|
361
367
|
};
|
|
@@ -412,12 +418,12 @@ const createStore = () => {
|
|
|
412
418
|
cellId,
|
|
413
419
|
added,
|
|
414
420
|
);
|
|
415
|
-
const cellChanged = (tableId, rowId, cellId, oldCell) =>
|
|
416
|
-
mapEnsure(
|
|
421
|
+
const cellChanged = (tableId, rowId, cellId, oldCell, newCell) =>
|
|
422
|
+
(mapEnsure(
|
|
417
423
|
mapEnsure(mapEnsure(changedCells, tableId, mapNew()), rowId, mapNew()),
|
|
418
424
|
cellId,
|
|
419
|
-
oldCell,
|
|
420
|
-
);
|
|
425
|
+
[oldCell],
|
|
426
|
+
)[1] = newCell);
|
|
421
427
|
const cellInvalid = (tableId, rowId, cellId, invalidCell, defaultedCell) => {
|
|
422
428
|
arrayPush(
|
|
423
429
|
mapEnsure(
|
|
@@ -429,19 +435,16 @@ const createStore = () => {
|
|
|
429
435
|
);
|
|
430
436
|
return defaultedCell;
|
|
431
437
|
};
|
|
432
|
-
const getCellChange = (tableId, rowId, cellId) =>
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
};
|
|
438
|
+
const getCellChange = (tableId, rowId, cellId) =>
|
|
439
|
+
ifNotUndefined(
|
|
440
|
+
mapGet(mapGet(mapGet(changedCells, tableId), rowId), cellId),
|
|
441
|
+
([oldCell, newCell]) => [true, oldCell, newCell],
|
|
442
|
+
() => [false, ...arrayPair(getCell(tableId, rowId, cellId))],
|
|
443
|
+
);
|
|
439
444
|
const callInvalidCellListeners = (mutator) =>
|
|
440
445
|
!collIsEmpty(invalidCells) && !collIsEmpty(invalidCellListeners[mutator])
|
|
441
446
|
? collForEach(
|
|
442
|
-
mutator
|
|
443
|
-
? mapClone(invalidCells, (table) => mapClone(table, mapClone))
|
|
444
|
-
: invalidCells,
|
|
447
|
+
mutator ? mapClone(invalidCells, mapClone2) : invalidCells,
|
|
445
448
|
(rows, tableId) =>
|
|
446
449
|
collForEach(rows, (cells, rowId) =>
|
|
447
450
|
collForEach(cells, (invalidCell, cellId) =>
|
|
@@ -469,9 +472,9 @@ const createStore = () => {
|
|
|
469
472
|
const changes = mutator
|
|
470
473
|
? [
|
|
471
474
|
mapClone(changedTableIds),
|
|
472
|
-
|
|
473
|
-
mapClone(changedCellIds,
|
|
474
|
-
mapClone(changedCells,
|
|
475
|
+
mapClone2(changedRowIds),
|
|
476
|
+
mapClone(changedCellIds, mapClone2),
|
|
477
|
+
mapClone(changedCells, mapClone2),
|
|
475
478
|
]
|
|
476
479
|
: [changedTableIds, changedRowIds, changedCellIds, changedCells];
|
|
477
480
|
if (!emptyIdListeners) {
|
|
@@ -497,8 +500,7 @@ const createStore = () => {
|
|
|
497
500
|
let tableChanged;
|
|
498
501
|
collForEach(rows, (cells, rowId) => {
|
|
499
502
|
let rowChanged;
|
|
500
|
-
collForEach(cells, (oldCell, cellId) => {
|
|
501
|
-
const newCell = getCell(tableId, rowId, cellId);
|
|
503
|
+
collForEach(cells, ([oldCell, newCell], cellId) => {
|
|
502
504
|
if (newCell !== oldCell) {
|
|
503
505
|
callListeners(
|
|
504
506
|
cellListeners[mutator],
|
|
@@ -534,7 +536,7 @@ const createStore = () => {
|
|
|
534
536
|
return store;
|
|
535
537
|
};
|
|
536
538
|
const getTables = () =>
|
|
537
|
-
mapToObj(tablesMap, (
|
|
539
|
+
mapToObj(tablesMap, (table) => mapToObj(table, mapToObj));
|
|
538
540
|
const getTableIds = () => mapKeys(tablesMap);
|
|
539
541
|
const getTable = (tableId) => mapToObj(mapGet(tablesMap, tableId), mapToObj);
|
|
540
542
|
const getRowIds = (tableId) => mapKeys(mapGet(tablesMap, tableId));
|
|
@@ -649,7 +651,7 @@ const createStore = () => {
|
|
|
649
651
|
setValidSchema({});
|
|
650
652
|
hasSchema = false;
|
|
651
653
|
});
|
|
652
|
-
const transaction = (actions) => {
|
|
654
|
+
const transaction = (actions, doRollback) => {
|
|
653
655
|
if (transactions == -1) {
|
|
654
656
|
return;
|
|
655
657
|
}
|
|
@@ -661,6 +663,38 @@ const createStore = () => {
|
|
|
661
663
|
callInvalidCellListeners(1);
|
|
662
664
|
callListenersForChanges(1);
|
|
663
665
|
transactions = -1;
|
|
666
|
+
if (
|
|
667
|
+
doRollback?.(
|
|
668
|
+
mapToObj(
|
|
669
|
+
changedCells,
|
|
670
|
+
(table) =>
|
|
671
|
+
mapToObj(
|
|
672
|
+
table,
|
|
673
|
+
(row) =>
|
|
674
|
+
mapToObj(
|
|
675
|
+
row,
|
|
676
|
+
(cells) => [...cells],
|
|
677
|
+
([oldCell, newCell]) => oldCell === newCell,
|
|
678
|
+
),
|
|
679
|
+
objIsEmpty,
|
|
680
|
+
),
|
|
681
|
+
objIsEmpty,
|
|
682
|
+
),
|
|
683
|
+
mapToObj(invalidCells, (map) => mapToObj(map, mapToObj)),
|
|
684
|
+
)
|
|
685
|
+
) {
|
|
686
|
+
transactions = 1;
|
|
687
|
+
collForEach(changedCells, (table, tableId) =>
|
|
688
|
+
collForEach(table, (row, rowId) =>
|
|
689
|
+
collForEach(row, ([oldCell], cellId) =>
|
|
690
|
+
isUndefined(oldCell)
|
|
691
|
+
? delCell(tableId, rowId, cellId, true)
|
|
692
|
+
: setCell(tableId, rowId, cellId, oldCell),
|
|
693
|
+
),
|
|
694
|
+
),
|
|
695
|
+
);
|
|
696
|
+
transactions = -1;
|
|
697
|
+
}
|
|
664
698
|
callInvalidCellListeners(0);
|
|
665
699
|
callListenersForChanges(0);
|
|
666
700
|
transactions = 0;
|
|
@@ -719,7 +753,7 @@ const createStore = () => {
|
|
|
719
753
|
]);
|
|
720
754
|
const callListener = (listenerId) => {
|
|
721
755
|
callListenerImpl(listenerId, [getTableIds, getRowIds, getCellIds], (ids) =>
|
|
722
|
-
isUndefined(ids[2]) ? [] :
|
|
756
|
+
isUndefined(ids[2]) ? [] : arrayPair(getCell(...ids)),
|
|
723
757
|
);
|
|
724
758
|
return store;
|
|
725
759
|
};
|
package/lib/debug/tinybase.js
CHANGED
|
@@ -15,6 +15,7 @@ const AVG = 'avg';
|
|
|
15
15
|
const MIN = 'min';
|
|
16
16
|
const MAX = 'max';
|
|
17
17
|
|
|
18
|
+
const arrayPair = (value) => [value, value];
|
|
18
19
|
const arrayHas = (array, value) => array.includes(value);
|
|
19
20
|
const arrayIsSorted = (array, sorter) =>
|
|
20
21
|
array.every(
|
|
@@ -87,10 +88,14 @@ const mapEnsure = (map, key, defaultValue, onWillAdd) => {
|
|
|
87
88
|
}
|
|
88
89
|
return mapGet(map, key);
|
|
89
90
|
};
|
|
90
|
-
const mapToObj = (map, childMapper) => {
|
|
91
|
+
const mapToObj = (map, childMapper, childExclude) => {
|
|
91
92
|
const obj = {};
|
|
92
93
|
const mapper = childMapper ?? ((mapValue) => mapValue);
|
|
93
|
-
collForEach(map, (value, key) =>
|
|
94
|
+
collForEach(map, (value, key) =>
|
|
95
|
+
ifNotUndefined(mapper(value), (mappedValue) =>
|
|
96
|
+
childExclude?.(mappedValue) ? 0 : (obj[key] = mappedValue),
|
|
97
|
+
),
|
|
98
|
+
);
|
|
94
99
|
return obj;
|
|
95
100
|
};
|
|
96
101
|
const mapClone = (map, childMapper) => {
|
|
@@ -99,6 +104,7 @@ const mapClone = (map, childMapper) => {
|
|
|
99
104
|
collForEach(map, (value, key) => map2.set(key, mapper(value)));
|
|
100
105
|
return map2;
|
|
101
106
|
};
|
|
107
|
+
const mapClone2 = (map) => mapClone(map, mapClone);
|
|
102
108
|
|
|
103
109
|
const setNew = (entries) => new Set(entries);
|
|
104
110
|
const setAdd = (set, value) => set?.add(value);
|
|
@@ -1381,7 +1387,7 @@ const createStore = () => {
|
|
|
1381
1387
|
}
|
|
1382
1388
|
const oldCell = mapGet(rowMap, cellId);
|
|
1383
1389
|
if (newCell !== oldCell) {
|
|
1384
|
-
cellChanged(tableId, rowId, cellId, oldCell);
|
|
1390
|
+
cellChanged(tableId, rowId, cellId, oldCell, newCell);
|
|
1385
1391
|
mapSet(rowMap, cellId, newCell);
|
|
1386
1392
|
}
|
|
1387
1393
|
};
|
|
@@ -1438,12 +1444,12 @@ const createStore = () => {
|
|
|
1438
1444
|
cellId,
|
|
1439
1445
|
added,
|
|
1440
1446
|
);
|
|
1441
|
-
const cellChanged = (tableId, rowId, cellId, oldCell) =>
|
|
1442
|
-
mapEnsure(
|
|
1447
|
+
const cellChanged = (tableId, rowId, cellId, oldCell, newCell) =>
|
|
1448
|
+
(mapEnsure(
|
|
1443
1449
|
mapEnsure(mapEnsure(changedCells, tableId, mapNew()), rowId, mapNew()),
|
|
1444
1450
|
cellId,
|
|
1445
|
-
oldCell,
|
|
1446
|
-
);
|
|
1451
|
+
[oldCell],
|
|
1452
|
+
)[1] = newCell);
|
|
1447
1453
|
const cellInvalid = (tableId, rowId, cellId, invalidCell, defaultedCell) => {
|
|
1448
1454
|
arrayPush(
|
|
1449
1455
|
mapEnsure(
|
|
@@ -1455,19 +1461,16 @@ const createStore = () => {
|
|
|
1455
1461
|
);
|
|
1456
1462
|
return defaultedCell;
|
|
1457
1463
|
};
|
|
1458
|
-
const getCellChange = (tableId, rowId, cellId) =>
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
};
|
|
1464
|
+
const getCellChange = (tableId, rowId, cellId) =>
|
|
1465
|
+
ifNotUndefined(
|
|
1466
|
+
mapGet(mapGet(mapGet(changedCells, tableId), rowId), cellId),
|
|
1467
|
+
([oldCell, newCell]) => [true, oldCell, newCell],
|
|
1468
|
+
() => [false, ...arrayPair(getCell(tableId, rowId, cellId))],
|
|
1469
|
+
);
|
|
1465
1470
|
const callInvalidCellListeners = (mutator) =>
|
|
1466
1471
|
!collIsEmpty(invalidCells) && !collIsEmpty(invalidCellListeners[mutator])
|
|
1467
1472
|
? collForEach(
|
|
1468
|
-
mutator
|
|
1469
|
-
? mapClone(invalidCells, (table) => mapClone(table, mapClone))
|
|
1470
|
-
: invalidCells,
|
|
1473
|
+
mutator ? mapClone(invalidCells, mapClone2) : invalidCells,
|
|
1471
1474
|
(rows, tableId) =>
|
|
1472
1475
|
collForEach(rows, (cells, rowId) =>
|
|
1473
1476
|
collForEach(cells, (invalidCell, cellId) =>
|
|
@@ -1495,9 +1498,9 @@ const createStore = () => {
|
|
|
1495
1498
|
const changes = mutator
|
|
1496
1499
|
? [
|
|
1497
1500
|
mapClone(changedTableIds),
|
|
1498
|
-
|
|
1499
|
-
mapClone(changedCellIds,
|
|
1500
|
-
mapClone(changedCells,
|
|
1501
|
+
mapClone2(changedRowIds),
|
|
1502
|
+
mapClone(changedCellIds, mapClone2),
|
|
1503
|
+
mapClone(changedCells, mapClone2),
|
|
1501
1504
|
]
|
|
1502
1505
|
: [changedTableIds, changedRowIds, changedCellIds, changedCells];
|
|
1503
1506
|
if (!emptyIdListeners) {
|
|
@@ -1523,8 +1526,7 @@ const createStore = () => {
|
|
|
1523
1526
|
let tableChanged;
|
|
1524
1527
|
collForEach(rows, (cells, rowId) => {
|
|
1525
1528
|
let rowChanged;
|
|
1526
|
-
collForEach(cells, (oldCell, cellId) => {
|
|
1527
|
-
const newCell = getCell(tableId, rowId, cellId);
|
|
1529
|
+
collForEach(cells, ([oldCell, newCell], cellId) => {
|
|
1528
1530
|
if (newCell !== oldCell) {
|
|
1529
1531
|
callListeners(
|
|
1530
1532
|
cellListeners[mutator],
|
|
@@ -1560,7 +1562,7 @@ const createStore = () => {
|
|
|
1560
1562
|
return store;
|
|
1561
1563
|
};
|
|
1562
1564
|
const getTables = () =>
|
|
1563
|
-
mapToObj(tablesMap, (
|
|
1565
|
+
mapToObj(tablesMap, (table) => mapToObj(table, mapToObj));
|
|
1564
1566
|
const getTableIds = () => mapKeys(tablesMap);
|
|
1565
1567
|
const getTable = (tableId) => mapToObj(mapGet(tablesMap, tableId), mapToObj);
|
|
1566
1568
|
const getRowIds = (tableId) => mapKeys(mapGet(tablesMap, tableId));
|
|
@@ -1675,7 +1677,7 @@ const createStore = () => {
|
|
|
1675
1677
|
setValidSchema({});
|
|
1676
1678
|
hasSchema = false;
|
|
1677
1679
|
});
|
|
1678
|
-
const transaction = (actions) => {
|
|
1680
|
+
const transaction = (actions, doRollback) => {
|
|
1679
1681
|
if (transactions == -1) {
|
|
1680
1682
|
return;
|
|
1681
1683
|
}
|
|
@@ -1687,6 +1689,38 @@ const createStore = () => {
|
|
|
1687
1689
|
callInvalidCellListeners(1);
|
|
1688
1690
|
callListenersForChanges(1);
|
|
1689
1691
|
transactions = -1;
|
|
1692
|
+
if (
|
|
1693
|
+
doRollback?.(
|
|
1694
|
+
mapToObj(
|
|
1695
|
+
changedCells,
|
|
1696
|
+
(table) =>
|
|
1697
|
+
mapToObj(
|
|
1698
|
+
table,
|
|
1699
|
+
(row) =>
|
|
1700
|
+
mapToObj(
|
|
1701
|
+
row,
|
|
1702
|
+
(cells) => [...cells],
|
|
1703
|
+
([oldCell, newCell]) => oldCell === newCell,
|
|
1704
|
+
),
|
|
1705
|
+
objIsEmpty,
|
|
1706
|
+
),
|
|
1707
|
+
objIsEmpty,
|
|
1708
|
+
),
|
|
1709
|
+
mapToObj(invalidCells, (map) => mapToObj(map, mapToObj)),
|
|
1710
|
+
)
|
|
1711
|
+
) {
|
|
1712
|
+
transactions = 1;
|
|
1713
|
+
collForEach(changedCells, (table, tableId) =>
|
|
1714
|
+
collForEach(table, (row, rowId) =>
|
|
1715
|
+
collForEach(row, ([oldCell], cellId) =>
|
|
1716
|
+
isUndefined(oldCell)
|
|
1717
|
+
? delCell(tableId, rowId, cellId, true)
|
|
1718
|
+
: setCell(tableId, rowId, cellId, oldCell),
|
|
1719
|
+
),
|
|
1720
|
+
),
|
|
1721
|
+
);
|
|
1722
|
+
transactions = -1;
|
|
1723
|
+
}
|
|
1690
1724
|
callInvalidCellListeners(0);
|
|
1691
1725
|
callListenersForChanges(0);
|
|
1692
1726
|
transactions = 0;
|
|
@@ -1745,7 +1779,7 @@ const createStore = () => {
|
|
|
1745
1779
|
]);
|
|
1746
1780
|
const callListener = (listenerId) => {
|
|
1747
1781
|
callListenerImpl(listenerId, [getTableIds, getRowIds, getCellIds], (ids) =>
|
|
1748
|
-
isUndefined(ids[2]) ? [] :
|
|
1782
|
+
isUndefined(ids[2]) ? [] : arrayPair(getCell(...ids)),
|
|
1749
1783
|
);
|
|
1750
1784
|
return store;
|
|
1751
1785
|
};
|
package/lib/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/store.d.ts
CHANGED
|
@@ -88,6 +88,18 @@ export type Row = {[cellId: Id]: Cell};
|
|
|
88
88
|
*/
|
|
89
89
|
export type Cell = string | number | boolean;
|
|
90
90
|
|
|
91
|
+
/**
|
|
92
|
+
* The CellOrUndefined type is a data structure representing the data in a
|
|
93
|
+
* single cell or the value `undefined`.
|
|
94
|
+
*
|
|
95
|
+
* This is used when describing a Cell that is present _or_ that is not present
|
|
96
|
+
* - such as when it has been deleted, or when describing a previous state where
|
|
97
|
+
* the Cell value has since been added.
|
|
98
|
+
*
|
|
99
|
+
* @category Store
|
|
100
|
+
*/
|
|
101
|
+
export type CellOrUndefined = Cell | undefined;
|
|
102
|
+
|
|
91
103
|
/**
|
|
92
104
|
* The TableCallback type describes a function that takes a Table's Id and a
|
|
93
105
|
* callback to loop over each Row within it.
|
|
@@ -149,7 +161,7 @@ export type CellCallback = (cellId: Id, cell: Cell) => void;
|
|
|
149
161
|
* @param cell The current value of the Cell to map to a new value.
|
|
150
162
|
* @category Callback
|
|
151
163
|
*/
|
|
152
|
-
export type MapCell = (cell:
|
|
164
|
+
export type MapCell = (cell: CellOrUndefined) => Cell;
|
|
153
165
|
|
|
154
166
|
/**
|
|
155
167
|
* The GetCell type describes a function that takes a Id and returns the Cell
|
|
@@ -162,7 +174,7 @@ export type MapCell = (cell: Cell | undefined) => Cell;
|
|
|
162
174
|
* @param cellId The Id of the Cell to fetch the value for.
|
|
163
175
|
* @category Callback
|
|
164
176
|
*/
|
|
165
|
-
export type GetCell = (cellId: Id) =>
|
|
177
|
+
export type GetCell = (cellId: Id) => CellOrUndefined;
|
|
166
178
|
|
|
167
179
|
/**
|
|
168
180
|
* The TablesListener type describes a function that is used to listen to
|
|
@@ -390,8 +402,8 @@ export type GetCellChange = (tableId: Id, rowId: Id, cellId: Id) => CellChange;
|
|
|
390
402
|
*/
|
|
391
403
|
export type CellChange = [
|
|
392
404
|
changed: boolean,
|
|
393
|
-
oldCell:
|
|
394
|
-
newCell:
|
|
405
|
+
oldCell: CellOrUndefined,
|
|
406
|
+
newCell: CellOrUndefined,
|
|
395
407
|
];
|
|
396
408
|
|
|
397
409
|
/**
|
|
@@ -461,6 +473,59 @@ export type CellSchema =
|
|
|
461
473
|
default?: boolean;
|
|
462
474
|
};
|
|
463
475
|
|
|
476
|
+
/**
|
|
477
|
+
* The ChangedCells type describes the Cell values that have been changed during
|
|
478
|
+
* a transaction, primarily used so that you can indicate whether the
|
|
479
|
+
* transaction should be rolled back.
|
|
480
|
+
*
|
|
481
|
+
* A ChangedCells object is provided to the `doRollback` callback when using the
|
|
482
|
+
* transaction method. See that method for specific examples.
|
|
483
|
+
*
|
|
484
|
+
* This type is a nested structure of Table, Row, and Cell objects, much like
|
|
485
|
+
* the Tables object, but one which provides both the old and new Cell values in
|
|
486
|
+
* a two-part array. These are describing the state of each changed Cell in
|
|
487
|
+
* Store at the _start_ of the transaction, and by the _end_ of the transaction.
|
|
488
|
+
*
|
|
489
|
+
* Hence, an `undefined` value for the first item in the array means that the
|
|
490
|
+
* Cell was added during the transaction. An `undefined` value for the second
|
|
491
|
+
* item in the array means that the Cell was removed during the transaction. An
|
|
492
|
+
* array with two different Cell values indicates that it was changed. The
|
|
493
|
+
* two-part array will never contain two items of the same value (including two
|
|
494
|
+
* `undefined` values), even if, during the transaction, a Cell was changed to a
|
|
495
|
+
* different value and then changed back.
|
|
496
|
+
*
|
|
497
|
+
* @category Transaction
|
|
498
|
+
*/
|
|
499
|
+
export type ChangedCells = {
|
|
500
|
+
[tableId: Id]: {
|
|
501
|
+
[rowId: Id]: {
|
|
502
|
+
[cellId: Id]: [CellOrUndefined, CellOrUndefined];
|
|
503
|
+
};
|
|
504
|
+
};
|
|
505
|
+
};
|
|
506
|
+
/**
|
|
507
|
+
* The InvalidCells type describes the invalid Cell values that have been
|
|
508
|
+
* attempted during a transaction, primarily used so that you can indicate
|
|
509
|
+
* whether the transaction should be rolled back.
|
|
510
|
+
*
|
|
511
|
+
* An InvalidCells object is provided to the `doRollback` callback when using
|
|
512
|
+
* the transaction method. See that method for specific examples.
|
|
513
|
+
*
|
|
514
|
+
* This type is a nested structure of Table, Row, and Cell objects, much like
|
|
515
|
+
* the Tables object, but one for which Cell values are listed in array
|
|
516
|
+
* (much like the InvalidCellListener type) so that multiple failed attempts to
|
|
517
|
+
* change a Cell during the transaction are described.
|
|
518
|
+
*
|
|
519
|
+
* @category Transaction
|
|
520
|
+
*/
|
|
521
|
+
export type InvalidCells = {
|
|
522
|
+
[tableId: Id]: {
|
|
523
|
+
[rowId: Id]: {
|
|
524
|
+
[cellId: Id]: any[];
|
|
525
|
+
};
|
|
526
|
+
};
|
|
527
|
+
};
|
|
528
|
+
|
|
464
529
|
/**
|
|
465
530
|
* The StoreListenerStats type describes the number of listeners registered with
|
|
466
531
|
* the Store, and can be used for debugging purposes.
|
|
@@ -897,7 +962,7 @@ export interface Store {
|
|
|
897
962
|
* ```
|
|
898
963
|
* @category Getter
|
|
899
964
|
*/
|
|
900
|
-
getCell(tableId: Id, rowId: Id, cellId: Id):
|
|
965
|
+
getCell(tableId: Id, rowId: Id, cellId: Id): CellOrUndefined;
|
|
901
966
|
|
|
902
967
|
/**
|
|
903
968
|
* The hasTables method returns a boolean indicating whether any Table objects
|
|
@@ -1603,7 +1668,15 @@ export interface Store {
|
|
|
1603
1668
|
* Transactions can be nested. Relevant listeners will be called only when the
|
|
1604
1669
|
* outermost one completes.
|
|
1605
1670
|
*
|
|
1671
|
+
* The second, optional parameter, `doRollback` is a callback that you can use
|
|
1672
|
+
* to rollback the transaction if it did not complete to your satisfaction. It
|
|
1673
|
+
* is called with `changedCells` and `invalidCells` parameters, which inform
|
|
1674
|
+
* you of the net changes that have been made during the transaction, and any
|
|
1675
|
+
* invalid attempts to do so, respectively.
|
|
1676
|
+
*
|
|
1606
1677
|
* @param actions The function to be executed as a transaction.
|
|
1678
|
+
* @param doRollback An optional callback that should return `true` if you
|
|
1679
|
+
* want to rollback the transaction at the end.
|
|
1607
1680
|
* @returns Whatever value the provided transaction function returns.
|
|
1608
1681
|
* @example
|
|
1609
1682
|
* This example makes changes to two Cells, first outside, and secondly
|
|
@@ -1653,9 +1726,46 @@ export interface Store {
|
|
|
1653
1726
|
* // -> undefined
|
|
1654
1727
|
* // No net change during the transaction, so the listener is not called.
|
|
1655
1728
|
* ```
|
|
1656
|
-
* @
|
|
1729
|
+
* @example
|
|
1730
|
+
* This example makes multiple changes to the Store, including some attempts
|
|
1731
|
+
* to update a Cell with invalid values. The `doRollback` callback receives
|
|
1732
|
+
* information about the changes and invalid attempts, and then judges that
|
|
1733
|
+
* the transaction should be rolled back to its original state.
|
|
1734
|
+
*
|
|
1735
|
+
* ```js
|
|
1736
|
+
* const store = createStore().setTables({
|
|
1737
|
+
* pets: {fido: {species: 'dog', color: 'brown'}},
|
|
1738
|
+
* });
|
|
1739
|
+
*
|
|
1740
|
+
* store.transaction(
|
|
1741
|
+
* () => {
|
|
1742
|
+
* store.setCell('pets', 'fido', 'color', 'black');
|
|
1743
|
+
* store.setCell('pets', 'fido', 'eyes', ['left', 'right']);
|
|
1744
|
+
* store.setCell('pets', 'fido', 'info', {sold: null});
|
|
1745
|
+
* },
|
|
1746
|
+
* (changedCells, invalidCells) => {
|
|
1747
|
+
* console.log(store.getTables());
|
|
1748
|
+
* // -> {pets: {fido: {species: 'dog', color: 'black'}}}
|
|
1749
|
+
* console.log(changedCells);
|
|
1750
|
+
* // -> {pets: {fido: {color: ['brown', 'black']}}}
|
|
1751
|
+
* console.log(invalidCells);
|
|
1752
|
+
* // -> {pets: {fido: {eyes: [['left', 'right']], info: [{sold: null}]}}}
|
|
1753
|
+
* return invalidCells['pets'] != null;
|
|
1754
|
+
* },
|
|
1755
|
+
* );
|
|
1756
|
+
*
|
|
1757
|
+
* console.log(store.getTables());
|
|
1758
|
+
* // -> {pets: {fido: {species: 'dog', color: 'brown'}}}
|
|
1759
|
+
* ```
|
|
1760
|
+
* @category Transaction
|
|
1657
1761
|
*/
|
|
1658
|
-
transaction<Return>(
|
|
1762
|
+
transaction<Return>(
|
|
1763
|
+
actions: () => Return,
|
|
1764
|
+
doRollback?: (
|
|
1765
|
+
changedCells: ChangedCells,
|
|
1766
|
+
invalidCells: InvalidCells,
|
|
1767
|
+
) => boolean,
|
|
1768
|
+
): Return;
|
|
1659
1769
|
|
|
1660
1770
|
/**
|
|
1661
1771
|
* The forEachTable method takes a function that it will then call for each
|
package/lib/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=e=>typeof e,t=e(""),s=e(!0),n=e(0),r=e(e),l=(e,t)=>e.forEach(t),
|
|
1
|
+
const e=e=>typeof e,t=e(""),s=e(!0),n=e(0),r=e(e),l=e=>[e,e],a=(e,t)=>e.forEach(t),o=e=>e.length,d=e=>0==o(e),c=e=>e.slice(1),i=(e,t)=>e.push(t),u=e=>JSON.stringify(e,((e,t)=>{return b(t,Map)?(s=[...t],n=(e,[t,s])=>(e[t]=s,e),r={},s.reduce(n,r)):t;var s,n,r})),f=JSON.parse,h=isFinite,b=(e,t)=>e instanceof t,g=e=>null==e,T=(e,t,s)=>g(e)?s?.():t(e),w=e=>e==t||e==s,p=(e,t)=>e?.has(t)??!1,L=e=>g(e)||0==(e=>e.size)(e),R=e=>e.clear(),C=(e,t)=>e?.forEach(t),y=(e,t)=>e?.delete(t),I=e=>new Map(e),S=(e=I)=>[e(),e()],v=e=>[...e?.keys()??[]],E=(e,t)=>e?.get(t),J=(e,t)=>C(e,((e,s)=>t(s,e))),m=(e,t,s)=>g(s)?(y(e,t),e):e?.set(t,s),z=(e,t,s,n)=>(p(e,t)||(n?.(s),e.set(t,s)),E(e,t)),O=(e,t,s)=>{const n={},r=t??(e=>e);return C(e,((e,t)=>T(r(e),(e=>s?.(e)?0:n[t]=e)))),n},k=(e,t)=>{const s=I(),n=t??(e=>e);return C(e,((e,t)=>s.set(t,n(e)))),s},F=e=>k(e,k),M=Object,N=M.keys,j=M.isFrozen,x=M.freeze,P=(e,t)=>!g(((e,t)=>T(e,(e=>e[t])))(e,t)),q=(e,t)=>delete e[t],A=(e,t)=>a(M.entries(e),(([e,s])=>t(s,e))),B=e=>d(N(e)),D=e=>new Set(e),G=(e,t)=>e?.add(t),H=(e,t,s)=>o(s)<2?G(d(s)?e:z(e,s[0],D()),t):H(z(e,s[0],I()),t,c(s)),K=e=>{const t=(s,n,...r)=>T(s,(s=>d(r)?e(s,n):a([r[0],null],(e=>t(E(s,e),n,...c(r))))));return t},Q=(e,t,s,n=m)=>{const r=(l=v(e),o=e=>!P(t,e),l.filter(o));var l,o;return a(N(t),(n=>s(e,n,t[n]))),a(r,(t=>n(e,t))),e},U=t=>{const s=e(t);return w(s)||s==n&&h(t)?s:void 0},V=(e,t,s)=>g(e)||!(e=>b(e,M)&&e.constructor==M)(e)||B(e)||j(e)?(s?.(),!1):(A(e,((s,n)=>{t(s,n)||q(e,n)})),!B(e)),W=(e,t,s)=>m(e,t,E(e,t)==-s?void 0:s),X=()=>{let t,s=0,d=0;const c=I(),h=I(),b=I(),M=I(),N=I(),j=I(),X=I(),Y=I(),Z=S(D),$=S(D),_=S(),ee=S(),te=S(),se=S(),ne=S(),re=S(),[le,ae,oe,de]=(e=>{let t,s=0;const n=[],r=I();return[(l,a,o=[])=>{t??=e();const d=n.pop()??""+s++;return m(r,d,[l,a,o]),H(a,d,o),d},(e,s=[],...n)=>K(C)(e,(e=>T(E(r,e),(([e])=>e(t,...s,...n)))),...s),e=>T(E(r,e),(([,t,s])=>(K(y)(t,e,...s),m(r,e),o(n)<1e3&&i(n,e),s)),(()=>[])),(e,s,n)=>T(E(r,e),(([e,,r])=>{const l=(...d)=>{const c=o(d);c==o(r)?e(t,...d,...n(d)):g(r[c])?a(s[c](...d),(e=>l(...d,e))):l(...d,r[c])};l()}))]})((()=>He)),ce=(e,s)=>(!t||p(j,s)||me(s))&&V(e,((e,t)=>ie(s,t,e)),(()=>me(s))),ie=(e,t,s,n)=>V(n?s:fe(s,e,t),((n,r)=>T(ue(e,t,r,n),(e=>(s[r]=e,!0)),(()=>!1))),(()=>me(e,t))),ue=(e,s,n,r)=>t?T(E(E(j,e),n),(t=>U(r)!=t.type?me(e,s,n,r,t.default):r),(()=>me(e,s,n,r))):g(U(r))?me(e,s,n,r):r,fe=(e,t,s)=>(T(E(X,t),(([n,r])=>{C(n,((t,s)=>{P(e,s)||(e[s]=t)})),C(r,(n=>{P(e,n)||me(t,s,n)}))})),e),he=e=>Q(j,e,((e,t,s)=>{const n=I(),r=D();Q(z(j,t,I()),s,((e,t,s)=>{m(e,t,s),T(s.default,(e=>m(n,t,e)),(()=>G(r,t)))})),m(X,t,[n,r])}),((e,t)=>{m(j,t),m(X,t)})),be=e=>Q(Y,e,((e,t,s)=>ge(t,s)),((e,t)=>Ce(t))),ge=(e,t)=>Q(z(Y,e,I(),(()=>Se(e,1))),t,((t,s,n)=>Te(e,t,s,n)),((t,s)=>ye(e,t,s))),Te=(e,t,s,n,r)=>Q(z(t,s,I(),(()=>ve(e,s,1))),n,((t,n,r)=>we(e,s,t,n,r)),((n,l)=>Ie(e,t,s,n,l,r))),we=(e,t,s,n,r)=>{p(s,n)||Ee(e,t,n,1);const l=E(s,n);r!==l&&(Je(e,t,n,l,r),m(s,n,r))},pe=(e,t,s,n,r)=>T(E(t,s),(t=>we(e,s,t,n,r)),(()=>Te(e,t,s,fe({[n]:r},e,s)))),Le=e=>{const t=""+s++;return p(e,t)?Le(e):t},Re=e=>E(Y,e)??ge(e,{}),Ce=e=>ge(e,{}),ye=(e,t,s)=>Te(e,t,s,{},!0),Ie=(e,t,s,n,r,l)=>{const a=E(E(X,e)?.[0],r);if(!g(a)&&!l)return we(e,s,n,r,a);const o=t=>{Je(e,s,t,E(n,t)),Ee(e,s,t,-1),m(n,t)};g(a)?o(r):J(n,o),L(n)&&(ve(e,s,-1),L(m(t,s))&&(Se(e,-1),m(Y,e)))},Se=(e,t)=>W(c,e,t),ve=(e,t,s)=>W(z(h,e,I()),t,s),Ee=(e,t,s,n)=>W(z(z(b,e,I()),t,I()),s,n),Je=(e,t,s,n,r)=>z(z(z(M,e,I()),t,I()),s,[n])[1]=r,me=(e,t,s,n,r)=>(i(z(z(z(N,e,I()),t,I()),s,[]),n),r),ze=(e,t,s)=>T(E(E(E(M,e),t),s),(([e,t])=>[!0,e,t]),(()=>[!1,...l(Pe(e,t,s))])),Oe=e=>L(N)||L(re[e])?0:C(e?k(N,F):N,((t,s)=>C(t,((t,n)=>C(t,((t,r)=>ae(re[e],[s,n,r],t))))))),ke=e=>{if(!L(M)){const t=L(se[e])&&L(ee[e])&&L($[e]),s=L(ne[e])&&L(te[e])&&L(_[e])&&L(Z[e]);if(!t||!s){const n=e?[k(c),F(h),k(b,F),k(M,F)]:[c,h,b,M];if(t||(C(n[2],((t,s)=>C(t,((t,n)=>{L(t)||ae(se[e],[s,n])})))),C(n[1],((t,s)=>{L(t)||ae(ee[e],[s])})),L(n[0])||ae($[e])),!s){let t;C(n[3],((s,n)=>{let r;C(s,((s,l)=>{let a;C(s,(([s,o],d)=>{o!==s&&(ae(ne[e],[n,l,d],o,s,ze),t=r=a=1)})),a&&ae(te[e],[n,l],ze)})),r&&ae(_[e],[n],ze)})),t&&ae(Z[e],[],ze)}}}},Fe=e=>(Ge(e),He),Me=()=>O(Y,(e=>O(e,O))),Ne=()=>v(Y),je=e=>v(E(Y,e)),xe=(e,t)=>v(E(E(Y,e),t)),Pe=(e,t,s)=>E(E(E(Y,e),t),s),qe=e=>Fe((()=>(e=>V(e,ce,me))(e)?be(e):0)),Ae=(t,s,n,l)=>Fe((()=>T(ue(t,s,n,e(l)==r?l(Pe(t,s,n)):l),(e=>pe(t,Re(t),s,n,e))))),Be=()=>Fe((()=>be({}))),De=(e,t,s,n)=>Fe((()=>T(E(Y,e),(r=>T(E(r,t),(l=>p(l,s)?Ie(e,r,t,l,s,n):0)))))),Ge=(e,t)=>{if(-1==d)return;d++;const s=e?.();return d--,0==d&&(d=1,Oe(1),ke(1),d=-1,t?.(O(M,(e=>O(e,(e=>O(e,(e=>[...e]),(([e,t])=>e===t))),B)),B),O(N,(e=>O(e,O))))&&(d=1,C(M,((e,t)=>C(e,((e,s)=>C(e,(([e],n)=>g(e)?De(t,s,n,!0):Ae(t,s,n,e))))))),d=-1),Oe(0),ke(0),d=0,a([M,N,c,h,b],R)),s},He={getTables:Me,getTableIds:Ne,getTable:e=>O(E(Y,e),O),getRowIds:je,getRow:(e,t)=>O(E(E(Y,e),t)),getCellIds:xe,getCell:Pe,hasTables:()=>!L(Y),hasTable:e=>p(Y,e),hasRow:(e,t)=>p(E(Y,e),t),hasCell:(e,t,s)=>p(E(E(Y,e),t),s),getJson:()=>u(Y),getSchemaJson:()=>u(j),setTables:qe,setTable:(e,t)=>Fe((()=>ce(t,e)?ge(e,t):0)),setRow:(e,t,s)=>Fe((()=>ie(e,t,s)?Te(e,Re(e),t,s):0)),addRow:(e,t)=>Ge((()=>{let s;return ie(e,s,t)&&Te(e,Re(e),s=Le(E(Y,e)),t),s})),setPartialRow:(e,t,s)=>Fe((()=>{if(ie(e,t,s,1)){const n=Re(e);A(s,((s,r)=>pe(e,n,t,r,s)))}})),setCell:Ae,setJson:e=>{try{"{}"===e?Be():qe(f(e))}catch{}return He},setSchema:e=>Fe((()=>{if((t=(e=>V(e,(e=>V(e,(e=>{if(!V(e,((e,t)=>["type","default"].includes(t))))return!1;const t=e.type;return!(!w(t)&&t!=n||(U(e.default)!=t&&q(e,"default"),0))})))))(e))&&(he(e),!L(Y))){const e=Me();Be(),qe(e)}})),delTables:Be,delTable:e=>Fe((()=>p(Y,e)?Ce(e):0)),delRow:(e,t)=>Fe((()=>T(E(Y,e),(s=>p(s,t)?ye(e,s,t):0)))),delCell:De,delSchema:()=>Fe((()=>{he({}),t=!1})),transaction:Ge,forEachTable:e=>C(Y,((t,s)=>e(s,(e=>C(t,((t,s)=>e(s,(e=>J(t,e))))))))),forEachRow:(e,t)=>C(E(Y,e),((e,s)=>t(s,(t=>J(e,t))))),forEachCell:(e,t,s)=>J(E(E(Y,e),t),s),addTablesListener:(e,t)=>le(e,Z[t?1:0]),addTableIdsListener:(e,t)=>le(e,$[t?1:0]),addTableListener:(e,t,s)=>le(t,_[s?1:0],[e]),addRowIdsListener:(e,t,s)=>le(t,ee[s?1:0],[e]),addRowListener:(e,t,s,n)=>le(s,te[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>le(s,se[n?1:0],[e,t]),addCellListener:(e,t,s,n,r)=>le(n,ne[r?1:0],[e,t,s]),addInvalidCellListener:(e,t,s,n,r)=>le(n,re[r?1:0],[e,t,s]),callListener:e=>(de(e,[Ne,je,xe],(e=>g(e[2])?[]:l(Pe(...e)))),He),delListener:e=>(oe(e),He),getListenerStats:()=>({})};return x(He)};export{X as createStore};
|
package/lib/store.js.gz
CHANGED
|
Binary file
|
package/lib/tinybase.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{promises as e,watch as t}from"fs";const s=e=>typeof e,n=s(""),o=s(!0),a=s(0),r=s(s),i=(e,t)=>e.includes(t),d=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),l=(e,t)=>e.sort(t),c=(e,t)=>e.forEach(t),u=e=>g(e,((e,t)=>e+t),0),h=e=>e.length,f=e=>0==h(e),g=(e,t,s)=>e.reduce(t,s),L=e=>e.slice(1),w=(e,t)=>e.push(t),v=e=>e.pop(),p=e=>JSON.stringify(e,((e,t)=>T(t,Map)?g([...t],((e,[t,s])=>(e[t]=s,e)),{}):t)),I=JSON.parse,S=Math.max,y=Math.min,R=isFinite,T=(e,t)=>e instanceof t,b=e=>null==e,C=(e,t,s)=>b(e)?s?.():t(e),m=e=>e==n||e==o,E=e=>s(e)==r,k=()=>{},M=e=>e.size,A=(e,t)=>e?.has(t)??!1,x=e=>b(e)||0==M(e),D=e=>[...e?.values()??[]],J=e=>e.clear(),F=(e,t)=>e?.forEach(t),z=(e,t)=>e?.delete(t),N=e=>new Map(e),O=(e=N)=>[e(),e()],j=e=>[...e?.keys()??[]],P=(e,t)=>e?.get(t),B=(e,t)=>F(e,((e,s)=>t(s,e))),H=(e,t,s)=>b(s)?(z(e,t),e):e?.set(t,s),W=(e,t,s,n)=>(A(e,t)||(n?.(s),e.set(t,s)),P(e,t)),q=(e,t)=>{const s={},n=t??(e=>e);return F(e,((e,t)=>s[t]=n(e))),s},G=(e,t)=>{const s=N(),n=t??(e=>e);return F(e,((e,t)=>s.set(t,n(e)))),s},K=e=>new Set(e),Q=(e,t)=>e?.add(t),U=(e,t,s)=>{const n=e.hasRow,o=N(),a=N(),r=N(),i=N(),d=N(),l=t=>C(P(d,t),(s=>{F(s,e.delListener),H(d,t)})),u=e=>{H(o,e),H(a,e),H(r,e),H(i,e),l(e)};return[()=>e,()=>j(o),e=>B(a,e),e=>A(a,e),e=>P(o,e),e=>P(a,e),(e,t)=>H(a,e,t),(u,h,f,g,L)=>{const w=N(),v=N();H(o,u,h),A(a,u)||(H(a,u,t()),H(r,u,N()),H(i,u,N()));const p=P(r,u),I=P(i,u),S=t=>{const o=s=>e.getCell(h,t,s),a=P(p,t),r=n(h,t)?s(g(o,t)):void 0;if(a!=r&&H(w,t,[a,r]),!b(L)){const e=P(I,t),s=n(h,t)?L(o,t):void 0;e!=s&&H(v,t,s)}},y=e=>{f((()=>{F(w,(([,e],t)=>H(p,t,e))),F(v,((e,t)=>H(I,t,e)))}),w,v,p,I,e),J(w),J(v)};B(p,S),e.hasTable(h)&&c(e.getRowIds(h),(e=>{A(p,e)||S(e)})),y(!0),l(u),H(d,u,K([e.addRowListener(h,null,((e,t,s)=>S(s))),e.addTableListener(h,(()=>y()))]))},u,()=>B(d,u)]},V=(e,t)=>s(e)==n?t=>t(e):e??(()=>t??""),X=e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))},Y=(e,t,s)=>h(s)<2?Q(f(s)?e:W(e,s[0],K()),t):Y(W(e,s[0],N()),t,L(s)),Z=e=>{const t=(s,n,...o)=>C(s,(s=>f(o)?e(s,n):c([o[0],null],(e=>t(P(s,e),n,...L(o))))));return t},$=e=>{let t,s=0;const n=[],o=N();return[(a,r,i=[])=>{t??=e();const d=v(n)??""+s++;return H(o,d,[a,r,i]),Y(r,d,i),d},(e,s=[],...n)=>Z(F)(e,(e=>C(P(o,e),(([e])=>e(t,...s,...n)))),...s),e=>C(P(o,e),(([,t,s])=>(Z(z)(t,e,...s),H(o,e),h(n)<1e3&&w(n,e),s)),(()=>[])),(e,s,n)=>C(P(o,e),(([e,,o])=>{const a=(...r)=>{const i=h(r);i==h(o)?e(t,...r,...n(r)):b(o[i])?c(s[i](...r),(e=>a(...r,e))):a(...r,o[i])};a()}))]},_=Object,ee=_.keys,te=_.isFrozen,se=_.freeze,ne=(e,t)=>!b(((e,t)=>C(e,(e=>e[t])))(e,t)),oe=(e,t)=>delete e[t],ae=(e,t)=>c(_.entries(e),(([e,s])=>t(s,e))),re=e=>f(ee(e)),ie=X((e=>{let t,s,n,o=100,a=N(),r=1;const d=K(),l=N(),[u,g,L]=$((()=>G)),p=N(),I=N(),S=[],y=[],R=(t,s)=>{r=0,e.transaction((()=>F(P(p,s),((s,n)=>F(s,((s,o)=>F(s,((s,a)=>b(s[t])?e.delCell(n,o,a,!0):e.setCell(n,o,a,s[t]))))))))),r=1},T=e=>{H(p,e),H(I,e),g(l,[e])},m=(e,t)=>c(((e,t)=>e.splice(0,t))(e,t??h(e)),T),E=()=>m(S,h(S)-o),k=e.addCellListener(null,null,null,((e,s,o,i,d,l)=>{if(r){C(t,(()=>{w(S,t),E(),m(y),t=void 0,n=1}));const e=W(a,s,N()),r=W(e,o,N()),c=W(r,i,[void 0,void 0],(e=>e[0]=l));c[1]=d,c[0]===c[1]&&x(H(r,i))&&x(H(e,o))&&x(H(a,s))&&(t=v(S),n=1),z()}})),M=(e="")=>(b(t)&&(t=""+s++,H(p,t,a),j(t,e),a=N(),n=1),t),D=()=>{f(S)||(y.unshift(M()),R(0,t),t=v(S),n=1)},J=()=>{f(y)||(w(S,t),t=y.shift(),R(1,t),n=1)},z=()=>{n&&(g(d),n=0)},O=e=>{const t=M(e);return z(),t},j=(e,t)=>(q(e)&&P(I,e)!==t&&(H(I,e,t),g(l,[e])),G),q=e=>A(p,e),G={setSize:e=>(o=e,E(),G),addCheckpoint:O,setCheckpoint:j,getStore:()=>e,getCheckpointIds:()=>[[...S],t,[...y]],forEachCheckpoint:e=>B(I,e),hasCheckpoint:q,getCheckpoint:e=>P(I,e),goBackward:()=>(D(),z(),G),goForward:()=>(J(),z(),G),goTo:e=>{const s=i(S,e)?D:i(y,e)?J:null;for(;!b(s)&&e!=t;)s();return z(),G},addCheckpointIdsListener:e=>u(e,d),addCheckpointListener:(e,t)=>u(t,l,[e]),delListener:e=>(L(e),G),clear:()=>(m(S),m(y),b(t)||T(t),t=void 0,s=0,O(),G),destroy:()=>{e.delListener(k)},getListenerStats:()=>({})};return se(G.clear())})),de=(e,t)=>e<t?-1:1,le=X((e=>{const t=N(),s=N(),[n,o,a,r,i,c,u,h,f,g]=U(e,N,(e=>b(e)?"":e+"")),[L,w,v]=$((()=>I)),p=(t,s,n)=>{const o=i(t);F(n,((t,n)=>s(n,(s=>F(t,(t=>s(t,(s=>e.forEachCell(o,t,s)))))))))},I={setIndexDefinition:(e,n,o,a,r,i=de)=>{const f=b(r)?void 0:([e],[t])=>r(e,t);return h(e,n,((n,o,r,h,g,L)=>{let v=0;const p=K(),I=K(),S=c(e);if(F(o,(([e,t],s)=>{b(e)||(Q(p,e),C(P(S,e),(t=>{z(t,s),x(t)&&(H(S,e),v=1)}))),b(t)||(Q(p,t),A(S,t)||(H(S,t,K()),v=1),Q(P(S,t),s),b(a)||Q(I,t))})),n(),x(g)||(L?B(S,(e=>Q(I,e))):B(r,(e=>C(P(h,e),(e=>Q(I,e))))),F(I,(e=>{const t=(t,s)=>i(P(g,t),P(g,s),e),s=[...P(S,e)];d(s,t)||(H(S,e,K(l(s,t))),Q(p,e))}))),(v||L)&&!b(f)){const t=[...S];d(t,f)||(u(e,N(l(t,f))),v=1)}v&&w(t,[e]),F(p,(t=>w(s,[e,t])))}),V(o),C(a,V)),I},delIndexDefinition:e=>(f(e),I),getStore:n,getIndexIds:o,forEachIndex:e=>a(((t,s)=>e(t,(e=>p(t,e,s))))),forEachSlice:(e,t)=>p(e,t,c(e)),hasIndex:r,hasSlice:(e,t)=>A(c(e),t),getTableId:i,getSliceIds:e=>j(c(e)),getSliceRowIds:(e,t)=>D(P(c(e),t)),addSliceIdsListener:(e,s)=>L(s,t,[e]),addSliceRowIdsListener:(e,t,n)=>L(n,s,[e,t]),delListener:e=>(v(e),I),destroy:g,getListenerStats:()=>({})};return se(I)})),ce=N([["avg",[(e,t)=>u(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>S(...e),(e,t)=>S(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:S(t,e)]],["min",[e=>y(...e),(e,t)=>y(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:y(t,e)]],["sum",[e=>u(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),ue=X((e=>{const t=N(),[s,n,o,a,r,i,d,l,c,u]=U(e,k,(e=>isNaN(e)||b(e)||!0===e||!1===e||""===e?void 0:1*e)),[h,f,g]=$((()=>L)),L={setMetricDefinition:(e,s,n,o,a,r,c)=>{const u=E(n)?[n,a,r,c]:P(ce,n)??P(ce,"sum");return l(e,s,((s,n,o,a,r,l)=>{let c=i(e),h=M(a);const[g,L,w,v]=u;l=l||b(c),F(n,(([e,t])=>{l||(c=b(e)?L?.(c,t,h++):b(t)?w?.(c,e,h--):v?.(c,t,e,h)),l=l||b(c)})),s(),x(a)?c=void 0:l&&(c=g(D(a),M(a))),R(c)||(c=void 0);const p=i(e);c!=p&&(d(e,c),f(t,[e],c,p))}),V(o,1)),L},delMetricDefinition:e=>(c(e),L),getStore:s,getMetricIds:n,forEachMetric:o,hasMetric:a,getTableId:r,getMetric:i,addMetricListener:(e,s)=>h(s,t,[e]),delListener:e=>(g(e),L),destroy:u,getListenerStats:()=>({})};return se(L)})),he=(e,t,s,n,o)=>{let a,r=0;const i={load:async s=>{if(2!=r){r=1;const n=await t();b(n)||""==n?e.setTables(s):e.setJson(n),r=0}return i},startAutoLoad:async e=>(i.stopAutoLoad(),await i.load(e),n(i.load),i),stopAutoLoad:()=>(o(),i),save:async()=>(1!=r&&(r=2,await s(e.getJson()),r=0),i),startAutoSave:async()=>(await i.stopAutoSave().save(),a=e.addTablesListener((()=>i.save())),i),stopAutoSave:()=>(C(a,e.delListener),i),getStore:()=>e,destroy:()=>i.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return se(i)},fe=globalThis.window,ge=(e,t,s)=>{let n;return he(e,(async()=>s.getItem(t)),(async e=>s.setItem(t,e)),(e=>{n=n=>{n.storageArea===s&&n.key===t&&e()},fe.addEventListener("storage",n)}),(()=>{fe.removeEventListener("storage",n),n=void 0}))},Le=(e,t)=>ge(e,t,localStorage),we=(e,t)=>ge(e,t,sessionStorage),ve=(s,n)=>{let o;return he(s,(async()=>{try{return await e.readFile(n,"utf8")}catch{}}),(async t=>{try{await e.writeFile(n,t,"utf8")}catch{}}),(e=>{o=t(n,e)}),(()=>{o?.close(),o=void 0}))},pe=e=>e.headers.get("ETag"),Ie=(e,t,s,n)=>{let o,a;return he(e,(async()=>{const e=await fetch(t);return a=pe(e),e.text()}),(async e=>await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:e})),(e=>{o=setInterval((async()=>{const s=await fetch(t,{method:"HEAD"}),n=pe(s);b(a)||b(n)||n==a||(a=n,e())}),1e3*n)}),(()=>{C(o,clearInterval),o=void 0}))},Se=X((e=>{const t=N(),s=N(),n=N(),o=N(),[a,r,i,d,l,c,u,h,f,g]=U(e,(()=>[N(),N(),N(),N()]),(e=>b(e)?void 0:e+"")),[L,w,v]=$((()=>y)),p=(e,t,s)=>C(c(e),(([n,,o])=>{if(!A(o,t)){const a=K();if(l(e)!=S(e))Q(a,t);else{let e=t;for(;!b(e)&&!A(a,e);)Q(a,e),e=P(n,e)}if(s)return a;H(o,t,a)}return P(o,t)})),I=(e,t)=>C(c(e),(([,,e])=>H(e,t))),S=e=>P(t,e),y={setRelationshipDefinition:(e,a,r,i)=>(H(t,e,r),h(e,a,((t,a)=>{const r=K(),i=K(),d=K(),[l,u]=c(e);F(a,(([t,s],n)=>{b(t)||(Q(i,t),C(P(u,t),(e=>{z(e,n),x(e)&&H(u,t)}))),b(s)||(Q(i,s),A(u,s)||H(u,s,K()),Q(P(u,s),n)),Q(r,n),H(l,n,s),B(P(o,e),(t=>{A(p(e,t),n)&&Q(d,t)}))})),t(),F(r,(t=>w(s,[e,t]))),F(i,(t=>w(n,[e,t]))),F(d,(t=>{I(e,t),w(o,[e,t])}))}),V(i)),y),delRelationshipDefinition:e=>(H(t,e),f(e),y),getStore:a,getRelationshipIds:r,forEachRelationship:t=>i((s=>t(s,(t=>e.forEachRow(l(s),t))))),hasRelationship:d,getLocalTableId:l,getRemoteTableId:S,getRemoteRowId:(e,t)=>P(c(e)?.[0],t),getLocalRowIds:(e,t)=>D(P(c(e)?.[1],t)),getLinkedRowIds:(e,t)=>b(c(e))?[t]:D(p(e,t,!0)),addRemoteRowIdListener:(e,t,n)=>L(n,s,[e,t]),addLocalRowIdsListener:(e,t,s)=>L(s,n,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(p(e,t),L(s,o,[e,t])),delListener:e=>(I(...v(e)),y),destroy:g,getListenerStats:()=>({})};return se(y)})),ye=(e,t,s,n=H)=>{const o=(a=j(e),r=e=>!ne(t,e),a.filter(r));var a,r;return c(ee(t),(n=>s(e,n,t[n]))),c(o,(t=>n(e,t))),e},Re=e=>{const t=s(e);return m(t)||t==a&&R(e)?t:void 0},Te=(e,t,s)=>b(e)||!(e=>T(e,_)&&e.constructor==_)(e)||re(e)||te(e)?(s?.(),!1):(ae(e,((s,n)=>{t(s,n)||oe(e,n)})),!re(e)),be=(e,t,s)=>H(e,t,P(e,t)==-s?void 0:s),Ce=()=>{let e,t=0,s=0;const n=N(),o=N(),r=N(),d=N(),l=N(),u=N(),h=N(),f=N(),g=O(K),L=O(K),v=O(),S=O(),y=O(),R=O(),T=O(),k=O(),[M,D,z,U]=$((()=>ze)),V=(t,s)=>(!e||A(u,s)||pe(s))&&Te(t,((e,t)=>X(s,t,e)),(()=>pe(s))),X=(e,t,s,n)=>Te(n?s:Z(s,e,t),((n,o)=>C(Y(e,t,o,n),(e=>(s[o]=e,!0)),(()=>!1))),(()=>pe(e,t))),Y=(t,s,n,o)=>e?C(P(P(u,t),n),(e=>Re(o)!=e.type?pe(t,s,n,o,e.default):o),(()=>pe(t,s,n,o))):b(Re(o))?pe(t,s,n,o):o,Z=(e,t,s)=>(C(P(h,t),(([n,o])=>{F(n,((t,s)=>{ne(e,s)||(e[s]=t)})),F(o,(n=>{ne(e,n)||pe(t,s,n)}))})),e),_=e=>ye(u,e,((e,t,s)=>{const n=N(),o=K();ye(W(u,t,N()),s,((e,t,s)=>{H(e,t,s),C(s.default,(e=>H(n,t,e)),(()=>Q(o,t)))})),H(h,t,[n,o])}),((e,t)=>{H(u,t),H(h,t)})),ee=e=>ye(f,e,((e,t,s)=>te(t,s)),((e,t)=>ue(t))),te=(e,t)=>ye(W(f,e,N(),(()=>ge(e,1))),t,((t,s,n)=>re(e,t,s,n)),((t,s)=>he(e,t,s))),re=(e,t,s,n,o)=>ye(W(t,s,N(),(()=>Le(e,s,1))),n,((t,n,o)=>ie(e,s,t,n,o)),((n,a)=>fe(e,t,s,n,a,o))),ie=(e,t,s,n,o)=>{A(s,n)||we(e,t,n,1);const a=P(s,n);o!==a&&(ve(e,t,n,a),H(s,n,o))},de=(e,t,s,n,o)=>C(P(t,s),(t=>ie(e,s,t,n,o)),(()=>re(e,t,s,Z({[n]:o},e,s)))),le=e=>{const s=""+t++;return A(e,s)?le(e):s},ce=e=>P(f,e)??te(e,{}),ue=e=>te(e,{}),he=(e,t,s)=>re(e,t,s,{},!0),fe=(e,t,s,n,o,a)=>{const r=P(P(h,e)?.[0],o);if(!b(r)&&!a)return ie(e,s,n,o,r);const i=t=>{ve(e,s,t,P(n,t)),we(e,s,t,-1),H(n,t)};b(r)?i(o):B(n,i),x(n)&&(Le(e,s,-1),x(H(t,s))&&(ge(e,-1),H(f,e)))},ge=(e,t)=>be(n,e,t),Le=(e,t,s)=>be(W(o,e,N()),t,s),we=(e,t,s,n)=>be(W(W(r,e,N()),t,N()),s,n),ve=(e,t,s,n)=>W(W(W(d,e,N()),t,N()),s,n),pe=(e,t,s,n,o)=>(w(W(W(W(l,e,N()),t,N()),s,[]),n),o),Ie=(e,t,s)=>{const n=P(P(d,e),t),o=xe(e,t,s);return A(n,s)?[!0,P(n,s),o]:[!1,o,o]},Se=e=>x(l)||x(k[e])?0:F(e?G(l,(e=>G(e,G))):l,((t,s)=>F(t,((t,n)=>F(t,((t,o)=>D(k[e],[s,n,o],t))))))),Ce=e=>{if(!x(d)){const t=x(R[e])&&x(S[e])&&x(L[e]),s=x(T[e])&&x(y[e])&&x(v[e])&&x(g[e]);if(!t||!s){const a=e?[G(n),G(o,G),G(r,(e=>G(e,G))),G(d,(e=>G(e,G)))]:[n,o,r,d];if(t||(F(a[2],((t,s)=>F(t,((t,n)=>{x(t)||D(R[e],[s,n])})))),F(a[1],((t,s)=>{x(t)||D(S[e],[s])})),x(a[0])||D(L[e])),!s){let t;F(a[3],((s,n)=>{let o;F(s,((s,a)=>{let r;F(s,((s,i)=>{const d=xe(n,a,i);d!==s&&(D(T[e],[n,a,i],d,s,Ie),t=o=r=1)})),r&&D(y[e],[n,a],Ie)})),o&&D(v[e],[n],Ie)})),t&&D(g[e],[],Ie)}}}},me=e=>(Fe(e),ze),Ee=()=>q(f,(e=>q(e,q))),ke=()=>j(f),Me=e=>j(P(f,e)),Ae=(e,t)=>j(P(P(f,e),t)),xe=(e,t,s)=>P(P(P(f,e),t),s),De=e=>me((()=>(e=>Te(e,V,pe))(e)?ee(e):0)),Je=()=>me((()=>ee({}))),Fe=e=>{if(-1==s)return;s++;const t=e?.();return s--,0==s&&(s=1,Se(1),Ce(1),s=-1,Se(0),Ce(0),s=0,c([d,l,n,o,r],J)),t},ze={getTables:Ee,getTableIds:ke,getTable:e=>q(P(f,e),q),getRowIds:Me,getRow:(e,t)=>q(P(P(f,e),t)),getCellIds:Ae,getCell:xe,hasTables:()=>!x(f),hasTable:e=>A(f,e),hasRow:(e,t)=>A(P(f,e),t),hasCell:(e,t,s)=>A(P(P(f,e),t),s),getJson:()=>p(f),getSchemaJson:()=>p(u),setTables:De,setTable:(e,t)=>me((()=>V(t,e)?te(e,t):0)),setRow:(e,t,s)=>me((()=>X(e,t,s)?re(e,ce(e),t,s):0)),addRow:(e,t)=>Fe((()=>{let s;return X(e,s,t)&&re(e,ce(e),s=le(P(f,e)),t),s})),setPartialRow:(e,t,s)=>me((()=>{if(X(e,t,s,1)){const n=ce(e);ae(s,((s,o)=>de(e,n,t,o,s)))}})),setCell:(e,t,s,n)=>me((()=>C(Y(e,t,s,E(n)?n(xe(e,t,s)):n),(n=>de(e,ce(e),t,s,n))))),setJson:e=>{try{"{}"===e?Je():De(I(e))}catch{}return ze},setSchema:t=>me((()=>{if((e=(e=>Te(e,(e=>Te(e,(e=>{if(!Te(e,((e,t)=>i(["type","default"],t))))return!1;const t=e.type;return!(!m(t)&&t!=a||(Re(e.default)!=t&&oe(e,"default"),0))})))))(t))&&(_(t),!x(f))){const e=Ee();Je(),De(e)}})),delTables:Je,delTable:e=>me((()=>A(f,e)?ue(e):0)),delRow:(e,t)=>me((()=>C(P(f,e),(s=>A(s,t)?he(e,s,t):0)))),delCell:(e,t,s,n)=>me((()=>C(P(f,e),(o=>C(P(o,t),(a=>A(a,s)?fe(e,o,t,a,s,n):0)))))),delSchema:()=>me((()=>{_({}),e=!1})),transaction:Fe,forEachTable:e=>F(f,((t,s)=>e(s,(e=>F(t,((t,s)=>e(s,(e=>B(t,e))))))))),forEachRow:(e,t)=>F(P(f,e),((e,s)=>t(s,(t=>B(e,t))))),forEachCell:(e,t,s)=>B(P(P(f,e),t),s),addTablesListener:(e,t)=>M(e,g[t?1:0]),addTableIdsListener:(e,t)=>M(e,L[t?1:0]),addTableListener:(e,t,s)=>M(t,v[s?1:0],[e]),addRowIdsListener:(e,t,s)=>M(t,S[s?1:0],[e]),addRowListener:(e,t,s,n)=>M(s,y[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>M(s,R[n?1:0],[e,t]),addCellListener:(e,t,s,n,o)=>M(n,T[o?1:0],[e,t,s]),addInvalidCellListener:(e,t,s,n,o)=>M(n,k[o?1:0],[e,t,s]),callListener:e=>(U(e,[ke,Me,Ae],(e=>b(e[2])?[]:[,,].fill(xe(...e)))),ze),delListener:e=>(z(e),ze),getListenerStats:()=>({})};return se(ze)};export{ie as createCheckpoints,he as createCustomPersister,ve as createFilePersister,le as createIndexes,Le as createLocalPersister,ue as createMetrics,Se as createRelationships,Ie as createRemotePersister,we as createSessionPersister,Ce as createStore,de as defaultSorter};
|
|
1
|
+
import{promises as e,watch as t}from"fs";const s=e=>typeof e,n=s(""),o=s(!0),a=s(0),r=s(s),i=e=>[e,e],d=(e,t)=>e.includes(t),l=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),c=(e,t)=>e.sort(t),u=(e,t)=>e.forEach(t),h=e=>L(e,((e,t)=>e+t),0),g=e=>e.length,f=e=>0==g(e),L=(e,t,s)=>e.reduce(t,s),w=e=>e.slice(1),v=(e,t)=>e.push(t),p=e=>e.pop(),I=e=>JSON.stringify(e,((e,t)=>b(t,Map)?L([...t],((e,[t,s])=>(e[t]=s,e)),{}):t)),S=JSON.parse,y=Math.max,R=Math.min,T=isFinite,b=(e,t)=>e instanceof t,C=e=>null==e,m=(e,t,s)=>C(e)?s?.():t(e),E=e=>e==n||e==o,k=e=>s(e)==r,M=()=>{},A=e=>e.size,x=(e,t)=>e?.has(t)??!1,D=e=>C(e)||0==A(e),J=e=>[...e?.values()??[]],F=e=>e.clear(),z=(e,t)=>e?.forEach(t),N=(e,t)=>e?.delete(t),O=e=>new Map(e),j=(e=O)=>[e(),e()],P=e=>[...e?.keys()??[]],B=(e,t)=>e?.get(t),H=(e,t)=>z(e,((e,s)=>t(s,e))),W=(e,t,s)=>C(s)?(N(e,t),e):e?.set(t,s),q=(e,t,s,n)=>(x(e,t)||(n?.(s),e.set(t,s)),B(e,t)),G=(e,t,s)=>{const n={},o=t??(e=>e);return z(e,((e,t)=>m(o(e),(e=>s?.(e)?0:n[t]=e)))),n},K=(e,t)=>{const s=O(),n=t??(e=>e);return z(e,((e,t)=>s.set(t,n(e)))),s},Q=e=>K(e,K),U=e=>new Set(e),V=(e,t)=>e?.add(t),X=(e,t,s)=>{const n=e.hasRow,o=O(),a=O(),r=O(),i=O(),d=O(),l=t=>m(B(d,t),(s=>{z(s,e.delListener),W(d,t)})),c=e=>{W(o,e),W(a,e),W(r,e),W(i,e),l(e)};return[()=>e,()=>P(o),e=>H(a,e),e=>x(a,e),e=>B(o,e),e=>B(a,e),(e,t)=>W(a,e,t),(c,h,g,f,L)=>{const w=O(),v=O();W(o,c,h),x(a,c)||(W(a,c,t()),W(r,c,O()),W(i,c,O()));const p=B(r,c),I=B(i,c),S=t=>{const o=s=>e.getCell(h,t,s),a=B(p,t),r=n(h,t)?s(f(o,t)):void 0;if(a!=r&&W(w,t,[a,r]),!C(L)){const e=B(I,t),s=n(h,t)?L(o,t):void 0;e!=s&&W(v,t,s)}},y=e=>{g((()=>{z(w,(([,e],t)=>W(p,t,e))),z(v,((e,t)=>W(I,t,e)))}),w,v,p,I,e),F(w),F(v)};H(p,S),e.hasTable(h)&&u(e.getRowIds(h),(e=>{x(p,e)||S(e)})),y(!0),l(c),W(d,c,U([e.addRowListener(h,null,((e,t,s)=>S(s))),e.addTableListener(h,(()=>y()))]))},c,()=>H(d,c)]},Y=(e,t)=>s(e)==n?t=>t(e):e??(()=>t??""),Z=e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))},$=(e,t,s)=>g(s)<2?V(f(s)?e:q(e,s[0],U()),t):$(q(e,s[0],O()),t,w(s)),_=e=>{const t=(s,n,...o)=>m(s,(s=>f(o)?e(s,n):u([o[0],null],(e=>t(B(s,e),n,...w(o))))));return t},ee=e=>{let t,s=0;const n=[],o=O();return[(a,r,i=[])=>{t??=e();const d=p(n)??""+s++;return W(o,d,[a,r,i]),$(r,d,i),d},(e,s=[],...n)=>_(z)(e,(e=>m(B(o,e),(([e])=>e(t,...s,...n)))),...s),e=>m(B(o,e),(([,t,s])=>(_(N)(t,e,...s),W(o,e),g(n)<1e3&&v(n,e),s)),(()=>[])),(e,s,n)=>m(B(o,e),(([e,,o])=>{const a=(...r)=>{const i=g(r);i==g(o)?e(t,...r,...n(r)):C(o[i])?u(s[i](...r),(e=>a(...r,e))):a(...r,o[i])};a()}))]},te=Object,se=te.keys,ne=te.isFrozen,oe=te.freeze,ae=(e,t)=>!C(((e,t)=>m(e,(e=>e[t])))(e,t)),re=(e,t)=>delete e[t],ie=(e,t)=>u(te.entries(e),(([e,s])=>t(s,e))),de=e=>f(se(e)),le=Z((e=>{let t,s,n,o=100,a=O(),r=1;const i=U(),l=O(),[c,h,L]=ee((()=>G)),w=O(),I=O(),S=[],y=[],R=(t,s)=>{r=0,e.transaction((()=>z(B(w,s),((s,n)=>z(s,((s,o)=>z(s,((s,a)=>C(s[t])?e.delCell(n,o,a,!0):e.setCell(n,o,a,s[t]))))))))),r=1},T=e=>{W(w,e),W(I,e),h(l,[e])},b=(e,t)=>u(((e,t)=>e.splice(0,t))(e,t??g(e)),T),E=()=>b(S,g(S)-o),k=e.addCellListener(null,null,null,((e,s,o,i,d,l)=>{if(r){m(t,(()=>{v(S,t),E(),b(y),t=void 0,n=1}));const e=q(a,s,O()),r=q(e,o,O()),c=q(r,i,[void 0,void 0],(e=>e[0]=l));c[1]=d,c[0]===c[1]&&D(W(r,i))&&D(W(e,o))&&D(W(a,s))&&(t=p(S),n=1),F()}})),M=(e="")=>(C(t)&&(t=""+s++,W(w,t,a),j(t,e),a=O(),n=1),t),A=()=>{f(S)||(y.unshift(M()),R(0,t),t=p(S),n=1)},J=()=>{f(y)||(v(S,t),t=y.shift(),R(1,t),n=1)},F=()=>{n&&(h(i),n=0)},N=e=>{const t=M(e);return F(),t},j=(e,t)=>(P(e)&&B(I,e)!==t&&(W(I,e,t),h(l,[e])),G),P=e=>x(w,e),G={setSize:e=>(o=e,E(),G),addCheckpoint:N,setCheckpoint:j,getStore:()=>e,getCheckpointIds:()=>[[...S],t,[...y]],forEachCheckpoint:e=>H(I,e),hasCheckpoint:P,getCheckpoint:e=>B(I,e),goBackward:()=>(A(),F(),G),goForward:()=>(J(),F(),G),goTo:e=>{const s=d(S,e)?A:d(y,e)?J:null;for(;!C(s)&&e!=t;)s();return F(),G},addCheckpointIdsListener:e=>c(e,i),addCheckpointListener:(e,t)=>c(t,l,[e]),delListener:e=>(L(e),G),clear:()=>(b(S),b(y),C(t)||T(t),t=void 0,s=0,N(),G),destroy:()=>{e.delListener(k)},getListenerStats:()=>({})};return oe(G.clear())})),ce=(e,t)=>e<t?-1:1,ue=Z((e=>{const t=O(),s=O(),[n,o,a,r,i,d,u,h,g,f]=X(e,O,(e=>C(e)?"":e+"")),[L,w,v]=ee((()=>I)),p=(t,s,n)=>{const o=i(t);z(n,((t,n)=>s(n,(s=>z(t,(t=>s(t,(s=>e.forEachCell(o,t,s)))))))))},I={setIndexDefinition:(e,n,o,a,r,i=ce)=>{const g=C(r)?void 0:([e],[t])=>r(e,t);return h(e,n,((n,o,r,h,f,L)=>{let v=0;const p=U(),I=U(),S=d(e);if(z(o,(([e,t],s)=>{C(e)||(V(p,e),m(B(S,e),(t=>{N(t,s),D(t)&&(W(S,e),v=1)}))),C(t)||(V(p,t),x(S,t)||(W(S,t,U()),v=1),V(B(S,t),s),C(a)||V(I,t))})),n(),D(f)||(L?H(S,(e=>V(I,e))):H(r,(e=>m(B(h,e),(e=>V(I,e))))),z(I,(e=>{const t=(t,s)=>i(B(f,t),B(f,s),e),s=[...B(S,e)];l(s,t)||(W(S,e,U(c(s,t))),V(p,e))}))),(v||L)&&!C(g)){const t=[...S];l(t,g)||(u(e,O(c(t,g))),v=1)}v&&w(t,[e]),z(p,(t=>w(s,[e,t])))}),Y(o),m(a,Y)),I},delIndexDefinition:e=>(g(e),I),getStore:n,getIndexIds:o,forEachIndex:e=>a(((t,s)=>e(t,(e=>p(t,e,s))))),forEachSlice:(e,t)=>p(e,t,d(e)),hasIndex:r,hasSlice:(e,t)=>x(d(e),t),getTableId:i,getSliceIds:e=>P(d(e)),getSliceRowIds:(e,t)=>J(B(d(e),t)),addSliceIdsListener:(e,s)=>L(s,t,[e]),addSliceRowIdsListener:(e,t,n)=>L(n,s,[e,t]),delListener:e=>(v(e),I),destroy:f,getListenerStats:()=>({})};return oe(I)})),he=O([["avg",[(e,t)=>h(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>y(...e),(e,t)=>y(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:y(t,e)]],["min",[e=>R(...e),(e,t)=>R(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:R(t,e)]],["sum",[e=>h(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),ge=Z((e=>{const t=O(),[s,n,o,a,r,i,d,l,c,u]=X(e,M,(e=>isNaN(e)||C(e)||!0===e||!1===e||""===e?void 0:1*e)),[h,g,f]=ee((()=>L)),L={setMetricDefinition:(e,s,n,o,a,r,c)=>{const u=k(n)?[n,a,r,c]:B(he,n)??B(he,"sum");return l(e,s,((s,n,o,a,r,l)=>{let c=i(e),h=A(a);const[f,L,w,v]=u;l=l||C(c),z(n,(([e,t])=>{l||(c=C(e)?L?.(c,t,h++):C(t)?w?.(c,e,h--):v?.(c,t,e,h)),l=l||C(c)})),s(),D(a)?c=void 0:l&&(c=f(J(a),A(a))),T(c)||(c=void 0);const p=i(e);c!=p&&(d(e,c),g(t,[e],c,p))}),Y(o,1)),L},delMetricDefinition:e=>(c(e),L),getStore:s,getMetricIds:n,forEachMetric:o,hasMetric:a,getTableId:r,getMetric:i,addMetricListener:(e,s)=>h(s,t,[e]),delListener:e=>(f(e),L),destroy:u,getListenerStats:()=>({})};return oe(L)})),fe=(e,t,s,n,o)=>{let a,r=0;const i={load:async s=>{if(2!=r){r=1;const n=await t();C(n)||""==n?e.setTables(s):e.setJson(n),r=0}return i},startAutoLoad:async e=>(i.stopAutoLoad(),await i.load(e),n(i.load),i),stopAutoLoad:()=>(o(),i),save:async()=>(1!=r&&(r=2,await s(e.getJson()),r=0),i),startAutoSave:async()=>(await i.stopAutoSave().save(),a=e.addTablesListener((()=>i.save())),i),stopAutoSave:()=>(m(a,e.delListener),i),getStore:()=>e,destroy:()=>i.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return oe(i)},Le=globalThis.window,we=(e,t,s)=>{let n;return fe(e,(async()=>s.getItem(t)),(async e=>s.setItem(t,e)),(e=>{n=n=>{n.storageArea===s&&n.key===t&&e()},Le.addEventListener("storage",n)}),(()=>{Le.removeEventListener("storage",n),n=void 0}))},ve=(e,t)=>we(e,t,localStorage),pe=(e,t)=>we(e,t,sessionStorage),Ie=(s,n)=>{let o;return fe(s,(async()=>{try{return await e.readFile(n,"utf8")}catch{}}),(async t=>{try{await e.writeFile(n,t,"utf8")}catch{}}),(e=>{o=t(n,e)}),(()=>{o?.close(),o=void 0}))},Se=e=>e.headers.get("ETag"),ye=(e,t,s,n)=>{let o,a;return fe(e,(async()=>{const e=await fetch(t);return a=Se(e),e.text()}),(async e=>await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:e})),(e=>{o=setInterval((async()=>{const s=await fetch(t,{method:"HEAD"}),n=Se(s);C(a)||C(n)||n==a||(a=n,e())}),1e3*n)}),(()=>{m(o,clearInterval),o=void 0}))},Re=Z((e=>{const t=O(),s=O(),n=O(),o=O(),[a,r,i,d,l,c,u,h,g,f]=X(e,(()=>[O(),O(),O(),O()]),(e=>C(e)?void 0:e+"")),[L,w,v]=ee((()=>y)),p=(e,t,s)=>m(c(e),(([n,,o])=>{if(!x(o,t)){const a=U();if(l(e)!=S(e))V(a,t);else{let e=t;for(;!C(e)&&!x(a,e);)V(a,e),e=B(n,e)}if(s)return a;W(o,t,a)}return B(o,t)})),I=(e,t)=>m(c(e),(([,,e])=>W(e,t))),S=e=>B(t,e),y={setRelationshipDefinition:(e,a,r,i)=>(W(t,e,r),h(e,a,((t,a)=>{const r=U(),i=U(),d=U(),[l,u]=c(e);z(a,(([t,s],n)=>{C(t)||(V(i,t),m(B(u,t),(e=>{N(e,n),D(e)&&W(u,t)}))),C(s)||(V(i,s),x(u,s)||W(u,s,U()),V(B(u,s),n)),V(r,n),W(l,n,s),H(B(o,e),(t=>{x(p(e,t),n)&&V(d,t)}))})),t(),z(r,(t=>w(s,[e,t]))),z(i,(t=>w(n,[e,t]))),z(d,(t=>{I(e,t),w(o,[e,t])}))}),Y(i)),y),delRelationshipDefinition:e=>(W(t,e),g(e),y),getStore:a,getRelationshipIds:r,forEachRelationship:t=>i((s=>t(s,(t=>e.forEachRow(l(s),t))))),hasRelationship:d,getLocalTableId:l,getRemoteTableId:S,getRemoteRowId:(e,t)=>B(c(e)?.[0],t),getLocalRowIds:(e,t)=>J(B(c(e)?.[1],t)),getLinkedRowIds:(e,t)=>C(c(e))?[t]:J(p(e,t,!0)),addRemoteRowIdListener:(e,t,n)=>L(n,s,[e,t]),addLocalRowIdsListener:(e,t,s)=>L(s,n,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(p(e,t),L(s,o,[e,t])),delListener:e=>(I(...v(e)),y),destroy:f,getListenerStats:()=>({})};return oe(y)})),Te=(e,t,s,n=W)=>{const o=(a=P(e),r=e=>!ae(t,e),a.filter(r));var a,r;return u(se(t),(n=>s(e,n,t[n]))),u(o,(t=>n(e,t))),e},be=e=>{const t=s(e);return E(t)||t==a&&T(e)?t:void 0},Ce=(e,t,s)=>C(e)||!(e=>b(e,te)&&e.constructor==te)(e)||de(e)||ne(e)?(s?.(),!1):(ie(e,((s,n)=>{t(s,n)||re(e,n)})),!de(e)),me=(e,t,s)=>W(e,t,B(e,t)==-s?void 0:s),Ee=()=>{let e,t=0,s=0;const n=O(),o=O(),r=O(),l=O(),c=O(),h=O(),g=O(),f=O(),L=j(U),w=j(U),p=j(),y=j(),R=j(),T=j(),b=j(),M=j(),[A,J,N,X]=ee((()=>Be)),Y=(t,s)=>(!e||x(h,s)||ye(s))&&Ce(t,((e,t)=>Z(s,t,e)),(()=>ye(s))),Z=(e,t,s,n)=>Ce(n?s:_(s,e,t),((n,o)=>m($(e,t,o,n),(e=>(s[o]=e,!0)),(()=>!1))),(()=>ye(e,t))),$=(t,s,n,o)=>e?m(B(B(h,t),n),(e=>be(o)!=e.type?ye(t,s,n,o,e.default):o),(()=>ye(t,s,n,o))):C(be(o))?ye(t,s,n,o):o,_=(e,t,s)=>(m(B(g,t),(([n,o])=>{z(n,((t,s)=>{ae(e,s)||(e[s]=t)})),z(o,(n=>{ae(e,n)||ye(t,s,n)}))})),e),te=e=>Te(h,e,((e,t,s)=>{const n=O(),o=U();Te(q(h,t,O()),s,((e,t,s)=>{W(e,t,s),m(s.default,(e=>W(n,t,e)),(()=>V(o,t)))})),W(g,t,[n,o])}),((e,t)=>{W(h,t),W(g,t)})),se=e=>Te(f,e,((e,t,s)=>ne(t,s)),((e,t)=>fe(t))),ne=(e,t)=>Te(q(f,e,O(),(()=>ve(e,1))),t,((t,s,n)=>le(e,t,s,n)),((t,s)=>Le(e,t,s))),le=(e,t,s,n,o)=>Te(q(t,s,O(),(()=>pe(e,s,1))),n,((t,n,o)=>ce(e,s,t,n,o)),((n,a)=>we(e,t,s,n,a,o))),ce=(e,t,s,n,o)=>{x(s,n)||Ie(e,t,n,1);const a=B(s,n);o!==a&&(Se(e,t,n,a,o),W(s,n,o))},ue=(e,t,s,n,o)=>m(B(t,s),(t=>ce(e,s,t,n,o)),(()=>le(e,t,s,_({[n]:o},e,s)))),he=e=>{const s=""+t++;return x(e,s)?he(e):s},ge=e=>B(f,e)??ne(e,{}),fe=e=>ne(e,{}),Le=(e,t,s)=>le(e,t,s,{},!0),we=(e,t,s,n,o,a)=>{const r=B(B(g,e)?.[0],o);if(!C(r)&&!a)return ce(e,s,n,o,r);const i=t=>{Se(e,s,t,B(n,t)),Ie(e,s,t,-1),W(n,t)};C(r)?i(o):H(n,i),D(n)&&(pe(e,s,-1),D(W(t,s))&&(ve(e,-1),W(f,e)))},ve=(e,t)=>me(n,e,t),pe=(e,t,s)=>me(q(o,e,O()),t,s),Ie=(e,t,s,n)=>me(q(q(r,e,O()),t,O()),s,n),Se=(e,t,s,n,o)=>q(q(q(l,e,O()),t,O()),s,[n])[1]=o,ye=(e,t,s,n,o)=>(v(q(q(q(c,e,O()),t,O()),s,[]),n),o),Re=(e,t,s)=>m(B(B(B(l,e),t),s),(([e,t])=>[!0,e,t]),(()=>[!1,...i(Fe(e,t,s))])),Ee=e=>D(c)||D(M[e])?0:z(e?K(c,Q):c,((t,s)=>z(t,((t,n)=>z(t,((t,o)=>J(M[e],[s,n,o],t))))))),ke=e=>{if(!D(l)){const t=D(T[e])&&D(y[e])&&D(w[e]),s=D(b[e])&&D(R[e])&&D(p[e])&&D(L[e]);if(!t||!s){const a=e?[K(n),Q(o),K(r,Q),K(l,Q)]:[n,o,r,l];if(t||(z(a[2],((t,s)=>z(t,((t,n)=>{D(t)||J(T[e],[s,n])})))),z(a[1],((t,s)=>{D(t)||J(y[e],[s])})),D(a[0])||J(w[e])),!s){let t;z(a[3],((s,n)=>{let o;z(s,((s,a)=>{let r;z(s,(([s,i],d)=>{i!==s&&(J(b[e],[n,a,d],i,s,Re),t=o=r=1)})),r&&J(R[e],[n,a],Re)})),o&&J(p[e],[n],Re)})),t&&J(L[e],[],Re)}}}},Me=e=>(Pe(e),Be),Ae=()=>G(f,(e=>G(e,G))),xe=()=>P(f),De=e=>P(B(f,e)),Je=(e,t)=>P(B(B(f,e),t)),Fe=(e,t,s)=>B(B(B(f,e),t),s),ze=e=>Me((()=>(e=>Ce(e,Y,ye))(e)?se(e):0)),Ne=(e,t,s,n)=>Me((()=>m($(e,t,s,k(n)?n(Fe(e,t,s)):n),(n=>ue(e,ge(e),t,s,n))))),Oe=()=>Me((()=>se({}))),je=(e,t,s,n)=>Me((()=>m(B(f,e),(o=>m(B(o,t),(a=>x(a,s)?we(e,o,t,a,s,n):0)))))),Pe=(e,t)=>{if(-1==s)return;s++;const a=e?.();return s--,0==s&&(s=1,Ee(1),ke(1),s=-1,t?.(G(l,(e=>G(e,(e=>G(e,(e=>[...e]),(([e,t])=>e===t))),de)),de),G(c,(e=>G(e,G))))&&(s=1,z(l,((e,t)=>z(e,((e,s)=>z(e,(([e],n)=>C(e)?je(t,s,n,!0):Ne(t,s,n,e))))))),s=-1),Ee(0),ke(0),s=0,u([l,c,n,o,r],F)),a},Be={getTables:Ae,getTableIds:xe,getTable:e=>G(B(f,e),G),getRowIds:De,getRow:(e,t)=>G(B(B(f,e),t)),getCellIds:Je,getCell:Fe,hasTables:()=>!D(f),hasTable:e=>x(f,e),hasRow:(e,t)=>x(B(f,e),t),hasCell:(e,t,s)=>x(B(B(f,e),t),s),getJson:()=>I(f),getSchemaJson:()=>I(h),setTables:ze,setTable:(e,t)=>Me((()=>Y(t,e)?ne(e,t):0)),setRow:(e,t,s)=>Me((()=>Z(e,t,s)?le(e,ge(e),t,s):0)),addRow:(e,t)=>Pe((()=>{let s;return Z(e,s,t)&&le(e,ge(e),s=he(B(f,e)),t),s})),setPartialRow:(e,t,s)=>Me((()=>{if(Z(e,t,s,1)){const n=ge(e);ie(s,((s,o)=>ue(e,n,t,o,s)))}})),setCell:Ne,setJson:e=>{try{"{}"===e?Oe():ze(S(e))}catch{}return Be},setSchema:t=>Me((()=>{if((e=(e=>Ce(e,(e=>Ce(e,(e=>{if(!Ce(e,((e,t)=>d(["type","default"],t))))return!1;const t=e.type;return!(!E(t)&&t!=a||(be(e.default)!=t&&re(e,"default"),0))})))))(t))&&(te(t),!D(f))){const e=Ae();Oe(),ze(e)}})),delTables:Oe,delTable:e=>Me((()=>x(f,e)?fe(e):0)),delRow:(e,t)=>Me((()=>m(B(f,e),(s=>x(s,t)?Le(e,s,t):0)))),delCell:je,delSchema:()=>Me((()=>{te({}),e=!1})),transaction:Pe,forEachTable:e=>z(f,((t,s)=>e(s,(e=>z(t,((t,s)=>e(s,(e=>H(t,e))))))))),forEachRow:(e,t)=>z(B(f,e),((e,s)=>t(s,(t=>H(e,t))))),forEachCell:(e,t,s)=>H(B(B(f,e),t),s),addTablesListener:(e,t)=>A(e,L[t?1:0]),addTableIdsListener:(e,t)=>A(e,w[t?1:0]),addTableListener:(e,t,s)=>A(t,p[s?1:0],[e]),addRowIdsListener:(e,t,s)=>A(t,y[s?1:0],[e]),addRowListener:(e,t,s,n)=>A(s,R[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>A(s,T[n?1:0],[e,t]),addCellListener:(e,t,s,n,o)=>A(n,b[o?1:0],[e,t,s]),addInvalidCellListener:(e,t,s,n,o)=>A(n,M[o?1:0],[e,t,s]),callListener:e=>(X(e,[xe,De,Je],(e=>C(e[2])?[]:i(Fe(...e)))),Be),delListener:e=>(N(e),Be),getListenerStats:()=>({})};return oe(Be)};export{le as createCheckpoints,fe as createCustomPersister,Ie as createFilePersister,ue as createIndexes,ve as createLocalPersister,ge as createMetrics,Re as createRelationships,ye as createRemotePersister,pe as createSessionPersister,Ee as createStore,ce as defaultSorter};
|
package/lib/tinybase.js.gz
CHANGED
|
Binary file
|
package/lib/umd/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t;e=this,t=function(e){"use strict";const t=e=>typeof e,s=t(""),n=t(!0),r=t(0),l=t(t),o="type",a="default",d=(e,t)=>e.forEach(t),c=e=>e.length,
|
|
1
|
+
var e,t;e=this,t=function(e){"use strict";const t=e=>typeof e,s=t(""),n=t(!0),r=t(0),l=t(t),o="type",a="default",d=e=>[e,e],i=(e,t)=>e.forEach(t),c=e=>e.length,f=e=>0==c(e),u=e=>e.slice(1),h=(e,t)=>e.push(t),b=e=>JSON.stringify(e,((e,t)=>{return g(t,Map)?(s=[...t],n=(e,[t,s])=>(e[t]=s,e),r={},s.reduce(n,r)):t;var s,n,r})),p=JSON.parse,T=isFinite,g=(e,t)=>e instanceof t,y=e=>null==e,w=(e,t,s)=>y(e)?s?.():t(e),L=e=>e==s||e==n,R=(e,t)=>e?.has(t)??!1,C=e=>y(e)||0==(e=>e.size)(e),S=e=>e.clear(),v=(e,t)=>e?.forEach(t),I=(e,t)=>e?.delete(t),m=e=>new Map(e),E=(e=m)=>[e(),e()],J=e=>[...e?.keys()??[]],O=(e,t)=>e?.get(t),j=(e,t)=>v(e,((e,s)=>t(s,e))),x=(e,t,s)=>y(s)?(I(e,t),e):e?.set(t,s),z=(e,t,s,n)=>(R(e,t)||(n?.(s),e.set(t,s)),O(e,t)),M=(e,t,s)=>{const n={},r=t??(e=>e);return v(e,((e,t)=>w(r(e),(e=>s?.(e)?0:n[t]=e)))),n},k=(e,t)=>{const s=m(),n=t??(e=>e);return v(e,((e,t)=>s.set(t,n(e)))),s},F=e=>k(e,k),N=Object,P=N.keys,_=N.isFrozen,B=N.freeze,q=(e,t)=>!y(((e,t)=>w(e,(e=>e[t])))(e,t)),A=(e,t)=>delete e[t],D=(e,t)=>i(N.entries(e),(([e,s])=>t(s,e))),G=e=>f(P(e)),H=e=>new Set(e),K=(e,t)=>e?.add(t),Q=(e,t,s)=>c(s)<2?K(f(s)?e:z(e,s[0],H()),t):Q(z(e,s[0],m()),t,u(s)),U=e=>{const t=(s,n,...r)=>w(s,(s=>f(r)?e(s,n):i([r[0],null],(e=>t(O(s,e),n,...u(r))))));return t},V=(e,t,s,n=x)=>{const r=(l=J(e),o=e=>!q(t,e),l.filter(o));var l,o;return i(P(t),(n=>s(e,n,t[n]))),i(r,(t=>n(e,t))),e},W=e=>{const s=t(e);return L(s)||s==r&&T(e)?s:void 0},X=(e,t,s)=>y(e)||!(e=>g(e,N)&&e.constructor==N)(e)||G(e)||_(e)?(s?.(),!1):(D(e,((s,n)=>{t(s,n)||A(e,n)})),!G(e)),Y=(e,t,s)=>x(e,t,O(e,t)==-s?void 0:s);e.createStore=()=>{let e,s=0,n=0;const f=m(),u=m(),T=m(),g=m(),N=m(),P=m(),_=m(),Z=m(),$=E(H),ee=E(H),te=E(),se=E(),ne=E(),re=E(),le=E(),oe=E(),[ae,de,ie,ce]=(e=>{let t,s=0;const n=[],r=m();return[(l,o,a=[])=>{t??=e();const d=n.pop()??""+s++;return x(r,d,[l,o,a]),Q(o,d,a),d},(e,s=[],...n)=>U(v)(e,(e=>w(O(r,e),(([e])=>e(t,...s,...n)))),...s),e=>w(O(r,e),(([,t,s])=>(U(I)(t,e,...s),x(r,e),c(n)<1e3&&h(n,e),s)),(()=>[])),(e,s,n)=>w(O(r,e),(([e,,r])=>{const l=(...o)=>{const a=c(o);a==c(r)?e(t,...o,...n(o)):y(r[a])?i(s[a](...o),(e=>l(...o,e))):l(...o,r[a])};l()}))]})((()=>Ke)),fe=(t,s)=>(!e||R(P,s)||je(s))&&X(t,((e,t)=>ue(s,t,e)),(()=>je(s))),ue=(e,t,s,n)=>X(n?s:be(s,e,t),((n,r)=>w(he(e,t,r,n),(e=>(s[r]=e,!0)),(()=>!1))),(()=>je(e,t))),he=(t,s,n,r)=>e?w(O(O(P,t),n),(e=>W(r)!=e.type?je(t,s,n,r,e.default):r),(()=>je(t,s,n,r))):y(W(r))?je(t,s,n,r):r,be=(e,t,s)=>(w(O(_,t),(([n,r])=>{v(n,((t,s)=>{q(e,s)||(e[s]=t)})),v(r,(n=>{q(e,n)||je(t,s,n)}))})),e),pe=e=>V(P,e,((e,t,s)=>{const n=m(),r=H();V(z(P,t,m()),s,((e,t,s)=>{x(e,t,s),w(s.default,(e=>x(n,t,e)),(()=>K(r,t)))})),x(_,t,[n,r])}),((e,t)=>{x(P,t),x(_,t)})),Te=e=>V(Z,e,((e,t,s)=>ge(t,s)),((e,t)=>Se(t))),ge=(e,t)=>V(z(Z,e,m(),(()=>me(e,1))),t,((t,s,n)=>ye(e,t,s,n)),((t,s)=>ve(e,t,s))),ye=(e,t,s,n,r)=>V(z(t,s,m(),(()=>Ee(e,s,1))),n,((t,n,r)=>we(e,s,t,n,r)),((n,l)=>Ie(e,t,s,n,l,r))),we=(e,t,s,n,r)=>{R(s,n)||Je(e,t,n,1);const l=O(s,n);r!==l&&(Oe(e,t,n,l,r),x(s,n,r))},Le=(e,t,s,n,r)=>w(O(t,s),(t=>we(e,s,t,n,r)),(()=>ye(e,t,s,be({[n]:r},e,s)))),Re=e=>{const t=""+s++;return R(e,t)?Re(e):t},Ce=e=>O(Z,e)??ge(e,{}),Se=e=>ge(e,{}),ve=(e,t,s)=>ye(e,t,s,{},!0),Ie=(e,t,s,n,r,l)=>{const o=O(O(_,e)?.[0],r);if(!y(o)&&!l)return we(e,s,n,r,o);const a=t=>{Oe(e,s,t,O(n,t)),Je(e,s,t,-1),x(n,t)};y(o)?a(r):j(n,a),C(n)&&(Ee(e,s,-1),C(x(t,s))&&(me(e,-1),x(Z,e)))},me=(e,t)=>Y(f,e,t),Ee=(e,t,s)=>Y(z(u,e,m()),t,s),Je=(e,t,s,n)=>Y(z(z(T,e,m()),t,m()),s,n),Oe=(e,t,s,n,r)=>z(z(z(g,e,m()),t,m()),s,[n])[1]=r,je=(e,t,s,n,r)=>(h(z(z(z(N,e,m()),t,m()),s,[]),n),r),xe=(e,t,s)=>w(O(O(O(g,e),t),s),(([e,t])=>[!0,e,t]),(()=>[!1,...d(Be(e,t,s))])),ze=e=>C(N)||C(oe[e])?0:v(e?k(N,F):N,((t,s)=>v(t,((t,n)=>v(t,((t,r)=>de(oe[e],[s,n,r],t))))))),Me=e=>{if(!C(g)){const t=C(re[e])&&C(se[e])&&C(ee[e]),s=C(le[e])&&C(ne[e])&&C(te[e])&&C($[e]);if(!t||!s){const n=e?[k(f),F(u),k(T,F),k(g,F)]:[f,u,T,g];if(t||(v(n[2],((t,s)=>v(t,((t,n)=>{C(t)||de(re[e],[s,n])})))),v(n[1],((t,s)=>{C(t)||de(se[e],[s])})),C(n[0])||de(ee[e])),!s){let t;v(n[3],((s,n)=>{let r;v(s,((s,l)=>{let o;v(s,(([s,a],d)=>{a!==s&&(de(le[e],[n,l,d],a,s,xe),t=r=o=1)})),o&&de(ne[e],[n,l],xe)})),r&&de(te[e],[n],xe)})),t&&de($[e],[],xe)}}}},ke=e=>(He(e),Ke),Fe=()=>M(Z,(e=>M(e,M))),Ne=()=>J(Z),Pe=e=>J(O(Z,e)),_e=(e,t)=>J(O(O(Z,e),t)),Be=(e,t,s)=>O(O(O(Z,e),t),s),qe=e=>ke((()=>(e=>X(e,fe,je))(e)?Te(e):0)),Ae=(e,s,n,r)=>ke((()=>w(he(e,s,n,t(r)==l?r(Be(e,s,n)):r),(t=>Le(e,Ce(e),s,n,t))))),De=()=>ke((()=>Te({}))),Ge=(e,t,s,n)=>ke((()=>w(O(Z,e),(r=>w(O(r,t),(l=>R(l,s)?Ie(e,r,t,l,s,n):0)))))),He=(e,t)=>{if(-1==n)return;n++;const s=e?.();return n--,0==n&&(n=1,ze(1),Me(1),n=-1,t?.(M(g,(e=>M(e,(e=>M(e,(e=>[...e]),(([e,t])=>e===t))),G)),G),M(N,(e=>M(e,M))))&&(n=1,v(g,((e,t)=>v(e,((e,s)=>v(e,(([e],n)=>y(e)?Ge(t,s,n,!0):Ae(t,s,n,e))))))),n=-1),ze(0),Me(0),n=0,i([g,N,f,u,T],S)),s},Ke={getTables:Fe,getTableIds:Ne,getTable:e=>M(O(Z,e),M),getRowIds:Pe,getRow:(e,t)=>M(O(O(Z,e),t)),getCellIds:_e,getCell:Be,hasTables:()=>!C(Z),hasTable:e=>R(Z,e),hasRow:(e,t)=>R(O(Z,e),t),hasCell:(e,t,s)=>R(O(O(Z,e),t),s),getJson:()=>b(Z),getSchemaJson:()=>b(P),setTables:qe,setTable:(e,t)=>ke((()=>fe(t,e)?ge(e,t):0)),setRow:(e,t,s)=>ke((()=>ue(e,t,s)?ye(e,Ce(e),t,s):0)),addRow:(e,t)=>He((()=>{let s;return ue(e,s,t)&&ye(e,Ce(e),s=Re(O(Z,e)),t),s})),setPartialRow:(e,t,s)=>ke((()=>{if(ue(e,t,s,1)){const n=Ce(e);D(s,((s,r)=>Le(e,n,t,r,s)))}})),setCell:Ae,setJson:e=>{try{"{}"===e?De():qe(p(e))}catch{}return Ke},setSchema:t=>ke((()=>{if((e=(e=>X(e,(e=>X(e,(e=>{if(!X(e,((e,t)=>[o,a].includes(t))))return!1;const t=e.type;return!(!L(t)&&t!=r||(W(e.default)!=t&&A(e,a),0))})))))(t))&&(pe(t),!C(Z))){const e=Fe();De(),qe(e)}})),delTables:De,delTable:e=>ke((()=>R(Z,e)?Se(e):0)),delRow:(e,t)=>ke((()=>w(O(Z,e),(s=>R(s,t)?ve(e,s,t):0)))),delCell:Ge,delSchema:()=>ke((()=>{pe({}),e=!1})),transaction:He,forEachTable:e=>v(Z,((t,s)=>e(s,(e=>v(t,((t,s)=>e(s,(e=>j(t,e))))))))),forEachRow:(e,t)=>v(O(Z,e),((e,s)=>t(s,(t=>j(e,t))))),forEachCell:(e,t,s)=>j(O(O(Z,e),t),s),addTablesListener:(e,t)=>ae(e,$[t?1:0]),addTableIdsListener:(e,t)=>ae(e,ee[t?1:0]),addTableListener:(e,t,s)=>ae(t,te[s?1:0],[e]),addRowIdsListener:(e,t,s)=>ae(t,se[s?1:0],[e]),addRowListener:(e,t,s,n)=>ae(s,ne[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>ae(s,re[n?1:0],[e,t]),addCellListener:(e,t,s,n,r)=>ae(n,le[r?1:0],[e,t,s]),addInvalidCellListener:(e,t,s,n,r)=>ae(n,oe[r?1:0],[e,t,s]),callListener:e=>(ce(e,[Ne,Pe,_e],(e=>y(e[2])?[]:d(Be(...e)))),Ke),delListener:e=>(ie(e),Ke),getListenerStats:()=>({})};return B(Ke)},Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBaseStore={});
|
package/lib/umd/store.js.gz
CHANGED
|
Binary file
|
package/lib/umd/tinybase.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e,t;e=this,t=function(e,t){"use strict";const s=e=>typeof e,n="",o=s(n),r=s(!0),a=s(0),i=s(s),d="type",l="default",c="utf8",u=(e,t)=>e.includes(t),f=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),h=(e,t)=>e.sort(t),g=(e,t)=>e.forEach(t),p=e=>v(e,((e,t)=>e+t),0),L=e=>e.length,w=e=>0==L(e),v=(e,t,s)=>e.reduce(t,s),I=e=>e.slice(1),y=(e,t)=>e.push(t),S=e=>e.pop(),R=e=>JSON.stringify(e,((e,t)=>k(t,Map)?v([...t],((e,[t,s])=>(e[t]=s,e)),{}):t)),T=JSON.parse,b=Math.max,C=Math.min,m=isFinite,k=(e,t)=>e instanceof t,E=e=>null==e,M=(e,t,s)=>E(e)?s?.():t(e),x=e=>e==o||e==r,A=e=>s(e)==i,P=()=>{},D=e=>e.size,J=(e,t)=>e?.has(t)??!1,F=e=>E(e)||0==D(e),O=e=>[...e?.values()??[]],j=e=>e.clear(),z=(e,t)=>e?.forEach(t),N=(e,t)=>e?.delete(t),B=e=>new Map(e),_=(e=B)=>[e(),e()],q=e=>[...e?.keys()??[]],H=(e,t)=>e?.get(t),W=(e,t)=>z(e,((e,s)=>t(s,e))),G=(e,t,s)=>E(s)?(N(e,t),e):e?.set(t,s),K=(e,t,s,n)=>(J(e,t)||(n?.(s),e.set(t,s)),H(e,t)),Q=(e,t)=>{const s={},n=t??(e=>e);return z(e,((e,t)=>s[t]=n(e))),s},U=(e,t)=>{const s=B(),n=t??(e=>e);return z(e,((e,t)=>s.set(t,n(e)))),s},V=e=>new Set(e),X=(e,t)=>e?.add(t),Y=(e,t,s)=>{const n=e.hasRow,o=B(),r=B(),a=B(),i=B(),d=B(),l=t=>M(H(d,t),(s=>{z(s,e.delListener),G(d,t)})),c=e=>{G(o,e),G(r,e),G(a,e),G(i,e),l(e)};return[()=>e,()=>q(o),e=>W(r,e),e=>J(r,e),e=>H(o,e),e=>H(r,e),(e,t)=>G(r,e,t),(c,u,f,h,p)=>{const L=B(),w=B();G(o,c,u),J(r,c)||(G(r,c,t()),G(a,c,B()),G(i,c,B()));const v=H(a,c),I=H(i,c),y=t=>{const o=s=>e.getCell(u,t,s),r=H(v,t),a=n(u,t)?s(h(o,t)):void 0;if(r!=a&&G(L,t,[r,a]),!E(p)){const e=H(I,t),s=n(u,t)?p(o,t):void 0;e!=s&&G(w,t,s)}},S=e=>{f((()=>{z(L,(([,e],t)=>G(v,t,e))),z(w,((e,t)=>G(I,t,e)))}),L,w,v,I,e),j(L),j(w)};W(v,y),e.hasTable(u)&&g(e.getRowIds(u),(e=>{J(v,e)||y(e)})),S(!0),l(c),G(d,c,V([e.addRowListener(u,null,((e,t,s)=>y(s))),e.addTableListener(u,(()=>S()))]))},c,()=>W(d,c)]},Z=(e,t)=>s(e)==o?t=>t(e):e??(()=>t??n),$=e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))},ee=(e,t,s)=>L(s)<2?X(w(s)?e:K(e,s[0],V()),t):ee(K(e,s[0],B()),t,I(s)),te=e=>{const t=(s,n,...o)=>M(s,(s=>w(o)?e(s,n):g([o[0],null],(e=>t(H(s,e),n,...I(o))))));return t},se=e=>{let t,s=0;const n=[],o=B();return[(r,a,i=[])=>{t??=e();const d=S(n)??""+s++;return G(o,d,[r,a,i]),ee(a,d,i),d},(e,s=[],...n)=>te(z)(e,(e=>M(H(o,e),(([e])=>e(t,...s,...n)))),...s),e=>M(H(o,e),(([,t,s])=>(te(N)(t,e,...s),G(o,e),L(n)<1e3&&y(n,e),s)),(()=>[])),(e,s,n)=>M(H(o,e),(([e,,o])=>{const r=(...a)=>{const i=L(a);i==L(o)?e(t,...a,...n(a)):E(o[i])?g(s[i](...a),(e=>r(...a,e))):r(...a,o[i])};r()}))]},ne=Object,oe=ne.keys,re=ne.isFrozen,ae=ne.freeze,ie=(e,t)=>!E(((e,t)=>M(e,(e=>e[t])))(e,t)),de=(e,t)=>delete e[t],le=(e,t)=>g(ne.entries(e),(([e,s])=>t(s,e))),ce=e=>w(oe(e)),ue=$((e=>{let t,s,n,o=100,r=B(),a=1;const i=V(),d=B(),[l,c,f]=se((()=>N)),h=B(),p=B(),v=[],I=[],R=(t,s)=>{a=0,e.transaction((()=>z(H(h,s),((s,n)=>z(s,((s,o)=>z(s,((s,r)=>E(s[t])?e.delCell(n,o,r,!0):e.setCell(n,o,r,s[t]))))))))),a=1},T=e=>{G(h,e),G(p,e),c(d,[e])},b=(e,t)=>g(((e,t)=>e.splice(0,t))(e,t??L(e)),T),C=()=>b(v,L(v)-o),m=e.addCellListener(null,null,null,((e,s,o,i,d,l)=>{if(a){M(t,(()=>{y(v,t),C(),b(I),t=void 0,n=1}));const e=K(r,s,B()),a=K(e,o,B()),c=K(a,i,[void 0,void 0],(e=>e[0]=l));c[1]=d,c[0]===c[1]&&F(G(a,i))&&F(G(e,o))&&F(G(r,s))&&(t=S(v),n=1),P()}})),k=(e="")=>(E(t)&&(t=""+s++,G(h,t,r),O(t,e),r=B(),n=1),t),x=()=>{w(v)||(I.unshift(k()),R(0,t),t=S(v),n=1)},A=()=>{w(I)||(y(v,t),t=I.shift(),R(1,t),n=1)},P=()=>{n&&(c(i),n=0)},D=e=>{const t=k(e);return P(),t},O=(e,t)=>(j(e)&&H(p,e)!==t&&(G(p,e,t),c(d,[e])),N),j=e=>J(h,e),N={setSize:e=>(o=e,C(),N),addCheckpoint:D,setCheckpoint:O,getStore:()=>e,getCheckpointIds:()=>[[...v],t,[...I]],forEachCheckpoint:e=>W(p,e),hasCheckpoint:j,getCheckpoint:e=>H(p,e),goBackward:()=>(x(),P(),N),goForward:()=>(A(),P(),N),goTo:e=>{const s=u(v,e)?x:u(I,e)?A:null;for(;!E(s)&&e!=t;)s();return P(),N},addCheckpointIdsListener:e=>l(e,i),addCheckpointListener:(e,t)=>l(t,d,[e]),delListener:e=>(f(e),N),clear:()=>(b(v),b(I),E(t)||T(t),t=void 0,s=0,D(),N),destroy:()=>{e.delListener(m)},getListenerStats:()=>({})};return ae(N.clear())})),fe=(e,t)=>e<t?-1:1,he=$((e=>{const t=B(),s=B(),[o,r,a,i,d,l,c,u,g,p]=Y(e,B,(e=>E(e)?n:e+n)),[L,w,v]=se((()=>y)),I=(t,s,n)=>{const o=d(t);z(n,((t,n)=>s(n,(s=>z(t,(t=>s(t,(s=>e.forEachCell(o,t,s)))))))))},y={setIndexDefinition:(e,n,o,r,a,i=fe)=>{const d=E(a)?void 0:([e],[t])=>a(e,t);return u(e,n,((n,o,a,u,g,p)=>{let L=0;const v=V(),I=V(),y=l(e);if(z(o,(([e,t],s)=>{E(e)||(X(v,e),M(H(y,e),(t=>{N(t,s),F(t)&&(G(y,e),L=1)}))),E(t)||(X(v,t),J(y,t)||(G(y,t,V()),L=1),X(H(y,t),s),E(r)||X(I,t))})),n(),F(g)||(p?W(y,(e=>X(I,e))):W(a,(e=>M(H(u,e),(e=>X(I,e))))),z(I,(e=>{const t=(t,s)=>i(H(g,t),H(g,s),e),s=[...H(y,e)];f(s,t)||(G(y,e,V(h(s,t))),X(v,e))}))),(L||p)&&!E(d)){const t=[...y];f(t,d)||(c(e,B(h(t,d))),L=1)}L&&w(t,[e]),z(v,(t=>w(s,[e,t])))}),Z(o),M(r,Z)),y},delIndexDefinition:e=>(g(e),y),getStore:o,getIndexIds:r,forEachIndex:e=>a(((t,s)=>e(t,(e=>I(t,e,s))))),forEachSlice:(e,t)=>I(e,t,l(e)),hasIndex:i,hasSlice:(e,t)=>J(l(e),t),getTableId:d,getSliceIds:e=>q(l(e)),getSliceRowIds:(e,t)=>O(H(l(e),t)),addSliceIdsListener:(e,s)=>L(s,t,[e]),addSliceRowIdsListener:(e,t,n)=>L(n,s,[e,t]),delListener:e=>(v(e),y),destroy:p,getListenerStats:()=>({})};return ae(y)})),ge=B([["avg",[(e,t)=>p(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>b(...e),(e,t)=>b(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:b(t,e)]],["min",[e=>C(...e),(e,t)=>C(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:C(t,e)]],["sum",[e=>p(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),pe=$((e=>{const t=B(),[s,o,r,a,i,d,l,c,u,f]=Y(e,P,(e=>isNaN(e)||E(e)||!0===e||!1===e||e===n?void 0:1*e)),[h,g,p]=se((()=>L)),L={setMetricDefinition:(e,s,n,o,r,a,i)=>{const u=A(n)?[n,r,a,i]:H(ge,n)??H(ge,"sum");return c(e,s,((s,n,o,r,a,i)=>{let c=d(e),f=D(r);const[h,p,L,w]=u;i=i||E(c),z(n,(([e,t])=>{i||(c=E(e)?p?.(c,t,f++):E(t)?L?.(c,e,f--):w?.(c,t,e,f)),i=i||E(c)})),s(),F(r)?c=void 0:i&&(c=h(O(r),D(r))),m(c)||(c=void 0);const v=d(e);c!=v&&(l(e,c),g(t,[e],c,v))}),Z(o,1)),L},delMetricDefinition:e=>(u(e),L),getStore:s,getMetricIds:o,forEachMetric:r,hasMetric:a,getTableId:i,getMetric:d,addMetricListener:(e,s)=>h(s,t,[e]),delListener:e=>(p(e),L),destroy:f,getListenerStats:()=>({})};return ae(L)})),Le=(e,t,s,n,o)=>{let r,a=0;const i={load:async s=>{if(2!=a){a=1;const n=await t();E(n)||""==n?e.setTables(s):e.setJson(n),a=0}return i},startAutoLoad:async e=>(i.stopAutoLoad(),await i.load(e),n(i.load),i),stopAutoLoad:()=>(o(),i),save:async()=>(1!=a&&(a=2,await s(e.getJson()),a=0),i),startAutoSave:async()=>(await i.stopAutoSave().save(),r=e.addTablesListener((()=>i.save())),i),stopAutoSave:()=>(M(r,e.delListener),i),getStore:()=>e,destroy:()=>i.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return ae(i)},we="storage",ve=globalThis.window,Ie=(e,t,s)=>{let n;return Le(e,(async()=>s.getItem(t)),(async e=>s.setItem(t,e)),(e=>{n=n=>{n.storageArea===s&&n.key===t&&e()},ve.addEventListener(we,n)}),(()=>{ve.removeEventListener(we,n),n=void 0}))},ye=e=>e.headers.get("ETag"),Se=$((e=>{const t=B(),s=B(),o=B(),r=B(),[a,i,d,l,c,u,f,h,g,p]=Y(e,(()=>[B(),B(),B(),B()]),(e=>E(e)?void 0:e+n)),[L,w,v]=se((()=>R)),I=(e,t,s)=>M(u(e),(([n,,o])=>{if(!J(o,t)){const r=V();if(c(e)!=S(e))X(r,t);else{let e=t;for(;!E(e)&&!J(r,e);)X(r,e),e=H(n,e)}if(s)return r;G(o,t,r)}return H(o,t)})),y=(e,t)=>M(u(e),(([,,e])=>G(e,t))),S=e=>H(t,e),R={setRelationshipDefinition:(e,n,a,i)=>(G(t,e,a),h(e,n,((t,n)=>{const a=V(),i=V(),d=V(),[l,c]=u(e);z(n,(([t,s],n)=>{E(t)||(X(i,t),M(H(c,t),(e=>{N(e,n),F(e)&&G(c,t)}))),E(s)||(X(i,s),J(c,s)||G(c,s,V()),X(H(c,s),n)),X(a,n),G(l,n,s),W(H(r,e),(t=>{J(I(e,t),n)&&X(d,t)}))})),t(),z(a,(t=>w(s,[e,t]))),z(i,(t=>w(o,[e,t]))),z(d,(t=>{y(e,t),w(r,[e,t])}))}),Z(i)),R),delRelationshipDefinition:e=>(G(t,e),g(e),R),getStore:a,getRelationshipIds:i,forEachRelationship:t=>d((s=>t(s,(t=>e.forEachRow(c(s),t))))),hasRelationship:l,getLocalTableId:c,getRemoteTableId:S,getRemoteRowId:(e,t)=>H(u(e)?.[0],t),getLocalRowIds:(e,t)=>O(H(u(e)?.[1],t)),getLinkedRowIds:(e,t)=>E(u(e))?[t]:O(I(e,t,!0)),addRemoteRowIdListener:(e,t,n)=>L(n,s,[e,t]),addLocalRowIdsListener:(e,t,s)=>L(s,o,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(I(e,t),L(s,r,[e,t])),delListener:e=>(y(...v(e)),R),destroy:p,getListenerStats:()=>({})};return ae(R)})),Re=(e,t,s,n=G)=>{const o=(r=q(e),a=e=>!ie(t,e),r.filter(a));var r,a;return g(oe(t),(n=>s(e,n,t[n]))),g(o,(t=>n(e,t))),e},Te=e=>{const t=s(e);return x(t)||t==a&&m(e)?t:void 0},be=(e,t,s)=>E(e)||!(e=>k(e,ne)&&e.constructor==ne)(e)||ce(e)||re(e)?(s?.(),!1):(le(e,((s,n)=>{t(s,n)||de(e,n)})),!ce(e)),Ce=(e,t,s)=>G(e,t,H(e,t)==-s?void 0:s);e.createCheckpoints=ue,e.createCustomPersister=Le,e.createFilePersister=(e,s)=>{let n;return Le(e,(async()=>{try{return await t.promises.readFile(s,c)}catch{}}),(async e=>{try{await t.promises.writeFile(s,e,c)}catch{}}),(e=>{n=t.watch(s,e)}),(()=>{n?.close(),n=void 0}))},e.createIndexes=he,e.createLocalPersister=(e,t)=>Ie(e,t,localStorage),e.createMetrics=pe,e.createRelationships=Se,e.createRemotePersister=(e,t,s,n)=>{let o,r;return Le(e,(async()=>{const e=await fetch(t);return r=ye(e),e.text()}),(async e=>await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:e})),(e=>{o=setInterval((async()=>{const s=await fetch(t,{method:"HEAD"}),n=ye(s);E(r)||E(n)||n==r||(r=n,e())}),1e3*n)}),(()=>{M(o,clearInterval),o=void 0}))},e.createSessionPersister=(e,t)=>Ie(e,t,sessionStorage),e.createStore=()=>{let e,t=0,s=0;const n=B(),o=B(),r=B(),i=B(),c=B(),f=B(),h=B(),p=B(),L=_(V),w=_(V),v=_(),I=_(),S=_(),b=_(),C=_(),m=_(),[k,P,D,O]=se((()=>je)),N=(t,s)=>(!e||J(f,s)||ye(s))&&be(t,((e,t)=>Y(s,t,e)),(()=>ye(s))),Y=(e,t,s,n)=>be(n?s:$(s,e,t),((n,o)=>M(Z(e,t,o,n),(e=>(s[o]=e,!0)),(()=>!1))),(()=>ye(e,t))),Z=(t,s,n,o)=>e?M(H(H(f,t),n),(e=>Te(o)!=e.type?ye(t,s,n,o,e.default):o),(()=>ye(t,s,n,o))):E(Te(o))?ye(t,s,n,o):o,$=(e,t,s)=>(M(H(h,t),(([n,o])=>{z(n,((t,s)=>{ie(e,s)||(e[s]=t)})),z(o,(n=>{ie(e,n)||ye(t,s,n)}))})),e),ee=e=>Re(f,e,((e,t,s)=>{const n=B(),o=V();Re(K(f,t,B()),s,((e,t,s)=>{G(e,t,s),M(s.default,(e=>G(n,t,e)),(()=>X(o,t)))})),G(h,t,[n,o])}),((e,t)=>{G(f,t),G(h,t)})),te=e=>Re(p,e,((e,t,s)=>ne(t,s)),((e,t)=>he(t))),ne=(e,t)=>Re(K(p,e,B(),(()=>Le(e,1))),t,((t,s,n)=>oe(e,t,s,n)),((t,s)=>ge(e,t,s))),oe=(e,t,s,n,o)=>Re(K(t,s,B(),(()=>we(e,s,1))),n,((t,n,o)=>re(e,s,t,n,o)),((n,r)=>pe(e,t,s,n,r,o))),re=(e,t,s,n,o)=>{J(s,n)||ve(e,t,n,1);const r=H(s,n);o!==r&&(Ie(e,t,n,r),G(s,n,o))},ce=(e,t,s,n,o)=>M(H(t,s),(t=>re(e,s,t,n,o)),(()=>oe(e,t,s,$({[n]:o},e,s)))),ue=e=>{const s=""+t++;return J(e,s)?ue(e):s},fe=e=>H(p,e)??ne(e,{}),he=e=>ne(e,{}),ge=(e,t,s)=>oe(e,t,s,{},!0),pe=(e,t,s,n,o,r)=>{const a=H(H(h,e)?.[0],o);if(!E(a)&&!r)return re(e,s,n,o,a);const i=t=>{Ie(e,s,t,H(n,t)),ve(e,s,t,-1),G(n,t)};E(a)?i(o):W(n,i),F(n)&&(we(e,s,-1),F(G(t,s))&&(Le(e,-1),G(p,e)))},Le=(e,t)=>Ce(n,e,t),we=(e,t,s)=>Ce(K(o,e,B()),t,s),ve=(e,t,s,n)=>Ce(K(K(r,e,B()),t,B()),s,n),Ie=(e,t,s,n)=>K(K(K(i,e,B()),t,B()),s,n),ye=(e,t,s,n,o)=>(y(K(K(K(c,e,B()),t,B()),s,[]),n),o),Se=(e,t,s)=>{const n=H(H(i,e),t),o=De(e,t,s);return J(n,s)?[!0,H(n,s),o]:[!1,o,o]},me=e=>F(c)||F(m[e])?0:z(e?U(c,(e=>U(e,U))):c,((t,s)=>z(t,((t,n)=>z(t,((t,o)=>P(m[e],[s,n,o],t))))))),ke=e=>{if(!F(i)){const t=F(b[e])&&F(I[e])&&F(w[e]),s=F(C[e])&&F(S[e])&&F(v[e])&&F(L[e]);if(!t||!s){const a=e?[U(n),U(o,U),U(r,(e=>U(e,U))),U(i,(e=>U(e,U)))]:[n,o,r,i];if(t||(z(a[2],((t,s)=>z(t,((t,n)=>{F(t)||P(b[e],[s,n])})))),z(a[1],((t,s)=>{F(t)||P(I[e],[s])})),F(a[0])||P(w[e])),!s){let t;z(a[3],((s,n)=>{let o;z(s,((s,r)=>{let a;z(s,((s,i)=>{const d=De(n,r,i);d!==s&&(P(C[e],[n,r,i],d,s,Se),t=o=a=1)})),a&&P(S[e],[n,r],Se)})),o&&P(v[e],[n],Se)})),t&&P(L[e],[],Se)}}}},Ee=e=>(Oe(e),je),Me=()=>Q(p,(e=>Q(e,Q))),xe=()=>q(p),Ae=e=>q(H(p,e)),Pe=(e,t)=>q(H(H(p,e),t)),De=(e,t,s)=>H(H(H(p,e),t),s),Je=e=>Ee((()=>(e=>be(e,N,ye))(e)?te(e):0)),Fe=()=>Ee((()=>te({}))),Oe=e=>{if(-1==s)return;s++;const t=e?.();return s--,0==s&&(s=1,me(1),ke(1),s=-1,me(0),ke(0),s=0,g([i,c,n,o,r],j)),t},je={getTables:Me,getTableIds:xe,getTable:e=>Q(H(p,e),Q),getRowIds:Ae,getRow:(e,t)=>Q(H(H(p,e),t)),getCellIds:Pe,getCell:De,hasTables:()=>!F(p),hasTable:e=>J(p,e),hasRow:(e,t)=>J(H(p,e),t),hasCell:(e,t,s)=>J(H(H(p,e),t),s),getJson:()=>R(p),getSchemaJson:()=>R(f),setTables:Je,setTable:(e,t)=>Ee((()=>N(t,e)?ne(e,t):0)),setRow:(e,t,s)=>Ee((()=>Y(e,t,s)?oe(e,fe(e),t,s):0)),addRow:(e,t)=>Oe((()=>{let s;return Y(e,s,t)&&oe(e,fe(e),s=ue(H(p,e)),t),s})),setPartialRow:(e,t,s)=>Ee((()=>{if(Y(e,t,s,1)){const n=fe(e);le(s,((s,o)=>ce(e,n,t,o,s)))}})),setCell:(e,t,s,n)=>Ee((()=>M(Z(e,t,s,A(n)?n(De(e,t,s)):n),(n=>ce(e,fe(e),t,s,n))))),setJson:e=>{try{"{}"===e?Fe():Je(T(e))}catch{}return je},setSchema:t=>Ee((()=>{if((e=(e=>be(e,(e=>be(e,(e=>{if(!be(e,((e,t)=>u([d,l],t))))return!1;const t=e.type;return!(!x(t)&&t!=a||(Te(e.default)!=t&&de(e,l),0))})))))(t))&&(ee(t),!F(p))){const e=Me();Fe(),Je(e)}})),delTables:Fe,delTable:e=>Ee((()=>J(p,e)?he(e):0)),delRow:(e,t)=>Ee((()=>M(H(p,e),(s=>J(s,t)?ge(e,s,t):0)))),delCell:(e,t,s,n)=>Ee((()=>M(H(p,e),(o=>M(H(o,t),(r=>J(r,s)?pe(e,o,t,r,s,n):0)))))),delSchema:()=>Ee((()=>{ee({}),e=!1})),transaction:Oe,forEachTable:e=>z(p,((t,s)=>e(s,(e=>z(t,((t,s)=>e(s,(e=>W(t,e))))))))),forEachRow:(e,t)=>z(H(p,e),((e,s)=>t(s,(t=>W(e,t))))),forEachCell:(e,t,s)=>W(H(H(p,e),t),s),addTablesListener:(e,t)=>k(e,L[t?1:0]),addTableIdsListener:(e,t)=>k(e,w[t?1:0]),addTableListener:(e,t,s)=>k(t,v[s?1:0],[e]),addRowIdsListener:(e,t,s)=>k(t,I[s?1:0],[e]),addRowListener:(e,t,s,n)=>k(s,S[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>k(s,b[n?1:0],[e,t]),addCellListener:(e,t,s,n,o)=>k(n,C[o?1:0],[e,t,s]),addInvalidCellListener:(e,t,s,n,o)=>k(n,m[o?1:0],[e,t,s]),callListener:e=>(O(e,[xe,Ae,Pe],(e=>E(e[2])?[]:[,,].fill(De(...e)))),je),delListener:e=>(D(e),je),getListenerStats:()=>({})};return ae(je)},e.defaultSorter=fe,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("fs")):"function"==typeof define&&define.amd?define(["exports","fs"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBase={},e.fs);
|
|
1
|
+
var e,t;e=this,t=function(e,t){"use strict";const s=e=>typeof e,n="",o=s(n),r=s(!0),a=s(0),i=s(s),d="type",l="default",c="utf8",u=e=>[e,e],f=(e,t)=>e.includes(t),h=(e,t)=>e.every(((s,n)=>0==n||t(e[n-1],s)<=0)),g=(e,t)=>e.sort(t),p=(e,t)=>e.forEach(t),L=e=>I(e,((e,t)=>e+t),0),w=e=>e.length,v=e=>0==w(e),I=(e,t,s)=>e.reduce(t,s),y=e=>e.slice(1),S=(e,t)=>e.push(t),R=e=>e.pop(),T=e=>JSON.stringify(e,((e,t)=>E(t,Map)?I([...t],((e,[t,s])=>(e[t]=s,e)),{}):t)),b=JSON.parse,C=Math.max,m=Math.min,k=isFinite,E=(e,t)=>e instanceof t,M=e=>null==e,x=(e,t,s)=>M(e)?s?.():t(e),A=e=>e==o||e==r,P=e=>s(e)==i,D=()=>{},J=e=>e.size,F=(e,t)=>e?.has(t)??!1,O=e=>M(e)||0==J(e),j=e=>[...e?.values()??[]],z=e=>e.clear(),N=(e,t)=>e?.forEach(t),B=(e,t)=>e?.delete(t),_=e=>new Map(e),q=(e=_)=>[e(),e()],H=e=>[...e?.keys()??[]],W=(e,t)=>e?.get(t),G=(e,t)=>N(e,((e,s)=>t(s,e))),K=(e,t,s)=>M(s)?(B(e,t),e):e?.set(t,s),Q=(e,t,s,n)=>(F(e,t)||(n?.(s),e.set(t,s)),W(e,t)),U=(e,t,s)=>{const n={},o=t??(e=>e);return N(e,((e,t)=>x(o(e),(e=>s?.(e)?0:n[t]=e)))),n},V=(e,t)=>{const s=_(),n=t??(e=>e);return N(e,((e,t)=>s.set(t,n(e)))),s},X=e=>V(e,V),Y=e=>new Set(e),Z=(e,t)=>e?.add(t),$=(e,t,s)=>{const n=e.hasRow,o=_(),r=_(),a=_(),i=_(),d=_(),l=t=>x(W(d,t),(s=>{N(s,e.delListener),K(d,t)})),c=e=>{K(o,e),K(r,e),K(a,e),K(i,e),l(e)};return[()=>e,()=>H(o),e=>G(r,e),e=>F(r,e),e=>W(o,e),e=>W(r,e),(e,t)=>K(r,e,t),(c,u,f,h,g)=>{const L=_(),w=_();K(o,c,u),F(r,c)||(K(r,c,t()),K(a,c,_()),K(i,c,_()));const v=W(a,c),I=W(i,c),y=t=>{const o=s=>e.getCell(u,t,s),r=W(v,t),a=n(u,t)?s(h(o,t)):void 0;if(r!=a&&K(L,t,[r,a]),!M(g)){const e=W(I,t),s=n(u,t)?g(o,t):void 0;e!=s&&K(w,t,s)}},S=e=>{f((()=>{N(L,(([,e],t)=>K(v,t,e))),N(w,((e,t)=>K(I,t,e)))}),L,w,v,I,e),z(L),z(w)};G(v,y),e.hasTable(u)&&p(e.getRowIds(u),(e=>{F(v,e)||y(e)})),S(!0),l(c),K(d,c,Y([e.addRowListener(u,null,((e,t,s)=>y(s))),e.addTableListener(u,(()=>S()))]))},c,()=>G(d,c)]},ee=(e,t)=>s(e)==o?t=>t(e):e??(()=>t??n),te=e=>{const t=new WeakMap;return s=>(t.has(s)||t.set(s,e(s)),t.get(s))},se=(e,t,s)=>w(s)<2?Z(v(s)?e:Q(e,s[0],Y()),t):se(Q(e,s[0],_()),t,y(s)),ne=e=>{const t=(s,n,...o)=>x(s,(s=>v(o)?e(s,n):p([o[0],null],(e=>t(W(s,e),n,...y(o))))));return t},oe=e=>{let t,s=0;const n=[],o=_();return[(r,a,i=[])=>{t??=e();const d=R(n)??""+s++;return K(o,d,[r,a,i]),se(a,d,i),d},(e,s=[],...n)=>ne(N)(e,(e=>x(W(o,e),(([e])=>e(t,...s,...n)))),...s),e=>x(W(o,e),(([,t,s])=>(ne(B)(t,e,...s),K(o,e),w(n)<1e3&&S(n,e),s)),(()=>[])),(e,s,n)=>x(W(o,e),(([e,,o])=>{const r=(...a)=>{const i=w(a);i==w(o)?e(t,...a,...n(a)):M(o[i])?p(s[i](...a),(e=>r(...a,e))):r(...a,o[i])};r()}))]},re=Object,ae=re.keys,ie=re.isFrozen,de=re.freeze,le=(e,t)=>!M(((e,t)=>x(e,(e=>e[t])))(e,t)),ce=(e,t)=>delete e[t],ue=(e,t)=>p(re.entries(e),(([e,s])=>t(s,e))),fe=e=>v(ae(e)),he=te((e=>{let t,s,n,o=100,r=_(),a=1;const i=Y(),d=_(),[l,c,u]=oe((()=>z)),h=_(),g=_(),L=[],I=[],y=(t,s)=>{a=0,e.transaction((()=>N(W(h,s),((s,n)=>N(s,((s,o)=>N(s,((s,r)=>M(s[t])?e.delCell(n,o,r,!0):e.setCell(n,o,r,s[t]))))))))),a=1},T=e=>{K(h,e),K(g,e),c(d,[e])},b=(e,t)=>p(((e,t)=>e.splice(0,t))(e,t??w(e)),T),C=()=>b(L,w(L)-o),m=e.addCellListener(null,null,null,((e,s,o,i,d,l)=>{if(a){x(t,(()=>{S(L,t),C(),b(I),t=void 0,n=1}));const e=Q(r,s,_()),a=Q(e,o,_()),c=Q(a,i,[void 0,void 0],(e=>e[0]=l));c[1]=d,c[0]===c[1]&&O(K(a,i))&&O(K(e,o))&&O(K(r,s))&&(t=R(L),n=1),P()}})),k=(e="")=>(M(t)&&(t=""+s++,K(h,t,r),J(t,e),r=_(),n=1),t),E=()=>{v(L)||(I.unshift(k()),y(0,t),t=R(L),n=1)},A=()=>{v(I)||(S(L,t),t=I.shift(),y(1,t),n=1)},P=()=>{n&&(c(i),n=0)},D=e=>{const t=k(e);return P(),t},J=(e,t)=>(j(e)&&W(g,e)!==t&&(K(g,e,t),c(d,[e])),z),j=e=>F(h,e),z={setSize:e=>(o=e,C(),z),addCheckpoint:D,setCheckpoint:J,getStore:()=>e,getCheckpointIds:()=>[[...L],t,[...I]],forEachCheckpoint:e=>G(g,e),hasCheckpoint:j,getCheckpoint:e=>W(g,e),goBackward:()=>(E(),P(),z),goForward:()=>(A(),P(),z),goTo:e=>{const s=f(L,e)?E:f(I,e)?A:null;for(;!M(s)&&e!=t;)s();return P(),z},addCheckpointIdsListener:e=>l(e,i),addCheckpointListener:(e,t)=>l(t,d,[e]),delListener:e=>(u(e),z),clear:()=>(b(L),b(I),M(t)||T(t),t=void 0,s=0,D(),z),destroy:()=>{e.delListener(m)},getListenerStats:()=>({})};return de(z.clear())})),ge=(e,t)=>e<t?-1:1,pe=te((e=>{const t=_(),s=_(),[o,r,a,i,d,l,c,u,f,p]=$(e,_,(e=>M(e)?n:e+n)),[L,w,v]=oe((()=>y)),I=(t,s,n)=>{const o=d(t);N(n,((t,n)=>s(n,(s=>N(t,(t=>s(t,(s=>e.forEachCell(o,t,s)))))))))},y={setIndexDefinition:(e,n,o,r,a,i=ge)=>{const d=M(a)?void 0:([e],[t])=>a(e,t);return u(e,n,((n,o,a,u,f,p)=>{let L=0;const v=Y(),I=Y(),y=l(e);if(N(o,(([e,t],s)=>{M(e)||(Z(v,e),x(W(y,e),(t=>{B(t,s),O(t)&&(K(y,e),L=1)}))),M(t)||(Z(v,t),F(y,t)||(K(y,t,Y()),L=1),Z(W(y,t),s),M(r)||Z(I,t))})),n(),O(f)||(p?G(y,(e=>Z(I,e))):G(a,(e=>x(W(u,e),(e=>Z(I,e))))),N(I,(e=>{const t=(t,s)=>i(W(f,t),W(f,s),e),s=[...W(y,e)];h(s,t)||(K(y,e,Y(g(s,t))),Z(v,e))}))),(L||p)&&!M(d)){const t=[...y];h(t,d)||(c(e,_(g(t,d))),L=1)}L&&w(t,[e]),N(v,(t=>w(s,[e,t])))}),ee(o),x(r,ee)),y},delIndexDefinition:e=>(f(e),y),getStore:o,getIndexIds:r,forEachIndex:e=>a(((t,s)=>e(t,(e=>I(t,e,s))))),forEachSlice:(e,t)=>I(e,t,l(e)),hasIndex:i,hasSlice:(e,t)=>F(l(e),t),getTableId:d,getSliceIds:e=>H(l(e)),getSliceRowIds:(e,t)=>j(W(l(e),t)),addSliceIdsListener:(e,s)=>L(s,t,[e]),addSliceRowIdsListener:(e,t,n)=>L(n,s,[e,t]),delListener:e=>(v(e),y),destroy:p,getListenerStats:()=>({})};return de(y)})),Le=_([["avg",[(e,t)=>L(e)/t,(e,t,s)=>e+(t-e)/(s+1),(e,t,s)=>e+(e-t)/(s-1),(e,t,s,n)=>e+(t-s)/n]],["max",[e=>C(...e),(e,t)=>C(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:C(t,e)]],["min",[e=>m(...e),(e,t)=>m(t,e),(e,t)=>t==e?void 0:e,(e,t,s)=>s==e?void 0:m(t,e)]],["sum",[e=>L(e),(e,t)=>e+t,(e,t)=>e-t,(e,t,s)=>e-s+t]]]),we=te((e=>{const t=_(),[s,o,r,a,i,d,l,c,u,f]=$(e,D,(e=>isNaN(e)||M(e)||!0===e||!1===e||e===n?void 0:1*e)),[h,g,p]=oe((()=>L)),L={setMetricDefinition:(e,s,n,o,r,a,i)=>{const u=P(n)?[n,r,a,i]:W(Le,n)??W(Le,"sum");return c(e,s,((s,n,o,r,a,i)=>{let c=d(e),f=J(r);const[h,p,L,w]=u;i=i||M(c),N(n,(([e,t])=>{i||(c=M(e)?p?.(c,t,f++):M(t)?L?.(c,e,f--):w?.(c,t,e,f)),i=i||M(c)})),s(),O(r)?c=void 0:i&&(c=h(j(r),J(r))),k(c)||(c=void 0);const v=d(e);c!=v&&(l(e,c),g(t,[e],c,v))}),ee(o,1)),L},delMetricDefinition:e=>(u(e),L),getStore:s,getMetricIds:o,forEachMetric:r,hasMetric:a,getTableId:i,getMetric:d,addMetricListener:(e,s)=>h(s,t,[e]),delListener:e=>(p(e),L),destroy:f,getListenerStats:()=>({})};return de(L)})),ve=(e,t,s,n,o)=>{let r,a=0;const i={load:async s=>{if(2!=a){a=1;const n=await t();M(n)||""==n?e.setTables(s):e.setJson(n),a=0}return i},startAutoLoad:async e=>(i.stopAutoLoad(),await i.load(e),n(i.load),i),stopAutoLoad:()=>(o(),i),save:async()=>(1!=a&&(a=2,await s(e.getJson()),a=0),i),startAutoSave:async()=>(await i.stopAutoSave().save(),r=e.addTablesListener((()=>i.save())),i),stopAutoSave:()=>(x(r,e.delListener),i),getStore:()=>e,destroy:()=>i.stopAutoLoad().stopAutoSave(),getStats:()=>({})};return de(i)},Ie="storage",ye=globalThis.window,Se=(e,t,s)=>{let n;return ve(e,(async()=>s.getItem(t)),(async e=>s.setItem(t,e)),(e=>{n=n=>{n.storageArea===s&&n.key===t&&e()},ye.addEventListener(Ie,n)}),(()=>{ye.removeEventListener(Ie,n),n=void 0}))},Re=e=>e.headers.get("ETag"),Te=te((e=>{const t=_(),s=_(),o=_(),r=_(),[a,i,d,l,c,u,f,h,g,p]=$(e,(()=>[_(),_(),_(),_()]),(e=>M(e)?void 0:e+n)),[L,w,v]=oe((()=>R)),I=(e,t,s)=>x(u(e),(([n,,o])=>{if(!F(o,t)){const r=Y();if(c(e)!=S(e))Z(r,t);else{let e=t;for(;!M(e)&&!F(r,e);)Z(r,e),e=W(n,e)}if(s)return r;K(o,t,r)}return W(o,t)})),y=(e,t)=>x(u(e),(([,,e])=>K(e,t))),S=e=>W(t,e),R={setRelationshipDefinition:(e,n,a,i)=>(K(t,e,a),h(e,n,((t,n)=>{const a=Y(),i=Y(),d=Y(),[l,c]=u(e);N(n,(([t,s],n)=>{M(t)||(Z(i,t),x(W(c,t),(e=>{B(e,n),O(e)&&K(c,t)}))),M(s)||(Z(i,s),F(c,s)||K(c,s,Y()),Z(W(c,s),n)),Z(a,n),K(l,n,s),G(W(r,e),(t=>{F(I(e,t),n)&&Z(d,t)}))})),t(),N(a,(t=>w(s,[e,t]))),N(i,(t=>w(o,[e,t]))),N(d,(t=>{y(e,t),w(r,[e,t])}))}),ee(i)),R),delRelationshipDefinition:e=>(K(t,e),g(e),R),getStore:a,getRelationshipIds:i,forEachRelationship:t=>d((s=>t(s,(t=>e.forEachRow(c(s),t))))),hasRelationship:l,getLocalTableId:c,getRemoteTableId:S,getRemoteRowId:(e,t)=>W(u(e)?.[0],t),getLocalRowIds:(e,t)=>j(W(u(e)?.[1],t)),getLinkedRowIds:(e,t)=>M(u(e))?[t]:j(I(e,t,!0)),addRemoteRowIdListener:(e,t,n)=>L(n,s,[e,t]),addLocalRowIdsListener:(e,t,s)=>L(s,o,[e,t]),addLinkedRowIdsListener:(e,t,s)=>(I(e,t),L(s,r,[e,t])),delListener:e=>(y(...v(e)),R),destroy:p,getListenerStats:()=>({})};return de(R)})),be=(e,t,s,n=K)=>{const o=(r=H(e),a=e=>!le(t,e),r.filter(a));var r,a;return p(ae(t),(n=>s(e,n,t[n]))),p(o,(t=>n(e,t))),e},Ce=e=>{const t=s(e);return A(t)||t==a&&k(e)?t:void 0},me=(e,t,s)=>M(e)||!(e=>E(e,re)&&e.constructor==re)(e)||fe(e)||ie(e)?(s?.(),!1):(ue(e,((s,n)=>{t(s,n)||ce(e,n)})),!fe(e)),ke=(e,t,s)=>K(e,t,W(e,t)==-s?void 0:s);e.createCheckpoints=he,e.createCustomPersister=ve,e.createFilePersister=(e,s)=>{let n;return ve(e,(async()=>{try{return await t.promises.readFile(s,c)}catch{}}),(async e=>{try{await t.promises.writeFile(s,e,c)}catch{}}),(e=>{n=t.watch(s,e)}),(()=>{n?.close(),n=void 0}))},e.createIndexes=pe,e.createLocalPersister=(e,t)=>Se(e,t,localStorage),e.createMetrics=we,e.createRelationships=Te,e.createRemotePersister=(e,t,s,n)=>{let o,r;return ve(e,(async()=>{const e=await fetch(t);return r=Re(e),e.text()}),(async e=>await fetch(s,{method:"POST",headers:{"Content-Type":"application/json"},body:e})),(e=>{o=setInterval((async()=>{const s=await fetch(t,{method:"HEAD"}),n=Re(s);M(r)||M(n)||n==r||(r=n,e())}),1e3*n)}),(()=>{x(o,clearInterval),o=void 0}))},e.createSessionPersister=(e,t)=>Se(e,t,sessionStorage),e.createStore=()=>{let e,t=0,s=0;const n=_(),o=_(),r=_(),i=_(),c=_(),h=_(),g=_(),L=_(),w=q(Y),v=q(Y),I=q(),y=q(),R=q(),C=q(),m=q(),k=q(),[E,D,J,j]=oe((()=>qe)),B=(t,s)=>(!e||F(h,s)||Te(s))&&me(t,((e,t)=>$(s,t,e)),(()=>Te(s))),$=(e,t,s,n)=>me(n?s:te(s,e,t),((n,o)=>x(ee(e,t,o,n),(e=>(s[o]=e,!0)),(()=>!1))),(()=>Te(e,t))),ee=(t,s,n,o)=>e?x(W(W(h,t),n),(e=>Ce(o)!=e.type?Te(t,s,n,o,e.default):o),(()=>Te(t,s,n,o))):M(Ce(o))?Te(t,s,n,o):o,te=(e,t,s)=>(x(W(g,t),(([n,o])=>{N(n,((t,s)=>{le(e,s)||(e[s]=t)})),N(o,(n=>{le(e,n)||Te(t,s,n)}))})),e),se=e=>be(h,e,((e,t,s)=>{const n=_(),o=Y();be(Q(h,t,_()),s,((e,t,s)=>{K(e,t,s),x(s.default,(e=>K(n,t,e)),(()=>Z(o,t)))})),K(g,t,[n,o])}),((e,t)=>{K(h,t),K(g,t)})),ne=e=>be(L,e,((e,t,s)=>re(t,s)),((e,t)=>Le(t))),re=(e,t)=>be(Q(L,e,_(),(()=>Ie(e,1))),t,((t,s,n)=>ae(e,t,s,n)),((t,s)=>we(e,t,s))),ae=(e,t,s,n,o)=>be(Q(t,s,_(),(()=>ye(e,s,1))),n,((t,n,o)=>ie(e,s,t,n,o)),((n,r)=>ve(e,t,s,n,r,o))),ie=(e,t,s,n,o)=>{F(s,n)||Se(e,t,n,1);const r=W(s,n);o!==r&&(Re(e,t,n,r,o),K(s,n,o))},he=(e,t,s,n,o)=>x(W(t,s),(t=>ie(e,s,t,n,o)),(()=>ae(e,t,s,te({[n]:o},e,s)))),ge=e=>{const s=""+t++;return F(e,s)?ge(e):s},pe=e=>W(L,e)??re(e,{}),Le=e=>re(e,{}),we=(e,t,s)=>ae(e,t,s,{},!0),ve=(e,t,s,n,o,r)=>{const a=W(W(g,e)?.[0],o);if(!M(a)&&!r)return ie(e,s,n,o,a);const i=t=>{Re(e,s,t,W(n,t)),Se(e,s,t,-1),K(n,t)};M(a)?i(o):G(n,i),O(n)&&(ye(e,s,-1),O(K(t,s))&&(Ie(e,-1),K(L,e)))},Ie=(e,t)=>ke(n,e,t),ye=(e,t,s)=>ke(Q(o,e,_()),t,s),Se=(e,t,s,n)=>ke(Q(Q(r,e,_()),t,_()),s,n),Re=(e,t,s,n,o)=>Q(Q(Q(i,e,_()),t,_()),s,[n])[1]=o,Te=(e,t,s,n,o)=>(S(Q(Q(Q(c,e,_()),t,_()),s,[]),n),o),Ee=(e,t,s)=>x(W(W(W(i,e),t),s),(([e,t])=>[!0,e,t]),(()=>[!1,...u(Oe(e,t,s))])),Me=e=>O(c)||O(k[e])?0:N(e?V(c,X):c,((t,s)=>N(t,((t,n)=>N(t,((t,o)=>D(k[e],[s,n,o],t))))))),xe=e=>{if(!O(i)){const t=O(C[e])&&O(y[e])&&O(v[e]),s=O(m[e])&&O(R[e])&&O(I[e])&&O(w[e]);if(!t||!s){const a=e?[V(n),X(o),V(r,X),V(i,X)]:[n,o,r,i];if(t||(N(a[2],((t,s)=>N(t,((t,n)=>{O(t)||D(C[e],[s,n])})))),N(a[1],((t,s)=>{O(t)||D(y[e],[s])})),O(a[0])||D(v[e])),!s){let t;N(a[3],((s,n)=>{let o;N(s,((s,r)=>{let a;N(s,(([s,i],d)=>{i!==s&&(D(m[e],[n,r,d],i,s,Ee),t=o=a=1)})),a&&D(R[e],[n,r],Ee)})),o&&D(I[e],[n],Ee)})),t&&D(w[e],[],Ee)}}}},Ae=e=>(_e(e),qe),Pe=()=>U(L,(e=>U(e,U))),De=()=>H(L),Je=e=>H(W(L,e)),Fe=(e,t)=>H(W(W(L,e),t)),Oe=(e,t,s)=>W(W(W(L,e),t),s),je=e=>Ae((()=>(e=>me(e,B,Te))(e)?ne(e):0)),ze=(e,t,s,n)=>Ae((()=>x(ee(e,t,s,P(n)?n(Oe(e,t,s)):n),(n=>he(e,pe(e),t,s,n))))),Ne=()=>Ae((()=>ne({}))),Be=(e,t,s,n)=>Ae((()=>x(W(L,e),(o=>x(W(o,t),(r=>F(r,s)?ve(e,o,t,r,s,n):0)))))),_e=(e,t)=>{if(-1==s)return;s++;const a=e?.();return s--,0==s&&(s=1,Me(1),xe(1),s=-1,t?.(U(i,(e=>U(e,(e=>U(e,(e=>[...e]),(([e,t])=>e===t))),fe)),fe),U(c,(e=>U(e,U))))&&(s=1,N(i,((e,t)=>N(e,((e,s)=>N(e,(([e],n)=>M(e)?Be(t,s,n,!0):ze(t,s,n,e))))))),s=-1),Me(0),xe(0),s=0,p([i,c,n,o,r],z)),a},qe={getTables:Pe,getTableIds:De,getTable:e=>U(W(L,e),U),getRowIds:Je,getRow:(e,t)=>U(W(W(L,e),t)),getCellIds:Fe,getCell:Oe,hasTables:()=>!O(L),hasTable:e=>F(L,e),hasRow:(e,t)=>F(W(L,e),t),hasCell:(e,t,s)=>F(W(W(L,e),t),s),getJson:()=>T(L),getSchemaJson:()=>T(h),setTables:je,setTable:(e,t)=>Ae((()=>B(t,e)?re(e,t):0)),setRow:(e,t,s)=>Ae((()=>$(e,t,s)?ae(e,pe(e),t,s):0)),addRow:(e,t)=>_e((()=>{let s;return $(e,s,t)&&ae(e,pe(e),s=ge(W(L,e)),t),s})),setPartialRow:(e,t,s)=>Ae((()=>{if($(e,t,s,1)){const n=pe(e);ue(s,((s,o)=>he(e,n,t,o,s)))}})),setCell:ze,setJson:e=>{try{"{}"===e?Ne():je(b(e))}catch{}return qe},setSchema:t=>Ae((()=>{if((e=(e=>me(e,(e=>me(e,(e=>{if(!me(e,((e,t)=>f([d,l],t))))return!1;const t=e.type;return!(!A(t)&&t!=a||(Ce(e.default)!=t&&ce(e,l),0))})))))(t))&&(se(t),!O(L))){const e=Pe();Ne(),je(e)}})),delTables:Ne,delTable:e=>Ae((()=>F(L,e)?Le(e):0)),delRow:(e,t)=>Ae((()=>x(W(L,e),(s=>F(s,t)?we(e,s,t):0)))),delCell:Be,delSchema:()=>Ae((()=>{se({}),e=!1})),transaction:_e,forEachTable:e=>N(L,((t,s)=>e(s,(e=>N(t,((t,s)=>e(s,(e=>G(t,e))))))))),forEachRow:(e,t)=>N(W(L,e),((e,s)=>t(s,(t=>G(e,t))))),forEachCell:(e,t,s)=>G(W(W(L,e),t),s),addTablesListener:(e,t)=>E(e,w[t?1:0]),addTableIdsListener:(e,t)=>E(e,v[t?1:0]),addTableListener:(e,t,s)=>E(t,I[s?1:0],[e]),addRowIdsListener:(e,t,s)=>E(t,y[s?1:0],[e]),addRowListener:(e,t,s,n)=>E(s,R[n?1:0],[e,t]),addCellIdsListener:(e,t,s,n)=>E(s,C[n?1:0],[e,t]),addCellListener:(e,t,s,n,o)=>E(n,m[o?1:0],[e,t,s]),addInvalidCellListener:(e,t,s,n,o)=>E(n,k[o?1:0],[e,t,s]),callListener:e=>(j(e,[De,Je,Fe],(e=>M(e[2])?[]:u(Oe(...e)))),qe),delListener:e=>(J(e),qe),getListenerStats:()=>({})};return de(qe)},e.defaultSorter=ge,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("fs")):"function"==typeof define&&define.amd?define(["exports","fs"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TinyBase={},e.fs);
|
package/lib/umd/tinybase.js.gz
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tinybase",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": "jamesgpearce",
|
|
5
5
|
"repository": "github:tinyplex/tinybase",
|
|
6
6
|
"license": "MIT",
|
|
@@ -57,11 +57,11 @@
|
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@babel/cli": "^7.17.0",
|
|
60
|
-
"@babel/core": "^7.17.
|
|
60
|
+
"@babel/core": "^7.17.2",
|
|
61
61
|
"@babel/preset-env": "^7.16.11",
|
|
62
62
|
"@babel/preset-react": "^7.16.7",
|
|
63
63
|
"@babel/preset-typescript": "^7.16.7",
|
|
64
|
-
"@rollup/plugin-replace": "^3.0
|
|
64
|
+
"@rollup/plugin-replace": "^3.1.0",
|
|
65
65
|
"@types/asciichart": "^1.5.6",
|
|
66
66
|
"@types/expect-puppeteer": "^4.4.7",
|
|
67
67
|
"@types/http-server": "^0.12.1",
|
|
@@ -73,34 +73,34 @@
|
|
|
73
73
|
"@types/react-dom": "^17.0.11",
|
|
74
74
|
"@types/react-test-renderer": "^17.0.1",
|
|
75
75
|
"@types/tmp": "^0.2.3",
|
|
76
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
77
|
-
"@typescript-eslint/parser": "^5.
|
|
76
|
+
"@typescript-eslint/eslint-plugin": "^5.12.0",
|
|
77
|
+
"@typescript-eslint/parser": "^5.12.0",
|
|
78
78
|
"asciichart": "^1.5.25",
|
|
79
79
|
"babel-eslint": "^10.1.0",
|
|
80
|
-
"babel-jest": "^27.5.
|
|
80
|
+
"babel-jest": "^27.5.1",
|
|
81
81
|
"babel-preset-minify": "^0.5.1",
|
|
82
82
|
"country-flag-emoji-json": "^2.0.0",
|
|
83
|
-
"cspell": "^5.18.
|
|
84
|
-
"esbuild": "^0.14.
|
|
85
|
-
"eslint": "^8.
|
|
83
|
+
"cspell": "^5.18.4",
|
|
84
|
+
"esbuild": "^0.14.21",
|
|
85
|
+
"eslint": "^8.9.0",
|
|
86
86
|
"eslint-config-prettier": "^8.3.0",
|
|
87
|
-
"eslint-plugin-jest": "^26.
|
|
88
|
-
"eslint-plugin-jsdoc": "^37.
|
|
87
|
+
"eslint-plugin-jest": "^26.1.0",
|
|
88
|
+
"eslint-plugin-jsdoc": "^37.9.1",
|
|
89
89
|
"eslint-plugin-react": "^7.28.0",
|
|
90
90
|
"eslint-plugin-react-hooks": "^4.3.0",
|
|
91
91
|
"gulp": "^4.0.2",
|
|
92
92
|
"gulp-gzip": "^1.4.2",
|
|
93
93
|
"http-server": "^14.1.0",
|
|
94
|
-
"jest": "^27.5.
|
|
94
|
+
"jest": "^27.5.1",
|
|
95
95
|
"jest-fetch-mock": "^3.0.3",
|
|
96
96
|
"jest-puppeteer": "^6.1.0",
|
|
97
97
|
"less": "^4.1.2",
|
|
98
98
|
"prettier": "^2.5.1",
|
|
99
|
-
"puppeteer": "^13.
|
|
99
|
+
"puppeteer": "^13.3.2",
|
|
100
100
|
"react": "^17.0.2",
|
|
101
101
|
"react-dom": "^17.0.2",
|
|
102
102
|
"react-test-renderer": "^17.0.2",
|
|
103
|
-
"rollup": "^2.67.
|
|
103
|
+
"rollup": "^2.67.2",
|
|
104
104
|
"rollup-plugin-esbuild": "^4.8.2",
|
|
105
105
|
"rollup-plugin-gzip": "^3.0.0",
|
|
106
106
|
"rollup-plugin-prettier": "^2.2.2",
|
package/readme.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
<section id="hero"><h2 id="a-javascript-library-for-structured-state">A JavaScript library for <em>structured state</em>.</h2><p>Using plain old JavaScript objects to manage data gets old very quickly. It's error-prone, tricky to track changes efficiently, and easy to mistakenly incur performance costs.</p><p><em>TinyBase is a smarter way to structure your application state:</em></p><ul><li>Familiar concepts of <a href="#set-and-get-tables-rows-and-cells">tables, rows, and cells</a>, and <a href="#apply-schemas-to-tables">schematization</a> to model your data domain.</li><li><a href="#register-listeners-at-any-granularity">Flexibly reactive</a> to reconciled updates, so you only spend cycles on the data that changes.</li><li><a href="#create-indexes-for-fast-lookups">Indexing</a>, <a href="#define-metrics-and-aggregations">metrics</a>, <a href="#configure-relationships-between-tables">relationships</a> - and even an <a href="#use-checkpoints-for-an-easy-undo-stack">undo stack</a> for your app state! - out of the box.</li><li>Easily <a href="#persist-data-to-browser-file-or-server">sync your data</a> to local or remote storage, and use <a href="#call-react-hooks-to-bind-to-data">idiomatic bindings</a> to your React UI.</li></ul><p><em>Tiny by name, tiny by nature</em>, TinyBase only costs <a href="#did-we-say-tiny">2.
|
|
1
|
+
<section id="hero"><h2 id="a-javascript-library-for-structured-state">A JavaScript library for <em>structured state</em>.</h2><p>Using plain old JavaScript objects to manage data gets old very quickly. It's error-prone, tricky to track changes efficiently, and easy to mistakenly incur performance costs.</p><p><em>TinyBase is a smarter way to structure your application state:</em></p><ul><li>Familiar concepts of <a href="#set-and-get-tables-rows-and-cells">tables, rows, and cells</a>, and <a href="#apply-schemas-to-tables">schematization</a> to model your data domain.</li><li><a href="#register-listeners-at-any-granularity">Flexibly reactive</a> to reconciled updates, so you only spend cycles on the data that changes.</li><li><a href="#create-indexes-for-fast-lookups">Indexing</a>, <a href="#define-metrics-and-aggregations">metrics</a>, <a href="#configure-relationships-between-tables">relationships</a> - and even an <a href="#use-checkpoints-for-an-easy-undo-stack">undo stack</a> for your app state! - out of the box.</li><li>Easily <a href="#persist-data-to-browser-file-or-server">sync your data</a> to local or remote storage, and use <a href="#call-react-hooks-to-bind-to-data">idiomatic bindings</a> to your React UI.</li></ul><p><em>Tiny by name, tiny by nature</em>, TinyBase only costs <a href="#did-we-say-tiny">2.9kB - 5.9kB</a> when compressed, and has zero dependencies. And of course it's <a href="#well-tested-and-documented">well tested</a>, <a href="https://tinybase.org/guides/the-basics/getting-started">fully documented</a>, and <a href="https://github.com/tinyplex/tinybase">open source</a>. Other <a href="https://tinybase.org/guides/faq/">FAQs</a>?</p></section><hr><p><a id="start" href="https://tinybase.org/guides/the-basics/getting-started">Get started</a></p><p><a href="https://tinybase.org/demos">Try the demos</a></p><p><a href="https://tinybase.org/api/store/interfaces/store/store/">Read the docs</a></p><hr><section><h2 id="set-and-get-tables-rows-and-cells">Set and get tables, rows, and cells.</h2><p>Creating a <a href="https://tinybase.org/api/store/interfaces/store/store"><code>Store</code></a> requires just a simple call to the <a href="https://tinybase.org/api/store/functions/creation/createstore"><code>createStore</code></a> function. Once you have one, you can easily set <a href="https://tinybase.org/api/store/type-aliases/store/table"><code>Table</code></a>, <a href="https://tinybase.org/api/store/type-aliases/store/row"><code>Row</code></a>, or <a href="https://tinybase.org/api/store/type-aliases/store/cell"><code>Cell</code></a> values by their <a href="https://tinybase.org/api/common/type-aliases/identity/id"><code>Id</code></a>. And of course you can easily get the values back out again.</p><p>Read more about setting and changing data in <a href="https://tinybase.org/guides/the-basics">The Basics</a> guide.</p></section>
|
|
2
2
|
|
|
3
3
|
```js
|
|
4
4
|
const store = createStore()
|
|
@@ -192,4 +192,4 @@ console.log(store.getCell('pets', 'felix', 'sold'));
|
|
|
192
192
|
// -> false
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
-
<section><h2 id="did-we-say-tiny">Did we say tiny?</h2><p>If you use the basic <a href="https://tinybase.org/api/store"><code>store</code></a> module alone, you'll only add a gzipped <em>2.
|
|
195
|
+
<section><h2 id="did-we-say-tiny">Did we say tiny?</h2><p>If you use the basic <a href="https://tinybase.org/api/store"><code>store</code></a> module alone, you'll only add a gzipped <em>2.9kB</em> to your app. You can incrementally add the other modules as you need more functionality, or get it all for <em>5.9kB</em>. The <code>ui-react</code> adaptor is just another <em>2.6kB</em>, and everything is fast.</p><p>Life's easy when you have zero dependencies.</p><p>Read more about how TinyBase is structured in the <a href="https://tinybase.org/guides/how-tinybase-is-built/architecture">Architecture</a> guide.</p></section><div class="table"><table class="fixed"><tbody><tr><th width="30%"> </th><th>.js.gz</th><th>.js</th><th>debug.js</th><th>.d.ts</th></tr><tr><th class="right"><a href="https://tinybase.org/api/store">store</a></th><td>2.9kB</td><td>6.5kB</td><td>27.4kB</td><td>102.0kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/indexes">indexes</a></th><td>1.6kB</td><td>3.2kB</td><td>14.1kB</td><td>32.9kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/metrics">metrics</a></th><td>1.5kB</td><td>3.1kB</td><td>12.6kB</td><td>29.1kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/relationships">relationships</a></th><td>1.6kB</td><td>3.2kB</td><td>14.8kB</td><td>42.1kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/checkpoints">checkpoints</a></th><td>1.3kB</td><td>2.5kB</td><td>10.4kB</td><td>33.0kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/persisters">persisters</a></th><td>0.8kB</td><td>1.6kB</td><td>4.9kB</td><td>26.7kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/common">common</a></th><td>0.1kB</td><td>0.1kB</td><td>0.1kB</td><td>3.5kB</td></tr><tr><th class="right"><a href="https://tinybase.org/api/tinybase">tinybase</a></th><td>5.9kB</td><td>14.0kB</td><td>59.1kB</td><td>0.3kB</td></tr></tbody></table></div><section><h2 id="well-tested-and-documented">Well tested and documented.</h2><p>TinyBase has <em>100.0%</em> test coverage, including the code throughout the documentation - even on this page! The guides, demos, and API examples are designed to make it as easy as possible to get up and running.</p><p>Read more about how TinyBase is tested in the Unit <a href="https://tinybase.org/guides/how-tinybase-is-built/testing">Testing</a> guide.</p></section><div class="table"><table class="fixed"><tbody><tr><th width="30%"> </th><th>Total</th><th>Tested</th><th>Coverage</th></tr><tr><th class="right">Lines</th><td>985</td><td>985</td><td>100.0%</td></tr><tr><th class="right">Statements</th><td>1,074</td><td>1,074</td><td>100.0%</td></tr><tr><th class="right">Functions</th><td>411</td><td>411</td><td>100.0%</td></tr><tr><th class="right">Branches</th><td>359</td><td>359</td><td>100.0%</td></tr><tr><th class="right">Tests</th><td colspan="3">1,776</td></tr><tr><th class="right">Assertions</th><td colspan="3">8,848</td></tr></tbody></table></div><hr><p><a id="start" href="https://tinybase.org/guides/the-basics/getting-started">Get started</a></p><p><a href="https://tinybase.org/demos">Try the demos</a></p><p><a href="https://tinybase.org/api/store/interfaces/store/store/">Read the docs</a></p><hr><section><h2 id="follow">Follow</h2><ul><li>News and updates on <a href="https://twitter.com/tinybasejs">Twitter</a> and <a href="https://facebook.com/tinybasejs">Facebook</a>.</li><li><a href="https://github.com/tinyplex/tinybase/issues">Issues</a> and <a href="https://github.com/tinyplex/tinybase/releases">release notes</a> on <a href="https://github.com/tinyplex/tinybase">GitHub</a></li><li>Packages on <a href="https://www.npmjs.com/package/tinybase/v/1.2.0">NPM</a>.</li></ul></section><section><h2 id="about">About</h2><p>Building TinyBase was an interesting exercise in API design, minification, and documentation. It's not <a href="https://www.linkedin.com/in/jamespearce">my day job</a>, but I do intend to maintain it, so please provide feedback. I could not have done this without these great <a href="https://tinybase.org/guides/how-tinybase-is-built/credits/#giants">projects</a> and <a href="https://tinybase.org/guides/how-tinybase-is-built/credits/#and-friends">friends</a>, and I hope you enjoy using it!</p></section>
|