tinybase 2.0.0-beta.2 → 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/lib/checkpoints.js +1 -1
  2. package/lib/checkpoints.js.gz +0 -0
  3. package/lib/debug/checkpoints.js +1 -4
  4. package/lib/debug/indexes.js +1 -3
  5. package/lib/debug/metrics.js +1 -4
  6. package/lib/debug/queries.d.ts +61 -296
  7. package/lib/debug/queries.js +59 -201
  8. package/lib/debug/relationships.js +1 -4
  9. package/lib/debug/store.d.ts +112 -90
  10. package/lib/debug/store.js +90 -149
  11. package/lib/debug/tinybase.js +130 -333
  12. package/lib/debug/ui-react.d.ts +81 -81
  13. package/lib/debug/ui-react.js +138 -173
  14. package/lib/indexes.js +1 -1
  15. package/lib/indexes.js.gz +0 -0
  16. package/lib/metrics.js +1 -1
  17. package/lib/metrics.js.gz +0 -0
  18. package/lib/queries.d.ts +61 -296
  19. package/lib/queries.js +1 -1
  20. package/lib/queries.js.gz +0 -0
  21. package/lib/relationships.js +1 -1
  22. package/lib/relationships.js.gz +0 -0
  23. package/lib/store.d.ts +112 -90
  24. package/lib/store.js +1 -1
  25. package/lib/store.js.gz +0 -0
  26. package/lib/tinybase.js +1 -1
  27. package/lib/tinybase.js.gz +0 -0
  28. package/lib/ui-react.d.ts +81 -81
  29. package/lib/ui-react.js +1 -1
  30. package/lib/ui-react.js.gz +0 -0
  31. package/lib/umd/checkpoints.js +1 -1
  32. package/lib/umd/checkpoints.js.gz +0 -0
  33. package/lib/umd/indexes.js +1 -1
  34. package/lib/umd/indexes.js.gz +0 -0
  35. package/lib/umd/metrics.js +1 -1
  36. package/lib/umd/metrics.js.gz +0 -0
  37. package/lib/umd/queries.js +1 -1
  38. package/lib/umd/queries.js.gz +0 -0
  39. package/lib/umd/relationships.js +1 -1
  40. package/lib/umd/relationships.js.gz +0 -0
  41. package/lib/umd/store.js +1 -1
  42. package/lib/umd/store.js.gz +0 -0
  43. package/lib/umd/tinybase.js +1 -1
  44. package/lib/umd/tinybase.js.gz +0 -0
  45. package/lib/umd/ui-react.js +1 -1
  46. package/lib/umd/ui-react.js.gz +0 -0
  47. package/package.json +9 -9
  48. package/readme.md +2 -2
@@ -7,6 +7,15 @@ const NUMBER = getTypeOf(0);
7
7
  const FUNCTION = getTypeOf(getTypeOf);
8
8
  const TYPE = 'type';
9
9
  const DEFAULT = 'default';
10
+ const LISTENER = 'Listener';
11
+ const ADD = 'add';
12
+ const TABLES = 'Tables';
13
+ const TABLE_IDS = 'TableIds';
14
+ const TABLE = 'Table';
15
+ const ROW_IDS = 'RowIds';
16
+ const ROW = 'Row';
17
+ const CELL_IDS = 'CellIds';
18
+ const CELL = 'Cell';
10
19
 
11
20
  const arrayHas = (array, value) => array.includes(value);
12
21
  const arrayEvery = (array, cb) => array.every(cb);
@@ -20,6 +29,7 @@ const arrayLength = (array) => array.length;
20
29
  const arrayIsEmpty = (array) => arrayLength(array) == 0;
21
30
  const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
22
31
  const arrayFilter = (array, cb) => array.filter(cb);
32
+ const arraySlice = (array, start, end) => array.slice(start, end);
23
33
  const arrayPush = (array, ...values) => array.push(...values);
24
34
  const arrayPop = (array) => array.pop();
25
35
 
@@ -167,8 +177,6 @@ const getListenerFunctions = (getThing) => {
167
177
  }
