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
@@ -1,17 +1,3 @@
1
- const arrayEvery = (array, cb) => array.every(cb);
2
- const arrayIsEqual = (array1, array2) =>
3
- arrayLength(array1) === arrayLength(array2) &&
4
- arrayEvery(array1, (value1, index) => array2[index] === value1);
5
- const arraySort = (array, sorter) => array.sort(sorter);
6
- const arrayForEach = (array, cb) => array.forEach(cb);
7
- const arrayMap = (array, cb) => array.map(cb);
8
- const arraySum = (array) => arrayReduce(array, (i, j) => i + j, 0);
9
- const arrayLength = (array) => array.length;
10
- const arrayIsEmpty = (array) => arrayLength(array) == 0;
11
- const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
12
- const arraySlice = (array, start, end) => array.slice(start, end);
13
- const arrayPush = (array, ...values) => array.push(...values);
14
-
15
1
  const getTypeOf = (thing) => typeof thing;
16
2
  const EMPTY_STRING = '';
17
3
  const STRING = getTypeOf(EMPTY_STRING);
@@ -22,6 +8,25 @@ const SUM = 'sum';
22
8
  const AVG = 'avg';
23
9
  const MIN = 'min';
24
10
  const MAX = 'max';
11
+ const LISTENER = 'Listener';
12
+ const RESULT = 'Result';
13
+ const GET = 'get';
14
+ const ADD = 'add';
15
+ const TABLE = 'Table';
16
+ const ROW_IDS = 'RowIds';
17
+ const SORTED_ROW_IDS = 'SortedRowIds';
18
+ const ROW = 'Row';
19
+ const CELL_IDS = 'CellIds';
20
+ const CELL = 'Cell';
21
+
22
+ const arrayEvery = (array, cb) => array.every(cb);
23
+ const arrayForEach = (array, cb) => array.forEach(cb);
24
+ const arraySum = (array) => arrayReduce(array, (i, j) => i + j, 0);
25
+ const arrayLength = (array) => array.length;
26
+ const arrayIsEmpty = (array) => arrayLength(array) == 0;
27
+ const arrayReduce = (array, cb, initial) => array.reduce(cb, initial);
28
+ const arraySlice = (array, start, end) => array.slice(start, end);
29
+ const arrayPush = (array, ...values) => array.push(...values);
25
30
 
26
31
  const mathMax = Math.max;
27
32
  const mathMin = Math.min;
@@ -307,10 +312,9 @@ const getCreateFunction = (getFunction) => {
307
312
  };
308
313
 
309
314
  const object = Object;
310
- const objIds = object.keys;
311
315
  const objFreeze = object.freeze;
312
-
313
- const defaultSorter = (sortKey1, sortKey2) => (sortKey1 < sortKey2 ? -1 : 1);
316
+ const objForEach = (obj, cb) =>
317
+ arrayForEach(object.entries(obj), ([id, value]) => cb(value, id));
314
318
 
