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