@domql/state 2.5.200 → 3.0.1
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 +11 -100
- package/dist/cjs/index.js +0 -2
- package/dist/cjs/methods.js +105 -50
- package/dist/cjs/updateState.js +41 -41
- package/dist/esm/create.js +15 -141
- package/dist/esm/index.js +0 -2
- package/dist/esm/methods.js +110 -51
- package/dist/esm/updateState.js +45 -36
- 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 -117
- package/dist/esm/ignore.js +0 -37
- package/dist/esm/inherit.js +0 -97
- 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,
|
|
@@ -43,51 +42,45 @@ const updateState = async function(obj, options = STATE_UPDATE_OPTIONS) {
|
|
|
43
42
|
options.preventInheritAtCurrentState = state;
|
|
44
43
|
} else if (options.preventInheritAtCurrentState) return;
|
|
45
44
|
if (!options.preventBeforeStateUpdateListener) {
|
|
46
|
-
const beforeStateUpdateReturns = await (0, import_event.triggerEventOnUpdate)(
|
|
45
|
+
const beforeStateUpdateReturns = await (0, import_event.triggerEventOnUpdate)(
|
|
46
|
+
"beforeStateUpdate",
|
|
47
|
+
obj,
|
|
48
|
+
element,
|
|
49
|
+
options
|
|
50
|
+
);
|
|
47
51
|
if (beforeStateUpdateReturns === false) return element;
|
|
48
52
|
}
|
|
49
|
-
|
|
50
|
-
const updateIsHoisted = hoistStateUpdate(state, obj, options);
|
|
53
|
+
(0, import_utils.overwriteState)(state, obj, options);
|
|
54
|
+
const updateIsHoisted = await hoistStateUpdate(state, obj, options);
|
|
51
55
|
if (updateIsHoisted) return state;
|
|
52
|
-
updateDependentState(state, obj, options);
|
|
53
|
-
applyElementUpdate(state, obj, options);
|
|
56
|
+
await updateDependentState(state, obj, options);
|
|
57
|
+
await applyElementUpdate(state, obj, options);
|
|
54
58
|
if (!options.preventStateUpdateListener) {
|
|
55
59
|
await (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
|
|
56
60
|
}
|
|
57
61
|
return state;
|
|
58
62
|
};
|
|
59
|
-
const
|
|
60
|
-
const { overwrite } = options;
|
|
61
|
-
if (!overwrite) return;
|
|
62
|
-
const shallow = overwrite === "shallow" || overwrite === "shallow-once";
|
|
63
|
-
const merge2 = overwrite === "merge";
|
|
64
|
-
if (merge2) {
|
|
65
|
-
(0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
|
|
69
|
-
if (options.overwrite === "shallow-once") options.overwrite = true;
|
|
70
|
-
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
71
|
-
};
|
|
72
|
-
const hoistStateUpdate = (state, obj, options) => {
|
|
63
|
+
const hoistStateUpdate = async (state, obj, options) => {
|
|
73
64
|
const element = state.__element;
|
|
74
65
|
const { parent, __ref: ref } = element;
|
|
75
66
|
const stateKey = ref == null ? void 0 : ref.__state;
|
|
76
67
|
const stateType = ref == null ? void 0 : ref.__stateType;
|
|
77
68
|
if (!stateKey) return;
|
|
78
|
-
const asksForInherit = (0,
|
|
79
|
-
const inheritedState = (0,
|
|
69
|
+
const asksForInherit = (0, import_utils.checkIfInherits)(element);
|
|
70
|
+
const inheritedState = (0, import_utils.findInheritedState)(element, parent, {
|
|
71
|
+
returnParent: true
|
|
72
|
+
});
|
|
80
73
|
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
|
|
81
74
|
if (!shouldPropagateState) return;
|
|
82
75
|
const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
|
|
83
76
|
const value = isStringState ? state.value : state.parse();
|
|
84
77
|
const passedValue = isStringState ? state.value : obj;
|
|
85
|
-
const findRootState = (0,
|
|
86
|
-
const findGrandParentState = (0,
|
|
87
|
-
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);
|
|
88
81
|
const targetParent = findRootState || findGrandParentState || parent.state;
|
|
89
82
|
if (options.replace) (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
|
|
90
|
-
targetParent.update(changesValue, {
|
|
83
|
+
await targetParent.update(changesValue, {
|
|
91
84
|
execStateFunction: false,
|
|
92
85
|
isHoisted: true,
|
|
93
86
|
preventUpdate: options.preventHoistElementUpdate,
|
|
@@ -96,30 +89,37 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
96
89
|
});
|
|
97
90
|
const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
|
|
98
91
|
if (!options.preventStateUpdateListener && hasNotUpdated) {
|
|
99
|
-
(0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
|
|
92
|
+
await (0, import_event.triggerEventOnUpdate)("stateUpdate", obj, element, options);
|
|
100
93
|
}
|
|
101
94
|
return true;
|
|
102
95
|
};
|
|
103
|
-
const updateDependentState = (state, obj, options) => {
|
|
96
|
+
const updateDependentState = async (state, obj, options) => {
|
|
104
97
|
if (!state.__depends) return;
|
|
105
98
|
for (const el in state.__depends) {
|
|
106
99
|
const dependentState = state.__depends[el];
|
|
107
|
-
dependentState.clean()
|
|
100
|
+
const cleanState = await dependentState.clean();
|
|
101
|
+
await cleanState.update(state.parse(), options);
|
|
108
102
|
}
|
|
109
103
|
};
|
|
110
|
-
const applyElementUpdate = (state, obj, options) => {
|
|
104
|
+
const applyElementUpdate = async (state, obj, options) => {
|
|
111
105
|
const element = state.__element;
|
|
112
106
|
if (options.preventUpdate !== true) {
|
|
113
|
-
element.update(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
107
|
+
await element.update(
|
|
108
|
+
{},
|
|
109
|
+
{
|
|
110
|
+
...options,
|
|
111
|
+
updateByState: true
|
|
112
|
+
}
|
|
113
|
+
);
|
|
117
114
|
} else if (options.preventUpdate === "recursive") {
|
|
118
|
-
element.update(
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
115
|
+
await element.update(
|
|
116
|
+
{},
|
|
117
|
+
{
|
|
118
|
+
...options,
|
|
119
|
+
isHoisted: false,
|
|
120
|
+
updateByState: true,
|
|
121
|
+
preventUpdate: true
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
124
|
}
|
|
125
125
|
};
|
package/dist/esm/create.js
CHANGED
|
@@ -1,161 +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
|
|
15
|
+
const objectizeState = await checkForStateTypes(element);
|
|
56
16
|
if (objectizeState === false) return parent.state || {};
|
|
57
17
|
else element.state = objectizeState;
|
|
58
|
-
const whatInitReturns = triggerEventOn("stateInit", element, options);
|
|
18
|
+
const whatInitReturns = await triggerEventOn("stateInit", element, options);
|
|
59
19
|
if (whatInitReturns === false) return element.state;
|
|
60
20
|
if (checkIfInherits(element)) {
|
|
61
21
|
const inheritedState = createInheritedState(element, parent);
|
|
62
22
|
element.state = isUndefined(inheritedState) ? {} : inheritedState;
|
|
63
23
|
}
|
|
64
|
-
const dependentState = applyDependentState(
|
|
24
|
+
const dependentState = await applyDependentState(
|
|
25
|
+
element,
|
|
26
|
+
element.state || parent.state || {}
|
|
27
|
+
);
|
|
65
28
|
if (dependentState) element.state = dependentState;
|
|
66
|
-
|
|
67
|
-
triggerEventOn("stateCreated", element);
|
|
29
|
+
applyStateMethods(element);
|
|
30
|
+
await triggerEventOn("stateCreated", element);
|
|
68
31
|
return element.state;
|
|
69
32
|
};
|
|
70
|
-
const applyDependentState = (element, state) => {
|
|
71
|
-
const { __ref, ref, __element } = state;
|
|
72
|
-
const origState = exec(__ref || ref || (__element == null ? void 0 : __element.state), element);
|
|
73
|
-
if (!origState) return;
|
|
74
|
-
const dependentState = deepClone(origState, IGNORE_STATE_PARAMS);
|
|
75
|
-
const newDepends = { [element.key]: dependentState };
|
|
76
|
-
const __depends = isObject(origState.__depends) ? __spreadValues(__spreadValues({}, origState.__depends), newDepends) : newDepends;
|
|
77
|
-
if (Array.isArray(origState)) {
|
|
78
|
-
addProtoToArray(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), { __depends }));
|
|
79
|
-
} else {
|
|
80
|
-
Object.setPrototypeOf(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), { __depends }));
|
|
81
|
-
}
|
|
82
|
-
return dependentState;
|
|
83
|
-
};
|
|
84
|
-
const checkForTypes = async (element) => {
|
|
85
|
-
const { state: orig, props, __ref: ref } = element;
|
|
86
|
-
const state = (props == null ? void 0 : props.state) || orig;
|
|
87
|
-
if (isFunction(state)) {
|
|
88
|
-
ref.__state = state;
|
|
89
|
-
return await exec(state, element);
|
|
90
|
-
} else if (is(state)("string", "number")) {
|
|
91
|
-
ref.__state = state;
|
|
92
|
-
return { value: state };
|
|
93
|
-
} else if (state === true) {
|
|
94
|
-
ref.__state = element.key;
|
|
95
|
-
return {};
|
|
96
|
-
} else if (state) {
|
|
97
|
-
ref.__hasRootState = true;
|
|
98
|
-
return state;
|
|
99
|
-
} else {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
const addProtoToArray = (state, proto) => {
|
|
104
|
-
for (const key in proto) {
|
|
105
|
-
Object.defineProperty(state, key, {
|
|
106
|
-
value: proto[key],
|
|
107
|
-
enumerable: false,
|
|
108
|
-
// Set this to true if you want the method to appear in for...in loops
|
|
109
|
-
configurable: true,
|
|
110
|
-
// Set this to true if you want to allow redefining/removing the property later
|
|
111
|
-
writable: true
|
|
112
|
-
// Set this to true if you want to allow changing the function later
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
const applyMethods = (element) => {
|
|
117
|
-
const state = element.state;
|
|
118
|
-
const ref = element.__ref;
|
|
119
|
-
const proto = {
|
|
120
|
-
clean: clean.bind(state),
|
|
121
|
-
parse: parse.bind(state),
|
|
122
|
-
destroy: destroy.bind(state),
|
|
123
|
-
update: updateState.bind(state),
|
|
124
|
-
rootUpdate: rootUpdate.bind(state),
|
|
125
|
-
parentUpdate: parentUpdate.bind(state),
|
|
126
|
-
create: createState.bind(state),
|
|
127
|
-
add: add.bind(state),
|
|
128
|
-
toggle: toggle.bind(state),
|
|
129
|
-
remove: remove.bind(state),
|
|
130
|
-
apply: apply.bind(state),
|
|
131
|
-
applyReplace: applyReplace.bind(state),
|
|
132
|
-
applyFunction: applyFunction.bind(state),
|
|
133
|
-
set: set.bind(state),
|
|
134
|
-
quietUpdate: quietUpdate.bind(state),
|
|
135
|
-
replace: replace.bind(state),
|
|
136
|
-
quietReplace: quietReplace.bind(state),
|
|
137
|
-
reset: reset.bind(state),
|
|
138
|
-
parent: element.parent.state || state,
|
|
139
|
-
setByPath: setByPath.bind(state),
|
|
140
|
-
setPathCollection: setPathCollection.bind(state),
|
|
141
|
-
removeByPath: removeByPath.bind(state),
|
|
142
|
-
removePathCollection: removePathCollection.bind(state),
|
|
143
|
-
getByPath: getByPath.bind(state),
|
|
144
|
-
keys: keys.bind(state),
|
|
145
|
-
values: values.bind(state),
|
|
146
|
-
__element: element,
|
|
147
|
-
__children: {},
|
|
148
|
-
root: ref.root ? ref.root.state : state
|
|
149
|
-
};
|
|
150
|
-
if (isArray(state)) {
|
|
151
|
-
addProtoToArray(state, proto);
|
|
152
|
-
} else {
|
|
153
|
-
Object.setPrototypeOf(state, proto);
|
|
154
|
-
}
|
|
155
|
-
if (state.parent && state.parent.__children) {
|
|
156
|
-
state.parent.__children[element.key] = state;
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
33
|
export {
|
|
160
34
|
applyInitialState,
|
|
161
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,124 +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
102
|
if (!state || !state.parent) return;
|
|
97
|
-
return state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
|
|
103
|
+
return await state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
|
|
98
104
|
};
|
|
99
|
-
const rootUpdate = function(obj, options = {}) {
|
|
105
|
+
const rootUpdate = async function(obj, options = {}) {
|
|
100
106
|
const state = this;
|
|
101
107
|
if (!state) return;
|
|
102
108
|
const rootState = state.__element.__ref.root.state;
|
|
103
|
-
return rootState.update(obj, __spreadValues({ isHoisted: false }, options));
|
|
109
|
+
return await rootState.update(obj, __spreadValues({ isHoisted: false }, options));
|
|
104
110
|
};
|
|
105
|
-
const add = function(value, options = {}) {
|
|
111
|
+
const add = async function(value, options = {}) {
|
|
106
112
|
const state = this;
|
|
107
113
|
if (isArray(state)) {
|
|
108
114
|
state.push(value);
|
|
109
|
-
state.update(state.parse(), __spreadValues({ overwrite: true }, options));
|
|
115
|
+
await state.update(state.parse(), __spreadValues({ overwrite: true }, options));
|
|
110
116
|
} else if (isObject(state)) {
|
|
111
117
|
const key = Object.keys(state).length;
|
|
112
|
-
state.update({ [key]: value }, options);
|
|
118
|
+
await state.update({ [key]: value }, options);
|
|
113
119
|
}
|
|
114
120
|
};
|
|
115
|
-
const toggle = function(key, options = {}) {
|
|
121
|
+
const toggle = async function(key, options = {}) {
|
|
116
122
|
const state = this;
|
|
117
|
-
state.update({ [key]: !state[key] }, options);
|
|
123
|
+
await state.update({ [key]: !state[key] }, options);
|
|
118
124
|
};
|
|
119
|
-
const remove = function(key, options = {}) {
|
|
125
|
+
const remove = async function(key, options = {}) {
|
|
120
126
|
const state = this;
|
|
121
127
|
if (isArray(state)) removeFromArray(state, key);
|
|
122
128
|
if (isObject(state)) removeFromObject(state, key);
|
|
123
|
-
if (options.applyReset)
|
|
124
|
-
|
|
129
|
+
if (options.applyReset) {
|
|
130
|
+
return await state.set(state.parse(), __spreadValues({ replace: true }, options));
|
|
131
|
+
}
|
|
132
|
+
return await state.update({}, options);
|
|
125
133
|
};
|
|
126
|
-
const set = function(val, options = {}) {
|
|
134
|
+
const set = async function(val, options = {}) {
|
|
127
135
|
const state = this;
|
|
128
136
|
const value = deepClone(val);
|
|
129
|
-
|
|
137
|
+
const cleanState = await state.clean(__spreadValues({ preventStateUpdate: true }, options));
|
|
138
|
+
return await cleanState.update(value, __spreadValues({ replace: true }, options));
|
|
130
139
|
};
|
|
131
|
-
const setByPath = function(path, val, options = {}) {
|
|
140
|
+
const setByPath = async function(path, val, options = {}) {
|
|
132
141
|
const state = this;
|
|
133
142
|
const value = deepClone(val);
|
|
134
143
|
setInObjectByPath(state, path, val);
|
|
135
144
|
const update = createNestedObject(path, value);
|
|
136
145
|
if (options.preventStateUpdate) return update;
|
|
137
|
-
return state.update(update, options);
|
|
146
|
+
return await state.update(update, options);
|
|
138
147
|
};
|
|
139
|
-
const setPathCollection = function(changes, options = {}) {
|
|
148
|
+
const setPathCollection = async function(changes, options = {}) {
|
|
140
149
|
const state = this;
|
|
141
|
-
const update = changes.reduce((
|
|
150
|
+
const update = await changes.reduce(async (promise, change) => {
|
|
151
|
+
const acc = await promise;
|
|
142
152
|
if (change[0] === "update") {
|
|
143
|
-
const result = setByPath.call(state, change[1], change[2], {
|
|
153
|
+
const result = setByPath.call(state, change[1], change[2], {
|
|
154
|
+
preventStateUpdate: true
|
|
155
|
+
});
|
|
144
156
|
return overwriteDeep(acc, result);
|
|
145
157
|
} else if (change[0] === "delete") {
|
|
146
|
-
removeByPath.call(state, change[1], options)
|
|
158
|
+
await removeByPath.call(state, change[1], __spreadProps(__spreadValues({}, options), {
|
|
159
|
+
preventUpdate: true
|
|
160
|
+
}));
|
|
147
161
|
}
|
|
148
162
|
return acc;
|
|
149
|
-
}, {});
|
|
163
|
+
}, Promise.resolve({}));
|
|
150
164
|
return state.update(update, options);
|
|
151
165
|
};
|
|
152
|
-
const removeByPath = function(path, options = {}) {
|
|
166
|
+
const removeByPath = async function(path, options = {}) {
|
|
153
167
|
const state = this;
|
|
154
168
|
removeNestedKeyByPath(state, path);
|
|
155
169
|
if (options.preventUpdate) return path;
|
|
156
|
-
return state.update({}, options);
|
|
170
|
+
return await state.update({}, options);
|
|
157
171
|
};
|
|
158
|
-
const removePathCollection = function(changes, options = {}) {
|
|
172
|
+
const removePathCollection = async function(changes, options = {}) {
|
|
159
173
|
const state = this;
|
|
160
|
-
changes.forEach((item) => {
|
|
161
|
-
removeByPath(item, { preventUpdate: true });
|
|
174
|
+
changes.forEach(async (item) => {
|
|
175
|
+
await removeByPath(item, { preventUpdate: true });
|
|
162
176
|
});
|
|
163
|
-
return state.update({}, options);
|
|
177
|
+
return await state.update({}, options);
|
|
164
178
|
};
|
|
165
179
|
const getByPath = function(path, options = {}) {
|
|
166
180
|
const state = this;
|
|
167
181
|
return getInObjectByPath(state, path);
|
|
168
182
|
};
|
|
169
|
-
const reset = function(options = {}) {
|
|
183
|
+
const reset = async function(options = {}) {
|
|
170
184
|
const state = this;
|
|
171
185
|
const value = deepClone(state.parse());
|
|
172
|
-
return state.set(value, __spreadValues({ replace: true }, options));
|
|
186
|
+
return await state.set(value, __spreadValues({ replace: true }, options));
|
|
173
187
|
};
|
|
174
|
-
const apply = function(func, options = {}) {
|
|
188
|
+
const apply = async function(func, options = {}) {
|
|
175
189
|
const state = this;
|
|
176
190
|
if (isFunction(func)) {
|
|
177
191
|
const value = func(state);
|
|
178
|
-
return state.update(value, __spreadValues({ replace: true }, options));
|
|
192
|
+
return await state.update(value, __spreadValues({ replace: true }, options));
|
|
179
193
|
}
|
|
180
194
|
};
|
|
181
|
-
const applyReplace = function(func, options = {}) {
|
|
195
|
+
const applyReplace = async function(func, options = {}) {
|
|
182
196
|
const state = this;
|
|
183
197
|
if (isFunction(func)) {
|
|
184
198
|
const value = func(state);
|
|
185
|
-
return state.replace(value, options);
|
|
199
|
+
return await state.replace(value, options);
|
|
186
200
|
}
|
|
187
201
|
};
|
|
188
|
-
const applyFunction = function(func, options = {}) {
|
|
202
|
+
const applyFunction = async function(func, options = {}) {
|
|
189
203
|
const state = this;
|
|
190
204
|
if (isFunction(func)) {
|
|
191
|
-
func(state);
|
|
192
|
-
return state.update(state.parse(), __spreadValues({ replace: true }, options));
|
|
205
|
+
await func(state);
|
|
206
|
+
return await state.update(state.parse(), __spreadValues({ replace: true }, options));
|
|
193
207
|
}
|
|
194
208
|
};
|
|
195
|
-
const quietUpdate = function(obj, options = {}) {
|
|
209
|
+
const quietUpdate = async function(obj, options = {}) {
|
|
196
210
|
const state = this;
|
|
197
|
-
return state.update(obj, __spreadValues({ preventUpdate: true }, options));
|
|
211
|
+
return await state.update(obj, __spreadValues({ preventUpdate: true }, options));
|
|
198
212
|
};
|
|
199
|
-
const replace = function(obj, options = {}) {
|
|
213
|
+
const replace = async function(obj, options = {}) {
|
|
200
214
|
const state = this;
|
|
201
215
|
for (const param in obj) {
|
|
202
216
|
state[param] = obj[param];
|
|
203
217
|
}
|
|
204
|
-
return state.update(obj, options);
|
|
218
|
+
return await state.update(obj, options);
|
|
205
219
|
};
|
|
206
|
-
const quietReplace = function(obj, options = {}) {
|
|
220
|
+
const quietReplace = async function(obj, options = {}) {
|
|
207
221
|
const state = this;
|
|
208
|
-
return state.replace(obj, __spreadValues({ preventUpdate: true }, options));
|
|
222
|
+
return await state.replace(obj, __spreadValues({ preventUpdate: true }, options));
|
|
209
223
|
};
|
|
210
224
|
const keys = function(obj, options = {}) {
|
|
211
225
|
const state = this;
|
|
@@ -215,11 +229,56 @@ const values = function(obj, options = {}) {
|
|
|
215
229
|
const state = this;
|
|
216
230
|
return Object.values(state);
|
|
217
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
|
+
};
|
|
218
276
|
export {
|
|
219
277
|
add,
|
|
220
278
|
apply,
|
|
221
279
|
applyFunction,
|
|
222
280
|
applyReplace,
|
|
281
|
+
applyStateMethods,
|
|
223
282
|
clean,
|
|
224
283
|
destroy,
|
|
225
284
|
getByPath,
|