@domql/state 2.5.203 → 2.5.206

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/create.js CHANGED
@@ -41,7 +41,7 @@ export const applyInitialState = async function (element, parent, options) {
41
41
  if (objectizeState === false) return parent.state || {}
42
42
  else element.state = objectizeState
43
43
 
44
- const whatInitReturns = triggerEventOn('stateInit', element, options)
44
+ const whatInitReturns = await triggerEventOn('stateInit', element, options)
45
45
  if (whatInitReturns === false) return element.state
46
46
 
47
47
  if (checkIfInherits(element)) {
@@ -55,7 +55,7 @@ export const applyInitialState = async function (element, parent, options) {
55
55
  applyMethods(element)
56
56
 
57
57
  // trigger `on.stateCreated`
58
- triggerEventOn('stateCreated', element)
58
+ await triggerEventOn('stateCreated', element)
59
59
 
60
60
  return element.state
61
61
  }
@@ -34,29 +34,24 @@ const createState = async function(element, parent, options) {
34
34
  };
35
35
  const applyInitialState = async function(element, parent, options) {
36
36
  const objectizeState = await checkForTypes(element);
37
- if (objectizeState === false)
38
- return parent.state || {};
39
- else
40
- element.state = objectizeState;
41
- const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element, options);
42
- if (whatInitReturns === false)
43
- return element.state;
37
+ if (objectizeState === false) return parent.state || {};
38
+ else element.state = objectizeState;
39
+ const whatInitReturns = await (0, import_event.triggerEventOn)("stateInit", element, options);
40
+ if (whatInitReturns === false) return element.state;
44
41
  if ((0, import_inherit.checkIfInherits)(element)) {
45
42
  const inheritedState = (0, import_inherit.createInheritedState)(element, parent);
46
43
  element.state = (0, import_utils.isUndefined)(inheritedState) ? {} : inheritedState;
47
44
  }
48
45
  const dependentState = applyDependentState(element, element.state || parent.state || {});
49
- if (dependentState)
50
- element.state = dependentState;
46
+ if (dependentState) element.state = dependentState;
51
47
  applyMethods(element);
52
- (0, import_event.triggerEventOn)("stateCreated", element);
48
+ await (0, import_event.triggerEventOn)("stateCreated", element);
53
49
  return element.state;
54
50
  };
55
51
  const applyDependentState = (element, state) => {
56
52
  const { __ref, ref, __element } = state;
57
53
  const origState = (0, import_utils.exec)(__ref || ref || (__element == null ? void 0 : __element.state), element);
58
- if (!origState)
59
- return;
54
+ if (!origState) return;
60
55
  const dependentState = (0, import_utils.deepClone)(origState, import_ignore.IGNORE_STATE_PARAMS);
61
56
  const newDepends = { [element.key]: dependentState };
62
57
  const __depends = (0, import_utils.isObject)(origState.__depends) ? { ...origState.__depends, ...newDepends } : newDepends;
package/dist/cjs/index.js CHANGED
@@ -13,10 +13,10 @@ var __copyProps = (to, from, except, desc) => {
13
13
  };
14
14
  var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
15
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var state_exports = {};
17
- module.exports = __toCommonJS(state_exports);
18
- __reExport(state_exports, require("./ignore.js"), module.exports);
19
- __reExport(state_exports, require("./create.js"), module.exports);
20
- __reExport(state_exports, require("./updateState.js"), module.exports);
21
- __reExport(state_exports, require("./methods.js"), module.exports);
22
- __reExport(state_exports, require("./inherit.js"), module.exports);
16
+ var index_exports = {};
17
+ module.exports = __toCommonJS(index_exports);
18
+ __reExport(index_exports, require("./ignore.js"), module.exports);
19
+ __reExport(index_exports, require("./create.js"), module.exports);
20
+ __reExport(index_exports, require("./updateState.js"), module.exports);
21
+ __reExport(index_exports, require("./methods.js"), module.exports);
22
+ __reExport(index_exports, require("./inherit.js"), module.exports);
@@ -31,20 +31,16 @@ module.exports = __toCommonJS(inherit_exports);
31
31
  var import_utils = require("@domql/utils");
32
32
  var import_ignore = require("./ignore.js");
33
33
  const getRootStateInKey = (stateKey, parentState) => {
34
- if (!stateKey.includes("~/"))
35
- return;
34
+ if (!stateKey.includes("~/")) return;
36
35
  const arr = stateKey.split("~/");
37
- if (arr.length > 1)
38
- return parentState.root;
36
+ if (arr.length > 1) return parentState.root;
39
37
  };
40
38
  const getParentStateInKey = (stateKey, parentState) => {
41
- if (!stateKey.includes("../"))
42
- return;
39
+ if (!stateKey.includes("../")) return;
43
40
  const arr = stateKey.split("../");
44
41
  const arrLength = arr.length - 1;
45
42
  for (let i = 0; i < arrLength; i++) {
46
- if (!parentState.parent)
47
- return null;
43
+ if (!parentState.parent) return null;
48
44
  parentState = parentState.parent;
49
45
  }
50
46
  return parentState;
@@ -55,25 +51,20 @@ const getChildStateInKey = (stateKey, parentState, options = {}) => {
55
51
  for (let i = 0; i < arrLength; i++) {
56
52
  const childKey = arr[i];
57
53
  const grandChildKey = arr[i + 1];
58
- if (childKey === "__proto__" || grandChildKey === "__proto__")
59
- return;
54
+ if (childKey === "__proto__" || grandChildKey === "__proto__") return;
60
55
  let childInParent = parentState[childKey];
61
- if (!childInParent)
62
- childInParent = parentState[childKey] = {};
63
- if (!childInParent[grandChildKey])
64
- childInParent[grandChildKey] = {};
56
+ if (!childInParent) childInParent = parentState[childKey] = {};
57
+ if (!childInParent[grandChildKey]) childInParent[grandChildKey] = {};
65
58
  stateKey = grandChildKey;
66
59
  parentState = childInParent;
67
60
  }
68
- if (options.returnParent)
69
- return parentState;
61
+ if (options.returnParent) return parentState;
70
62
  return parentState[stateKey];
71
63
  };
72
64
  const findInheritedState = (element, parent, options = {}) => {
73
65
  const ref = element.__ref;
74
66
  let stateKey = ref.__state;
75
- if (!checkIfInherits(element))
76
- return;
67
+ if (!checkIfInherits(element)) return;
77
68
  const rootState = getRootStateInKey(stateKey, parent.state);
78
69
  let parentState = parent.state;
79
70
  if (rootState) {
@@ -86,15 +77,13 @@ const findInheritedState = (element, parent, options = {}) => {
86
77
  stateKey = stateKey.replaceAll("../", "");
87
78
  }
88
79
  }
89
- if (!parentState)
90
- return;
80
+ if (!parentState) return;
91
81
  return getChildStateInKey(stateKey, parentState, options);
92
82
  };
93
83
  const createInheritedState = (element, parent) => {
94
84
  const ref = element.__ref;
95
85
  const inheritedState = findInheritedState(element, parent);
96
- if ((0, import_utils.isUndefined)(inheritedState))
97
- return element.state;
86
+ if ((0, import_utils.isUndefined)(inheritedState)) return element.state;
98
87
  if ((0, import_utils.is)(inheritedState)("object", "array")) {
99
88
  return (0, import_utils.deepClone)(inheritedState, { exclude: import_ignore.IGNORE_STATE_PARAMS });
100
89
  } else if ((0, import_utils.is)(inheritedState)("string", "number", "boolean")) {
@@ -106,13 +95,11 @@ const createInheritedState = (element, parent) => {
106
95
  const checkIfInherits = (element) => {
107
96
  const ref = element.__ref;
108
97
  const stateKey = ref.__state;
109
- if (stateKey && (0, import_utils.is)(stateKey)("number", "string", "boolean"))
110
- return true;
98
+ if (stateKey && (0, import_utils.is)(stateKey)("number", "string", "boolean")) return true;
111
99
  return false;
112
100
  };
113
101
  const isState = function(state) {
114
- if (!(0, import_utils.isObjectLike)(state))
115
- return false;
102
+ if (!(0, import_utils.isObjectLike)(state)) return false;
116
103
  return state.update && state.parse && state.clean && state.create && state.parent && state.destroy && state.rootUpdate && state.parentUpdate && state.keys && state.values && state.toggle && state.replace && state.quietUpdate && state.quietReplace && state.add && state.apply && state.applyReplace && state.setByPath && state.setPathCollection && state.removeByPath && state.removePathCollection && state.getByPath && state.applyFunction && state.__element && state.__children;
117
104
  };
118
105
  const createNestedObjectByKeyPath = (path, value) => {
@@ -109,14 +109,12 @@ const destroy = function(options = {}) {
109
109
  };
110
110
  const parentUpdate = function(obj, options = {}) {
111
111
  const state = this;
112
- if (!state || !state.parent)
113
- return;
112
+ if (!state || !state.parent) return;
114
113
  return state.parent.update(obj, { isHoisted: true, ...options });
115
114
  };
116
115
  const rootUpdate = function(obj, options = {}) {
117
116
  const state = this;
118
- if (!state)
119
- return;
117
+ if (!state) return;
120
118
  const rootState = state.__element.__ref.root.state;
121
119
  return rootState.update(obj, { isHoisted: false, ...options });
122
120
  };
@@ -136,12 +134,9 @@ const toggle = function(key, options = {}) {
136
134
  };
137
135
  const remove = function(key, options = {}) {
138
136
  const state = this;
139
- if ((0, import_utils.isArray)(state))
140
- (0, import_utils.removeFromArray)(state, key);
141
- if ((0, import_utils.isObject)(state))
142
- (0, import_utils.removeFromObject)(state, key);
143
- if (options.applyReset)
144
- return state.set(state.parse(), { replace: true, ...options });
137
+ if ((0, import_utils.isArray)(state)) (0, import_utils.removeFromArray)(state, key);
138
+ if ((0, import_utils.isObject)(state)) (0, import_utils.removeFromObject)(state, key);
139
+ if (options.applyReset) return state.set(state.parse(), { replace: true, ...options });
145
140
  return state.update();
146
141
  };
147
142
  const set = function(val, options = {}) {
@@ -154,8 +149,7 @@ const setByPath = function(path, val, options = {}) {
154
149
  const value = (0, import_utils.deepClone)(val);
155
150
  (0, import_utils.setInObjectByPath)(state, path, val);
156
151
  const update = (0, import_utils.createNestedObject)(path, value);
157
- if (options.preventStateUpdate)
158
- return update;
152
+ if (options.preventStateUpdate) return update;
159
153
  return state.update(update, options);
160
154
  };
161
155
  const setPathCollection = function(changes, options = {}) {
@@ -174,8 +168,7 @@ const setPathCollection = function(changes, options = {}) {
174
168
  const removeByPath = function(path, options = {}) {
175
169
  const state = this;
176
170
  (0, import_utils.removeNestedKeyByPath)(state, path);
177
- if (options.preventUpdate)
178
- return path;
171
+ if (options.preventUpdate) return path;
179
172
  return state.update({}, options);
180
173
  };
181
174
  const removePathCollection = function(changes, options = {}) {
@@ -36,27 +36,21 @@ const STATE_UPDATE_OPTIONS = {
36
36
  const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
37
37
  const state = this;
38
38
  const element = state.__element;
39
- if (options.onEach)
40
- options.onEach(element, state, element.context, options);
41
- if (!options.updateByState)
42
- (0, import_utils.merge)(options, STATE_UPDATE_OPTIONS);
43
- if (!state.__element)
44
- (0, import_report.report)("ElementOnStateIsNotDefined");
39
+ if (options.onEach) options.onEach(element, state, element.context, options);
40
+ if (!options.updateByState) (0, import_utils.merge)(options, STATE_UPDATE_OPTIONS);
41
+ if (!state.__element) (0, import_report.report)("ElementOnStateIsNotDefined");
45
42
  if (options.preventInheritAtCurrentState === true) {
46
43
  options.preventInheritAtCurrentState = state;
47
- } else if (options.preventInheritAtCurrentState)
48
- return;
44
+ } else if (options.preventInheritAtCurrentState) return;
49
45
  if (!options.preventBeforeStateUpdateListener) {
50
46
  const beforeStateUpdateReturns = await (0, import_event.triggerEventOnUpdate)("beforeStateUpdate", obj, element, options);
51
- if (beforeStateUpdateReturns === false)
52
- return element;
47
+ if (beforeStateUpdateReturns === false) return element;
53
48
  }
54
49
  applyOverwrite(state, obj, options);
55
- const updateIsHoisted = hoistStateUpdate(state, obj, options);
56
- if (updateIsHoisted)
57
- return state;
58
- updateDependentState(state, obj, options);
59
- applyElementUpdate(state, obj, options);
50
+ const updateIsHoisted = await hoistStateUpdate(state, obj, options);
51
+ if (updateIsHoisted) return state;
52
+ await updateDependentState(state, obj, options);
53
+ await applyElementUpdate(state, obj, options);
60
54
  if (!options.preventStateUpdateListener) {
61
55
  await (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
62
56
  }
@@ -64,8 +58,7 @@ const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
64
58
  };
65
59
  const applyOverwrite = (state, obj, options) => {
66
60
  const { overwrite } = options;
67
- if (!overwrite)
68
- return;
61
+ if (!overwrite) return;
69
62
  const shallow = overwrite === "shallow" || overwrite === "shallow-once";
70
63
  const merge2 = overwrite === "merge";
71
64
  if (merge2) {
@@ -73,22 +66,19 @@ const applyOverwrite = (state, obj, options) => {
73
66
  return;
74
67
  }
75
68
  const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
76
- if (options.overwrite === "shallow-once")
77
- options.overwrite = true;
69
+ if (options.overwrite === "shallow-once") options.overwrite = true;
78
70
  overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
79
71
  };
80
- const hoistStateUpdate = (state, obj, options) => {
72
+ const hoistStateUpdate = async (state, obj, options) => {
81
73
  const element = state.__element;
82
74
  const { parent, __ref: ref } = element;
83
75
  const stateKey = ref == null ? void 0 : ref.__state;
84
76
  const stateType = ref == null ? void 0 : ref.__stateType;
85
- if (!stateKey)
86
- return;
77
+ if (!stateKey) return;
87
78
  const asksForInherit = (0, import_inherit.checkIfInherits)(element);
88
79
  const inheritedState = (0, import_inherit.findInheritedState)(element, parent, { returnParent: true });
89
80
  const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
90
- if (!shouldPropagateState)
91
- return;
81
+ if (!shouldPropagateState) return;
92
82
  const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
93
83
  const value = isStringState ? state.value : state.parse();
94
84
  const passedValue = isStringState ? state.value : obj;
@@ -96,9 +86,8 @@ const hoistStateUpdate = (state, obj, options) => {
96
86
  const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
97
87
  const changesValue = (0, import_inherit.createNestedObjectByKeyPath)(stateKey, passedValue);
98
88
  const targetParent = findRootState || findGrandParentState || parent.state;
99
- if (options.replace)
100
- (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
101
- targetParent.update(changesValue, {
89
+ if (options.replace) (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
90
+ await targetParent.update(changesValue, {
102
91
  execStateFunction: false,
103
92
  isHoisted: true,
104
93
  preventUpdate: options.preventHoistElementUpdate,
@@ -107,27 +96,26 @@ const hoistStateUpdate = (state, obj, options) => {
107
96
  });
108
97
  const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
109
98
  if (!options.preventStateUpdateListener && hasNotUpdated) {
110
- (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
99
+ await (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
111
100
  }
112
101
  return true;
113
102
  };
114
- const updateDependentState = (state, obj, options) => {
115
- if (!state.__depends)
116
- return;
103
+ const updateDependentState = async (state, obj, options) => {
104
+ if (!state.__depends) return;
117
105
  for (const el in state.__depends) {
118
106
  const dependentState = state.__depends[el];
119
- dependentState.clean().update(state.parse(), options);
107
+ await dependentState.clean().update(state.parse(), options);
120
108
  }
121
109
  };
122
- const applyElementUpdate = (state, obj, options) => {
110
+ const applyElementUpdate = async (state, obj, options) => {
123
111
  const element = state.__element;
124
112
  if (options.preventUpdate !== true) {
125
- element.update({}, {
113
+ await element.update({}, {
126
114
  ...options,
127
115
  updateByState: true
128
116
  });
129
117
  } else if (options.preventUpdate === "recursive") {
130
- element.update({}, {
118
+ await element.update({}, {
131
119
  ...options,
132
120
  isHoisted: false,
133
121
  updateByState: true,
@@ -53,29 +53,24 @@ const createState = async function(element, parent, options) {
53
53
  };
54
54
  const applyInitialState = async function(element, parent, options) {
55
55
  const objectizeState = await checkForTypes(element);
56
- if (objectizeState === false)
57
- return parent.state || {};
58
- else
59
- element.state = objectizeState;
60
- const whatInitReturns = triggerEventOn("stateInit", element, options);
61
- if (whatInitReturns === false)
62
- return element.state;
56
+ if (objectizeState === false) return parent.state || {};
57
+ else element.state = objectizeState;
58
+ const whatInitReturns = await triggerEventOn("stateInit", element, options);
59
+ if (whatInitReturns === false) return element.state;
63
60
  if (checkIfInherits(element)) {
64
61
  const inheritedState = createInheritedState(element, parent);
65
62
  element.state = isUndefined(inheritedState) ? {} : inheritedState;
66
63
  }
67
64
  const dependentState = applyDependentState(element, element.state || parent.state || {});
68
- if (dependentState)
69
- element.state = dependentState;
65
+ if (dependentState) element.state = dependentState;
70
66
  applyMethods(element);
71
- triggerEventOn("stateCreated", element);
67
+ await triggerEventOn("stateCreated", element);
72
68
  return element.state;
73
69
  };
74
70
  const applyDependentState = (element, state) => {
75
71
  const { __ref, ref, __element } = state;
76
72
  const origState = exec(__ref || ref || (__element == null ? void 0 : __element.state), element);
77
- if (!origState)
78
- return;
73
+ if (!origState) return;
79
74
  const dependentState = deepClone(origState, IGNORE_STATE_PARAMS);
80
75
  const newDepends = { [element.key]: dependentState };
81
76
  const __depends = isObject(origState.__depends) ? __spreadValues(__spreadValues({}, origState.__depends), newDepends) : newDepends;
@@ -1,20 +1,16 @@
1
1
  import { deepClone, is, isObjectLike, isUndefined } from "@domql/utils";
2
2
  import { IGNORE_STATE_PARAMS } from "./ignore.js";
3
3
  const getRootStateInKey = (stateKey, parentState) => {
4
- if (!stateKey.includes("~/"))
5
- return;
4
+ if (!stateKey.includes("~/")) return;
6
5
  const arr = stateKey.split("~/");
7
- if (arr.length > 1)
8
- return parentState.root;
6
+ if (arr.length > 1) return parentState.root;
9
7
  };
10
8
  const getParentStateInKey = (stateKey, parentState) => {
11
- if (!stateKey.includes("../"))
12
- return;
9
+ if (!stateKey.includes("../")) return;
13
10
  const arr = stateKey.split("../");
14
11
  const arrLength = arr.length - 1;
15
12
  for (let i = 0; i < arrLength; i++) {
16
- if (!parentState.parent)
17
- return null;
13
+ if (!parentState.parent) return null;
18
14
  parentState = parentState.parent;
19
15
  }
20
16
  return parentState;
@@ -25,25 +21,20 @@ const getChildStateInKey = (stateKey, parentState, options = {}) => {
25
21
  for (let i = 0; i < arrLength; i++) {
26
22
  const childKey = arr[i];
27
23
  const grandChildKey = arr[i + 1];
28
- if (childKey === "__proto__" || grandChildKey === "__proto__")
29
- return;
24
+ if (childKey === "__proto__" || grandChildKey === "__proto__") return;
30
25
  let childInParent = parentState[childKey];
31
- if (!childInParent)
32
- childInParent = parentState[childKey] = {};
33
- if (!childInParent[grandChildKey])
34
- childInParent[grandChildKey] = {};
26
+ if (!childInParent) childInParent = parentState[childKey] = {};
27
+ if (!childInParent[grandChildKey]) childInParent[grandChildKey] = {};
35
28
  stateKey = grandChildKey;
36
29
  parentState = childInParent;
37
30
  }
38
- if (options.returnParent)
39
- return parentState;
31
+ if (options.returnParent) return parentState;
40
32
  return parentState[stateKey];
41
33
  };
42
34
  const findInheritedState = (element, parent, options = {}) => {
43
35
  const ref = element.__ref;
44
36
  let stateKey = ref.__state;
45
- if (!checkIfInherits(element))
46
- return;
37
+ if (!checkIfInherits(element)) return;
47
38
  const rootState = getRootStateInKey(stateKey, parent.state);
48
39
  let parentState = parent.state;
49
40
  if (rootState) {
@@ -56,15 +47,13 @@ const findInheritedState = (element, parent, options = {}) => {
56
47
  stateKey = stateKey.replaceAll("../", "");
57
48
  }
58
49
  }
59
- if (!parentState)
60
- return;
50
+ if (!parentState) return;
61
51
  return getChildStateInKey(stateKey, parentState, options);
62
52
  };
63
53
  const createInheritedState = (element, parent) => {
64
54
  const ref = element.__ref;
65
55
  const inheritedState = findInheritedState(element, parent);
66
- if (isUndefined(inheritedState))
67
- return element.state;
56
+ if (isUndefined(inheritedState)) return element.state;
68
57
  if (is(inheritedState)("object", "array")) {
69
58
  return deepClone(inheritedState, { exclude: IGNORE_STATE_PARAMS });
70
59
  } else if (is(inheritedState)("string", "number", "boolean")) {
@@ -76,13 +65,11 @@ const createInheritedState = (element, parent) => {
76
65
  const checkIfInherits = (element) => {
77
66
  const ref = element.__ref;
78
67
  const stateKey = ref.__state;
79
- if (stateKey && is(stateKey)("number", "string", "boolean"))
80
- return true;
68
+ if (stateKey && is(stateKey)("number", "string", "boolean")) return true;
81
69
  return false;
82
70
  };
83
71
  const isState = function(state) {
84
- if (!isObjectLike(state))
85
- return false;
72
+ if (!isObjectLike(state)) return false;
86
73
  return state.update && state.parse && state.clean && state.create && state.parent && state.destroy && state.rootUpdate && state.parentUpdate && state.keys && state.values && state.toggle && state.replace && state.quietUpdate && state.quietReplace && state.add && state.apply && state.applyReplace && state.setByPath && state.setPathCollection && state.removeByPath && state.removePathCollection && state.getByPath && state.applyFunction && state.__element && state.__children;
87
74
  };
88
75
  const createNestedObjectByKeyPath = (path, value) => {
@@ -93,14 +93,12 @@ const destroy = function(options = {}) {
93
93
  };
94
94
  const parentUpdate = function(obj, options = {}) {
95
95
  const state = this;
96
- if (!state || !state.parent)
97
- return;
96
+ if (!state || !state.parent) return;
98
97
  return state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
99
98
  };
100
99
  const rootUpdate = function(obj, options = {}) {
101
100
  const state = this;
102
- if (!state)
103
- return;
101
+ if (!state) return;
104
102
  const rootState = state.__element.__ref.root.state;
105
103
  return rootState.update(obj, __spreadValues({ isHoisted: false }, options));
106
104
  };
@@ -120,12 +118,9 @@ const toggle = function(key, options = {}) {
120
118
  };
121
119
  const remove = function(key, options = {}) {
122
120
  const state = this;
123
- if (isArray(state))
124
- removeFromArray(state, key);
125
- if (isObject(state))
126
- removeFromObject(state, key);
127
- if (options.applyReset)
128
- return state.set(state.parse(), __spreadValues({ replace: true }, options));
121
+ if (isArray(state)) removeFromArray(state, key);
122
+ if (isObject(state)) removeFromObject(state, key);
123
+ if (options.applyReset) return state.set(state.parse(), __spreadValues({ replace: true }, options));
129
124
  return state.update();
130
125
  };
131
126
  const set = function(val, options = {}) {
@@ -138,8 +133,7 @@ const setByPath = function(path, val, options = {}) {
138
133
  const value = deepClone(val);
139
134
  setInObjectByPath(state, path, val);
140
135
  const update = createNestedObject(path, value);
141
- if (options.preventStateUpdate)
142
- return update;
136
+ if (options.preventStateUpdate) return update;
143
137
  return state.update(update, options);
144
138
  };
145
139
  const setPathCollection = function(changes, options = {}) {
@@ -158,8 +152,7 @@ const setPathCollection = function(changes, options = {}) {
158
152
  const removeByPath = function(path, options = {}) {
159
153
  const state = this;
160
154
  removeNestedKeyByPath(state, path);
161
- if (options.preventUpdate)
162
- return path;
155
+ if (options.preventUpdate) return path;
163
156
  return state.update({}, options);
164
157
  };
165
158
  const removePathCollection = function(changes, options = {}) {
@@ -32,27 +32,21 @@ const STATE_UPDATE_OPTIONS = {
32
32
  const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
33
33
  const state = this;
34
34
  const element = state.__element;
35
- if (options.onEach)
36
- options.onEach(element, state, element.context, options);
37
- if (!options.updateByState)
38
- merge(options, STATE_UPDATE_OPTIONS);
39
- if (!state.__element)
40
- report("ElementOnStateIsNotDefined");
35
+ if (options.onEach) options.onEach(element, state, element.context, options);
36
+ if (!options.updateByState) merge(options, STATE_UPDATE_OPTIONS);
37
+ if (!state.__element) report("ElementOnStateIsNotDefined");
41
38
  if (options.preventInheritAtCurrentState === true) {
42
39
  options.preventInheritAtCurrentState = state;
43
- } else if (options.preventInheritAtCurrentState)
44
- return;
40
+ } else if (options.preventInheritAtCurrentState) return;
45
41
  if (!options.preventBeforeStateUpdateListener) {
46
42
  const beforeStateUpdateReturns = await triggerEventOnUpdate("beforeStateUpdate", obj, element, options);
47
- if (beforeStateUpdateReturns === false)
48
- return element;
43
+ if (beforeStateUpdateReturns === false) return element;
49
44
  }
50
45
  applyOverwrite(state, obj, options);
51
- const updateIsHoisted = hoistStateUpdate(state, obj, options);
52
- if (updateIsHoisted)
53
- return state;
54
- updateDependentState(state, obj, options);
55
- applyElementUpdate(state, obj, options);
46
+ const updateIsHoisted = await hoistStateUpdate(state, obj, options);
47
+ if (updateIsHoisted) return state;
48
+ await updateDependentState(state, obj, options);
49
+ await applyElementUpdate(state, obj, options);
56
50
  if (!options.preventStateUpdateListener) {
57
51
  await triggerEventOnUpdate("stateUpdate", obj, element, options);
58
52
  }
@@ -60,8 +54,7 @@ const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
60
54
  };
61
55
  const applyOverwrite = (state, obj, options) => {
62
56
  const { overwrite } = options;
63
- if (!overwrite)
64
- return;
57
+ if (!overwrite) return;
65
58
  const shallow = overwrite === "shallow" || overwrite === "shallow-once";
66
59
  const merge2 = overwrite === "merge";
67
60
  if (merge2) {
@@ -69,22 +62,19 @@ const applyOverwrite = (state, obj, options) => {
69
62
  return;
70
63
  }
71
64
  const overwriteFunc = shallow ? overwriteShallow : overwriteDeep;
72
- if (options.overwrite === "shallow-once")
73
- options.overwrite = true;
65
+ if (options.overwrite === "shallow-once") options.overwrite = true;
74
66
  overwriteFunc(state, obj, IGNORE_STATE_PARAMS);
75
67
  };
76
- const hoistStateUpdate = (state, obj, options) => {
68
+ const hoistStateUpdate = async (state, obj, options) => {
77
69
  const element = state.__element;
78
70
  const { parent, __ref: ref } = element;
79
71
  const stateKey = ref == null ? void 0 : ref.__state;
80
72
  const stateType = ref == null ? void 0 : ref.__stateType;
81
- if (!stateKey)
82
- return;
73
+ if (!stateKey) return;
83
74
  const asksForInherit = checkIfInherits(element);
84
75
  const inheritedState = findInheritedState(element, parent, { returnParent: true });
85
76
  const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
86
- if (!shouldPropagateState)
87
- return;
77
+ if (!shouldPropagateState) return;
88
78
  const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
89
79
  const value = isStringState ? state.value : state.parse();
90
80
  const passedValue = isStringState ? state.value : obj;
@@ -92,9 +82,8 @@ const hoistStateUpdate = (state, obj, options) => {
92
82
  const findGrandParentState = getParentStateInKey(stateKey, parent.state);
93
83
  const changesValue = createNestedObjectByKeyPath(stateKey, passedValue);
94
84
  const targetParent = findRootState || findGrandParentState || parent.state;
95
- if (options.replace)
96
- overwriteDeep(targetParent, changesValue || value);
97
- targetParent.update(changesValue, __spreadValues({
85
+ if (options.replace) overwriteDeep(targetParent, changesValue || value);
86
+ await targetParent.update(changesValue, __spreadValues({
98
87
  execStateFunction: false,
99
88
  isHoisted: true,
100
89
  preventUpdate: options.preventHoistElementUpdate,
@@ -102,26 +91,25 @@ const hoistStateUpdate = (state, obj, options) => {
102
91
  }, options));
103
92
  const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
104
93
  if (!options.preventStateUpdateListener && hasNotUpdated) {
105
- triggerEventOnUpdate("stateUpdate", obj, element, options);
94
+ await triggerEventOnUpdate("stateUpdate", obj, element, options);
106
95
  }
107
96
  return true;
108
97
  };
109
- const updateDependentState = (state, obj, options) => {
110
- if (!state.__depends)
111
- return;
98
+ const updateDependentState = async (state, obj, options) => {
99
+ if (!state.__depends) return;
112
100
  for (const el in state.__depends) {
113
101
  const dependentState = state.__depends[el];
114
- dependentState.clean().update(state.parse(), options);
102
+ await dependentState.clean().update(state.parse(), options);
115
103
  }
116
104
  };
117
- const applyElementUpdate = (state, obj, options) => {
105
+ const applyElementUpdate = async (state, obj, options) => {
118
106
  const element = state.__element;
119
107
  if (options.preventUpdate !== true) {
120
- element.update({}, __spreadProps(__spreadValues({}, options), {
108
+ await element.update({}, __spreadProps(__spreadValues({}, options), {
121
109
  updateByState: true
122
110
  }));
123
111
  } else if (options.preventUpdate === "recursive") {
124
- element.update({}, __spreadProps(__spreadValues({}, options), {
112
+ await element.update({}, __spreadProps(__spreadValues({}, options), {
125
113
  isHoisted: false,
126
114
  updateByState: true,
127
115
  preventUpdate: true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.5.203",
3
+ "version": "2.5.206",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -32,5 +32,5 @@
32
32
  "@domql/report": "^2.5.203",
33
33
  "@domql/utils": "^2.5.203"
34
34
  },
35
- "gitHead": "f8c712416647ab3789253b7176eaa762627fc677"
35
+ "gitHead": "8d50915b5d72df7c8bdf35096b0a9cf50dc4d22e"
36
36
  }
package/updateState.js CHANGED
@@ -33,12 +33,12 @@ export const updateState = async function (obj, options = STATE_UPDATE_OPTIONS)
33
33
  }
34
34
 
35
35
  applyOverwrite(state, obj, options)
36
- const updateIsHoisted = hoistStateUpdate(state, obj, options)
36
+ const updateIsHoisted = await hoistStateUpdate(state, obj, options)
37
37
  if (updateIsHoisted) return state
38
38
 
39
- updateDependentState(state, obj, options)
39
+ await updateDependentState(state, obj, options)
40
40
 
41
- applyElementUpdate(state, obj, options)
41
+ await applyElementUpdate(state, obj, options)
42
42
 
43
43
  if (!options.preventStateUpdateListener) {
44
44
  await triggerEventOnUpdate('stateUpdate', obj, element, options)
@@ -64,7 +64,7 @@ const applyOverwrite = (state, obj, options) => {
64
64
  overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
65
65
  }
66
66
 
67
- const hoistStateUpdate = (state, obj, options) => {
67
+ const hoistStateUpdate = async (state, obj, options) => {
68
68
  const element = state.__element
69
69
  const { parent, __ref: ref } = element
70
70
 
@@ -86,7 +86,7 @@ const hoistStateUpdate = (state, obj, options) => {
86
86
  const changesValue = createNestedObjectByKeyPath(stateKey, passedValue)
87
87
  const targetParent = findRootState || findGrandParentState || parent.state
88
88
  if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createNestedObjectByKeyPath
89
- targetParent.update(changesValue, {
89
+ await targetParent.update(changesValue, {
90
90
  execStateFunction: false,
91
91
  isHoisted: true,
92
92
  preventUpdate: options.preventHoistElementUpdate,
@@ -95,28 +95,28 @@ const hoistStateUpdate = (state, obj, options) => {
95
95
  })
96
96
  const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate
97
97
  if (!options.preventStateUpdateListener && hasNotUpdated) {
98
- triggerEventOnUpdate('stateUpdate', obj, element, options)
98
+ await triggerEventOnUpdate('stateUpdate', obj, element, options)
99
99
  }
100
100
  return true
101
101
  }
102
102
 
103
- const updateDependentState = (state, obj, options) => {
103
+ const updateDependentState = async (state, obj, options) => {
104
104
  if (!state.__depends) return
105
105
  for (const el in state.__depends) {
106
106
  const dependentState = state.__depends[el]
107
- dependentState.clean().update(state.parse(), options)
107
+ await dependentState.clean().update(state.parse(), options)
108
108
  }
109
109
  }
110
110
 
111
- const applyElementUpdate = (state, obj, options) => {
111
+ const applyElementUpdate = async (state, obj, options) => {
112
112
  const element = state.__element
113
113
  if (options.preventUpdate !== true) {
114
- element.update({}, {
114
+ await element.update({}, {
115
115
  ...options,
116
116
  updateByState: true
117
117
  })
118
118
  } else if (options.preventUpdate === 'recursive') {
119
- element.update({}, {
119
+ await element.update({}, {
120
120
  ...options,
121
121
  isHoisted: false,
122
122
  updateByState: true,