tinybase 7.2.0-beta.2 → 7.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.
Files changed (44) hide show
  1. package/@types/queries/index.d.ts +333 -4
  2. package/@types/queries/with-schemas/index.d.ts +359 -4
  3. package/@types/ui-react/index.d.ts +477 -8
  4. package/@types/ui-react/with-schemas/index.d.ts +519 -8
  5. package/agents.md +34 -0
  6. package/index.js +100 -18
  7. package/min/index.js +1 -1
  8. package/min/index.js.gz +0 -0
  9. package/min/omni/index.js +1 -1
  10. package/min/omni/index.js.gz +0 -0
  11. package/min/omni/with-schemas/index.js +1 -1
  12. package/min/omni/with-schemas/index.js.gz +0 -0
  13. package/min/queries/index.js +1 -1
  14. package/min/queries/index.js.gz +0 -0
  15. package/min/queries/with-schemas/index.js +1 -1
  16. package/min/queries/with-schemas/index.js.gz +0 -0
  17. package/min/ui-react/index.js +1 -1
  18. package/min/ui-react/index.js.gz +0 -0
  19. package/min/ui-react/with-schemas/index.js +1 -1
  20. package/min/ui-react/with-schemas/index.js.gz +0 -0
  21. package/min/ui-react-dom/index.js +1 -1
  22. package/min/ui-react-dom/index.js.gz +0 -0
  23. package/min/ui-react-dom/with-schemas/index.js +1 -1
  24. package/min/ui-react-dom/with-schemas/index.js.gz +0 -0
  25. package/min/ui-react-inspector/index.js +1 -1
  26. package/min/ui-react-inspector/index.js.gz +0 -0
  27. package/min/ui-react-inspector/with-schemas/index.js +1 -1
  28. package/min/ui-react-inspector/with-schemas/index.js.gz +0 -0
  29. package/min/with-schemas/index.js +1 -1
  30. package/min/with-schemas/index.js.gz +0 -0
  31. package/omni/index.js +173 -48
  32. package/omni/with-schemas/index.js +173 -48
  33. package/package.json +3 -3
  34. package/queries/index.js +120 -18
  35. package/queries/with-schemas/index.js +120 -18
  36. package/readme.md +13 -13
  37. package/releases.md +40 -40
  38. package/ui-react/index.js +98 -30
  39. package/ui-react/with-schemas/index.js +98 -30
  40. package/ui-react-dom/index.js +38 -15
  41. package/ui-react-dom/with-schemas/index.js +38 -15
  42. package/ui-react-inspector/index.js +40 -17
  43. package/ui-react-inspector/with-schemas/index.js +40 -17
  44. package/with-schemas/index.js +100 -18
@@ -78,6 +78,10 @@ const arrayEvery = (array, cb) => array.every(cb);
78
78
  const arrayIsEqual = (array1, array2) =>
79
79
  size(array1) === size(array2) &&
80
80
  arrayEvery(array1, (value1, index) => array2[index] === value1);
81
+ const arrayOrValueEqual = (value1, value2) =>
82
+ isArray(value1) && isArray(value2)
83
+ ? arrayIsEqual(value1, value2)
84
+ : value1 === value2;
81
85
  const arrayIsSorted = (array, sorter) =>
