@domql/state 3.1.2 → 3.2.7

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.
@@ -1,22 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
1
  import {
21
2
  isArray,
22
3
  deepClone,
@@ -40,33 +21,33 @@ const parse = function() {
40
21
  if (isObject(state)) {
41
22
  const obj = {};
42
23
  for (const param in state) {
43
- if (!STATE_METHODS.includes(param)) {
24
+ if (!STATE_METHODS.has(param)) {
44
25
  obj[param] = state[param];
45
26
  }
46
27
  }
47
28
  return obj;
48
29
  } else if (isArray(state)) {
49
- return state.filter((item) => !STATE_METHODS.includes(item));
30
+ return state.filter((item) => !STATE_METHODS.has(item));
50
31
  }
51
32
  };
52
- const clean = async function(options = {}) {
33
+ const clean = function(options = {}) {
53
34
  const state = this;
54
35
  for (const param in state) {
55
- if (!STATE_METHODS.includes(param) && Object.hasOwnProperty.call(state, param)) {
36
+ if (!STATE_METHODS.has(param) && Object.prototype.hasOwnProperty.call(state, param)) {
56
37
  delete state[param];
57
38
  }
58
39
  }
59
40
  if (!options.preventStateUpdate) {
60
- await state.update(state, __spreadValues({ replace: true }, options));
41
+ state.update(state, { replace: true, ...options });
61
42
  }
62
43
  return state;
63
44
  };
64
- const destroy = async function(options = {}) {
45
+ const destroy = function(options = {}) {
65
46
  const state = this;
66
47
  const element = state.__element;
67
48
  const stateKey = element.__ref.__state;
68
49
  if (isString(stateKey)) {
69
- element.parent.state.remove(stateKey, __spreadValues({ isHoisted: true }, options));
50
+ element.parent.state.remove(stateKey, { isHoisted: true, ...options });
70
51
  return element.state;
71
52
  }
72
53
  delete element.state;
@@ -94,132 +75,132 @@ const destroy = async function(options = {}) {
94
75
  }
95
76
  }
96
77
  }
97
- await element.state.update({}, __spreadValues({ isHoisted: true }, options));
78
+ element.state.update({}, { isHoisted: true, ...options });
98
79
  return element.state;
99
80
  };
100
- const parentUpdate = async function(obj, options = {}) {
81
+ const parentUpdate = function(obj, options = {}) {
101
82
  const state = this;
102
83
  if (!state || !state.parent) return;
103
- return await state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
84
+ return state.parent.update(obj, { isHoisted: true, ...options });
104
85
  };
105
- const rootUpdate = async function(obj, options = {}) {
86
+ const rootUpdate = function(obj, options = {}) {
106
87
  const state = this;
107
88
  if (!state) return;
108
89
  const rootState = state.__element.__ref.root.state;
109
- return await rootState.update(obj, __spreadValues({ isHoisted: false }, options));
90
+ return rootState.update(obj, { isHoisted: false, ...options });
110
91
  };
111
- const add = async function(value, options = {}) {
92
+ const add = function(value, options = {}) {
112
93
  const state = this;
113
94
  if (isArray(state)) {
114
95
  state.push(value);
115
- await state.update(state.parse(), __spreadValues({ overwrite: true }, options));
96
+ state.update(state.parse(), { overwrite: true, ...options });
116
97
  } else if (isObject(state)) {
117
98
  const key = Object.keys(state).length;
118
- await state.update({ [key]: value }, options);
99
+ state.update({ [key]: value }, options);
119
100
  }
120
101
  };
121
- const toggle = async function(key, options = {}) {
102
+ const toggle = function(key, options = {}) {
122
103
  const state = this;
123
- await state.update({ [key]: !state[key] }, options);
104
+ state.update({ [key]: !state[key] }, options);
124
105
  };
125
- const remove = async function(key, options = {}) {
106
+ const remove = function(key, options = {}) {
126
107
  const state = this;
127
108
  if (isArray(state)) removeFromArray(state, key);
128
- if (isObject(state)) removeFromObject(state, key);
109
+ else if (isObject(state)) removeFromObject(state, key);
129
110
  if (options.applyReset) {
130
- return await state.set(state.parse(), __spreadValues({ replace: true }, options));
111
+ return state.set(state.parse(), { replace: true, ...options });
131
112
  }
132
- return await state.update({}, options);
113
+ return state.update({}, options);
133
114
  };
134
- const set = async function(val, options = {}) {
115
+ const set = function(val, options = {}) {
135
116
  const state = this;
136
117
  const value = deepClone(val);
137
- const cleanState = await state.clean(__spreadValues({ preventStateUpdate: true }, options));
138
- return await cleanState.update(value, __spreadValues({ replace: true }, options));
118
+ const cleanState = state.clean({ preventStateUpdate: true, ...options });
119
+ return cleanState.update(value, { replace: true, ...options });
139
120
  };
140
- const setByPath = async function(path, val, options = {}) {
121
+ const setByPath = function(path, val, options = {}) {
141
122
  const state = this;
142
123
  const value = deepClone(val);
143
- setInObjectByPath(state, path, val);
124
+ if (!options.preventReplace) setInObjectByPath(state, path, val);
144
125
  const update = createNestedObject(path, value);
145
126
  if (options.preventStateUpdate) return update;
146
- return await state.update(update, options);
127
+ return state.update(update, options);
147
128
  };
148
- const setPathCollection = async function(changes, options = {}) {
129
+ const setPathCollection = function(changes, options = {}) {
149
130
  const state = this;
150
- const update = await changes.reduce(async (promise, change) => {
151
- const acc = await promise;
131
+ const update = changes.reduce((acc, change) => {
152
132
  if (change[0] === "update") {
153
133
  const result = setByPath.call(state, change[1], change[2], {
154
134
  preventStateUpdate: true
155
135
  });
156
136
  return overwriteDeep(acc, result);
157
137
  } else if (change[0] === "delete") {
158
- await removeByPath.call(state, change[1], __spreadProps(__spreadValues({}, options), {
138
+ removeByPath.call(state, change[1], {
139
+ ...options,
159
140
  preventUpdate: true
160
- }));
141
+ });
161
142
  }
162
143
  return acc;
163
- }, Promise.resolve({}));
144
+ }, {});
164
145
  return state.update(update, options);
165
146
  };
166
- const removeByPath = async function(path, options = {}) {
147
+ const removeByPath = function(path, options = {}) {
167
148
  const state = this;
168
149
  removeNestedKeyByPath(state, path);
169
150
  if (options.preventUpdate) return path;
170
- return await state.update({}, options);
151
+ return state.update({}, options);
171
152
  };
172
- const removePathCollection = async function(changes, options = {}) {
153
+ const removePathCollection = function(changes, options = {}) {
173
154
  const state = this;
174
- changes.forEach(async (item) => {
175
- await removeByPath(item, { preventUpdate: true });
176
- });
177
- return await state.update({}, options);
155
+ for (let i = 0; i < changes.length; i++) {
156
+ removeByPath(changes[i], { preventUpdate: true });
157
+ }
158
+ return state.update({}, options);
178
159
  };
179
160
  const getByPath = function(path, options = {}) {
180
161
  const state = this;
181
162
  return getInObjectByPath(state, path);
182
163
  };
183
- const reset = async function(options = {}) {
164
+ const reset = function(options = {}) {
184
165
  const state = this;
185
166
  const value = deepClone(state.parse());
186
- return await state.set(value, __spreadValues({ replace: true }, options));
167
+ return state.set(value, { replace: true, ...options });
187
168
  };
188
- const apply = async function(func, options = {}) {
169
+ const apply = function(func, options = {}) {
189
170
  const state = this;
190
171
  if (isFunction(func)) {
191
172
  const value = func(state);
192
- return await state.update(value, __spreadValues({ replace: true }, options));
173
+ return state.update(value, { replace: true, ...options });
193
174
  }
194
175
  };
195
- const applyReplace = async function(func, options = {}) {
176
+ const applyReplace = function(func, options = {}) {
196
177
  const state = this;
197
178
  if (isFunction(func)) {
198
179
  const value = func(state);
199
- return await state.replace(value, options);
180
+ return state.replace(value, options);
200
181
  }
201
182
  };
202
- const applyFunction = async function(func, options = {}) {
183
+ const applyFunction = function(func, options = {}) {
203
184
  const state = this;
204
185
  if (isFunction(func)) {
205
- await func(state);
206
- return await state.update(state.parse(), __spreadValues({ replace: true }, options));
186
+ func(state);
187
+ return state.update(state.parse(), { replace: true, ...options });
207
188
  }
208
189
  };
209
- const quietUpdate = async function(obj, options = {}) {
190
+ const quietUpdate = function(obj, options = {}) {
210
191
  const state = this;
211
- return await state.update(obj, __spreadValues({ preventUpdate: true }, options));
192
+ return state.update(obj, { preventUpdate: true, ...options });
212
193
  };
213
- const replace = async function(obj, options = {}) {
194
+ const replace = function(obj, options = {}) {
214
195
  const state = this;
215
196
  for (const param in obj) {
216
197
  state[param] = obj[param];
217
198
  }
218
- return await state.update(obj, options);
199
+ return state.update(obj, options);
219
200
  };
220
- const quietReplace = async function(obj, options = {}) {
201
+ const quietReplace = function(obj, options = {}) {
221
202
  const state = this;
222
- return await state.replace(obj, __spreadValues({ preventUpdate: true }, options));
203
+ return state.replace(obj, { preventUpdate: true, ...options });
223
204
  };
224
205
  const keys = function(obj, options = {}) {
225
206
  const state = this;
@@ -230,7 +211,6 @@ const values = function(obj, options = {}) {
230
211
  return Object.values(state);
231
212
  };
232
213
  const applyStateMethods = (element) => {
233
- var _a;
234
214
  const state = element.state;
235
215
  const ref = element.__ref;
236
216
  const proto = {
@@ -252,7 +232,7 @@ const applyStateMethods = (element) => {
252
232
  replace: replace.bind(state),
253
233
  quietReplace: quietReplace.bind(state),
254
234
  reset: reset.bind(state),
255
- parent: ((_a = element.parent) == null ? void 0 : _a.state) || state,
235
+ parent: element.parent?.state || state,
256
236
  setByPath: setByPath.bind(state),
257
237
  setPathCollection: setPathCollection.bind(state),
258
238
  removeByPath: removeByPath.bind(state),
@@ -262,7 +242,7 @@ const applyStateMethods = (element) => {
262
242
  values: values.bind(state),
263
243
  __element: element,
264
244
  __children: {},
265
- root: (ref == null ? void 0 : ref.root) ? ref.root.state : state
245
+ root: ref?.root ? ref.root.state : state
266
246
  };
267
247
  if (isArray(state)) {
268
248
  addProtoToArray(state, proto);
@@ -1,24 +1,4 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
1
  import { report } from "@domql/report";
21
- import { triggerEventOnUpdate } from "@domql/event";
22
2
  import {
23
3
  checkIfInherits,
24
4
  createNestedObjectByKeyPath,
@@ -27,7 +7,8 @@ import {
27
7
  getRootStateInKey,
28
8
  merge,
29
9
  overwriteDeep,
30
- overwriteState
10
+ overwriteState,
11
+ triggerEventOnUpdate
31
12
  } from "@domql/utils";
32
13
  const STATE_UPDATE_OPTIONS = {
33
14
  overwrite: true,
@@ -36,7 +17,7 @@ const STATE_UPDATE_OPTIONS = {
36
17
  isHoisted: true,
37
18
  execStateFunction: true
38
19
  };
39
- const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
20
+ const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
40
21
  const state = this;
41
22
  const element = state.__element;
42
23
  if (options.onEach) options.onEach(element, state, element.context, options);
@@ -46,7 +27,7 @@ const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
46
27
  options.preventInheritAtCurrentState = state;
47
28
  } else if (options.preventInheritAtCurrentState) return;
48
29
  if (!options.preventBeforeStateUpdateListener) {
49
- const beforeStateUpdateReturns = await triggerEventOnUpdate(
30
+ const beforeStateUpdateReturns = triggerEventOnUpdate(
50
31
  "beforeStateUpdate",
51
32
  obj,
52
33
  element,
@@ -55,20 +36,20 @@ const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
55
36
  if (beforeStateUpdateReturns === false) return element;
56
37
  }
57
38
  overwriteState(state, obj, options);
58
- const updateIsHoisted = await hoistStateUpdate(state, obj, options);
39
+ const updateIsHoisted = hoistStateUpdate(state, obj, options);
59
40
  if (updateIsHoisted) return state;
60
- await updateDependentState(state, obj, options);
61
- await applyElementUpdate(state, obj, options);
41
+ updateDependentState(state, obj, options);
42
+ applyElementUpdate(state, obj, options);
62
43
  if (!options.preventStateUpdateListener) {
63
- await triggerEventOnUpdate("stateUpdate", obj, element, options);
44
+ triggerEventOnUpdate("stateUpdate", obj, element, options);
64
45
  }
65
46
  return state;
66
47
  };
67
- const hoistStateUpdate = async (state, obj, options) => {
48
+ const hoistStateUpdate = (state, obj, options) => {
68
49
  const element = state.__element;
69
50
  const { parent, __ref: ref } = element;
70
- const stateKey = ref == null ? void 0 : ref.__state;
71
- const stateType = ref == null ? void 0 : ref.__stateType;
51
+ const stateKey = ref?.__state;
52
+ const stateType = ref?.__stateType;
72
53
  if (!stateKey) return;
73
54
  const asksForInherit = checkIfInherits(element);
74
55
  const inheritedState = findInheritedState(element, parent, {
@@ -84,43 +65,46 @@ const hoistStateUpdate = async (state, obj, options) => {
84
65
  const changesValue = createNestedObjectByKeyPath(stateKey, passedValue);
85
66
  const targetParent = findRootState || findGrandParentState || parent.state;
86
67
  if (options.replace) overwriteDeep(targetParent, changesValue || value);
87
- await targetParent.update(changesValue, __spreadValues({
68
+ targetParent.update(changesValue, {
88
69
  execStateFunction: false,
89
70
  isHoisted: true,
90
71
  preventUpdate: options.preventHoistElementUpdate,
91
- overwrite: !options.replace
92
- }, options));
72
+ overwrite: !options.replace,
73
+ ...options
74
+ });
93
75
  const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
94
76
  if (!options.preventStateUpdateListener && hasNotUpdated) {
95
- await triggerEventOnUpdate("stateUpdate", obj, element, options);
77
+ triggerEventOnUpdate("stateUpdate", obj, element, options);
96
78
  }
97
79
  return true;
98
80
  };
99
- const updateDependentState = async (state, obj, options) => {
81
+ const updateDependentState = (state, obj, options) => {
100
82
  if (!state.__depends) return;
101
83
  for (const el in state.__depends) {
102
84
  const dependentState = state.__depends[el];
103
- const cleanState = await dependentState.clean();
104
- await cleanState.update(state.parse(), options);
85
+ const cleanState = dependentState.clean();
86
+ cleanState.update(state.parse(), options);
105
87
  }
106
88
  };
107
- const applyElementUpdate = async (state, obj, options) => {
89
+ const applyElementUpdate = (state, obj, options) => {
108
90
  const element = state.__element;
109
91
  if (options.preventUpdate !== true) {
110
- await element.update(
92
+ element.update(
111
93
  {},
112
- __spreadProps(__spreadValues({}, options), {
94
+ {
95
+ ...options,
113
96
  updateByState: true
114
- })
97
+ }
115
98
  );
116
99
  } else if (options.preventUpdate === "recursive") {
117
- await element.update(
100
+ element.update(
118
101
  {},
119
- __spreadProps(__spreadValues({}, options), {
102
+ {
103
+ ...options,
120
104
  isHoisted: false,
121
105
  updateByState: true,
122
106
  preventUpdate: true
123
- })
107
+ }
124
108
  );
125
109
  }
126
110
  };