315
319
  const createQueries = getCreateFunction((store) => {
316
320
  const createStore = store.createStore;
@@ -329,25 +333,22 @@ const createQueries = getCreateFunction((store) => {
329
333
  addStoreListeners,
330
334
  delStoreListeners,
331
335
  ] = getDefinableFunctions(store, () => true, getUndefined);
332
- const preStore1 = createStore();
333
- const preStore2 = createStore();
336
+ const preStore = createStore();
334
337
  const resultStore = createStore();
335
338
  const preStoreListenerIds = mapNew();
336
- const addPreStoreListener = (preStore, queryId, ...listenerIds) =>
339
+ const addPreStoreListener = (preStore2, queryId, ...listenerIds) =>
337
340
  arrayForEach(listenerIds, (listenerId) =>
338
341
  setAdd(
339
342
  mapEnsure(
340
343
  mapEnsure(preStoreListenerIds, queryId, mapNew),
341
- preStore,
344
+ preStore2,
342
345
  setNew,
343
346
  ),
344
347
  listenerId,
345
348
  ),
346
349
  );
347
350
  const cleanPreStores = (queryId) =>
348
- arrayForEach([resultStore, preStore2, preStore1], (store2) =>
349
- store2.delTable(queryId),
350
- );
351
+ arrayForEach([resultStore, preStore], (store2) => store2.delTable(queryId));
351
352
  const synchronizeTransactions = (queryId, fromStore, toStore) =>
352
353
  addPreStoreListener(
353
354
  fromStore,
@@ -360,13 +361,11 @@ const createQueries = getCreateFunction((store) => {
360
361
  const setQueryDefinition = (queryId, tableId, build) => {
361
362
  setDefinition(queryId, tableId);
362
363
  cleanPreStores(queryId);
363
- let offsetLimit;
364
364
  const selectEntries = [];
365
365
  const joinEntries = [[null, [tableId, null, null, [], mapNew()]]];
366
366
  const wheres = [];
367
367
  const groupEntries = [];
368
368
  const havings = [];
369
- const orders = [];
370
369
  const select = (arg1, arg2) => {
371
370
  const selectEntry = isFunction(arg1)
372
371
  ? [arrayLength(selectEntries) + EMPTY_STRING, arg1]
@@ -432,17 +431,7 @@ const createQueries = getCreateFunction((store) => {
432
431
  : (getSelectedOrGroupedCell) =>
433
432
  getSelectedOrGroupedCell(arg1) === arg2,
434
433
  );
435
- const order = (arg1, descending) =>
436
- arrayPush(orders, [
437
- isFunction(arg1)
438
- ? arg1
439
- : (getSelectedOrGroupedCell) => getSelectedOrGroupedCell(arg1) ?? 0,
440
- descending,
441
- ]);
442
- const limit = (arg1, arg2) => {
443
- offsetLimit = isUndefined(arg2) ? [0, arg1] : [arg1, arg2];
444
- };
445
- build({select, join, where, group, having, order, limit});
434
+ build({select, join, where, group, having});
446
435
  const selects = mapNew(selectEntries);
447
436
  if (collIsEmpty(selects)) {
448
437
  return queries;
@@ -454,95 +443,11 @@ const createQueries = getCreateFunction((store) => {
454
443
  ),
455
444
  );
456
445
  const groups = mapNew(groupEntries);
457
- let selectJoinWhereStore = preStore1;
458
- let groupHavingStore = preStore2;
459
- if (arrayIsEmpty(orders) && isUndefined(offsetLimit)) {
460
- groupHavingStore = resultStore;
461
- } else {
462
- synchronizeTransactions(queryId, groupHavingStore, resultStore);
463
- const groupRowIdSorter = (rowId1, rowId2) => {
464
- const sortKeys1 = mapGet(sortKeysByGroupRowId, rowId1) ?? [];
465
- const sortKeys2 = mapGet(sortKeysByGroupRowId, rowId2) ?? [];
466
- const orderIndex = orders.findIndex(
467
- (_order, index) => sortKeys1[index] !== sortKeys2[index],
468
- );
469
- return orderIndex < 0
470
- ? 0
471
- : defaultSorter(sortKeys1[orderIndex], sortKeys2[orderIndex]) *
472
- (orders[orderIndex][1] ? -1 : 1);
473
- };
474
- const sortKeysByGroupRowId = mapNew();
475
- const sortedGroupRowIds = mapNew();
476
- addPreStoreListener(
477
- groupHavingStore,
478
- queryId,
479
- arrayIsEmpty(orders)
480
- ? groupHavingStore.addRowIdsListener(queryId, () =>
481
- collClear(sortedGroupRowIds),
482
- )
483
- : groupHavingStore.addRowListener(
484
- queryId,
485
- null,
486
- (_store, _tableId, groupRowId) => {
487
- let newSortKeys = null;
488
- if (groupHavingStore.hasRow(queryId, groupRowId)) {
489
- const oldSortKeys =
490
- mapGet(sortKeysByGroupRowId, groupRowId) ?? [];
491
- const groupRow = groupHavingStore.getRow(queryId, groupRowId);
492
- const getCell = (getSelectedOrGroupedCell) =>
493
- groupRow[getSelectedOrGroupedCell];
494
- newSortKeys = arrayMap(orders, ([getSortKey]) =>
495
- getSortKey(getCell, groupRowId),
496
- );
497
- if (arrayIsEqual(oldSortKeys, newSortKeys)) {
498
- if (mapGet(sortedGroupRowIds, groupRowId)) {
499
- resultStore.setRow(queryId, groupRowId, groupRow);
500
- }
501
- return;
502
- }
503
- }
504
- mapSet(sortKeysByGroupRowId, groupRowId, newSortKeys);
505
- collClear(sortedGroupRowIds);
506
- },
507
- ),
508
- groupHavingStore.addTableListener(queryId, () => {
509
- if (collIsEmpty(sortedGroupRowIds)) {
510
- resultStore.delTable(queryId);
511
- if (groupHavingStore.hasTable(queryId)) {
512
- const groupTable = groupHavingStore.getTable(queryId);
513
- arrayForEach(
514
- arraySort(objIds(groupTable), groupRowIdSorter),
515
- (id) => mapSet(sortedGroupRowIds, id, 0),
516
- );
517
- arrayForEach(
518
- ifNotUndefined(
519
- offsetLimit,
520
- ([offset, limit2]) =>
521
- arraySlice(
522
- mapKeys(sortedGroupRowIds),
523
- offset,
524
- offset + limit2,
525
- ),
526
- () => mapKeys(sortedGroupRowIds),
527
- ),
528
- (groupRowId) => {
529
- resultStore.setRow(
530
- queryId,
531
- groupRowId,
532
- groupTable[groupRowId],
533
- );
534
- mapSet(sortedGroupRowIds, groupRowId, 1);
535
- },
536
- );
537
- }
538
- }
539
- }),
540
- );
541
- }
446
+ let selectJoinWhereStore = preStore;
542
447
  if (collIsEmpty(groups) && arrayIsEmpty(havings)) {
543
- selectJoinWhereStore = groupHavingStore;
448
+ selectJoinWhereStore = resultStore;
544
449
  } else {
545
- synchronizeTransactions(queryId, selectJoinWhereStore, groupHavingStore);
450
+ synchronizeTransactions(queryId, selectJoinWhereStore, resultStore);
546
451
  const groupedSelectedCellIds = mapNew();
547
452
  mapForEach(groups, (groupedCellId, [selectedCellId, aggregators]) =>
548
453
  setAdd(mapEnsure(groupedSelectedCellIds, selectedCellId, setNew), [
@@ -604,8 +509,8 @@ const createQueries = getCreateFunction((store) => {
604
509
  !arrayEvery(havings, (having2) =>
605
510
  having2((cellId) => groupRow[cellId]),
606
511
  )
607
- ? groupHavingStore.delRow
608
- : groupHavingStore.setRow)(queryId, groupRowId, groupRow);
512
+ ? resultStore.delRow
513
+ : resultStore.setRow)(queryId, groupRowId, groupRow);
609
514
  },
610
515
  );
611
516
  addPreStoreListener(
@@ -652,7 +557,7 @@ const createQueries = getCreateFunction((store) => {
652
557
  ([, selectedRowIds, groupRowId]) => {
653
558
  collDel(selectedRowIds, selectedRowId);
654
559
  if (collIsEmpty(selectedRowIds)) {
655
- groupHavingStore.delRow(queryId, groupRowId);
560
+ resultStore.delRow(queryId, groupRowId);
656
561
  return 1;
657
562
  }
658
563
  },
@@ -682,7 +587,7 @@ const createQueries = getCreateFunction((store) => {
682
587
  return [
683
588
  mapNew(),
684
589
  setNew(),
685
- groupHavingStore.addRow(queryId, groupRow, 1),
590
+ resultStore.addRow(queryId, groupRow, 1),
686
591
  groupRow,
687
592
  ];
688
593
  },
@@ -789,68 +694,15 @@ const createQueries = getCreateFunction((store) => {
789
694
  return queries;
790
695
  };
791
696
  const delQueryDefinition = (queryId) => {
792
- mapForEach(mapGet(preStoreListenerIds, queryId), (preStore, listenerIds) =>
697
+ mapForEach(mapGet(preStoreListenerIds, queryId), (preStore2, listenerIds) =>
793
698
  collForEach(listenerIds, (listenerId) =>
794
- preStore.delListener(listenerId),
699
+ preStore2.delListener(listenerId),
795
700
  ),
796
701
  );
797
702
  cleanPreStores(queryId);
798
703
  delDefinition(queryId);
799
704
  return queries;
800
705
  };
801
- const getResultTable = (queryId) => resultStore.getTable(queryId);
802
- const getResultRowIds = (queryId) => resultStore.getRowIds(queryId);
803
- const getResultSortedRowIds = (queryId, cellId, descending) =>
804
- resultStore.getSortedRowIds(queryId, cellId, descending);
805
- const getResultRow = (queryId, rowId) => resultStore.getRow(queryId, rowId);
806
- const getResultCellIds = (queryId, rowId) =>
807
- resultStore.getCellIds(queryId, rowId);
808
- const getResultCell = (queryId, rowId, cellId) =>
809
- resultStore.getCell(queryId, rowId, cellId);
810
- const hasResultTable = (queryId) => resultStore.hasTable(queryId);
811
- const hasResultRow = (queryId, rowId) => resultStore.hasRow(queryId, rowId);
812
- const hasResultCell = (queryId, rowId, cellId) =>
813
- resultStore.hasCell(queryId, rowId, cellId);
814
- const forEachResultTable = (tableCallback) =>
815
- resultStore.forEachTable(tableCallback);
816
- const forEachResultRow = (queryId, rowCallback) =>
817
- resultStore.forEachRow(queryId, rowCallback);
818
- const forEachResultCell = (queryId, rowId, cellCallback) =>
819
- resultStore.forEachCell(queryId, rowId, cellCallback);
820
- const addResultTableListener = (queryId, listener) =>
821
- resultStore.addTableListener(queryId, (_store, ...args) =>
822
- listener(queries, ...args),
823
- );
824
- const addResultRowIdsListener = (queryId, listener, trackReorder) =>
825
- resultStore.addRowIdsListener(
826
- queryId,
827
- (_store, ...args) => listener(queries, ...args),
828
- trackReorder,
829
- );
830
- const addResultSortedRowIdsListener = (
831
- queryId,
832
- cellId,
833
- descending,
834
- listener,
835
- ) =>
836
- resultStore.addSortedRowIdsListener(
837
- queryId,
838
- cellId,
839
- descending,
840
- (_store, ...args) => listener(queries, ...args),
841
- );
842
- const addResultRowListener = (queryId, rowId, listener) =>
843
- resultStore.addRowListener(queryId, rowId, (_store, ...args) =>
844
- listener(queries, ...args),
845
- );
846
- const addResultCellIdsListener = (queryId, rowId, listener) =>
847
- resultStore.addCellIdsListener(queryId, rowId, (_store, ...args) =>
848
- listener(queries, ...args),
849
- );
850
- const addResultCellListener = (queryId, rowId, cellId, listener) =>
851
- resultStore.addCellListener(queryId, rowId, cellId, (_store, ...args) =>
852
- listener(queries, ...args),
853
- );
854
706
  const delListener = (listenerId) => {
855
707
  resultStore.delListener(listenerId);
856
708
  return queries;
@@ -872,28 +724,34 @@ const createQueries = getCreateFunction((store) => {
872
724
  forEachQuery,
873
725
  hasQuery,
874
726
  getTableId,
875
- getResultTable,
876
- getResultRowIds,
877
- getResultSortedRowIds,
878
- getResultRow,
879
- getResultCellIds,
880
- getResultCell,
881
- hasResultTable,
882
- hasResultRow,
883
- hasResultCell,
884
- forEachResultTable,
885
- forEachResultRow,
886
- forEachResultCell,
887
- addResultTableListener,
888
- addResultRowIdsListener,
889
- addResultSortedRowIdsListener,
890
- addResultRowListener,
891
- addResultCellIdsListener,
892
- addResultCellListener,
893
727
  delListener,
894
728
  destroy,
895
729
  getListenerStats,
896
730
  };
731
+ objForEach(
732
+ {
733
+ [TABLE]: [1, 1],
734
+ [ROW_IDS]: [0, 1],
735
+ [SORTED_ROW_IDS]: [0, 5],
736
+ [ROW]: [1, 2],
737
+ [CELL_IDS]: [0, 2],
738
+ [CELL]: [1, 3],
739
+ },
740
+ ([hasAndForEach, argumentCount], gettable) => {
741
+ arrayForEach(
742
+ hasAndForEach ? [GET, 'has', 'forEach'] : [GET],
743
+ (prefix) =>
744
+ (queries[prefix + RESULT + gettable] = (...args) =>
745
+ resultStore[prefix + gettable](...args)),
746
+ );
747
+ queries[ADD + RESULT + gettable + LISTENER] = (...args) =>
748
+ resultStore[ADD + gettable + LISTENER](
749
+ ...arraySlice(args, 0, argumentCount),
750
+ (_store, ...listenerArgs) =>
751
+ args[argumentCount](queries, ...listenerArgs),
752
+ );
753
+ },
754
+ );
897
755
  return objFreeze(queries);
898
756
  });
899
757
 
@@ -2,7 +2,6 @@ const getTypeOf = (thing) => typeof thing;
2
2
  const EMPTY_STRING = '';
3
3
  const STRING = getTypeOf(EMPTY_STRING);
4
4
 
5
- const arrayEvery = (array, cb) => array.every(cb);
6
5
  const arrayForEach = (array, cb) => array.forEach(cb);
7
6
  const arrayLength = (array) => array.length;
8
7
  const arrayIsEmpty = (array) => arrayLength(array) == 0;
@@ -257,8 +256,6 @@ const getListenerFunctions = (getThing) => {
257
256
  }
258
257
  return idOrNulls;
259
258
  });
260
- const hasListeners = (idSetNode, ids) =>
261
- !arrayEvery(getWildcardedLeaves(idSetNode, ids), isUndefined);
262
259
  const callListener = (id, idNullGetters, extraArgsGetter) =>
263
260
  ifNotUndefined(mapGet(allListeners, id), ([listener, , idOrNulls = []]) => {
264
261
  const callWithIds = (...ids) => {
@@ -273,7 +270,7 @@ const getListenerFunctions = (getThing) => {
273
270
  };
274
271
  callWithIds();
275
272
  });
276
- return [addListener, callListeners, delListener, hasListeners, callListener];
273
+ return [addListener, callListeners, delListener, callListener];
277
274
  };
278
275
 
279
276
  const object = Object;