@domql/state 2.3.122 → 2.3.125
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/createState.js +6 -4
- package/dist/cjs/createState.js +33 -31
- package/dist/cjs/ignore.js +1 -0
- package/dist/cjs/inherit.js +29 -9
- package/dist/cjs/methods.js +18 -6
- package/dist/cjs/updateState.js +23 -11
- package/ignore.js +1 -1
- package/inherit.js +27 -8
- package/methods.js +18 -6
- package/package.json +2 -2
- package/updateState.js +27 -12
package/createState.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { triggerEventOn } from '@domql/event'
|
|
4
4
|
import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
|
|
5
5
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
|
-
import { add, apply, clean, destroy, parse, remove, rootUpdate, toggle } from './methods'
|
|
6
|
+
import { add, apply, clean, destroy, parse, remove, rootUpdate, set, toggle } from './methods'
|
|
7
7
|
import { updateState } from './updateState'
|
|
8
8
|
import { checkIfInherits, createInheritedState } from './inherit'
|
|
9
9
|
|
|
@@ -12,16 +12,17 @@ export const createState = function (element, parent, opts) {
|
|
|
12
12
|
|
|
13
13
|
const objectizeState = checkForTypes(element)
|
|
14
14
|
if (objectizeState === false) return parent.state || {}
|
|
15
|
-
else element.state = objectizeState
|
|
15
|
+
else element.state = deepClone(objectizeState, IGNORE_STATE_PARAMS)
|
|
16
16
|
|
|
17
17
|
const whatInitReturns = triggerEventOn('stateInit', element)
|
|
18
18
|
if (whatInitReturns === false) return element.state
|
|
19
19
|
|
|
20
20
|
if (checkIfInherits(element)) {
|
|
21
|
-
|
|
21
|
+
const inheritedState = createInheritedState(element, parent)
|
|
22
|
+
element.state = inheritedState || {}
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
const dependentState = applyDependentState(element, state)
|
|
25
|
+
const dependentState = applyDependentState(element, element.state)
|
|
25
26
|
if (dependentState) element.state = dependentState
|
|
26
27
|
|
|
27
28
|
// NOTE: Only true when 'onlyResolveExtends' option is set to true
|
|
@@ -83,6 +84,7 @@ const applyMethods = (element) => {
|
|
|
83
84
|
state.remove = remove
|
|
84
85
|
state.apply = apply
|
|
85
86
|
state.parent = element.parent.state
|
|
87
|
+
state.set = set
|
|
86
88
|
state.__element = element
|
|
87
89
|
state.__children = {}
|
|
88
90
|
state.__root = ref.__root ? ref.__root.state : state
|
package/dist/cjs/createState.js
CHANGED
|
@@ -33,14 +33,15 @@ const createState = function(element, parent, opts) {
|
|
|
33
33
|
if (objectizeState === false)
|
|
34
34
|
return parent.state || {};
|
|
35
35
|
else
|
|
36
|
-
element.state = objectizeState;
|
|
36
|
+
element.state = (0, import_utils.deepClone)(objectizeState, import_ignore.IGNORE_STATE_PARAMS);
|
|
37
37
|
const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element);
|
|
38
38
|
if (whatInitReturns === false)
|
|
39
39
|
return element.state;
|
|
40
40
|
if ((0, import_inherit.checkIfInherits)(element)) {
|
|
41
|
-
|
|
41
|
+
const inheritedState = (0, import_inherit.createInheritedState)(element, parent);
|
|
42
|
+
element.state = inheritedState || {};
|
|
42
43
|
}
|
|
43
|
-
const dependentState = applyDependentState(element, state);
|
|
44
|
+
const dependentState = applyDependentState(element, element.state);
|
|
44
45
|
if (dependentState)
|
|
45
46
|
element.state = dependentState;
|
|
46
47
|
if (skip)
|
|
@@ -49,8 +50,8 @@ const createState = function(element, parent, opts) {
|
|
|
49
50
|
(0, import_event.triggerEventOn)("stateCreated", element);
|
|
50
51
|
return element.state;
|
|
51
52
|
};
|
|
52
|
-
const applyDependentState = (element,
|
|
53
|
-
const { __ref: ref } =
|
|
53
|
+
const applyDependentState = (element, state) => {
|
|
54
|
+
const { __ref: ref } = state;
|
|
54
55
|
if (!ref)
|
|
55
56
|
return;
|
|
56
57
|
const dependentState = (0, import_utils.deepClone)(ref, import_ignore.IGNORE_STATE_PARAMS);
|
|
@@ -59,42 +60,43 @@ const applyDependentState = (element, state2) => {
|
|
|
59
60
|
return dependentState;
|
|
60
61
|
};
|
|
61
62
|
const checkForTypes = (element) => {
|
|
62
|
-
const { state
|
|
63
|
-
if ((0, import_utils.isFunction)(
|
|
64
|
-
ref.__state =
|
|
65
|
-
return (0, import_utils.exec)(
|
|
63
|
+
const { state, __ref: ref } = element;
|
|
64
|
+
if ((0, import_utils.isFunction)(state)) {
|
|
65
|
+
ref.__state = state;
|
|
66
|
+
return (0, import_utils.exec)(state, element);
|
|
66
67
|
}
|
|
67
|
-
if ((0, import_utils.is)(
|
|
68
|
-
ref.__state =
|
|
68
|
+
if ((0, import_utils.is)(state)("string", "number")) {
|
|
69
|
+
ref.__state = state;
|
|
69
70
|
return {};
|
|
70
71
|
}
|
|
71
|
-
if (
|
|
72
|
+
if (state === true) {
|
|
72
73
|
ref.__state = element.key;
|
|
73
74
|
return {};
|
|
74
75
|
}
|
|
75
|
-
if (
|
|
76
|
+
if (state) {
|
|
76
77
|
ref.__hasRootState = true;
|
|
77
|
-
return
|
|
78
|
+
return state;
|
|
78
79
|
}
|
|
79
80
|
return false;
|
|
80
81
|
};
|
|
81
82
|
const applyMethods = (element) => {
|
|
82
|
-
const
|
|
83
|
+
const state = element.state;
|
|
83
84
|
const ref = element.__ref;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
85
|
+
state.clean = import_methods.clean;
|
|
86
|
+
state.parse = import_methods.parse;
|
|
87
|
+
state.destroy = import_methods.destroy;
|
|
88
|
+
state.update = import_updateState.updateState;
|
|
89
|
+
state.rootUpdate = import_methods.rootUpdate;
|
|
90
|
+
state.create = createState;
|
|
91
|
+
state.add = import_methods.add;
|
|
92
|
+
state.toggle = import_methods.toggle;
|
|
93
|
+
state.remove = import_methods.remove;
|
|
94
|
+
state.apply = import_methods.apply;
|
|
95
|
+
state.parent = element.parent.state;
|
|
96
|
+
state.set = import_methods.set;
|
|
97
|
+
state.__element = element;
|
|
98
|
+
state.__children = {};
|
|
99
|
+
state.__root = ref.__root ? ref.__root.state : state;
|
|
100
|
+
if (state.parent)
|
|
101
|
+
state.parent.__children[element.key] = state;
|
|
100
102
|
};
|
package/dist/cjs/ignore.js
CHANGED
package/dist/cjs/inherit.js
CHANGED
|
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var inherit_exports = {};
|
|
20
20
|
__export(inherit_exports, {
|
|
21
21
|
checkIfInherits: () => checkIfInherits,
|
|
22
|
+
createChangesByKey: () => createChangesByKey,
|
|
22
23
|
createInheritedState: () => createInheritedState,
|
|
23
24
|
findInheritedState: () => findInheritedState,
|
|
24
25
|
getChildStateInKey: () => getChildStateInKey,
|
|
@@ -27,7 +28,10 @@ __export(inherit_exports, {
|
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(inherit_exports);
|
|
29
30
|
var import_utils = require("@domql/utils");
|
|
31
|
+
var import_ignore = require("./ignore");
|
|
30
32
|
const getParentStateInKey = (stateKey2, parentState) => {
|
|
33
|
+
if (!stateKey2.includes("../"))
|
|
34
|
+
return;
|
|
31
35
|
const arr = stateKey2.split("../");
|
|
32
36
|
const arrLength = arr.length - 1;
|
|
33
37
|
for (let i = 0; i < arrLength; i++) {
|
|
@@ -37,7 +41,7 @@ const getParentStateInKey = (stateKey2, parentState) => {
|
|
|
37
41
|
}
|
|
38
42
|
return parentState;
|
|
39
43
|
};
|
|
40
|
-
const getChildStateInKey = (stateKey2, parentState) => {
|
|
44
|
+
const getChildStateInKey = (stateKey2, parentState, options) => {
|
|
41
45
|
const arr = stateKey2.split("/");
|
|
42
46
|
const arrLength = arr.length - 1;
|
|
43
47
|
for (let i = 0; i < arrLength; i++) {
|
|
@@ -50,29 +54,32 @@ const getChildStateInKey = (stateKey2, parentState) => {
|
|
|
50
54
|
} else
|
|
51
55
|
return;
|
|
52
56
|
}
|
|
57
|
+
if (options.returnParent)
|
|
58
|
+
return parentState;
|
|
53
59
|
return parentState[stateKey2];
|
|
54
60
|
};
|
|
55
|
-
const findInheritedState = (element, parent) => {
|
|
61
|
+
const findInheritedState = (element, parent, options = {}) => {
|
|
56
62
|
const ref = element.__ref;
|
|
57
63
|
let stateKey2 = ref.__state;
|
|
58
|
-
if (!
|
|
59
|
-
return
|
|
64
|
+
if (!checkIfInherits(element))
|
|
65
|
+
return;
|
|
60
66
|
let parentState = parent.state;
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
const findGrandParentState = getParentStateInKey(stateKey2, parent.state);
|
|
68
|
+
if (findGrandParentState) {
|
|
69
|
+
parentState = findGrandParentState;
|
|
63
70
|
stateKey2 = stateKey2.replaceAll("../", "");
|
|
64
71
|
}
|
|
65
72
|
if (!parentState)
|
|
66
73
|
return;
|
|
67
|
-
return getChildStateInKey(stateKey2, parentState);
|
|
74
|
+
return getChildStateInKey(stateKey2, parentState, options);
|
|
68
75
|
};
|
|
69
76
|
const createInheritedState = (element, parent) => {
|
|
70
77
|
const ref = element.__ref;
|
|
71
78
|
const inheritedState = findInheritedState(element, parent);
|
|
72
79
|
if (!inheritedState)
|
|
73
|
-
return;
|
|
80
|
+
return element.state;
|
|
74
81
|
if ((0, import_utils.is)(inheritedState)("object", "array")) {
|
|
75
|
-
return (0, import_utils.deepClone)(inheritedState);
|
|
82
|
+
return (0, import_utils.deepClone)(inheritedState, import_ignore.IGNORE_STATE_PARAMS);
|
|
76
83
|
} else if ((0, import_utils.is)(inheritedState)("string", "number")) {
|
|
77
84
|
ref.__stateType = "string";
|
|
78
85
|
return { value: inheritedState };
|
|
@@ -92,3 +99,16 @@ const isState = function(state) {
|
|
|
92
99
|
const keys = Object.keys(state);
|
|
93
100
|
return (0, import_utils.arrayContainsOtherArray)(keys, ["update", "parse", "clean", "create", "parent", "rootUpdate"]);
|
|
94
101
|
};
|
|
102
|
+
const createChangesByKey = (path, value) => {
|
|
103
|
+
if (!path) {
|
|
104
|
+
return value || {};
|
|
105
|
+
}
|
|
106
|
+
const keys = path.split("/");
|
|
107
|
+
let obj = {};
|
|
108
|
+
let ref = obj;
|
|
109
|
+
keys.forEach((key, index) => {
|
|
110
|
+
ref[key] = index === keys.length - 1 ? value || {} : {};
|
|
111
|
+
ref = ref[key];
|
|
112
|
+
});
|
|
113
|
+
return obj;
|
|
114
|
+
};
|
package/dist/cjs/methods.js
CHANGED
|
@@ -25,6 +25,7 @@ __export(methods_exports, {
|
|
|
25
25
|
parse: () => parse,
|
|
26
26
|
remove: () => remove,
|
|
27
27
|
rootUpdate: () => rootUpdate,
|
|
28
|
+
set: () => set,
|
|
28
29
|
toggle: () => toggle
|
|
29
30
|
});
|
|
30
31
|
module.exports = __toCommonJS(methods_exports);
|
|
@@ -51,7 +52,9 @@ const clean = function(options = {}) {
|
|
|
51
52
|
delete state[param];
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
|
-
|
|
55
|
+
if (!options.preventStateUpdate) {
|
|
56
|
+
state.update(state, { replace: true, skipOverwrite: true, options });
|
|
57
|
+
}
|
|
55
58
|
return state;
|
|
56
59
|
};
|
|
57
60
|
const destroy = function() {
|
|
@@ -77,6 +80,7 @@ const rootUpdate = function(obj, options = {}) {
|
|
|
77
80
|
const state = this;
|
|
78
81
|
if (!state)
|
|
79
82
|
return;
|
|
83
|
+
console.log(options);
|
|
80
84
|
const rootState = state.__element.__ref.__root.state;
|
|
81
85
|
return rootState.update(obj, options);
|
|
82
86
|
};
|
|
@@ -84,26 +88,34 @@ const add = function(value, options = {}) {
|
|
|
84
88
|
const state = this;
|
|
85
89
|
if ((0, import_utils.isArray)(state)) {
|
|
86
90
|
state.push(value);
|
|
87
|
-
state.update(state.parse(), {
|
|
91
|
+
state.update(state.parse(), { replace: true, ...options });
|
|
92
|
+
} else if ((0, import_utils.isObject)(state)) {
|
|
93
|
+
const key = Object.keys(state).length;
|
|
94
|
+
state.update({ [key]: value }, options);
|
|
88
95
|
}
|
|
89
96
|
};
|
|
90
97
|
const toggle = function(key, options = {}) {
|
|
91
98
|
const state = this;
|
|
92
|
-
state[key]
|
|
93
|
-
state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options });
|
|
99
|
+
state.update({ [key]: !state[key] }, options);
|
|
94
100
|
};
|
|
95
101
|
const remove = function(key, options = {}) {
|
|
96
102
|
const state = this;
|
|
103
|
+
console.log(state);
|
|
97
104
|
if ((0, import_utils.isArray)(state))
|
|
98
105
|
(0, import_utils.removeFromArray)(state, key);
|
|
99
106
|
if ((0, import_utils.isObject)(state))
|
|
100
107
|
(0, import_utils.removeFromObject)(state, key);
|
|
101
|
-
return state.update(state.parse(), {
|
|
108
|
+
return state.update(state.parse(), { replace: true, ...options });
|
|
109
|
+
};
|
|
110
|
+
const set = function(value, options = {}) {
|
|
111
|
+
const state = this;
|
|
112
|
+
state.clean({ preventStateUpdate: true });
|
|
113
|
+
return state.update(value, { replace: true, ...options });
|
|
102
114
|
};
|
|
103
115
|
const apply = function(func, options = {}) {
|
|
104
116
|
const state = this;
|
|
105
117
|
if ((0, import_utils.isFunction)(func)) {
|
|
106
118
|
func(state);
|
|
107
|
-
return state.update(state, {
|
|
119
|
+
return state.update(state, { replace: true, ...options });
|
|
108
120
|
}
|
|
109
121
|
};
|
package/dist/cjs/updateState.js
CHANGED
|
@@ -26,18 +26,28 @@ var import_event = require("@domql/event");
|
|
|
26
26
|
var import_ignore = require("./ignore");
|
|
27
27
|
var import_utils = require("@domql/utils");
|
|
28
28
|
var import_inherit = require("./inherit");
|
|
29
|
-
const
|
|
29
|
+
const STATE_UPDATE_OPTIONS = {
|
|
30
|
+
preventHoistElementUpdate: false,
|
|
31
|
+
updateByState: true
|
|
32
|
+
};
|
|
33
|
+
const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
|
|
30
34
|
const state = this;
|
|
31
35
|
const element = state.__element;
|
|
32
36
|
if (!state.__element)
|
|
33
37
|
(0, import_report.report)("ElementOnStateIsNotDefined");
|
|
38
|
+
if (options.preventInheritAtCurrentState === true) {
|
|
39
|
+
options.preventInheritAtCurrentState = state;
|
|
40
|
+
} else if (options.preventInheritAtCurrentState)
|
|
41
|
+
return;
|
|
34
42
|
if (!options.preventInitStateUpdateListener) {
|
|
35
43
|
const initStateUpdateReturns = (0, import_event.triggerEventOn)("initStateUpdated", element, obj);
|
|
36
44
|
if (initStateUpdateReturns === false)
|
|
37
45
|
return element;
|
|
38
46
|
}
|
|
39
47
|
applyOverwrite(state, obj, options);
|
|
40
|
-
hoistStateUpdate(state, obj, options);
|
|
48
|
+
const updateIsHousted = hoistStateUpdate(state, obj, options);
|
|
49
|
+
if (updateIsHousted)
|
|
50
|
+
return state;
|
|
41
51
|
updateDependentState(state, obj, options);
|
|
42
52
|
applyElementUpdate(state, obj, options);
|
|
43
53
|
if (!options.preventStateUpdateListener) {
|
|
@@ -63,20 +73,24 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
63
73
|
if (!stateKey)
|
|
64
74
|
return;
|
|
65
75
|
const asksForInherit = (0, import_inherit.checkIfInherits)(element);
|
|
66
|
-
const inheritedState = (0, import_inherit.findInheritedState)(element, parent);
|
|
76
|
+
const inheritedState = (0, import_inherit.findInheritedState)(element, parent, { returnParent: true });
|
|
67
77
|
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
|
|
68
78
|
if (!shouldPropagateState)
|
|
69
79
|
return;
|
|
70
|
-
console.log(inheritedState);
|
|
71
80
|
const isStringState = ref.__stateType === "string";
|
|
72
81
|
const value = isStringState ? state.value : state.parse();
|
|
73
82
|
const passedValue = isStringState ? state.value : obj;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
83
|
+
const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
|
|
84
|
+
const changesValue = (0, import_inherit.createChangesByKey)(stateKey, passedValue);
|
|
85
|
+
const targetParent = findGrandParentState || parent.state;
|
|
86
|
+
if (options.replace)
|
|
87
|
+
targetParent[stateKey] = value;
|
|
88
|
+
targetParent.update(changesValue, {
|
|
89
|
+
...options,
|
|
77
90
|
preventUpdate: options.preventHoistElementUpdate,
|
|
78
|
-
|
|
91
|
+
skipOverwrite: options.replace
|
|
79
92
|
});
|
|
93
|
+
return true;
|
|
80
94
|
};
|
|
81
95
|
const updateDependentState = (state, obj, options) => {
|
|
82
96
|
if (!state.__depends)
|
|
@@ -91,13 +105,11 @@ const applyElementUpdate = (state, obj, options) => {
|
|
|
91
105
|
if (!options.preventUpdate) {
|
|
92
106
|
element.update({}, {
|
|
93
107
|
...options,
|
|
94
|
-
updateByState: true
|
|
95
|
-
preventUpdateTriggerStateUpdate: true
|
|
108
|
+
updateByState: true
|
|
96
109
|
});
|
|
97
110
|
} else if (options.preventUpdate === "recursive") {
|
|
98
111
|
element.update({}, {
|
|
99
112
|
...options,
|
|
100
|
-
preventUpdateTriggerStateUpdate: true,
|
|
101
113
|
updateByState: true,
|
|
102
114
|
preventUpdate: true
|
|
103
115
|
});
|
package/ignore.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
export const IGNORE_STATE_PARAMS = [
|
|
4
|
-
'update', 'parse', 'clean', 'create', 'destroy', 'add', 'toggle', 'remove', 'apply',
|
|
4
|
+
'update', 'parse', 'clean', 'create', 'destroy', 'add', 'toggle', 'remove', 'apply', 'set',
|
|
5
5
|
'rootUpdate', 'parent', '__element', '__depends', '__ref', '__children', '__root'
|
|
6
6
|
]
|
package/inherit.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
import { arrayContainsOtherArray, deepClone, is, isNot, isObjectLike } from '@domql/utils'
|
|
4
|
+
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
4
5
|
|
|
5
6
|
export const getParentStateInKey = (stateKey, parentState) => {
|
|
7
|
+
if (!stateKey.includes('../')) return
|
|
6
8
|
const arr = stateKey.split('../')
|
|
7
9
|
const arrLength = arr.length - 1
|
|
8
10
|
for (let i = 0; i < arrLength; i++) {
|
|
@@ -12,7 +14,7 @@ export const getParentStateInKey = (stateKey, parentState) => {
|
|
|
12
14
|
return parentState
|
|
13
15
|
}
|
|
14
16
|
|
|
15
|
-
export const getChildStateInKey = (stateKey, parentState) => {
|
|
17
|
+
export const getChildStateInKey = (stateKey, parentState, options) => {
|
|
16
18
|
const arr = stateKey.split('/')
|
|
17
19
|
const arrLength = arr.length - 1
|
|
18
20
|
for (let i = 0; i < arrLength; i++) {
|
|
@@ -24,30 +26,33 @@ export const getChildStateInKey = (stateKey, parentState) => {
|
|
|
24
26
|
parentState = childInParent
|
|
25
27
|
} else return
|
|
26
28
|
}
|
|
29
|
+
if (options.returnParent) return parentState
|
|
27
30
|
return parentState[stateKey]
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
export const findInheritedState = (element, parent) => {
|
|
33
|
+
export const findInheritedState = (element, parent, options = {}) => {
|
|
31
34
|
const ref = element.__ref
|
|
32
35
|
let stateKey = ref.__state
|
|
33
|
-
if (!
|
|
36
|
+
if (!checkIfInherits(element)) return
|
|
34
37
|
|
|
35
38
|
let parentState = parent.state
|
|
36
|
-
|
|
37
|
-
|
|
39
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state)
|
|
40
|
+
if (findGrandParentState) {
|
|
41
|
+
parentState = findGrandParentState
|
|
38
42
|
stateKey = stateKey.replaceAll('../', '')
|
|
39
43
|
}
|
|
44
|
+
|
|
40
45
|
if (!parentState) return
|
|
41
|
-
return getChildStateInKey(stateKey, parentState)
|
|
46
|
+
return getChildStateInKey(stateKey, parentState, options)
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
export const createInheritedState = (element, parent) => {
|
|
45
50
|
const ref = element.__ref
|
|
46
51
|
const inheritedState = findInheritedState(element, parent)
|
|
47
|
-
if (!inheritedState) return
|
|
52
|
+
if (!inheritedState) return element.state
|
|
48
53
|
|
|
49
54
|
if (is(inheritedState)('object', 'array')) {
|
|
50
|
-
return deepClone(inheritedState)
|
|
55
|
+
return deepClone(inheritedState, IGNORE_STATE_PARAMS)
|
|
51
56
|
} else if (is(inheritedState)('string', 'number')) {
|
|
52
57
|
ref.__stateType = 'string'
|
|
53
58
|
return { value: inheritedState }
|
|
@@ -68,3 +73,17 @@ export const isState = function (state) {
|
|
|
68
73
|
const keys = Object.keys(state)
|
|
69
74
|
return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
|
|
70
75
|
}
|
|
76
|
+
|
|
77
|
+
export const createChangesByKey = (path, value) => {
|
|
78
|
+
if (!path) {
|
|
79
|
+
return value || {};
|
|
80
|
+
}
|
|
81
|
+
const keys = path.split('/');
|
|
82
|
+
let obj = {};
|
|
83
|
+
let ref = obj;
|
|
84
|
+
keys.forEach((key, index) => {
|
|
85
|
+
ref[key] = index === keys.length - 1 ? value || {} : {};
|
|
86
|
+
ref = ref[key];
|
|
87
|
+
});
|
|
88
|
+
return obj;
|
|
89
|
+
}
|
package/methods.js
CHANGED
|
@@ -26,7 +26,9 @@ export const clean = function (options = {}) {
|
|
|
26
26
|
delete state[param]
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
if (!options.preventStateUpdate) {
|
|
30
|
+
state.update(state, { replace: true, skipOverwrite: true, options })
|
|
31
|
+
}
|
|
30
32
|
return state
|
|
31
33
|
}
|
|
32
34
|
|
|
@@ -56,6 +58,7 @@ export const destroy = function () {
|
|
|
56
58
|
export const rootUpdate = function (obj, options = {}) {
|
|
57
59
|
const state = this
|
|
58
60
|
if (!state) return
|
|
61
|
+
console.log(options)
|
|
59
62
|
const rootState = (state.__element.__ref.__root).state
|
|
60
63
|
return rootState.update(obj, options)
|
|
61
64
|
}
|
|
@@ -64,27 +67,36 @@ export const add = function (value, options = {}) {
|
|
|
64
67
|
const state = this
|
|
65
68
|
if (isArray(state)) {
|
|
66
69
|
state.push(value)
|
|
67
|
-
state.update(state.parse(), {
|
|
70
|
+
state.update(state.parse(), { replace: true, ...options })
|
|
71
|
+
} else if (isObject(state)) {
|
|
72
|
+
const key = Object.keys(state).length
|
|
73
|
+
state.update({ [key]: value }, options)
|
|
68
74
|
}
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
export const toggle = function (key, options = {}) {
|
|
72
78
|
const state = this
|
|
73
|
-
state[key]
|
|
74
|
-
state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options })
|
|
79
|
+
state.update({ [key]: !state[key] }, options)
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
export const remove = function (key, options = {}) {
|
|
78
83
|
const state = this
|
|
84
|
+
console.log(state)
|
|
79
85
|
if (isArray(state)) removeFromArray(state, key)
|
|
80
86
|
if (isObject(state)) removeFromObject(state, key)
|
|
81
|
-
return state.update(state.parse(), {
|
|
87
|
+
return state.update(state.parse(), { replace: true, ...options })
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const set = function (value, options = {}) {
|
|
91
|
+
const state = this
|
|
92
|
+
state.clean({ preventStateUpdate: true })
|
|
93
|
+
return state.update(value, { replace: true, ...options })
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
export const apply = function (func, options = {}) {
|
|
85
97
|
const state = this
|
|
86
98
|
if (isFunction(func)) {
|
|
87
99
|
func(state)
|
|
88
|
-
return state.update(state, {
|
|
100
|
+
return state.update(state, { replace: true, ...options })
|
|
89
101
|
}
|
|
90
102
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.125",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"@domql/report": "latest",
|
|
27
27
|
"@domql/utils": "latest"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "c62e01157f488334d07a8bbd67f892d617d48f06"
|
|
30
30
|
}
|
package/updateState.js
CHANGED
|
@@ -4,13 +4,21 @@ import { report } from '@domql/report'
|
|
|
4
4
|
import { triggerEventOn } from '@domql/event'
|
|
5
5
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
6
|
import { deepMerge, overwriteDeep, overwriteShallow } from '@domql/utils'
|
|
7
|
-
import { checkIfInherits, findInheritedState } from './inherit'
|
|
7
|
+
import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit'
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const STATE_UPDATE_OPTIONS = {
|
|
10
|
+
preventHoistElementUpdate: false,
|
|
11
|
+
updateByState: true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
|
|
10
15
|
const state = this
|
|
11
16
|
const element = state.__element
|
|
12
17
|
|
|
13
18
|
if (!state.__element) report('ElementOnStateIsNotDefined')
|
|
19
|
+
if (options.preventInheritAtCurrentState === true) {
|
|
20
|
+
options.preventInheritAtCurrentState = state
|
|
21
|
+
} else if (options.preventInheritAtCurrentState) return
|
|
14
22
|
|
|
15
23
|
if (!options.preventInitStateUpdateListener) {
|
|
16
24
|
const initStateUpdateReturns = triggerEventOn('initStateUpdated', element, obj)
|
|
@@ -19,7 +27,8 @@ export const updateState = function (obj, options = {}) {
|
|
|
19
27
|
|
|
20
28
|
applyOverwrite(state, obj, options)
|
|
21
29
|
|
|
22
|
-
hoistStateUpdate(state, obj, options)
|
|
30
|
+
const updateIsHousted = hoistStateUpdate(state, obj, options)
|
|
31
|
+
if (updateIsHousted) return state
|
|
23
32
|
|
|
24
33
|
updateDependentState(state, obj, options)
|
|
25
34
|
|
|
@@ -40,6 +49,11 @@ const applyOverwrite = (state, obj, options) => {
|
|
|
40
49
|
return
|
|
41
50
|
}
|
|
42
51
|
|
|
52
|
+
// if (skipOverwrite === 'skipOverwrite') {
|
|
53
|
+
// deepMerge(state, obj, IGNORE_STATE_PARAMS)
|
|
54
|
+
// return
|
|
55
|
+
// }
|
|
56
|
+
|
|
43
57
|
if (!skipOverwrite) {
|
|
44
58
|
const overwriteFunc = shallow ? overwriteShallow : overwriteDeep
|
|
45
59
|
overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
|
|
@@ -53,21 +67,24 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
53
67
|
if (!stateKey) return
|
|
54
68
|
|
|
55
69
|
const asksForInherit = checkIfInherits(element)
|
|
56
|
-
const inheritedState = findInheritedState(element, parent)
|
|
70
|
+
const inheritedState = findInheritedState(element, parent, { returnParent: true })
|
|
57
71
|
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation
|
|
58
72
|
if (!shouldPropagateState) return
|
|
59
|
-
console.log(inheritedState)
|
|
60
73
|
|
|
61
74
|
const isStringState = (ref.__stateType === 'string')
|
|
62
75
|
const value = isStringState ? state.value : state.parse()
|
|
63
76
|
const passedValue = isStringState ? state.value : obj
|
|
64
77
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
78
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state)
|
|
79
|
+
const changesValue = createChangesByKey(stateKey, passedValue)
|
|
80
|
+
const targetParent = findGrandParentState || parent.state
|
|
81
|
+
if (options.replace) targetParent[stateKey] = value
|
|
82
|
+
targetParent.update(changesValue, {
|
|
83
|
+
...options,
|
|
68
84
|
preventUpdate: options.preventHoistElementUpdate,
|
|
69
|
-
|
|
85
|
+
skipOverwrite: options.replace
|
|
70
86
|
})
|
|
87
|
+
return true
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
const updateDependentState = (state, obj, options) => {
|
|
@@ -83,13 +100,11 @@ const applyElementUpdate = (state, obj, options) => {
|
|
|
83
100
|
if (!options.preventUpdate) {
|
|
84
101
|
element.update({}, {
|
|
85
102
|
...options,
|
|
86
|
-
updateByState: true
|
|
87
|
-
preventUpdateTriggerStateUpdate: true
|
|
103
|
+
updateByState: true
|
|
88
104
|
})
|
|
89
105
|
} else if (options.preventUpdate === 'recursive') {
|
|
90
106
|
element.update({}, {
|
|
91
107
|
...options,
|
|
92
|
-
preventUpdateTriggerStateUpdate: true,
|
|
93
108
|
updateByState: true,
|
|
94
109
|
preventUpdate: true
|
|
95
110
|
})
|