168
178
  return idOrNulls;
169
179
  });
170
- const hasListeners = (idSetNode, ids) =>
171
- !arrayEvery(getWildcardedLeaves(idSetNode, ids), isUndefined);
172
180
  const callListener = (id, idNullGetters, extraArgsGetter) =>
173
181
  ifNotUndefined(mapGet(allListeners, id), ([listener, , idOrNulls = []]) => {
174
182
  const callWithIds = (...ids) => {
@@ -183,16 +191,12 @@ const getListenerFunctions = (getThing) => {
183
191
  };
184
192
  callWithIds();
185
193
  });
186
- return [addListener, callListeners, delListener, hasListeners, callListener];
194
+ return [addListener, callListeners, delListener, callListener];
187
195
  };
188
196
 
189
197
  const pairNew = (value) => [value, value];
190
198
  const pairCollSize2 = (pair, func = collSize2) => func(pair[0]) + func(pair[1]);
191
- const pairCollIsEmpty = (pair) => pairCollSize2(pair) == 0;
192
199
  const pairNewMap = () => [mapNew(), mapNew()];
193
- const pair2CollSize2 = (pair2, func = collSize2) =>
194
- pairCollSize2(pair2[0], func) + pairCollSize2(pair2[1], func);
195
- const pair2NewMap = () => [pairNewMap(), pairNewMap()];
196
200
 
