@domql/state 2.5.198 → 3.0.0
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 +16 -131
- package/dist/cjs/create.js +15 -109
- package/dist/cjs/index.js +5 -7
- package/dist/cjs/methods.js +111 -63
- package/dist/cjs/updateState.js +51 -63
- package/dist/esm/create.js +19 -150
- package/dist/esm/index.js +0 -2
- package/dist/esm/methods.js +116 -64
- package/dist/esm/updateState.js +55 -58
- package/index.js +0 -2
- package/methods.js +117 -53
- package/package.json +5 -5
- package/updateState.js +56 -46
- package/dist/cjs/ignore.js +0 -57
- package/dist/cjs/inherit.js +0 -130
- package/dist/esm/ignore.js +0 -37
- package/dist/esm/inherit.js +0 -110
- package/ignore.js +0 -8
- package/inherit.js +0 -131
package/dist/cjs/updateState.js
CHANGED
|
@@ -18,14 +18,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var updateState_exports = {};
|
|
20
20
|
__export(updateState_exports, {
|
|
21
|
+
hoistStateUpdate: () => hoistStateUpdate,
|
|
21
22
|
updateState: () => updateState
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(updateState_exports);
|
|
24
25
|
var import_report = require("@domql/report");
|
|
25
26
|
var import_event = require("@domql/event");
|
|
26
|
-
var import_ignore = require("./ignore.js");
|
|
27
27
|
var import_utils = require("@domql/utils");
|
|
28
|
-
var import_inherit = require("./inherit.js");
|
|
29
28
|
const STATE_UPDATE_OPTIONS = {
|
|
30
29
|
overwrite: true,
|
|
31
30
|
preventHoistElementUpdate: false,
|
|
@@ -36,69 +35,52 @@ const STATE_UPDATE_OPTIONS = {
|
|
|
36
35
|
const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
|
|
37
36
|
const state = this;
|
|
38
37
|
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");
|
|
38
|
+
if (options.onEach) options.onEach(element, state, element.context, options);
|
|
39
|
+
if (!options.updateByState) (0, import_utils.merge)(options, STATE_UPDATE_OPTIONS);
|
|
40
|
+
if (!state.__element) (0, import_report.report)("ElementOnStateIsNotDefined");
|
|
45
41
|
if (options.preventInheritAtCurrentState === true) {
|
|
46
42
|
options.preventInheritAtCurrentState = state;
|
|
47
|
-
} else if (options.preventInheritAtCurrentState)
|
|
48
|
-
return;
|
|
43
|
+
} else if (options.preventInheritAtCurrentState) return;
|
|
49
44
|
if (!options.preventBeforeStateUpdateListener) {
|
|
50
|
-
const beforeStateUpdateReturns = await (0, import_event.triggerEventOnUpdate)(
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
const beforeStateUpdateReturns = await (0, import_event.triggerEventOnUpdate)(
|
|
46
|
+
"beforeStateUpdate",
|
|
47
|
+
obj,
|
|
48
|
+
element,
|
|
49
|
+
options
|
|
50
|
+
);
|
|
51
|
+
if (beforeStateUpdateReturns === false) return element;
|
|
53
52
|
}
|
|
54
|
-
|
|
55
|
-
const updateIsHoisted = hoistStateUpdate(state, obj, options);
|
|
56
|
-
if (updateIsHoisted)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
applyElementUpdate(state, obj, options);
|
|
53
|
+
(0, import_utils.overwriteState)(state, obj, options);
|
|
54
|
+
const updateIsHoisted = await hoistStateUpdate(state, obj, options);
|
|
55
|
+
if (updateIsHoisted) return state;
|
|
56
|
+
await updateDependentState(state, obj, options);
|
|
57
|
+
await applyElementUpdate(state, obj, options);
|
|
60
58
|
if (!options.preventStateUpdateListener) {
|
|
61
59
|
await (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
|
|
62
60
|
}
|
|
63
61
|
return state;
|
|
64
62
|
};
|
|
65
|
-
const
|
|
66
|
-
const { overwrite } = options;
|
|
67
|
-
if (!overwrite)
|
|
68
|
-
return;
|
|
69
|
-
const shallow = overwrite === "shallow" || overwrite === "shallow-once";
|
|
70
|
-
const merge2 = overwrite === "merge";
|
|
71
|
-
if (merge2) {
|
|
72
|
-
(0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
|
|
76
|
-
if (options.overwrite === "shallow-once")
|
|
77
|
-
options.overwrite = true;
|
|
78
|
-
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
79
|
-
};
|
|
80
|
-
const hoistStateUpdate = (state, obj, options) => {
|
|
63
|
+
const hoistStateUpdate = async (state, obj, options) => {
|
|
81
64
|
const element = state.__element;
|
|
82
65
|
const { parent, __ref: ref } = element;
|
|
83
66
|
const stateKey = ref == null ? void 0 : ref.__state;
|
|
84
67
|
const stateType = ref == null ? void 0 : ref.__stateType;
|
|
85
|
-
if (!stateKey)
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
68
|
+
if (!stateKey) return;
|
|
69
|
+
const asksForInherit = (0, import_utils.checkIfInherits)(element);
|
|
70
|
+
const inheritedState = (0, import_utils.findInheritedState)(element, parent, {
|
|
71
|
+
returnParent: true
|
|
72
|
+
});
|
|
89
73
|
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
|
|
90
|
-
if (!shouldPropagateState)
|
|
91
|
-
return;
|
|
74
|
+
if (!shouldPropagateState) return;
|
|
92
75
|
const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
|
|
93
76
|
const value = isStringState ? state.value : state.parse();
|
|
94
77
|
const passedValue = isStringState ? state.value : obj;
|
|
95
|
-
const findRootState = (0,
|
|
96
|
-
const findGrandParentState = (0,
|
|
97
|
-
const changesValue = (0,
|
|
78
|
+
const findRootState = (0, import_utils.getRootStateInKey)(stateKey, parent.state);
|
|
79
|
+
const findGrandParentState = (0, import_utils.getParentStateInKey)(stateKey, parent.state);
|
|
80
|
+
const changesValue = (0, import_utils.createNestedObjectByKeyPath)(stateKey, passedValue);
|
|
98
81
|
const targetParent = findRootState || findGrandParentState || parent.state;
|
|
99
|
-
if (options.replace)
|
|
100
|
-
|
|
101
|
-
targetParent.update(changesValue, {
|
|
82
|
+
if (options.replace) (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
|
|
83
|
+
await targetParent.update(changesValue, {
|
|
102
84
|
execStateFunction: false,
|
|
103
85
|
isHoisted: true,
|
|
104
86
|
preventUpdate: options.preventHoistElementUpdate,
|
|
@@ -107,31 +89,37 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
107
89
|
});
|
|
108
90
|
const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
|
|
109
91
|
if (!options.preventStateUpdateListener && hasNotUpdated) {
|
|
110
|
-
(0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
|
|
92
|
+
await (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
|
|
111
93
|
}
|
|
112
94
|
return true;
|
|
113
95
|
};
|
|
114
|
-
const updateDependentState = (state, obj, options) => {
|
|
115
|
-
if (!state.__depends)
|
|
116
|
-
return;
|
|
96
|
+
const updateDependentState = async (state, obj, options) => {
|
|
97
|
+
if (!state.__depends) return;
|
|
117
98
|
for (const el in state.__depends) {
|
|
118
99
|
const dependentState = state.__depends[el];
|
|
119
|
-
dependentState.clean()
|
|
100
|
+
const cleanState = await dependentState.clean();
|
|
101
|
+
await cleanState.update(state.parse(), options);
|
|
120
102
|
}
|
|
121
103
|
};
|
|
122
|
-
const applyElementUpdate = (state, obj, options) => {
|
|
104
|
+
const applyElementUpdate = async (state, obj, options) => {
|
|
123
105
|
const element = state.__element;
|
|
124
106
|
if (options.preventUpdate !== true) {
|
|
125
|
-
element.update(
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
107
|
+
await element.update(
|
|
108
|
+
{},
|
|
109
|
+
{
|
|
110
|
+
...options,
|
|
111
|
+
updateByState: true
|
|
112
|
+
}
|
|
113
|
+
);
|
|
129
114
|
} else if (options.preventUpdate === "recursive") {
|
|
130
|
-
element.update(
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
115
|
+
await element.update(
|
|
116
|
+
{},
|
|
117
|
+
{
|
|
118
|
+
...options,
|
|
119
|
+
isHoisted: false,
|
|
120
|
+
updateByState: true,
|
|
121
|
+
preventUpdate: true
|
|
122
|
+
}
|
|
123
|
+
);
|
|
136
124
|
}
|
|
137
125
|
};
|
package/dist/esm/create.js
CHANGED
|
@@ -1,166 +1,35 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
1
|
import { triggerEventOn } from "@domql/event";
|
|
21
|
-
import { deepClone, exec, is, isArray, isFunction, isObject, isUndefined } from "@domql/utils";
|
|
22
|
-
import { IGNORE_STATE_PARAMS } from "./ignore.js";
|
|
23
2
|
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
remove,
|
|
32
|
-
rootUpdate,
|
|
33
|
-
set,
|
|
34
|
-
reset,
|
|
35
|
-
toggle,
|
|
36
|
-
replace,
|
|
37
|
-
quietUpdate,
|
|
38
|
-
quietReplace,
|
|
39
|
-
applyReplace,
|
|
40
|
-
setByPath,
|
|
41
|
-
setPathCollection,
|
|
42
|
-
removeByPath,
|
|
43
|
-
removePathCollection,
|
|
44
|
-
getByPath,
|
|
45
|
-
keys,
|
|
46
|
-
values
|
|
47
|
-
} from "./methods.js";
|
|
48
|
-
import { updateState } from "./updateState.js";
|
|
49
|
-
import { checkIfInherits, createInheritedState } from "./inherit.js";
|
|
3
|
+
applyDependentState,
|
|
4
|
+
checkForStateTypes,
|
|
5
|
+
checkIfInherits,
|
|
6
|
+
createInheritedState,
|
|
7
|
+
isUndefined
|
|
8
|
+
} from "@domql/utils";
|
|
9
|
+
import { applyStateMethods } from "./methods";
|
|
50
10
|
const createState = async function(element, parent, options) {
|
|
51
11
|
element.state = await applyInitialState(element, parent, options);
|
|
52
12
|
return element.state;
|
|
53
13
|
};
|
|
54
14
|
const applyInitialState = async function(element, parent, options) {
|
|
55
|
-
const objectizeState = await
|
|
56
|
-
if (objectizeState === false)
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const whatInitReturns = triggerEventOn("stateInit", element, options);
|
|
61
|
-
if (whatInitReturns === false)
|
|
62
|
-
return element.state;
|
|
15
|
+
const objectizeState = await checkForStateTypes(element);
|
|
16
|
+
if (objectizeState === false) return parent.state || {};
|
|
17
|
+
else element.state = objectizeState;
|
|
18
|
+
const whatInitReturns = await triggerEventOn("stateInit", element, options);
|
|
19
|
+
if (whatInitReturns === false) return element.state;
|
|
63
20
|
if (checkIfInherits(element)) {
|
|
64
21
|
const inheritedState = createInheritedState(element, parent);
|
|
65
22
|
element.state = isUndefined(inheritedState) ? {} : inheritedState;
|
|
66
23
|
}
|
|
67
|
-
const dependentState = applyDependentState(
|
|
68
|
-
|
|
69
|
-
element.state
|
|
70
|
-
|
|
71
|
-
|
|
24
|
+
const dependentState = await applyDependentState(
|
|
25
|
+
element,
|
|
26
|
+
element.state || parent.state || {}
|
|
27
|
+
);
|
|
28
|
+
if (dependentState) element.state = dependentState;
|
|
29
|
+
applyStateMethods(element);
|
|
30
|
+
await triggerEventOn("stateCreated", element);
|
|
72
31
|
return element.state;
|
|
73
32
|
};
|
|
74
|
-
const applyDependentState = (element, state) => {
|
|
75
|
-
const { __ref, ref, __element } = state;
|
|
76
|
-
const origState = exec(__ref || ref || (__element == null ? void 0 : __element.state), element);
|
|
77
|
-
if (!origState)
|
|
78
|
-
return;
|
|
79
|
-
const dependentState = deepClone(origState, IGNORE_STATE_PARAMS);
|
|
80
|
-
const newDepends = { [element.key]: dependentState };
|
|
81
|
-
const __depends = isObject(origState.__depends) ? __spreadValues(__spreadValues({}, origState.__depends), newDepends) : newDepends;
|
|
82
|
-
if (Array.isArray(origState)) {
|
|
83
|
-
addProtoToArray(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), { __depends }));
|
|
84
|
-
} else {
|
|
85
|
-
Object.setPrototypeOf(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), { __depends }));
|
|
86
|
-
}
|
|
87
|
-
return dependentState;
|
|
88
|
-
};
|
|
89
|
-
const checkForTypes = async (element) => {
|
|
90
|
-
const { state: orig, props, __ref: ref } = element;
|
|
91
|
-
const state = (props == null ? void 0 : props.state) || orig;
|
|
92
|
-
if (isFunction(state)) {
|
|
93
|
-
ref.__state = state;
|
|
94
|
-
return await exec(state, element);
|
|
95
|
-
} else if (is(state)("string", "number")) {
|
|
96
|
-
ref.__state = state;
|
|
97
|
-
return { value: state };
|
|
98
|
-
} else if (state === true) {
|
|
99
|
-
ref.__state = element.key;
|
|
100
|
-
return {};
|
|
101
|
-
} else if (state) {
|
|
102
|
-
ref.__hasRootState = true;
|
|
103
|
-
return state;
|
|
104
|
-
} else {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
const addProtoToArray = (state, proto) => {
|
|
109
|
-
for (const key in proto) {
|
|
110
|
-
Object.defineProperty(state, key, {
|
|
111
|
-
value: proto[key],
|
|
112
|
-
enumerable: false,
|
|
113
|
-
// Set this to true if you want the method to appear in for...in loops
|
|
114
|
-
configurable: true,
|
|
115
|
-
// Set this to true if you want to allow redefining/removing the property later
|
|
116
|
-
writable: true
|
|
117
|
-
// Set this to true if you want to allow changing the function later
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
const applyMethods = (element) => {
|
|
122
|
-
const state = element.state;
|
|
123
|
-
const ref = element.__ref;
|
|
124
|
-
const proto = {
|
|
125
|
-
clean: clean.bind(state),
|
|
126
|
-
parse: parse.bind(state),
|
|
127
|
-
destroy: destroy.bind(state),
|
|
128
|
-
update: updateState.bind(state),
|
|
129
|
-
rootUpdate: rootUpdate.bind(state),
|
|
130
|
-
parentUpdate: parentUpdate.bind(state),
|
|
131
|
-
create: createState.bind(state),
|
|
132
|
-
add: add.bind(state),
|
|
133
|
-
toggle: toggle.bind(state),
|
|
134
|
-
remove: remove.bind(state),
|
|
135
|
-
apply: apply.bind(state),
|
|
136
|
-
applyReplace: applyReplace.bind(state),
|
|
137
|
-
applyFunction: applyFunction.bind(state),
|
|
138
|
-
set: set.bind(state),
|
|
139
|
-
quietUpdate: quietUpdate.bind(state),
|
|
140
|
-
replace: replace.bind(state),
|
|
141
|
-
quietReplace: quietReplace.bind(state),
|
|
142
|
-
reset: reset.bind(state),
|
|
143
|
-
parent: element.parent.state || state,
|
|
144
|
-
setByPath: setByPath.bind(state),
|
|
145
|
-
setPathCollection: setPathCollection.bind(state),
|
|
146
|
-
removeByPath: removeByPath.bind(state),
|
|
147
|
-
removePathCollection: removePathCollection.bind(state),
|
|
148
|
-
getByPath: getByPath.bind(state),
|
|
149
|
-
keys: keys.bind(state),
|
|
150
|
-
values: values.bind(state),
|
|
151
|
-
__element: element,
|
|
152
|
-
__children: {},
|
|
153
|
-
root: ref.root ? ref.root.state : state
|
|
154
|
-
};
|
|
155
|
-
if (isArray(state)) {
|
|
156
|
-
addProtoToArray(state, proto);
|
|
157
|
-
} else {
|
|
158
|
-
Object.setPrototypeOf(state, proto);
|
|
159
|
-
}
|
|
160
|
-
if (state.parent && state.parent.__children) {
|
|
161
|
-
state.parent.__children[element.key] = state;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
33
|
export {
|
|
165
34
|
applyInitialState,
|
|
166
35
|
createState
|
package/dist/esm/index.js
CHANGED
package/dist/esm/methods.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
2
4
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
6
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
@@ -14,6 +16,7 @@ var __spreadValues = (a, b) => {
|
|
|
14
16
|
}
|
|
15
17
|
return a;
|
|
16
18
|
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
17
20
|
import {
|
|
18
21
|
isArray,
|
|
19
22
|
deepClone,
|
|
@@ -26,36 +29,39 @@ import {
|
|
|
26
29
|
createNestedObject,
|
|
27
30
|
getInObjectByPath,
|
|
28
31
|
removeNestedKeyByPath,
|
|
29
|
-
setInObjectByPath
|
|
32
|
+
setInObjectByPath,
|
|
33
|
+
STATE_METHODS,
|
|
34
|
+
addProtoToArray
|
|
30
35
|
} from "@domql/utils";
|
|
31
|
-
import {
|
|
36
|
+
import { updateState } from "./updateState.js";
|
|
37
|
+
import { createState } from "./create.js";
|
|
32
38
|
const parse = function() {
|
|
33
39
|
const state = this;
|
|
34
40
|
if (isObject(state)) {
|
|
35
41
|
const obj = {};
|
|
36
42
|
for (const param in state) {
|
|
37
|
-
if (!
|
|
43
|
+
if (!STATE_METHODS.includes(param)) {
|
|
38
44
|
obj[param] = state[param];
|
|
39
45
|
}
|
|
40
46
|
}
|
|
41
47
|
return obj;
|
|
42
48
|
} else if (isArray(state)) {
|
|
43
|
-
return state.filter((item) => !
|
|
49
|
+
return state.filter((item) => !STATE_METHODS.includes(item));
|
|
44
50
|
}
|
|
45
51
|
};
|
|
46
|
-
const clean = function(options = {}) {
|
|
52
|
+
const clean = async function(options = {}) {
|
|
47
53
|
const state = this;
|
|
48
54
|
for (const param in state) {
|
|
49
|
-
if (!
|
|
55
|
+
if (!STATE_METHODS.includes(param) && Object.hasOwnProperty.call(state, param)) {
|
|
50
56
|
delete state[param];
|
|
51
57
|
}
|
|
52
58
|
}
|
|
53
59
|
if (!options.preventStateUpdate) {
|
|
54
|
-
state.update(state, __spreadValues({ replace: true }, options));
|
|
60
|
+
await state.update(state, __spreadValues({ replace: true }, options));
|
|
55
61
|
}
|
|
56
62
|
return state;
|
|
57
63
|
};
|
|
58
|
-
const destroy = function(options = {}) {
|
|
64
|
+
const destroy = async function(options = {}) {
|
|
59
65
|
const state = this;
|
|
60
66
|
const element = state.__element;
|
|
61
67
|
const stateKey = element.__ref.__state;
|
|
@@ -88,131 +94,132 @@ const destroy = function(options = {}) {
|
|
|
88
94
|
}
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
|
-
element.state.update({}, __spreadValues({ isHoisted: true }, options));
|
|
97
|
+
await element.state.update({}, __spreadValues({ isHoisted: true }, options));
|
|
92
98
|
return element.state;
|
|
93
99
|
};
|
|
94
|
-
const parentUpdate = function(obj, options = {}) {
|
|
100
|
+
const parentUpdate = async function(obj, options = {}) {
|
|
95
101
|
const state = this;
|
|
96
|
-
if (!state || !state.parent)
|
|
97
|
-
|
|
98
|
-
return state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
|
|
102
|
+
if (!state || !state.parent) return;
|
|
103
|
+
return await state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
|
|
99
104
|
};
|
|
100
|
-
const rootUpdate = function(obj, options = {}) {
|
|
105
|
+
const rootUpdate = async function(obj, options = {}) {
|
|
101
106
|
const state = this;
|
|
102
|
-
if (!state)
|
|
103
|
-
return;
|
|
107
|
+
if (!state) return;
|
|
104
108
|
const rootState = state.__element.__ref.root.state;
|
|
105
|
-
return rootState.update(obj, __spreadValues({ isHoisted: false }, options));
|
|
109
|
+
return await rootState.update(obj, __spreadValues({ isHoisted: false }, options));
|
|
106
110
|
};
|
|
107
|
-
const add = function(value, options = {}) {
|
|
111
|
+
const add = async function(value, options = {}) {
|
|
108
112
|
const state = this;
|
|
109
113
|
if (isArray(state)) {
|
|
110
114
|
state.push(value);
|
|
111
|
-
state.update(state.parse(), __spreadValues({ overwrite: true }, options));
|
|
115
|
+
await state.update(state.parse(), __spreadValues({ overwrite: true }, options));
|
|
112
116
|
} else if (isObject(state)) {
|
|
113
117
|
const key = Object.keys(state).length;
|
|
114
|
-
state.update({ [key]: value }, options);
|
|
118
|
+
await state.update({ [key]: value }, options);
|
|
115
119
|
}
|
|
116
120
|
};
|
|
117
|
-
const toggle = function(key, options = {}) {
|
|
121
|
+
const toggle = async function(key, options = {}) {
|
|
118
122
|
const state = this;
|
|
119
|
-
state.update({ [key]: !state[key] }, options);
|
|
123
|
+
await state.update({ [key]: !state[key] }, options);
|
|
120
124
|
};
|
|
121
|
-
const remove = function(key, options = {}) {
|
|
125
|
+
const remove = async function(key, options = {}) {
|
|
122
126
|
const state = this;
|
|
123
|
-
if (isArray(state))
|
|
124
|
-
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
return state.update();
|
|
127
|
+
if (isArray(state)) removeFromArray(state, key);
|
|
128
|
+
if (isObject(state)) removeFromObject(state, key);
|
|
129
|
+
if (options.applyReset) {
|
|
130
|
+
return await state.set(state.parse(), __spreadValues({ replace: true }, options));
|
|
131
|
+
}
|
|
132
|
+
return await state.update({}, options);
|
|
130
133
|
};
|
|
131
|
-
const set = function(val, options = {}) {
|
|
134
|
+
const set = async function(val, options = {}) {
|
|
132
135
|
const state = this;
|
|
133
136
|
const value = deepClone(val);
|
|
134
|
-
|
|
137
|
+
const cleanState = await state.clean(__spreadValues({ preventStateUpdate: true }, options));
|
|
138
|
+
return await cleanState.update(value, __spreadValues({ replace: true }, options));
|
|
135
139
|
};
|
|
136
|
-
const setByPath = function(path, val, options = {}) {
|
|
140
|
+
const setByPath = async function(path, val, options = {}) {
|
|
137
141
|
const state = this;
|
|
138
142
|
const value = deepClone(val);
|
|
139
143
|
setInObjectByPath(state, path, val);
|
|
140
144
|
const update = createNestedObject(path, value);
|
|
141
|
-
if (options.preventStateUpdate)
|
|
142
|
-
|
|
143
|
-
return state.update(update, options);
|
|
145
|
+
if (options.preventStateUpdate) return update;
|
|
146
|
+
return await state.update(update, options);
|
|
144
147
|
};
|
|
145
|
-
const setPathCollection = function(changes, options = {}) {
|
|
148
|
+
const setPathCollection = async function(changes, options = {}) {
|
|
146
149
|
const state = this;
|
|
147
|
-
const update = changes.reduce((
|
|
150
|
+
const update = await changes.reduce(async (promise, change) => {
|
|
151
|
+
const acc = await promise;
|
|
148
152
|
if (change[0] === "update") {
|
|
149
|
-
const result = setByPath.call(state, change[1], change[2], {
|
|
153
|
+
const result = setByPath.call(state, change[1], change[2], {
|
|
154
|
+
preventStateUpdate: true
|
|
155
|
+
});
|
|
150
156
|
return overwriteDeep(acc, result);
|
|
151
157
|
} else if (change[0] === "delete") {
|
|
152
|
-
removeByPath.call(state, change[1], options)
|
|
158
|
+
await removeByPath.call(state, change[1], __spreadProps(__spreadValues({}, options), {
|
|
159
|
+
preventUpdate: true
|
|
160
|
+
}));
|
|
153
161
|
}
|
|
154
162
|
return acc;
|
|
155
|
-
}, {});
|
|
163
|
+
}, Promise.resolve({}));
|
|
156
164
|
return state.update(update, options);
|
|
157
165
|
};
|
|
158
|
-
const removeByPath = function(path, options = {}) {
|
|
166
|
+
const removeByPath = async function(path, options = {}) {
|
|
159
167
|
const state = this;
|
|
160
168
|
removeNestedKeyByPath(state, path);
|
|
161
|
-
if (options.preventUpdate)
|
|
162
|
-
|
|
163
|
-
return state.update({}, options);
|
|
169
|
+
if (options.preventUpdate) return path;
|
|
170
|
+
return await state.update({}, options);
|
|
164
171
|
};
|
|
165
|
-
const removePathCollection = function(changes, options = {}) {
|
|
172
|
+
const removePathCollection = async function(changes, options = {}) {
|
|
166
173
|
const state = this;
|
|
167
|
-
changes.forEach((item) => {
|
|
168
|
-
removeByPath(item, { preventUpdate: true });
|
|
174
|
+
changes.forEach(async (item) => {
|
|
175
|
+
await removeByPath(item, { preventUpdate: true });
|
|
169
176
|
});
|
|
170
|
-
return state.update({}, options);
|
|
177
|
+
return await state.update({}, options);
|
|
171
178
|
};
|
|
172
179
|
const getByPath = function(path, options = {}) {
|
|
173
180
|
const state = this;
|
|
174
181
|
return getInObjectByPath(state, path);
|
|
175
182
|
};
|
|
176
|
-
const reset = function(options = {}) {
|
|
183
|
+
const reset = async function(options = {}) {
|
|
177
184
|
const state = this;
|
|
178
185
|
const value = deepClone(state.parse());
|
|
179
|
-
return state.set(value, __spreadValues({ replace: true }, options));
|
|
186
|
+
return await state.set(value, __spreadValues({ replace: true }, options));
|
|
180
187
|
};
|
|
181
|
-
const apply = function(func, options = {}) {
|
|
188
|
+
const apply = async function(func, options = {}) {
|
|
182
189
|
const state = this;
|
|
183
190
|
if (isFunction(func)) {
|
|
184
191
|
const value = func(state);
|
|
185
|
-
return state.update(value, __spreadValues({ replace: true }, options));
|
|
192
|
+
return await state.update(value, __spreadValues({ replace: true }, options));
|
|
186
193
|
}
|
|
187
194
|
};
|
|
188
|
-
const applyReplace = function(func, options = {}) {
|
|
195
|
+
const applyReplace = async function(func, options = {}) {
|
|
189
196
|
const state = this;
|
|
190
197
|
if (isFunction(func)) {
|
|
191
198
|
const value = func(state);
|
|
192
|
-
return state.replace(value, options);
|
|
199
|
+
return await state.replace(value, options);
|
|
193
200
|
}
|
|
194
201
|
};
|
|
195
|
-
const applyFunction = function(func, options = {}) {
|
|
202
|
+
const applyFunction = async function(func, options = {}) {
|
|
196
203
|
const state = this;
|
|
197
204
|
if (isFunction(func)) {
|
|
198
|
-
func(state);
|
|
199
|
-
return state.update(state.parse(), __spreadValues({ replace: true }, options));
|
|
205
|
+
await func(state);
|
|
206
|
+
return await state.update(state.parse(), __spreadValues({ replace: true }, options));
|
|
200
207
|
}
|
|
201
208
|
};
|
|
202
|
-
const quietUpdate = function(obj, options = {}) {
|
|
209
|
+
const quietUpdate = async function(obj, options = {}) {
|
|
203
210
|
const state = this;
|
|
204
|
-
return state.update(obj, __spreadValues({ preventUpdate: true }, options));
|
|
211
|
+
return await state.update(obj, __spreadValues({ preventUpdate: true }, options));
|
|
205
212
|
};
|
|
206
|
-
const replace = function(obj, options = {}) {
|
|
213
|
+
const replace = async function(obj, options = {}) {
|
|
207
214
|
const state = this;
|
|
208
215
|
for (const param in obj) {
|
|
209
216
|
state[param] = obj[param];
|
|
210
217
|
}
|
|
211
|
-
return state.update(obj, options);
|
|
218
|
+
return await state.update(obj, options);
|
|
212
219
|
};
|
|
213
|
-
const quietReplace = function(obj, options = {}) {
|
|
220
|
+
const quietReplace = async function(obj, options = {}) {
|
|
214
221
|
const state = this;
|
|
215
|
-
return state.replace(obj, __spreadValues({ preventUpdate: true }, options));
|
|
222
|
+
return await state.replace(obj, __spreadValues({ preventUpdate: true }, options));
|
|
216
223
|
};
|
|
217
224
|
const keys = function(obj, options = {}) {
|
|
218
225
|
const state = this;
|
|
@@ -222,11 +229,56 @@ const values = function(obj, options = {}) {
|
|
|
222
229
|
const state = this;
|
|
223
230
|
return Object.values(state);
|
|
224
231
|
};
|
|
232
|
+
const applyStateMethods = (element) => {
|
|
233
|
+
var _a;
|
|
234
|
+
const state = element.state;
|
|
235
|
+
const ref = element.__ref;
|
|
236
|
+
const proto = {
|
|
237
|
+
clean: clean.bind(state),
|
|
238
|
+
parse: parse.bind(state),
|
|
239
|
+
destroy: destroy.bind(state),
|
|
240
|
+
update: updateState.bind(state),
|
|
241
|
+
rootUpdate: rootUpdate.bind(state),
|
|
242
|
+
parentUpdate: parentUpdate.bind(state),
|
|
243
|
+
create: createState.bind(state),
|
|
244
|
+
add: add.bind(state),
|
|
245
|
+
toggle: toggle.bind(state),
|
|
246
|
+
remove: remove.bind(state),
|
|
247
|
+
apply: apply.bind(state),
|
|
248
|
+
applyReplace: applyReplace.bind(state),
|
|
249
|
+
applyFunction: applyFunction.bind(state),
|
|
250
|
+
set: set.bind(state),
|
|
251
|
+
quietUpdate: quietUpdate.bind(state),
|
|
252
|
+
replace: replace.bind(state),
|
|
253
|
+
quietReplace: quietReplace.bind(state),
|
|
254
|
+
reset: reset.bind(state),
|
|
255
|
+
parent: ((_a = element.parent) == null ? void 0 : _a.state) || state,
|
|
256
|
+
setByPath: setByPath.bind(state),
|
|
257
|
+
setPathCollection: setPathCollection.bind(state),
|
|
258
|
+
removeByPath: removeByPath.bind(state),
|
|
259
|
+
removePathCollection: removePathCollection.bind(state),
|
|
260
|
+
getByPath: getByPath.bind(state),
|
|
261
|
+
keys: keys.bind(state),
|
|
262
|
+
values: values.bind(state),
|
|
263
|
+
__element: element,
|
|
264
|
+
__children: {},
|
|
265
|
+
root: (ref == null ? void 0 : ref.root) ? ref.root.state : state
|
|
266
|
+
};
|
|
267
|
+
if (isArray(state)) {
|
|
268
|
+
addProtoToArray(state, proto);
|
|
269
|
+
} else {
|
|
270
|
+
Object.setPrototypeOf(state, proto);
|
|
271
|
+
}
|
|
272
|
+
if (state.parent && state.parent.__children) {
|
|
273
|
+
state.parent.__children[element.key] = state;
|
|
274
|
+
}
|
|
275
|
+
};
|
|
225
276
|
export {
|
|
226
277
|
add,
|
|
227
278
|
apply,
|
|
228
279
|
applyFunction,
|
|
229
280
|
applyReplace,
|
|
281
|
+
applyStateMethods,
|
|
230
282
|
clean,
|
|
231
283
|
destroy,
|
|
232
284
|
getByPath,
|