tinybase 7.2.0-beta.3 → 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.
package/queries/index.js CHANGED
@@ -28,8 +28,10 @@ const math = Math;
28
28
  const mathMax = math.max;
29
29
  const mathMin = math.min;
30
30
  const isFiniteNumber = isFinite;
31
+ const isNullish = (thing) => thing == null;
31
32
  const isUndefined = (thing) => thing === void 0;
32
33
  const isNull = (thing) => thing === null;
34
+ const ifNotNullish = getIfNotFunction(isNullish);
33
35
  const ifNotUndefined = getIfNotFunction(isUndefined);
34
36
  const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
35
37
  const isFunction = (thing) => getTypeOf(thing) == FUNCTION;
@@ -66,15 +68,49 @@ const collForEach = (coll, cb) => coll?.forEach(cb);
66
68
  const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
67
69
 
68
70
  const object = Object;
71
+ const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
69
72
  const objEntries = object.entries;
73
+ const isObject = (obj) =>
74
+ !isNullish(obj) &&
75
+ ifNotNullish(
76
+ getPrototypeOf(obj),
77
+ (objPrototype) =>
78
+ objPrototype == object.prototype ||
79
+ isNullish(getPrototypeOf(objPrototype)),
80
+
81
+ /* istanbul ignore next */
82
+ () => true,
83
+ );
84
+ const objIds = object.keys;
70
85
  const objFreeze = object.freeze;
71
86
  const objNew = (entries = []) => object.fromEntries(entries);
72
- const objHas = (obj, id) => id in obj;
87
+ const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
73
88
  const objToMap = (obj) => new Map(objEntries(obj));
74
89
  const objToArray = (obj, cb) =>
75
90
  arrayMap(objEntries(obj), ([id, value]) => cb(value, id));
76
91
  const objMap = (obj, cb) =>
77
92
  objNew(objToArray(obj, (value, id) => [id, cb(value, id)]));
93
+ const objSize = (obj) => size(objIds(obj));
94
+
95
+ /* istanbul ignore next */
96
+ const objIsEqual = (
97
+ obj1,
98
+ obj2,
99
+ isEqual = (value1, value2) => value1 === value2,
100
+ ) => {
101
+ const entries1 = objEntries(obj1);
102
+ return (
103
+ size(entries1) === objSize(obj2) &&
104
+ arrayEvery(entries1, ([index, value1]) =>
105
+ isObject(value1)
106
+ ? /* istanbul ignore next */
107
+ isObject(obj2[index])
108
+ ? objIsEqual(obj2[index], value1)
109
+ : false
110
+ : isEqual(value1, obj2[index]),
111
+ )
112
+ );
113
+ };
78
114
 
79
115
  const mapNew = (entries) => new Map(entries);
80
116
  const mapKeys = (map) => [...(map?.keys() ?? [])];
@@ -91,17 +127,12 @@ const mapEnsure = (map, key, getDefaultValue, hadExistingValue) => {
91
127
  }
92
128
  return mapGet(map, key);
93
129
  };
