tinybase 8.0.0-beta.3 → 8.0.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.
Files changed (49) hide show
  1. package/@types/middleware/index.d.ts +44 -124
  2. package/@types/middleware/with-schemas/index.d.ts +44 -147
  3. package/@types/ui-react-dom/index.d.ts +6 -4
  4. package/@types/ui-react-dom/with-schemas/index.d.ts +6 -4
  5. package/index.js +100 -69
  6. package/mergeable-store/index.js +99 -48
  7. package/mergeable-store/with-schemas/index.js +99 -48
  8. package/middleware/index.js +1 -39
  9. package/middleware/with-schemas/index.js +1 -39
  10. package/min/index.js +1 -1
  11. package/min/index.js.gz +0 -0
  12. package/min/mergeable-store/index.js +1 -1
  13. package/min/mergeable-store/index.js.gz +0 -0
  14. package/min/mergeable-store/with-schemas/index.js +1 -1
  15. package/min/mergeable-store/with-schemas/index.js.gz +0 -0
  16. package/min/middleware/index.js +1 -1
  17. package/min/middleware/index.js.gz +0 -0
  18. package/min/middleware/with-schemas/index.js +1 -1
  19. package/min/middleware/with-schemas/index.js.gz +0 -0
  20. package/min/omni/index.js +1 -1
  21. package/min/omni/index.js.gz +0 -0
  22. package/min/omni/with-schemas/index.js +1 -1
  23. package/min/omni/with-schemas/index.js.gz +0 -0
  24. package/min/store/index.js +1 -1
  25. package/min/store/index.js.gz +0 -0
  26. package/min/store/with-schemas/index.js +1 -1
  27. package/min/store/with-schemas/index.js.gz +0 -0
  28. package/min/ui-react-dom/index.js +1 -1
  29. package/min/ui-react-dom/index.js.gz +0 -0
  30. package/min/ui-react-dom/with-schemas/index.js +1 -1
  31. package/min/ui-react-dom/with-schemas/index.js.gz +0 -0
  32. package/min/ui-react-inspector/index.js +1 -1
  33. package/min/ui-react-inspector/index.js.gz +0 -0
  34. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  35. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  36. package/min/with-schemas/index.js +1 -1
  37. package/min/with-schemas/index.js.gz +0 -0
  38. package/omni/index.js +156 -102
  39. package/omni/with-schemas/index.js +156 -102
  40. package/package.json +6 -6
  41. package/readme.md +3 -3
  42. package/releases.md +39 -4
  43. package/store/index.js +99 -48
  44. package/store/with-schemas/index.js +99 -48
  45. package/ui-react-dom/index.js +56 -34
  46. package/ui-react-dom/with-schemas/index.js +56 -34
  47. package/ui-react-inspector/index.js +155 -82
  48. package/ui-react-inspector/with-schemas/index.js +155 -82
  49. package/with-schemas/index.js +100 -69
package/omni/index.js CHANGED
@@ -121,6 +121,14 @@ const structuredClone = GLOBAL.structuredClone;
121
121
  const errorNew = (message) => {
122
122
  throw new Error(message);
123
123
  };
