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