@domql/state 2.5.198 → 2.5.200
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/dist/cjs/create.js +5 -10
- package/dist/cjs/index.js +7 -7
- package/dist/cjs/inherit.js +13 -26
- package/dist/cjs/methods.js +7 -14
- package/dist/cjs/updateState.js +12 -24
- package/dist/esm/create.js +5 -10
- package/dist/esm/inherit.js +13 -26
- package/dist/esm/methods.js +7 -14
- package/dist/esm/updateState.js +12 -24
- package/package.json +4 -4
package/dist/cjs/create.js
CHANGED
|
@@ -34,20 +34,16 @@ 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
|
-
|
|
39
|
-
else
|
|
40
|
-
element.state = objectizeState;
|
|
37
|
+
if (objectizeState === false) return parent.state || {};
|
|
38
|
+
else element.state = objectizeState;
|
|
41
39
|
const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element, options);
|
|
42
|
-
if (whatInitReturns === false)
|
|
43
|
-
return element.state;
|
|
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
48
|
(0, import_event.triggerEventOn)("stateCreated", element);
|
|
53
49
|
return element.state;
|
|
@@ -55,8 +51,7 @@ const applyInitialState = async function(element, parent, options) {
|
|
|
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
|
|
17
|
-
module.exports = __toCommonJS(
|
|
18
|
-
__reExport(
|
|
19
|
-
__reExport(
|
|
20
|
-
__reExport(
|
|
21
|
-
__reExport(
|
|
22
|
-
__reExport(
|
|
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);
|
package/dist/cjs/inherit.js
CHANGED
|
@@ -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
|
-
|
|
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) => {
|
package/dist/cjs/methods.js
CHANGED
|
@@ -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
|
-
|
|
141
|
-
if (
|
|
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 = {}) {
|
package/dist/cjs/updateState.js
CHANGED
|
@@ -36,25 +36,19 @@ 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
|
-
|
|
41
|
-
if (!
|
|
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
50
|
const updateIsHoisted = hoistStateUpdate(state, obj, options);
|
|
56
|
-
if (updateIsHoisted)
|
|
57
|
-
return state;
|
|
51
|
+
if (updateIsHoisted) return state;
|
|
58
52
|
updateDependentState(state, obj, options);
|
|
59
53
|
applyElementUpdate(state, obj, options);
|
|
60
54
|
if (!options.preventStateUpdateListener) {
|
|
@@ -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,8 +66,7 @@ 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
72
|
const hoistStateUpdate = (state, obj, options) => {
|
|
@@ -82,13 +74,11 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
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,8 +86,7 @@ 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);
|
|
89
|
+
if (options.replace) (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
|
|
101
90
|
targetParent.update(changesValue, {
|
|
102
91
|
execStateFunction: false,
|
|
103
92
|
isHoisted: true,
|
|
@@ -112,8 +101,7 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
112
101
|
return true;
|
|
113
102
|
};
|
|
114
103
|
const updateDependentState = (state, obj, options) => {
|
|
115
|
-
if (!state.__depends)
|
|
116
|
-
return;
|
|
104
|
+
if (!state.__depends) return;
|
|
117
105
|
for (const el in state.__depends) {
|
|
118
106
|
const dependentState = state.__depends[el];
|
|
119
107
|
dependentState.clean().update(state.parse(), options);
|
package/dist/esm/create.js
CHANGED
|
@@ -53,20 +53,16 @@ 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
|
-
|
|
58
|
-
else
|
|
59
|
-
element.state = objectizeState;
|
|
56
|
+
if (objectizeState === false) return parent.state || {};
|
|
57
|
+
else element.state = objectizeState;
|
|
60
58
|
const whatInitReturns = triggerEventOn("stateInit", element, options);
|
|
61
|
-
if (whatInitReturns === false)
|
|
62
|
-
return element.state;
|
|
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
67
|
triggerEventOn("stateCreated", element);
|
|
72
68
|
return element.state;
|
|
@@ -74,8 +70,7 @@ const applyInitialState = async function(element, parent, options) {
|
|
|
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;
|
package/dist/esm/inherit.js
CHANGED
|
@@ -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
|
-
|
|
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) => {
|
package/dist/esm/methods.js
CHANGED
|
@@ -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
|
-
|
|
125
|
-
if (
|
|
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 = {}) {
|
package/dist/esm/updateState.js
CHANGED
|
@@ -32,25 +32,19 @@ 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
|
-
|
|
37
|
-
if (!
|
|
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
46
|
const updateIsHoisted = hoistStateUpdate(state, obj, options);
|
|
52
|
-
if (updateIsHoisted)
|
|
53
|
-
return state;
|
|
47
|
+
if (updateIsHoisted) return state;
|
|
54
48
|
updateDependentState(state, obj, options);
|
|
55
49
|
applyElementUpdate(state, obj, options);
|
|
56
50
|
if (!options.preventStateUpdateListener) {
|
|
@@ -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,8 +62,7 @@ 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
68
|
const hoistStateUpdate = (state, obj, options) => {
|
|
@@ -78,13 +70,11 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
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,8 +82,7 @@ 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);
|
|
85
|
+
if (options.replace) overwriteDeep(targetParent, changesValue || value);
|
|
97
86
|
targetParent.update(changesValue, __spreadValues({
|
|
98
87
|
execStateFunction: false,
|
|
99
88
|
isHoisted: true,
|
|
@@ -107,8 +96,7 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
107
96
|
return true;
|
|
108
97
|
};
|
|
109
98
|
const updateDependentState = (state, obj, options) => {
|
|
110
|
-
if (!state.__depends)
|
|
111
|
-
return;
|
|
99
|
+
if (!state.__depends) return;
|
|
112
100
|
for (const el in state.__depends) {
|
|
113
101
|
const dependentState = state.__depends[el];
|
|
114
102
|
dependentState.clean().update(state.parse(), options);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.200",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"prepublish": "rimraf -I dist && npm run build && npm run copy:package:cjs"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@domql/event": "^2.5.
|
|
31
|
+
"@domql/event": "^2.5.200",
|
|
32
32
|
"@domql/report": "^2.5.186",
|
|
33
|
-
"@domql/utils": "^2.5.
|
|
33
|
+
"@domql/utils": "^2.5.200"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "0afb63ec375f0526f47ff300885de393138b01e8"
|
|
36
36
|
}
|