124
+ const tryReturn = (tryF, catchReturn) => {
125
+ try {
126
+ return tryF();
127
+ } catch {
128
+ /* istanbul ignore next */
129
+ return catchReturn;
130
+ }
131
+ };
124
132
  const tryCatch = async (action, then1, then2) => {
125
133
  try {
126
134
  return await action();
@@ -1578,10 +1586,24 @@ const createStore = () => {
1578
1586
  );
1579
1587
  const setOrDelTables = (tables) =>
1580
1588
  objIsEmpty(tables) ? delTables() : setTables(tables);
1581
- const setOrDelCell = (tableId, rowId, cellId, cell, skipMiddleware) =>
1589
+ const setOrDelCell = (
1590
+ tableId,
1591
+ rowId,
1592
+ cellId,
1593
+ cell,
1594
+ skipMiddleware,
1595
+ skipRowMiddleware,
1596
+ ) =>
1582
1597
  isUndefined(cell)
1583
1598
  ? delCell(tableId, rowId, cellId, true, skipMiddleware)
1584
- : setCell(tableId, rowId, cellId, cell, skipMiddleware);
1599
+ : setCell(
1600
+ tableId,
1601
+ rowId,
1602
+ cellId,
1603
+ cell,
1604
+ skipMiddleware,
1605
+ skipRowMiddleware,
1606
+ );
1585
1607
  const setOrDelValues = (values) =>
1586
1608
  objIsEmpty(values) ? delValues() : setValues(values);
1587
1609
  const setOrDelValue = (valueId, value, skipMiddleware) =>
@@ -1681,6 +1703,30 @@ const createStore = () => {
1681
1703
  ),
1682
1704
  objIsEqual,
1683
1705
  );
1706
+ const applyRowDirectly = (tableId, tableMap, rowId, row, skipMiddleware) => {
1707
+ mapMatch(
1708
+ mapEnsure(tableMap, rowId, () => {
1709
+ rowIdsChanged(tableId, rowId, 1);
1710
+ return mapNew();
1711
+ }),
1712
+ row,
1713
+ (rowMap, cellId, cell) =>
1714
+ ifNotUndefined(
1715
+ getValidatedCell(tableId, rowId, cellId, cell),
1716
+ (validCell) =>
1717
+ setValidCell(
1718
+ tableId,
1719
+ rowId,
1720
+ rowMap,
1721
+ cellId,
1722
+ validCell,
1723
+ skipMiddleware,
1724
+ ),
1725
+ ),
1726
+ (rowMap, cellId) =>
1727
+ delValidCell(tableId, tableMap, rowId, rowMap, cellId, true),
1728
+ );
1729
+ };
1684
1730
  const setValidCell = (tableId, rowId, rowMap, cellId, cell, skipMiddleware) =>
1685
1731
  ifTransformed(
1686
1732
  cell,
@@ -2357,7 +2403,14 @@ const createStore = () => {
2357
2403
  tableId,
2358
2404
  rowId,
2359
2405
  );
2360
- const setCell = (tableId, rowId, cellId, cell, skipMiddleware) =>
2406
+ const setCell = (
2407
+ tableId,
2408
+ rowId,
2409
+ cellId,
2410
+ cell,
2411
+ skipMiddleware,
2412
+ skipRowMiddleware,
2413
+ ) =>
2361
2414
  fluentTransaction(
2362
2415
  (tableId2, rowId2, cellId2) =>
2363
2416
  ifNotUndefined(
@@ -2367,15 +2420,47 @@ const createStore = () => {
2367
2420
  cellId2,
2368
2421
  isFunction(cell) ? cell(getCell(tableId2, rowId2, cellId2)) : cell,
2369
2422
  ),
2370
- (validCell) =>
2371
- setCellIntoNewRow(
2372
- tableId2,
2373
- getOrCreateTable(tableId2),
2374
- rowId2,
2375
- cellId2,
2376
- validCell,
2377
- skipMiddleware,
2378
- ),
2423
+ (validCell) => {
2424
+ const tableMap = getOrCreateTable(tableId2);
2425
+ ifNotUndefined(
2426
+ skipMiddleware || skipRowMiddleware || !middleware[14]?.()
2427
+ ? void 0
2428
+ : middleware[3],
2429
+ (willSetRow) => {
2430
+ const existingRowMap = mapGet(tableMap, rowId2);
2431
+ const prospectiveRow = {
2432
+ ...(existingRowMap ? mapToObj(existingRowMap) : {}),
2433
+ [cellId2]: validCell,
2434
+ };
2435
+ ifNotUndefined(
2436
+ whileMutating(() =>
2437
+ willSetRow(
2438
+ tableId2,
2439
+ rowId2,
2440
+ structuredClone(prospectiveRow),
2441
+ ),
2442
+ ),
2443
+ (row) =>
2444
+ applyRowDirectly(
2445
+ tableId2,
2446
+ tableMap,
2447
+ rowId2,
2448
+ row,
2449
+ skipMiddleware,
2450
+ ),
2451
+ );
2452
+ },
2453
+ () =>
2454
+ setCellIntoNewRow(
2455
+ tableId2,
2456
+ tableMap,
2457
+ rowId2,
2458
+ cellId2,
2459
+ validCell,
2460
+ skipMiddleware,
2461
+ ),
2462
+ );
2463
+ },
2379
2464
  ),
2380
2465
  tableId,
2381
2466
  rowId,
@@ -2424,7 +2509,14 @@ const createStore = () => {
2424
2509
  isUndefined(row)
2425
2510
  ? delRow(tableId, rowId)
2426
2511
  : objMap(row, (cell, cellId) =>
2427
- setOrDelCell(tableId, rowId, cellId, cell),
2512
+ setOrDelCell(
2513
+ tableId,
2514
+ rowId,
2515
+ cellId,
2516
+ cell,
2517
+ void 0,
2518
+ true,
2519
+ ),
2428
2520
  ),
2429
2521
  ),
2430
2522
  );
@@ -2587,37 +2679,6 @@ const createStore = () => {
2587
2679
  mapToObj3(changedCellIds),
2588
2680
  mapToObj(changedValueIds),
2589
2681
  ];
2590
- const doDidSetRows = () => {
2591
- if (middleware[14]) {
2592
- const changedCells2 = clonedChangedCells(changedCells);
2593
- collForEach(changedCells2, (rows, tableId) =>
2594
- collForEach(rows, (cells, rowId) => {
2595
- if (
2596
- !arrayEvery(
2597
- collValues(cells),
2598
- ([oldCell, newCell]) => oldCell === newCell,
2599
- )
2600
- ) {
2601
- const newRow = getRow(tableId, rowId);
2602
- const oldRow = objMerge(newRow);
2603
- collForEach(cells, ([oldCell], cellId) =>
2604
- isUndefined(oldCell)
2605
- ? objDel(oldRow, cellId)
2606
- : (oldRow[cellId] = oldCell),
2607
- );
2608
- const didSetRow = middleware[14](tableId, rowId, oldRow, newRow);
2609
- if (!objIsEqual(didSetRow, newRow)) {
2610
- const setOrDelRow = objMap(newRow, () => void 0);
2611
- objMap(didSetRow, (cell, cellId) => (setOrDelRow[cellId] = cell));
2612
- objMap(setOrDelRow, (cell, cellId) =>
2613
- setOrDelCell(tableId, rowId, cellId, cell, true),
2614
- );
2615
- }
2616
- }
2617
- }),
2618
- );
2619
- }
2620
- };
2621
2682
  const finishTransaction = (doRollback) => {
2622
2683
  if (transactions > 0) {
2623
2684
  transactions--;
@@ -2627,7 +2688,6 @@ const createStore = () => {
2627
2688
  callInvalidCellListeners(1);
2628
2689
  if (!collIsEmpty(changedCells)) {
2629
2690
  callTabularListenersForChanges(1);
2630
- doDidSetRows();
2631
2691
  }
2632
2692
  callInvalidValueListeners(1);
2633
2693
  if (!collIsEmpty(changedValues)) {
@@ -2798,7 +2858,7 @@ const createStore = () => {
2798
2858
  willDelValues,
2799
2859
  willDelValue,
2800
2860
  willApplyChanges,
2801
- didSetRow,
2861
+ hasWillSetRowCallbacks,
2802
2862
  ) =>
2803
2863
  (middleware = [
2804
2864
  willSetContent,
@@ -2815,7 +2875,7 @@ const createStore = () => {
2815
2875
  willDelValues,
2816
2876
  willDelValue,
2817
2877
  willApplyChanges,
2818
- didSetRow,
2878
+ hasWillSetRowCallbacks,
2819
2879
  ]);
2820
2880
  const setInternalListeners = (
2821
2881
  preStartTransaction,
@@ -3721,7 +3781,6 @@ const createMiddleware = getCreateFunction((store) => {
3721
3781
  const willDelValuesCallbacks = [];
3722
3782
  const willDelValueCallbacks = [];
3723
3783
  const willApplyChangesCallbacks = [];
3724
- const didSetRowCallbacksMap = mapNew();
3725
3784
  const willSetContent = (content) =>
3726
3785
  reduceCallbacks(willSetContentCallbacks, content);
3727
3786
  const willSetTables = (tables) =>
@@ -3748,17 +3807,6 @@ const createMiddleware = getCreateFunction((store) => {
3748
3807
  everyCallback(willDelValueCallbacks, valueId);
3749
3808
  const willApplyChanges = (changes) =>
3750
3809
  reduceCallbacks(willApplyChangesCallbacks, changes);
3751
- const didSetRow = (tableId, rowId, oldRow, newRow) =>
3752
- ifNotUndefined(
3753
- mapGet(didSetRowCallbacksMap, tableId),
3754
- (callbacks) =>
3755
- arrayReduce(
3756
- callbacks,
3757
- (current, callback) => callback(tableId, rowId, oldRow, current),
3758
- newRow,
3759
- ),
3760
- () => newRow,
3761
- );
3762
3810
  const getStore = () => store;
3763
3811
  const addWillSetContentCallback = addCallback(willSetContentCallbacks);
3764
3812
  const addWillSetTablesCallback = addCallback(willSetTablesCallbacks);
@@ -3774,13 +3822,6 @@ const createMiddleware = getCreateFunction((store) => {
3774
3822
  const addWillDelValuesCallback = addCallback(willDelValuesCallbacks);
3775
3823
  const addWillDelValueCallback = addCallback(willDelValueCallbacks);
3776
3824
  const addWillApplyChangesCallback = addCallback(willApplyChangesCallbacks);
3777
- const addDidSetRowCallback = (tableId, callback) =>
3778
- fluent(() =>
3779
- arrayPush(
3780
- mapEnsure(didSetRowCallbacksMap, tableId, () => []),
3781
- callback,
3782
- ),
3783
- );
3784
3825
  const destroy = () => {};
3785
3826
  const middleware = objFreeze({
3786
3827
  getStore,
@@ -3798,7 +3839,6 @@ const createMiddleware = getCreateFunction((store) => {
3798
3839
  addWillDelValuesCallback,
3799
3840
  addWillDelValueCallback,
3800
3841
  addWillApplyChangesCallback,
3801
- addDidSetRowCallback,
3802
3842
  destroy,
3803
3843
  });
3804
3844
  store._[4](
@@ -3816,7 +3856,7 @@ const createMiddleware = getCreateFunction((store) => {
3816
3856
  willDelValues,
3817
3857
  willDelValue,
3818
3858
  willApplyChanges,
3819
- didSetRow,
3859
+ () => willSetRowCallbacks.length > 0,
3820
3860
  );
3821
3861
  return middleware;
3822
3862
  });
@@ -9721,15 +9761,17 @@ const EditableThing = ({
9721
9761
  const [stringThing, setStringThing] = useState();
9722
9762
  const [numberThing, setNumberThing] = useState();
9723
9763
  const [booleanThing, setBooleanThing] = useState();
9724
- const [objectThingJson, setObjectThingJson] = useState(EMPTY_STRING);
9725
- const [arrayThingJson, setArrayThingJson] = useState(EMPTY_STRING);
9764
+ const [objectThing, setObjectThing] = useState('{}');
9765
+ const [arrayThing, setArrayThing] = useState('[]');
9766
+ const [objectClassName, setObjectClassName] = useState('');
9767
+ const [arrayClassName, setArrayClassName] = useState('');
9726
9768
  if (currentThing !== thing) {
9727
9769
  setThingType(getCellOrValueType(thing));
9728
9770
  setCurrentThing(thing);
9729
9771
  if (isObject(thing)) {
9730
- setObjectThingJson(jsonString(thing));
9772
+ setObjectThing(jsonString(thing));
9731
9773
  } else if (isArray(thing)) {
9732
- setArrayThingJson(jsonString(thing));
9774
+ setArrayThing(jsonString(thing));
9733
9775
  } else {
9734
9776
  setStringThing(String(thing));
9735
9777
  setNumberThing(Number(thing) || 0);
@@ -9744,6 +9786,22 @@ const EditableThing = ({
9744
9786
  },
9745
9787
  [onThingChange],
9746
9788
  );
9789
+ const handleJsonThingChange = useCallback(
9790
+ (value, setTypedThing, isThing, setTypedClassName) => {
9791
+ setTypedThing(value);
9792
+ try {
9793
+ const object = jsonParse(value);
9794
+ if (isThing(object)) {
9795
+ setCurrentThing(object);
9796
+ onThingChange(object);
9797
+ setTypedClassName('');
9798
+ }
9799
+ } catch {
9800
+ setTypedClassName('invalid');
9801
+ }
9802
+ },
9803
+ [onThingChange],
9804
+ );
9747
9805
  const handleTypeChange = useCallback(() => {
9748
9806
  if (!hasSchema?.()) {
9749
9807
  const nextType = getTypeCase(
@@ -9759,8 +9817,8 @@ const EditableThing = ({
9759
9817
  stringThing,
9760
9818
  numberThing,
9761
9819
  booleanThing,
9762
- objectThingJson ? jsonParse(objectThingJson) : {},
9763
- arrayThingJson ? jsonParse(arrayThingJson) : [],
9820
+ tryReturn(() => jsonParse(objectThing), {}),
9821
+ tryReturn(() => jsonParse(arrayThing), []),
9764
9822
  );
9765
9823
  setThingType(nextType);
9766
9824
  setCurrentThing(thing2);
@@ -9772,8 +9830,8 @@ const EditableThing = ({
9772
9830
  stringThing,
9773
9831
  numberThing,
9774
9832
  booleanThing,
9775
- objectThingJson,
9776
- arrayThingJson,
9833
+ objectThing,
9834
+ arrayThing,
9777
9835
  thingType,
9778
9836
  ]);
9779
9837
  const widget = getTypeCase(
@@ -9826,41 +9884,37 @@ const EditableThing = ({
9826
9884
  thingType,
9827
9885
  ),
9828
9886
  /* @__PURE__ */ jsx(
9829
- 'textarea',
9887
+ 'input',
9830
9888
  {
9831
- value: objectThingJson,
9889
+ value: objectThing,
9890
+ className: objectClassName,
9832
9891
  onChange: useCallback(
9833
- (event) => {
9834
- const str = event[CURRENT_TARGET][_VALUE];
9835
- setObjectThingJson(str);
9836
- try {
9837
- const parsed = jsonParse(str);
9838
- if (isObject(parsed)) {
9839
- onThingChange(parsed);
9840
- }
9841
- } catch {}
9842
- },
9843
- [onThingChange],
9892
+ (event) =>
9893
+ handleJsonThingChange(
9894
+ event[CURRENT_TARGET][_VALUE],
9895
+ setObjectThing,
9896
+ isObject,
9897
+ setObjectClassName,
9898
+ ),
9899
+ [handleJsonThingChange],
9844
9900
  ),
9845
9901
  },
9846
9902
  thingType,
9847
9903
  ),
9848
9904
  /* @__PURE__ */ jsx(
9849
- 'textarea',
9905
+ 'input',
9850
9906
  {
9851
- value: arrayThingJson,
9907
+ value: arrayThing,
9908
+ className: arrayClassName,
9852
9909
  onChange: useCallback(
9853
- (event) => {
9854
- const str = event[CURRENT_TARGET][_VALUE];
9855
- setArrayThingJson(str);
9856
- try {
9857
- const parsed = jsonParse(str);
9858
- if (isArray(parsed)) {
9859
- onThingChange(parsed);
9860
- }
9861
- } catch {}
9862
- },
9863
- [onThingChange],
9910
+ (event) =>
9911
+ handleJsonThingChange(
9912
+ event[CURRENT_TARGET][_VALUE],
9913
+ setArrayThing,
9914
+ isArray,
9915
+ setArrayClassName,
9916
+ ),
9917
+ [handleJsonThingChange],
9864
9918
  ),
9865
9919
  },
9866
9920
  thingType,