82
86
  arrayEvery(
83
87
  array,
@@ -145,6 +149,7 @@ const isObject = (obj) =>
145
149
  const objIds = object.keys;
146
150
  const objFreeze = object.freeze;
147
151
  const objNew = (entries = []) => object.fromEntries(entries);
152
+ const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
148
153
  const objHas = (obj, id) => id in obj;
149
154
  const objDel = (obj, id) => {
150
155
  delete obj[id];
@@ -159,6 +164,26 @@ const objMap = (obj, cb) =>
159
164
  objNew(objToArray(obj, (value, id) => [id, cb(value, id)]));
160
165
  const objSize = (obj) => size(objIds(obj));
161
166
  const objIsEmpty = (obj) => isObject(obj) && objSize(obj) == 0;
167
+
168
+ /* istanbul ignore next */
169
+ const objIsEqual = (
170
+ obj1,
171
+ obj2,
172
+ isEqual = (value1, value2) => value1 === value2,
173
+ ) => {
174
+ const entries1 = objEntries(obj1);
175
+ return (
176
+ size(entries1) === objSize(obj2) &&
177
+ arrayEvery(entries1, ([index, value1]) =>
178
+ isObject(value1)
179
+ ? /* istanbul ignore next */
180
+ isObject(obj2[index])
181
+ ? objIsEqual(obj2[index], value1)
182
+ : false
183
+ : isEqual(value1, obj2[index]),
184
+ )
185
+ );
186
+ };
162
187
  const objEnsure = (obj, id, getDefaultValue) => {
163
188
  if (!objHas(obj, id)) {
164
189
  obj[id] = getDefaultValue();
@@ -3221,6 +3246,8 @@ const createQueries = getCreateFunction((store) => {
3221
3246
  const preStore = createStore();
3222
3247
  const resultStore = createStore();
3223
3248
  const preStoreListenerIds = mapNew();
3249
+ const paramValuesListeners = mapNew();
3250
+ const paramValueListeners = mapNew();
3224
3251
  const {
3225
3252
  addListener,
3226
3253
  callListeners,
@@ -3243,7 +3270,7 @@ const createQueries = getCreateFunction((store) => {
3243
3270
  delStoreListeners,
3244
3271
  ] = getDefinableFunctions(
3245
3272
  store,
3246
- () => [() => {}, mapNew()],
3273
+ () => [],
3247
3274
  getUndefined,
3248
3275
  addListener,
3249
3276
  callListeners,
@@ -3283,8 +3310,10 @@ const createQueries = getCreateFunction((store) => {
3283
3310
  ),
3284
3311
  );
3285
3312
  const setQueryDefinition = (queryId, tableId, build, paramValues = {}) => {
3313
+ const oldParamValues = getParamValues(queryId);
3286
3314
  setDefinition(queryId, tableId);
3287
3315
  setQueryArgs(queryId, [build, objToMap(paramValues)]);
3316
+ callParamListeners(queryId, oldParamValues, paramValues);
3288
3317
  resetPreStores(queryId);
3289
3318
  const [, paramsMap] = getQueryArgs(queryId);
3290
3319
  const selectEntries = [];
@@ -3613,31 +3642,76 @@ const createQueries = getCreateFunction((store) => {
3613
3642
  );
3614
3643
  return queries;
3615
3644
  };
3645
+ const callParamListeners = (queryId, oldParamValues, newParamValues) => {
3646
+ const allParamIds = setNew([
3647
+ ...objIds(oldParamValues),
3648
+ ...objIds(newParamValues),
3649
+ ]);
3650
+ let changed = 0;
3651
+ collForEach(allParamIds, (paramId) => {
3652
+ const newParamValue = objGet(newParamValues, paramId);
3653
+ if (!arrayOrValueEqual(objGet(oldParamValues, paramId), newParamValue)) {
3654
+ changed = 1;
3655
+ callListeners(paramValueListeners, [queryId, paramId], newParamValue);
3656
+ }
3657
+ });
3658
+ if (changed) {
3659
+ callListeners(paramValuesListeners, [queryId], newParamValues);
3660
+ }
3661
+ };
3616
3662
  const delQueryDefinition = (queryId) => {
3663
+ const oldParamValues = getParamValues(queryId);
3617
3664
  resetPreStores(queryId);
3618
3665
  delDefinition(queryId);
3666
+ callParamListeners(queryId, oldParamValues, {});
3619
3667
  return queries;
3620
3668
  };
3621
- const setParamValues = (queryId, paramValues) => {
3622
- ifNotUndefined(getQueryArgs(queryId), (definition) =>
3623
- resultStore.transaction(() =>
3624
- setQueryDefinition(
3625
- queryId,
3626
- getTableId(queryId),
3627
- definition[0],
3628
- paramValues,
3629
- ),
3630
- ),
3631
- );
3669
+ const setParamValues = (queryId, paramValues, force = 0) => {
3670
+ ifNotUndefined(getQueryArgs(queryId), ([definition, oldParamValues]) => {
3671
+ if (
3672
+ force ||
3673
+ !objIsEqual(mapToObj(oldParamValues), paramValues, arrayOrValueEqual)
3674
+ ) {
3675
+ resultStore.transaction(() =>
3676
+ setQueryDefinition(
3677
+ queryId,
3678
+ getTableId(queryId),
3679
+ definition,
3680
+ paramValues,
3681
+ ),
3682
+ );
3683
+ }
3684
+ });
3632
3685
  return queries;
3633
3686
  };
3634
- const setParamValue = (queryId, paramId, value) =>
3635
- setParamValues(queryId, {
3636
- ...mapToObj(getQueryArgs(queryId)?.[1]),
3637
- [paramId]: value,
3638
- });
3687
+ const setParamValue = (queryId, paramId, value) => {
3688
+ if (!arrayOrValueEqual(getParamValue(queryId, paramId), value)) {
3689
+ setParamValues(
3690
+ queryId,
3691
+ {...getParamValues(queryId), [paramId]: value},
3692
+ 1,
3693
+ );
3694
+ }
3695
+ return queries;
3696
+ };
3697
+ const getParamValues = (queryId) => mapToObj(getQueryArgs(queryId)?.[1]);
3698
+ const getParamValue = (queryId, paramId) =>
3699
+ mapGet(getQueryArgs(queryId)?.[1], paramId);
3639
3700
  const addQueryIdsListener = (listener) =>
3640
3701
  addQueryIdsListenerImpl(() => listener(queries));
3702
+ const addParamValuesListener = (queryId, listener) =>
3703
+ addListener(
3704
+ (_, queryId2, paramValues) => listener(queries, queryId2, paramValues),
3705
+ paramValuesListeners,
3706
+ [queryId],
3707
+ );
3708
+ const addParamValueListener = (queryId, paramId, listener) =>
3709
+ addListener(
3710
+ (_, queryId2, paramId2, paramValue) =>
3711
+ listener(queries, queryId2, paramId2, paramValue),
3712
+ paramValueListeners,
3713
+ [queryId, paramId],
3714
+ );
3641
3715
  const delListener = (listenerId) => {
3642
3716
  delListenerImpl(listenerId);
3643
3717
  return queries;
@@ -3649,11 +3723,17 @@ const createQueries = getCreateFunction((store) => {
3649
3723
  transaction: _3,
3650
3724
  ...stats
3651
3725
  } = resultStore.getListenerStats();
3652
- return stats;
3726
+ return {
3727
+ ...stats,
3728
+ paramValues: collSize2(paramValuesListeners),
3729
+ paramValue: collSize3(paramValueListeners),
3730
+ };
3653
3731
  };
3654
3732
  const queries = {
3655
3733
  setQueryDefinition,
3656
3734
  delQueryDefinition,
3735
+ getParamValues,
3736
+ getParamValue,
3657
3737
  setParamValues,
3658
3738
  setParamValue,
3659
3739
  getStore,
@@ -3662,6 +3742,8 @@ const createQueries = getCreateFunction((store) => {
3662
3742
  hasQuery,
3663
3743
  getTableId,
3664
3744
  addQueryIdsListener,
3745
+ addParamValuesListener,
3746
+ addParamValueListener,
3665
3747
  delListener,
3666
3748
  destroy,
3667
3749
  getListenerStats,