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
@@ -108,6 +108,14 @@ const structuredClone = GLOBAL.structuredClone;
108
108
  const errorNew = (message) => {
109
109
  throw new Error(message);
110
110
  };
111
+ const tryReturn = (tryF, catchReturn) => {
112
+ try {
113
+ return tryF();
114
+ } catch {
115
+ /* istanbul ignore next */
116
+ return catchReturn;
117
+ }
118
+ };
111
119
  const tryCatch = async (action, then1, then2) => {
112
120
  try {
113
121
  return await action();
@@ -155,7 +163,6 @@ const isObject = (obj) =>
155
163
  const objIds = object.keys;
156
164
  const objFreeze = object.freeze;
157
165
  const objNew = (entries = []) => object.fromEntries(entries);
158
- const objMerge = (...objs) => object.assign({}, ...objs);
159
166
  const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
160
167
  const objHas = (obj, id) => id in obj;
161
168
  const objDel = (obj, id) => {
@@ -1019,10 +1026,24 @@ const createStore = () => {
1019
1026
  );
1020
1027
  const setOrDelTables = (tables) =>
1021
1028
  objIsEmpty(tables) ? delTables() : setTables(tables);
1022
- const setOrDelCell = (tableId, rowId, cellId, cell, skipMiddleware) =>
1029
+ const setOrDelCell = (
1030
+ tableId,
1031
+ rowId,
1032
+ cellId,
1033
+ cell,
1034
+ skipMiddleware,
1035
+ skipRowMiddleware,
1036
+ ) =>
1023
1037
  isUndefined(cell)
1024
1038
  ? delCell(tableId, rowId, cellId, true, skipMiddleware)
1025
- : setCell(tableId, rowId, cellId, cell, skipMiddleware);
1039
+ : setCell(
1040
+ tableId,
1041
+ rowId,
1042
+ cellId,
1043
+ cell,
1044
+ skipMiddleware,
1045
+ skipRowMiddleware,
1046
+ );
1026
1047
  const setOrDelValues = (values) =>
1027
1048
  objIsEmpty(values) ? delValues() : setValues(values);
1028
1049
  const setOrDelValue = (valueId, value, skipMiddleware) =>
@@ -1122,6 +1143,30 @@ const createStore = () => {
1122
1143
  ),
1123
1144
  objIsEqual,
1124
1145
  );
1146
+ const applyRowDirectly = (tableId, tableMap, rowId, row, skipMiddleware) => {
1147
+ mapMatch(
1148
+ mapEnsure(tableMap, rowId, () => {
1149
+ rowIdsChanged(tableId, rowId, 1);
1150
+ return mapNew();
1151
+ }),
1152
+ row,
1153
+ (rowMap, cellId, cell) =>
1154
+ ifNotUndefined(
1155
+ getValidatedCell(tableId, rowId, cellId, cell),
1156
+ (validCell) =>
1157
+ setValidCell(
1158
+ tableId,
1159
+ rowId,
1160
+ rowMap,
1161
+ cellId,
1162
+ validCell,
1163
+ skipMiddleware,
1164
+ ),
1165
+ ),
1166
+ (rowMap, cellId) =>
1167
+ delValidCell(tableId, tableMap, rowId, rowMap, cellId, true),
1168
+ );
1169
+ };
1125
1170
  const setValidCell = (tableId, rowId, rowMap, cellId, cell, skipMiddleware) =>
1126
1171
  ifTransformed(
1127
1172
  cell,
@@ -1798,7 +1843,14 @@ const createStore = () => {
1798
1843
  tableId,
1799
1844
  rowId,
1800
1845
  );
1801
- const setCell = (tableId, rowId, cellId, cell, skipMiddleware) =>
1846
+ const setCell = (
1847
+ tableId,
1848
+ rowId,
1849
+ cellId,
1850
+ cell,
1851
+ skipMiddleware,
1852
+ skipRowMiddleware,
1853
+ ) =>
1802
1854
  fluentTransaction(
1803
1855
  (tableId2, rowId2, cellId2) =>
1804
1856
  ifNotUndefined(
@@ -1808,15 +1860,47 @@ const createStore = () => {
1808
1860
  cellId2,
1809
1861
  isFunction(cell) ? cell(getCell(tableId2, rowId2, cellId2)) : cell,
1810
1862
  ),
1811
- (validCell) =>
1812
- setCellIntoNewRow(
1813
- tableId2,
1814
- getOrCreateTable(tableId2),
1815
- rowId2,
1816
- cellId2,
1817
- validCell,
1818
- skipMiddleware,
1819
- ),
1863
+ (validCell) => {
1864
+ const tableMap = getOrCreateTable(tableId2);
1865
+ ifNotUndefined(
1866
+ skipMiddleware || skipRowMiddleware || !middleware[14]?.()
1867
+ ? void 0
1868
+ : middleware[3],
1869
+ (willSetRow) => {
1870
+ const existingRowMap = mapGet(tableMap, rowId2);
1871
+ const prospectiveRow = {
1872
+ ...(existingRowMap ? mapToObj(existingRowMap) : {}),
1873
+ [cellId2]: validCell,
1874
+ };
1875
+ ifNotUndefined(
1876
+ whileMutating(() =>
1877
+ willSetRow(
1878
+ tableId2,
1879
+ rowId2,
1880
+ structuredClone(prospectiveRow),
1881
+ ),
1882
+ ),
1883
+ (row) =>
1884
+ applyRowDirectly(
1885
+ tableId2,
1886
+ tableMap,
1887
+ rowId2,
1888
+ row,
1889
+ skipMiddleware,
1890
+ ),
1891
+ );
1892
+ },
1893
+ () =>
1894
+ setCellIntoNewRow(
1895
+ tableId2,
1896
+ tableMap,
1897
+ rowId2,
1898
+ cellId2,
1899
+ validCell,
1900
+ skipMiddleware,
1901
+ ),
1902
+ );
1903
+ },
1820
1904
  ),
1821
1905
  tableId,
1822
1906
  rowId,
@@ -1865,7 +1949,14 @@ const createStore = () => {
1865
1949
  isUndefined(row)
1866
1950
  ? delRow(tableId, rowId)
1867
1951
  : objMap(row, (cell, cellId) =>
1868
- setOrDelCell(tableId, rowId, cellId, cell),
1952
+ setOrDelCell(
1953
+ tableId,
1954
+ rowId,
1955
+ cellId,
1956
+ cell,
1957
+ void 0,
1958
+ true,
1959
+ ),
1869
1960
  ),
1870
1961
  ),
1871
1962
  );
@@ -2028,37 +2119,6 @@ const createStore = () => {
2028
2119
  mapToObj3(changedCellIds),
2029
2120
  mapToObj(changedValueIds),
2030
2121
  ];
2031
- const doDidSetRows = () => {
2032
- if (middleware[14]) {
2033
- const changedCells2 = clonedChangedCells(changedCells);
2034
- collForEach(changedCells2, (rows, tableId) =>
2035
- collForEach(rows, (cells, rowId) => {
2036
- if (
2037
- !arrayEvery(
2038
- collValues(cells),
2039
- ([oldCell, newCell]) => oldCell === newCell,
2040
- )
2041
- ) {
2042
- const newRow = getRow(tableId, rowId);
2043
- const oldRow = objMerge(newRow);
2044
- collForEach(cells, ([oldCell], cellId) =>
2045
- isUndefined(oldCell)
2046
- ? objDel(oldRow, cellId)
2047
- : (oldRow[cellId] = oldCell),
2048
- );
2049
- const didSetRow = middleware[14](tableId, rowId, oldRow, newRow);
2050
- if (!objIsEqual(didSetRow, newRow)) {
2051
- const setOrDelRow = objMap(newRow, () => void 0);
2052
- objMap(didSetRow, (cell, cellId) => (setOrDelRow[cellId] = cell));
2053
- objMap(setOrDelRow, (cell, cellId) =>
2054
- setOrDelCell(tableId, rowId, cellId, cell, true),
2055
- );
2056
- }
2057
- }
2058
- }),
2059
- );
2060
- }
2061
- };
2062
2122
  const finishTransaction = (doRollback) => {
2063
2123
  if (transactions > 0) {
2064
2124
  transactions--;
@@ -2068,7 +2128,6 @@ const createStore = () => {
2068
2128
  callInvalidCellListeners(1);
2069
2129
  if (!collIsEmpty(changedCells)) {
2070
2130
  callTabularListenersForChanges(1);
2071
- doDidSetRows();
2072
2131
  }
2073
2132
  callInvalidValueListeners(1);
2074
2133
  if (!collIsEmpty(changedValues)) {
@@ -2239,7 +2298,7 @@ const createStore = () => {
2239
2298
  willDelValues,
2240
2299
  willDelValue,
2241
2300
  willApplyChanges,
2242
- didSetRow,
2301
+ hasWillSetRowCallbacks,
2243
2302
  ) =>
2244
2303
  (middleware = [
2245
2304
  willSetContent,
@@ -2256,7 +2315,7 @@ const createStore = () => {
2256
2315
  willDelValues,
2257
2316
  willDelValue,
2258
2317
  willApplyChanges,
2259
- didSetRow,
2318
+ hasWillSetRowCallbacks,
2260
2319
  ]);
2261
2320
  const setInternalListeners = (
2262
2321
  preStartTransaction,
@@ -3078,15 +3137,17 @@ const EditableThing = ({
3078
3137
  const [stringThing, setStringThing] = useState();
3079
3138
  const [numberThing, setNumberThing] = useState();
3080
3139
  const [booleanThing, setBooleanThing] = useState();
3081
- const [objectThingJson, setObjectThingJson] = useState(EMPTY_STRING);
3082
- const [arrayThingJson, setArrayThingJson] = useState(EMPTY_STRING);
3140
+ const [objectThing, setObjectThing] = useState('{}');
3141
+ const [arrayThing, setArrayThing] = useState('[]');
3142
+ const [objectClassName, setObjectClassName] = useState('');
3143
+ const [arrayClassName, setArrayClassName] = useState('');
3083
3144
  if (currentThing !== thing) {
3084
3145
  setThingType(getCellOrValueType(thing));
3085
3146
  setCurrentThing(thing);
3086
3147
  if (isObject(thing)) {
3087
- setObjectThingJson(jsonString(thing));
3148
+ setObjectThing(jsonString(thing));
3088
3149
  } else if (isArray(thing)) {
3089
- setArrayThingJson(jsonString(thing));
3150
+ setArrayThing(jsonString(thing));
3090
3151
  } else {
3091
3152
  setStringThing(String(thing));
3092
3153
  setNumberThing(Number(thing) || 0);
@@ -3101,6 +3162,22 @@ const EditableThing = ({
3101
3162
  },
3102
3163
  [onThingChange],
3103
3164
  );
3165
+ const handleJsonThingChange = useCallback(
3166
+ (value, setTypedThing, isThing, setTypedClassName) => {
3167
+ setTypedThing(value);
3168
+ try {
3169
+ const object = jsonParse(value);
3170
+ if (isThing(object)) {
3171
+ setCurrentThing(object);
3172
+ onThingChange(object);
3173
+ setTypedClassName('');
3174
+ }
3175
+ } catch {
3176
+ setTypedClassName('invalid');
3177
+ }
3178
+ },
3179
+ [onThingChange],
3180
+ );
3104
3181
  const handleTypeChange = useCallback(() => {
3105
3182
  if (!hasSchema?.()) {
3106
3183
  const nextType = getTypeCase(
@@ -3116,8 +3193,8 @@ const EditableThing = ({
3116
3193
  stringThing,
3117
3194
  numberThing,
3118
3195
  booleanThing,
3119
- objectThingJson ? jsonParse(objectThingJson) : {},
3120
- arrayThingJson ? jsonParse(arrayThingJson) : [],
3196
+ tryReturn(() => jsonParse(objectThing), {}),
3197
+ tryReturn(() => jsonParse(arrayThing), []),
3121
3198
  );
3122
3199
  setThingType(nextType);
3123
3200
  setCurrentThing(thing2);
@@ -3129,8 +3206,8 @@ const EditableThing = ({
3129
3206
  stringThing,
3130
3207
  numberThing,
3131
3208
  booleanThing,
3132
- objectThingJson,
3133
- arrayThingJson,
3209
+ objectThing,
3210
+ arrayThing,
3134
3211
  thingType,
3135
3212
  ]);
3136
3213
  const widget = getTypeCase(
@@ -3183,41 +3260,37 @@ const EditableThing = ({
3183
3260
  thingType,
3184
3261
  ),
3185
3262
  /* @__PURE__ */ jsx(
3186
- 'textarea',
3263
+ 'input',
3187
3264
  {
3188
- value: objectThingJson,
3265
+ value: objectThing,
3266
+ className: objectClassName,
3189
3267
  onChange: useCallback(
3190
- (event) => {
3191
- const str = event[CURRENT_TARGET][_VALUE];
3192
- setObjectThingJson(str);
3193
- try {
3194
- const parsed = jsonParse(str);
3195
- if (isObject(parsed)) {
3196
- onThingChange(parsed);
3197
- }
3198
- } catch {}
3199
- },
3200
- [onThingChange],
3268
+ (event) =>
3269
+ handleJsonThingChange(
3270
+ event[CURRENT_TARGET][_VALUE],
3271
+ setObjectThing,
3272
+ isObject,
3273
+ setObjectClassName,
3274
+ ),
3275
+ [handleJsonThingChange],
3201
3276
  ),
3202
3277
  },
3203
3278
  thingType,
3204
3279
  ),
3205
3280
  /* @__PURE__ */ jsx(
3206
- 'textarea',
3281
+ 'input',
3207
3282
  {
3208
- value: arrayThingJson,
3283
+ value: arrayThing,
3284
+ className: arrayClassName,
3209
3285
  onChange: useCallback(
3210
- (event) => {
3211
- const str = event[CURRENT_TARGET][_VALUE];
3212
- setArrayThingJson(str);
3213
- try {
3214
- const parsed = jsonParse(str);
3215
- if (isArray(parsed)) {
3216
- onThingChange(parsed);
3217
- }
3218
- } catch {}
3219
- },
3220
- [onThingChange],
3286
+ (event) =>
3287
+ handleJsonThingChange(
3288
+ event[CURRENT_TARGET][_VALUE],
3289
+ setArrayThing,
3290
+ isArray,
3291
+ setArrayClassName,
3292
+ ),
3293
+ [handleJsonThingChange],
3221
3294
  ),
3222
3295
  },
3223
3296
  thingType,