197
201
  const getCellType = (cell) => {
198
202
  const type = getTypeOf(cell);
@@ -247,22 +251,17 @@ const createStore = () => {
247
251
  const schemaRowCache = mapNew();
248
252
  const tablesMap = mapNew();
249
253
  const tablesListeners = pairNewMap();
250
- const tableIdsListeners = pair2NewMap();
254
+ const tableIdsListeners = pairNewMap();
251
255
  const tableListeners = pairNewMap();
252
- const rowIdsListeners = pair2NewMap();
256
+ const rowIdsListeners = pairNewMap();
253
257
  const sortedRowIdsListeners = pairNewMap();
254
258
  const rowListeners = pairNewMap();
255
- const cellIdsListeners = pair2NewMap();
259
+ const cellIdsListeners = pairNewMap();
256
260
  const cellListeners = pairNewMap();
257
261
  const invalidCellListeners = pairNewMap();
258
262
  const finishTransactionListeners = pairNewMap();
259
- const [
260
- addListener,
261
- callListeners,
262
- delListenerImpl,
263
- hasListeners,
264
- callListenerImpl,
265
- ] = getListenerFunctions(() => store);
263
+ const [addListener, callListeners, delListenerImpl, callListenerImpl] =
264
+ getListenerFunctions(() => store);
266
265
  const validateSchema = (schema) =>
267
266
  validate(schema, (tableSchema) =>
268
267
  validate(tableSchema, (cellSchema) => {
@@ -444,49 +443,12 @@ const createStore = () => {
444
443
  }
445
444
  };
446
445
  const tableIdsChanged = (tableId, added) =>
447
- idsChanged(
448
- collIsEmpty(changedTableIds)
449
- ? mapSet(
450
- changedTableIds,
451
- null,
452
- hasListeners(tableIdsListeners[0][1]) ||
453
- hasListeners(tableIdsListeners[1][1])
454
- ? getTableIds()
455
- : 0,
456
- )
457
- : changedTableIds,
458
- tableId,
459
- added,
460
- );
446
+ idsChanged(changedTableIds, tableId, added);
461
447
  const rowIdsChanged = (tableId, rowId, added) =>
462
- idsChanged(
463
- mapEnsure(changedRowIds, tableId, () =>
464
- mapNew([
465
- [
466
- null,
467
- hasListeners(rowIdsListeners[0][1], [tableId]) ||
468
- hasListeners(rowIdsListeners[1][1], [tableId])
469
- ? getRowIds(tableId)
470
- : 0,
471
- ],
472
- ]),
473
- ),
474
- rowId,
475
- added,
476
- );
448
+ idsChanged(mapEnsure(changedRowIds, tableId, mapNew), rowId, added);
477
449
  const cellIdsChanged = (tableId, rowId, cellId, added) =>
478
450
  idsChanged(
479
- mapEnsure(mapEnsure(changedCellIds, tableId, mapNew), rowId, () =>
480
- mapNew([
481
- [
482
- null,
483
- hasListeners(cellIdsListeners[0][1], [tableId, rowId]) ||
484
- hasListeners(cellIdsListeners[1][1], [tableId, rowId])
485
- ? getCellIds(tableId, rowId)
486
- : 0,
487
- ],
488
- ]),
489
- ),
451
+ mapEnsure(mapEnsure(changedCellIds, tableId, mapNew), rowId, mapNew),
490
452
  cellId,
491
453
  added,
492
454
  );
@@ -529,18 +491,9 @@ const createStore = () => {
529
491
  ),
530
492
  )
531
493
  : 0;
532
- const callIdsListenersIfChanged = (listeners, changedIds, getIds, ids) => {
533
- if (collSize(changedIds) > 1) {
534
- callListeners(listeners[0], ids);
535
- callListeners(listeners[1], ids);
536
- return 1;
537
- }
538
- if (
539
- !collIsEmpty(changedIds) &&
540
- mapGet(changedIds, null) != 0 &&
541
- !arrayIsEqual(mapGet(changedIds, null), getIds(...(ids ?? [])))
542
- ) {
543
- callListeners(listeners[1], ids);
494
+ const callIdsListenersIfChanged = (listeners, changedIds, ids) => {
495
+ if (!collIsEmpty(changedIds)) {
496
+ callListeners(listeners, ids);
544
497
  return 1;
545
498
  }
546
499
  };
@@ -549,10 +502,10 @@ const createStore = () => {
549
502
  sortedRowIdsListeners[mutator],
550
503
  );
551
504
  const emptyIdListeners =
552
- pairCollIsEmpty(cellIdsListeners[mutator]) &&
553
- pairCollIsEmpty(rowIdsListeners[mutator]) &&
505
+ collIsEmpty(cellIdsListeners[mutator]) &&
506
+ collIsEmpty(rowIdsListeners[mutator]) &&
554
507
  emptySortedRowIdListeners &&
555
- pairCollIsEmpty(tableIdsListeners[mutator]);
508
+ collIsEmpty(tableIdsListeners[mutator]);
556
509
  const emptyOtherListeners =
557
510
  collIsEmpty(cellListeners[mutator]) &&
558
511
  collIsEmpty(rowListeners[mutator]) &&
@@ -570,23 +523,18 @@ const createStore = () => {
570
523
  if (!emptyIdListeners) {
571
524
  collForEach(changes[2], (rowCellIds, tableId) =>
572
525
  collForEach(rowCellIds, (changedIds, rowId) =>
573
- callIdsListenersIfChanged(
574
- cellIdsListeners[mutator],
575
- changedIds,
576
- getCellIds,
577
- [tableId, rowId],
578
- ),
526
+ callIdsListenersIfChanged(cellIdsListeners[mutator], changedIds, [
527
+ tableId,
528
+ rowId,
529
+ ]),
579
530
  ),
580
531
  );
581
532
  const calledSortableTableIds = setNew();
582
533
  collForEach(changes[1], (changedIds, tableId) => {
583
534
  if (
584
- callIdsListenersIfChanged(
585
- rowIdsListeners[mutator],
586
- changedIds,
587
- getRowIds,
588
- [tableId],
589
- ) &&
535
+ callIdsListenersIfChanged(rowIdsListeners[mutator], changedIds, [
536
+ tableId,
537
+ ]) &&
590
538
  !emptySortedRowIdListeners
591
539
  ) {
592
540
  callListeners(sortedRowIdsListeners[mutator], [tableId, null]);
@@ -613,11 +561,7 @@ const createStore = () => {
613
561
  }
614
562
  });
615
563
  }
616
- callIdsListenersIfChanged(
617
- tableIdsListeners[mutator],
618
- changes[0],
619
- getTableIds,
620
- );
564
+ callIdsListenersIfChanged(tableIdsListeners[mutator], changes[0]);
621
565
  }
622
566
  if (!emptyOtherListeners) {
623
567
  let tablesChanged;
@@ -665,7 +609,7 @@ const createStore = () => {
665
609
  const getTable = (tableId) =>
666
610
  mapToObj(mapGet(tablesMap, id(tableId)), mapToObj);
667
611
  const getRowIds = (tableId) => mapKeys(mapGet(tablesMap, id(tableId)));
668
- const getSortedRowIds = (tableId, cellId, descending) => {
612
+ const getSortedRowIds = (tableId, cellId, descending, offset = 0, limit) => {
669
613
  const cells = [];
670
614
  mapForEach(mapGet(tablesMap, id(tableId)), (rowId, row) =>
671
615
  arrayPush(cells, [
@@ -674,10 +618,14 @@ const createStore = () => {
674
618
  ]),
675
619
  );
676
620
  return arrayMap(
677
- arraySort(
678
- cells,
679
- ([cell1], [cell2]) =>
680
- defaultSorter(cell1, cell2) * (descending ? -1 : 1),
621
+ arraySlice(
622
+ arraySort(
623
+ cells,
624
+ ([cell1], [cell2]) =>
625
+ defaultSorter(cell1, cell2) * (descending ? -1 : 1),
626
+ ),
627
+ offset,
628
+ isUndefined(limit) ? limit : offset + limit,
681
629
  ),
682
630
  ([, rowId]) => rowId,
683
631
  );
@@ -922,67 +870,48 @@ const createStore = () => {
922
870
  );
923
871
  const forEachCell = (tableId, rowId, cellCallback) =>
924
872
  mapForEach(mapGet(mapGet(tablesMap, id(tableId)), id(rowId)), cellCallback);
925
- const addTablesListener = (listener, mutator) =>
926
- addListener(listener, tablesListeners[mutator ? 1 : 0]);
927
- const addTableIdsListener = (listener, trackReorder, mutator) =>
928
- addListener(
929
- listener,
930
- tableIdsListeners[mutator ? 1 : 0][trackReorder ? 1 : 0],
931
- );
932
- const addTableListener = (tableId, listener, mutator) =>
933
- addListener(listener, tableListeners[mutator ? 1 : 0], [tableId]);
934
- const addRowIdsListener = (tableId, listener, trackReorder, mutator) =>
935
- addListener(
936
- listener,
937
- rowIdsListeners[mutator ? 1 : 0][trackReorder ? 1 : 0],
938
- [tableId],
939
- );
940
873
  const addSortedRowIdsListener = (
941
874
  tableId,
942
875
  cellId,
943
876
  descending,
877
+ offset,
878
+ limit,
944
879
  listener,
945
880
  mutator,
946
881
  ) => {
947
- let sortedRowIds = getSortedRowIds(tableId, cellId, descending);
882
+ let sortedRowIds = getSortedRowIds(
883
+ tableId,
884
+ cellId,
885
+ descending,
886
+ offset,
887
+ limit,
888
+ );
948
889
  return addListener(
949
890
  () => {
950
- const newSortedRowIds = getSortedRowIds(tableId, cellId, descending);
891
+ const newSortedRowIds = getSortedRowIds(
892
+ tableId,
893
+ cellId,
894
+ descending,
895
+ offset,
896
+ limit,
897
+ );
951
898
  if (!arrayIsEqual(newSortedRowIds, sortedRowIds)) {
952
899
  sortedRowIds = newSortedRowIds;
953
- listener(store, tableId, cellId, descending, sortedRowIds);
900
+ listener(
901
+ store,
902
+ tableId,
903
+ cellId,
904
+ descending,
905
+ offset,
906
+ limit,
907
+ sortedRowIds,
908
+ );
954
909
  }
955
910
  },
956
911
  sortedRowIdsListeners[mutator ? 1 : 0],
957
912
  [tableId, cellId],
958
913
  );
959
914
  };
960
- const addRowListener = (tableId, rowId, listener, mutator) =>
961
- addListener(listener, rowListeners[mutator ? 1 : 0], [tableId, rowId]);
962
- const addCellIdsListener = (
963
- tableId,
964
- rowId,
965
- listener,
966
- trackReorder,
967
- mutator,
968
- ) =>
969
- addListener(
970
- listener,
971
- cellIdsListeners[mutator ? 1 : 0][trackReorder ? 1 : 0],
972
- [tableId, rowId],
973
- );
974
- const addCellListener = (tableId, rowId, cellId, listener, mutator) =>
975
- addListener(listener, cellListeners[mutator ? 1 : 0], [
976
- tableId,
977
- rowId,
978
- cellId,
979
- ]);
980
- const addInvalidCellListener = (tableId, rowId, cellId, listener, mutator) =>
981
- addListener(listener, invalidCellListeners[mutator ? 1 : 0], [
982
- tableId,
983
- rowId,
984
- cellId,
985
- ]);
986
915
  const addWillFinishTransactionListener = (listener) =>
987
916
  addListener(listener, finishTransactionListeners[0]);
988
917
  const addDidFinishTransactionListener = (listener) =>
@@ -999,12 +928,12 @@ const createStore = () => {
999
928
  };
1000
929
  const getListenerStats = () => ({
1001
930
  tables: pairCollSize2(tablesListeners),
1002
- tableIds: pair2CollSize2(tableIdsListeners),
931
+ tableIds: pairCollSize2(tableIdsListeners),
1003
932
  table: pairCollSize2(tableListeners),
1004
- rowIds: pair2CollSize2(rowIdsListeners),
933
+ rowIds: pairCollSize2(rowIdsListeners),
1005
934
  sortedRowIds: pairCollSize2(sortedRowIdsListeners),
1006
935
  row: pairCollSize2(rowListeners, collSize3),
1007
- cellIds: pair2CollSize2(cellIdsListeners, collSize3),
936
+ cellIds: pairCollSize2(cellIdsListeners, collSize3),
1008
937
  cell: pairCollSize2(cellListeners, collSize4),
1009
938
  invalidCell: pairCollSize2(invalidCellListeners, collSize4),
1010
939
  transaction: pairCollSize2(finishTransactionListeners),
@@ -1043,15 +972,7 @@ const createStore = () => {
1043
972
  forEachTable,
1044
973
  forEachRow,
1045
974
  forEachCell,
1046
- addTablesListener,
1047
- addTableIdsListener,
1048
- addTableListener,
1049
- addRowIdsListener,
1050
975
  addSortedRowIdsListener,
1051
- addRowListener,
1052
- addCellIdsListener,
1053
- addCellListener,
1054
- addInvalidCellListener,
1055
976
  addWillFinishTransactionListener,
1056
977
  addDidFinishTransactionListener,
1057
978
  callListener,
@@ -1059,6 +980,26 @@ const createStore = () => {
1059
980
  getListenerStats,
1060
981
  createStore,
1061
982
  };
983
+ objForEach(
984
+ {
985
+ [TABLES]: [0, tablesListeners],
986
+ [TABLE_IDS]: [0, tableIdsListeners],
987
+ [TABLE]: [1, tableListeners],
988
+ [ROW_IDS]: [1, rowIdsListeners],
989
+ [ROW]: [2, rowListeners],
990
+ [CELL_IDS]: [2, cellIdsListeners],
991
+ [CELL]: [3, cellListeners],
992
+ InvalidCell: [3, invalidCellListeners],
993
+ },
994
+ ([argumentCount, idSetNode], listenable) => {
995
+ store[ADD + listenable + LISTENER] = (...args) =>
996
+ addListener(
997
+ args[argumentCount],
998
+ idSetNode[args[argumentCount + 1] ? 1 : 0],
999
+ argumentCount > 0 ? arraySlice(args, 0, argumentCount) : void 0,
1000
+ );
1001
+ },
1002
+ );
1062
1003
  return objFreeze(store);
1063
1004
  };
1064
1005