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