94
- const mapMatch = (map, obj, set, del = mapSet) => {
95
- objMap(obj, (value, id) => set(map, id, value));
96
- mapForEach(map, (id) => (objHas(obj, id) ? 0 : del(map, id)));
97
- return map;
98
- };
99
130
  const mapToObj = (map, valueMapper, excludeMapValue, excludeObjValue) => {
100
131
  const obj = {};
101
132
  collForEach(map, (mapValue, id) => {
102
- if (!excludeMapValue?.(mapValue, id)) {
103
- const objValue = valueMapper ? valueMapper(mapValue, id) : mapValue;
104
- if (!excludeObjValue?.(objValue)) {
133
+ {
134
+ const objValue = mapValue;
135
+ {
105
136
  obj[id] = objValue;
106
137
  }
107
138
  }
@@ -458,8 +489,10 @@ const createQueries = getCreateFunction((store) => {
458
489
  ),
459
490
  );
460
491
  const setQueryDefinition = (queryId, tableId, build, paramValues = {}) => {
492
+ const oldParamValues = getParamValues(queryId);
461
493
  setDefinition(queryId, tableId);
462
494
  setQueryArgs(queryId, [build, objToMap(paramValues)]);
495
+ callParamListeners(queryId, oldParamValues, paramValues);
463
496
  resetPreStores(queryId);
464
497
  const [, paramsMap] = getQueryArgs(queryId);
465
498
  const selectEntries = [];
@@ -788,32 +821,36 @@ const createQueries = getCreateFunction((store) => {
788
821
  );
789
822
  return queries;
790
823
  };
824
+ const callParamListeners = (queryId, oldParamValues, newParamValues) => {
825
+ const allParamIds = setNew([
826
+ ...objIds(oldParamValues),
827
+ ...objIds(newParamValues),
828
+ ]);
829
+ let changed = 0;
830
+ collForEach(allParamIds, (paramId) => {
831
+ const newParamValue = objGet(newParamValues, paramId);
832
+ if (!arrayOrValueEqual(objGet(oldParamValues, paramId), newParamValue)) {
833
+ changed = 1;
834
+ callListeners(paramValueListeners, [queryId, paramId], newParamValue);
835
+ }
836
+ });
837
+ if (changed) {
838
+ callListeners(paramValuesListeners, [queryId], newParamValues);
839
+ }
840
+ };
791
841
  const delQueryDefinition = (queryId) => {
792
- const oldParamValues = getQueryArgs(queryId)?.[1];
842
+ const oldParamValues = getParamValues(queryId);
793
843
  resetPreStores(queryId);
794
844
  delDefinition(queryId);
795
- ifNotUndefined(oldParamValues, (paramValues) => {
796
- mapForEach(paramValues, (paramId) =>
797
- callListeners(paramValueListeners, [queryId, paramId], void 0),
798
- );
799
- callListeners(paramValuesListeners, [queryId], {});
800
- });
845
+ callParamListeners(queryId, oldParamValues, {});
801
846
  return queries;
802
847
  };
803
- const setParamValues = (queryId, paramValues) => {
848
+ const setParamValues = (queryId, paramValues, force = 0) => {
804
849
  ifNotUndefined(getQueryArgs(queryId), ([definition, oldParamValues]) => {
805
- const changedParamValues = mapNew();
806
- mapMatch(
807
- oldParamValues,
808
- paramValues,
809
- (_, paramId, newValue) => {
810
- if (!arrayOrValueEqual(newValue, mapGet(oldParamValues, paramId))) {
811
- mapSet(changedParamValues, paramId, newValue);
812
- }
813
- },
814
- (_, paramId) => mapSet(changedParamValues, paramId, void 0),
815
- );
816
- if (!collIsEmpty(changedParamValues)) {
850
+ if (
851
+ force ||
852
+ !objIsEqual(mapToObj(oldParamValues), paramValues, arrayOrValueEqual)
853
+ ) {
817
854
  resultStore.transaction(() =>
818
855
  setQueryDefinition(
819
856
  queryId,
@@ -822,21 +859,21 @@ const createQueries = getCreateFunction((store) => {
822
859
  paramValues,
823
860
  ),
824
861
  );
825
- mapForEach(changedParamValues, (paramId, value) =>
826
- callListeners(paramValueListeners, [queryId, paramId], value),
827
- );
828
- callListeners(paramValuesListeners, [queryId], {...paramValues});
829
862
  }
830
863
  });
831
864
  return queries;
832
865
  };
833
- const setParamValue = (queryId, paramId, value) =>
834
- setParamValues(queryId, {
835
- ...mapToObj(getQueryArgs(queryId)?.[1]),
836
- [paramId]: value,
837
- });
838
- const getParamValues = (queryId) =>
839
- ifNotUndefined(getQueryArgs(queryId)?.[1], mapToObj) ?? {};
866
+ const setParamValue = (queryId, paramId, value) => {
867
+ if (!arrayOrValueEqual(getParamValue(queryId, paramId), value)) {
868
+ setParamValues(
869
+ queryId,
870
+ {...getParamValues(queryId), [paramId]: value},
871
+ 1,
872
+ );
873
+ }
874
+ return queries;
875
+ };
876
+ const getParamValues = (queryId) => mapToObj(getQueryArgs(queryId)?.[1]);
840
877
  const getParamValue = (queryId, paramId) =>
841
878
  mapGet(getQueryArgs(queryId)?.[1], paramId);
842
879
  const addQueryIdsListener = (listener) =>
@@ -28,8 +28,10 @@ const math = Math;
28
28
  const mathMax = math.max;
29
29
  const mathMin = math.min;
30
30
  const isFiniteNumber = isFinite;
31
+ const isNullish = (thing) => thing == null;
31
32
  const isUndefined = (thing) => thing === void 0;
32
33
  const isNull = (thing) => thing === null;
34
+ const ifNotNullish = getIfNotFunction(isNullish);
33
35
  const ifNotUndefined = getIfNotFunction(isUndefined);
34
36
  const isTypeStringOrBoolean = (type) => type == STRING || type == BOOLEAN;
35
37
  const isFunction = (thing) => getTypeOf(thing) == FUNCTION;
@@ -66,15 +68,49 @@ const collForEach = (coll, cb) => coll?.forEach(cb);
66
68
  const collDel = (coll, keyOrValue) => coll?.delete(keyOrValue);
67
69
 
68
70
  const object = Object;
71
+ const getPrototypeOf = (obj) => object.getPrototypeOf(obj);
69
72
  const objEntries = object.entries;
73
+ const isObject = (obj) =>
74
+ !isNullish(obj) &&
75
+ ifNotNullish(
76
+ getPrototypeOf(obj),
77
+ (objPrototype) =>
78
+ objPrototype == object.prototype ||
79
+ isNullish(getPrototypeOf(objPrototype)),
80
+
81
+ /* istanbul ignore next */
82
+ () => true,
83
+ );
84
+ const objIds = object.keys;
70
85
  const objFreeze = object.freeze;
71
86
  const objNew = (entries = []) => object.fromEntries(entries);
72
- const objHas = (obj, id) => id in obj;
87
+ const objGet = (obj, id) => ifNotUndefined(obj, (obj2) => obj2[id]);
73
88
  const objToMap = (obj) => new Map(objEntries(obj));
74
89
  const objToArray = (obj, cb) =>
75
90
  arrayMap(objEntries(obj), ([id, value]) => cb(value, id));
76
91
  const objMap = (obj, cb) =>
77
92
  objNew(objToArray(obj, (value, id) => [id, cb(value, id)]));
93
+ const objSize = (obj) => size(objIds(obj));
94
+
95
+ /* istanbul ignore next */
96
+ const objIsEqual = (
97
+ obj1,
98
+ obj2,
99
+ isEqual = (value1, value2) => value1 === value2,
100
+ ) => {
101
+ const entries1 = objEntries(obj1);
102
+ return (
103
+ size(entries1) === objSize(obj2) &&
104
+ arrayEvery(entries1, ([index, value1]) =>
105
+ isObject(value1)
106
+ ? /* istanbul ignore next */
107
+ isObject(obj2[index])
108
+ ? objIsEqual(obj2[index], value1)
109
+ : false
110
+ : isEqual(value1, obj2[index]),
111
+ )
112
+ );
113
+ };
78
114
 
79
115
  const mapNew = (entries) => new Map(entries);
80
116
  const mapKeys = (map) => [...(map?.keys() ?? [])];
@@ -91,17 +127,12 @@ const mapEnsure = (map, key, getDefaultValue, hadExistingValue) => {
91
127
  }
92
128
  return mapGet(map, key);
93
129
  };
94
- const mapMatch = (map, obj, set, del = mapSet) => {
95
- objMap(obj, (value, id) => set(map, id, value));
96
- mapForEach(map, (id) => (objHas(obj, id) ? 0 : del(map, id)));
97
- return map;
98
- };
99
130
  const mapToObj = (map, valueMapper, excludeMapValue, excludeObjValue) => {
100
131
  const obj = {};
101
132
  collForEach(map, (mapValue, id) => {
102
- if (!excludeMapValue?.(mapValue, id)) {
103
- const objValue = valueMapper ? valueMapper(mapValue, id) : mapValue;
104
- if (!excludeObjValue?.(objValue)) {
133
+ {
134
+ const objValue = mapValue;
135
+ {
105
136
  obj[id] = objValue;
106
137
  }
107
138
  }
@@ -458,8 +489,10 @@ const createQueries = getCreateFunction((store) => {
458
489
  ),
459
490
  );
460
491
  const setQueryDefinition = (queryId, tableId, build, paramValues = {}) => {
492
+ const oldParamValues = getParamValues(queryId);
461
493
  setDefinition(queryId, tableId);
462
494
  setQueryArgs(queryId, [build, objToMap(paramValues)]);
495
+ callParamListeners(queryId, oldParamValues, paramValues);
463
496
  resetPreStores(queryId);
464
497
  const [, paramsMap] = getQueryArgs(queryId);
465
498
  const selectEntries = [];
@@ -788,32 +821,36 @@ const createQueries = getCreateFunction((store) => {
788
821
  );
789
822
  return queries;
790
823
  };
824
+ const callParamListeners = (queryId, oldParamValues, newParamValues) => {
825
+ const allParamIds = setNew([
826
+ ...objIds(oldParamValues),
827
+ ...objIds(newParamValues),
828
+ ]);
829
+ let changed = 0;
830
+ collForEach(allParamIds, (paramId) => {
831
+ const newParamValue = objGet(newParamValues, paramId);
832
+ if (!arrayOrValueEqual(objGet(oldParamValues, paramId), newParamValue)) {
833
+ changed = 1;
834
+ callListeners(paramValueListeners, [queryId, paramId], newParamValue);
835
+ }
836
+ });
837
+ if (changed) {
838
+ callListeners(paramValuesListeners, [queryId], newParamValues);
839
+ }
840
+ };
791
841
  const delQueryDefinition = (queryId) => {
792
- const oldParamValues = getQueryArgs(queryId)?.[1];
842
+ const oldParamValues = getParamValues(queryId);
793
843
  resetPreStores(queryId);
794
844
  delDefinition(queryId);
795
- ifNotUndefined(oldParamValues, (paramValues) => {
796
- mapForEach(paramValues, (paramId) =>
797
- callListeners(paramValueListeners, [queryId, paramId], void 0),
798
- );
799
- callListeners(paramValuesListeners, [queryId], {});
800
- });
845
+ callParamListeners(queryId, oldParamValues, {});
801
846
  return queries;
802
847
  };
803
- const setParamValues = (queryId, paramValues) => {
848
+ const setParamValues = (queryId, paramValues, force = 0) => {
804
849
  ifNotUndefined(getQueryArgs(queryId), ([definition, oldParamValues]) => {
805
- const changedParamValues = mapNew();
806
- mapMatch(
807
- oldParamValues,
808
- paramValues,
809
- (_, paramId, newValue) => {
810
- if (!arrayOrValueEqual(newValue, mapGet(oldParamValues, paramId))) {
811
- mapSet(changedParamValues, paramId, newValue);
812
- }
813
- },
814
- (_, paramId) => mapSet(changedParamValues, paramId, void 0),
815
- );
816
- if (!collIsEmpty(changedParamValues)) {
850
+ if (
851
+ force ||
852
+ !objIsEqual(mapToObj(oldParamValues), paramValues, arrayOrValueEqual)
853
+ ) {
817
854
  resultStore.transaction(() =>
818
855
  setQueryDefinition(
819
856
  queryId,
@@ -822,21 +859,21 @@ const createQueries = getCreateFunction((store) => {
822
859
  paramValues,
823
860
  ),
824
861
  );
825
- mapForEach(changedParamValues, (paramId, value) =>
826
- callListeners(paramValueListeners, [queryId, paramId], value),
827
- );
828
- callListeners(paramValuesListeners, [queryId], {...paramValues});
829
862
  }
830
863
  });
831
864
  return queries;
832
865
  };
833
- const setParamValue = (queryId, paramId, value) =>
834
- setParamValues(queryId, {
835
- ...mapToObj(getQueryArgs(queryId)?.[1]),
836
- [paramId]: value,
837
- });
838
- const getParamValues = (queryId) =>
839
- ifNotUndefined(getQueryArgs(queryId)?.[1], mapToObj) ?? {};
866
+ const setParamValue = (queryId, paramId, value) => {
867
+ if (!arrayOrValueEqual(getParamValue(queryId, paramId), value)) {
868
+ setParamValues(
869
+ queryId,
870
+ {...getParamValues(queryId), [paramId]: value},
871
+ 1,
872
+ );
873
+ }
874
+ return queries;
875
+ };
876
+ const getParamValues = (queryId) => mapToObj(getQueryArgs(queryId)?.[1]);
840
877
  const getParamValue = (queryId, paramId) =>
841
878
  mapGet(getQueryArgs(queryId)?.[1], paramId);
842
879
  const addQueryIdsListener = (listener) =>