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