@domql/state 2.3.121 → 2.3.123
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 +5 -4
- package/dist/cjs/createState.js +34 -33
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/{utils.js → inherit.js} +53 -28
- package/dist/cjs/methods.js +7 -5
- package/dist/cjs/updateState.js +19 -12
- package/index.js +1 -1
- package/{utils.js → inherit.js} +39 -16
- package/methods.js +8 -5
- package/package.json +2 -2
- package/updateState.js +19 -12
package/createState.js
CHANGED
|
@@ -5,23 +5,24 @@ import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
|
|
|
5
5
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
6
|
import { add, apply, clean, destroy, parse, remove, rootUpdate, toggle } from './methods'
|
|
7
7
|
import { updateState } from './updateState'
|
|
8
|
-
import { checkIfInherits, createInheritedState } from './
|
|
8
|
+
import { checkIfInherits, createInheritedState } from './inherit'
|
|
9
9
|
|
|
10
10
|
export const createState = function (element, parent, opts) {
|
|
11
11
|
const skip = (opts && opts.skip) ? opts.skip : false
|
|
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
|
package/dist/cjs/createState.js
CHANGED
|
@@ -26,21 +26,22 @@ var import_utils = require("@domql/utils");
|
|
|
26
26
|
var import_ignore = require("./ignore");
|
|
27
27
|
var import_methods = require("./methods");
|
|
28
28
|
var import_updateState = require("./updateState");
|
|
29
|
-
var
|
|
29
|
+
var import_inherit = require("./inherit");
|
|
30
30
|
const createState = function(element, parent, opts) {
|
|
31
31
|
const skip = opts && opts.skip ? opts.skip : false;
|
|
32
32
|
const objectizeState = checkForTypes(element);
|
|
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
|
-
if ((0,
|
|
41
|
-
|
|
40
|
+
if ((0, import_inherit.checkIfInherits)(element)) {
|
|
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,42 @@ 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
|
-
if (
|
|
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.__element = element;
|
|
97
|
+
state.__children = {};
|
|
98
|
+
state.__root = ref.__root ? ref.__root.state : state;
|
|
99
|
+
if (state.parent)
|
|
100
|
+
state.parent.__children[element.key] = state;
|
|
100
101
|
};
|
package/dist/cjs/index.js
CHANGED
|
@@ -19,4 +19,4 @@ __reExport(state_exports, require("./ignore"), module.exports);
|
|
|
19
19
|
__reExport(state_exports, require("./createState"), module.exports);
|
|
20
20
|
__reExport(state_exports, require("./updateState"), module.exports);
|
|
21
21
|
__reExport(state_exports, require("./methods"), module.exports);
|
|
22
|
-
__reExport(state_exports, require("./
|
|
22
|
+
__reExport(state_exports, require("./inherit"), module.exports);
|
|
@@ -16,18 +16,23 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
19
|
+
var inherit_exports = {};
|
|
20
|
+
__export(inherit_exports, {
|
|
21
21
|
checkIfInherits: () => checkIfInherits,
|
|
22
|
+
createChangesByKey: () => createChangesByKey,
|
|
22
23
|
createInheritedState: () => createInheritedState,
|
|
24
|
+
findInheritedState: () => findInheritedState,
|
|
23
25
|
getChildStateInKey: () => getChildStateInKey,
|
|
24
26
|
getParentStateInKey: () => getParentStateInKey,
|
|
25
27
|
isState: () => isState
|
|
26
28
|
});
|
|
27
|
-
module.exports = __toCommonJS(
|
|
29
|
+
module.exports = __toCommonJS(inherit_exports);
|
|
28
30
|
var import_utils = require("@domql/utils");
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
var import_ignore = require("./ignore");
|
|
32
|
+
const getParentStateInKey = (stateKey2, parentState) => {
|
|
33
|
+
if (!stateKey2.includes("../"))
|
|
34
|
+
return;
|
|
35
|
+
const arr = stateKey2.split("../");
|
|
31
36
|
const arrLength = arr.length - 1;
|
|
32
37
|
for (let i = 0; i < arrLength; i++) {
|
|
33
38
|
if (!parentState.parent)
|
|
@@ -36,48 +41,55 @@ const getParentStateInKey = (stateKey, parentState) => {
|
|
|
36
41
|
}
|
|
37
42
|
return parentState;
|
|
38
43
|
};
|
|
39
|
-
const getChildStateInKey = (
|
|
40
|
-
const arr =
|
|
44
|
+
const getChildStateInKey = (stateKey2, parentState, options) => {
|
|
45
|
+
const arr = stateKey2.split("/");
|
|
41
46
|
const arrLength = arr.length - 1;
|
|
42
47
|
for (let i = 0; i < arrLength; i++) {
|
|
43
48
|
const childKey = arr[i];
|
|
44
49
|
const grandChildKey = arr[i + 1];
|
|
45
50
|
const childInParent = parentState[childKey];
|
|
46
51
|
if (childInParent && childInParent[grandChildKey]) {
|
|
47
|
-
|
|
52
|
+
stateKey2 = grandChildKey;
|
|
48
53
|
parentState = childInParent;
|
|
49
54
|
} else
|
|
50
55
|
return;
|
|
51
56
|
}
|
|
52
|
-
|
|
57
|
+
if (options.returnParent)
|
|
58
|
+
return parentState;
|
|
59
|
+
return parentState[stateKey2];
|
|
53
60
|
};
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
let
|
|
57
|
-
if (!
|
|
58
|
-
return
|
|
61
|
+
const findInheritedState = (element, parent, options = {}) => {
|
|
62
|
+
const ref = element.__ref;
|
|
63
|
+
let stateKey2 = ref.__state;
|
|
64
|
+
if (!checkIfInherits(element))
|
|
65
|
+
return;
|
|
59
66
|
let parentState = parent.state;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
const findGrandParentState = getParentStateInKey(stateKey2, parent.state);
|
|
68
|
+
if (findGrandParentState) {
|
|
69
|
+
parentState = findGrandParentState;
|
|
70
|
+
stateKey2 = stateKey2.replaceAll("../", "");
|
|
63
71
|
}
|
|
64
72
|
if (!parentState)
|
|
65
73
|
return;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
return getChildStateInKey(stateKey2, parentState, options);
|
|
75
|
+
};
|
|
76
|
+
const createInheritedState = (element, parent) => {
|
|
77
|
+
const ref = element.__ref;
|
|
78
|
+
const inheritedState = findInheritedState(element, parent);
|
|
79
|
+
if (!inheritedState)
|
|
80
|
+
return element.state;
|
|
81
|
+
if ((0, import_utils.is)(inheritedState)("object", "array")) {
|
|
82
|
+
return (0, import_utils.deepClone)(inheritedState, import_ignore.IGNORE_STATE_PARAMS);
|
|
83
|
+
} else if ((0, import_utils.is)(inheritedState)("string", "number")) {
|
|
84
|
+
ref.__stateType = "string";
|
|
85
|
+
return { value: inheritedState };
|
|
74
86
|
}
|
|
75
87
|
console.warn(stateKey, "is not present. Replacing with", {});
|
|
76
88
|
};
|
|
77
89
|
const checkIfInherits = (element) => {
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
if (!
|
|
90
|
+
const ref = element.__ref;
|
|
91
|
+
const stateKey2 = ref.__state;
|
|
92
|
+
if (!stateKey2 || (0, import_utils.isNot)(stateKey2)("number", "string"))
|
|
81
93
|
return false;
|
|
82
94
|
return true;
|
|
83
95
|
};
|
|
@@ -87,3 +99,16 @@ const isState = function(state) {
|
|
|
87
99
|
const keys = Object.keys(state);
|
|
88
100
|
return (0, import_utils.arrayContainsOtherArray)(keys, ["update", "parse", "clean", "create", "parent", "rootUpdate"]);
|
|
89
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
|
@@ -84,13 +84,15 @@ const add = function(value, options = {}) {
|
|
|
84
84
|
const state = this;
|
|
85
85
|
if ((0, import_utils.isArray)(state)) {
|
|
86
86
|
state.push(value);
|
|
87
|
-
state.update(state.parse(), {
|
|
87
|
+
state.update(state.parse(), { replace: true, ...options });
|
|
88
|
+
} else if ((0, import_utils.isObject)(state)) {
|
|
89
|
+
const key = Object.keys(state).length;
|
|
90
|
+
state.update({ [key]: value }, options);
|
|
88
91
|
}
|
|
89
92
|
};
|
|
90
93
|
const toggle = function(key, options = {}) {
|
|
91
94
|
const state = this;
|
|
92
|
-
state[key]
|
|
93
|
-
state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options });
|
|
95
|
+
state.update({ [key]: !state[key] }, options);
|
|
94
96
|
};
|
|
95
97
|
const remove = function(key, options = {}) {
|
|
96
98
|
const state = this;
|
|
@@ -98,12 +100,12 @@ const remove = function(key, options = {}) {
|
|
|
98
100
|
(0, import_utils.removeFromArray)(state, key);
|
|
99
101
|
if ((0, import_utils.isObject)(state))
|
|
100
102
|
(0, import_utils.removeFromObject)(state, key);
|
|
101
|
-
return state.update(state.parse(), {
|
|
103
|
+
return state.update(state.parse(), { replace: true, ...options });
|
|
102
104
|
};
|
|
103
105
|
const apply = function(func, options = {}) {
|
|
104
106
|
const state = this;
|
|
105
107
|
if ((0, import_utils.isFunction)(func)) {
|
|
106
108
|
func(state);
|
|
107
|
-
return state.update(state, {
|
|
109
|
+
return state.update(state, { replace: true, ...options });
|
|
108
110
|
}
|
|
109
111
|
};
|
package/dist/cjs/updateState.js
CHANGED
|
@@ -25,8 +25,11 @@ var import_report = require("@domql/report");
|
|
|
25
25
|
var import_event = require("@domql/event");
|
|
26
26
|
var import_ignore = require("./ignore");
|
|
27
27
|
var import_utils = require("@domql/utils");
|
|
28
|
-
var
|
|
29
|
-
const
|
|
28
|
+
var import_inherit = require("./inherit");
|
|
29
|
+
const STATE_UPDATE_OPTIONS = {
|
|
30
|
+
preventHoistElementUpdate: true
|
|
31
|
+
};
|
|
32
|
+
const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
|
|
30
33
|
const state = this;
|
|
31
34
|
const element = state.__element;
|
|
32
35
|
if (!state.__element)
|
|
@@ -58,23 +61,27 @@ const applyOverwrite = (state, obj, options) => {
|
|
|
58
61
|
};
|
|
59
62
|
const hoistStateUpdate = (state, obj, options) => {
|
|
60
63
|
const element = state.__element;
|
|
61
|
-
const
|
|
62
|
-
const stateKey =
|
|
64
|
+
const { parent, __ref: ref } = element;
|
|
65
|
+
const stateKey = ref.__state;
|
|
63
66
|
if (!stateKey)
|
|
64
67
|
return;
|
|
65
|
-
const asksForInherit = (0,
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
+
const asksForInherit = (0, import_inherit.checkIfInherits)(element);
|
|
69
|
+
const inheritedState = (0, import_inherit.findInheritedState)(element, parent, { returnParent: true });
|
|
70
|
+
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
|
|
68
71
|
if (!shouldPropagateState)
|
|
69
72
|
return;
|
|
70
|
-
const isStringState =
|
|
73
|
+
const isStringState = ref.__stateType === "string";
|
|
71
74
|
const value = isStringState ? state.value : state.parse();
|
|
72
75
|
const passedValue = isStringState ? state.value : obj;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
|
|
77
|
+
const changesValue = (0, import_inherit.createChangesByKey)(stateKey, passedValue);
|
|
78
|
+
const targetParent = findGrandParentState || parent.state;
|
|
79
|
+
if (options.replace)
|
|
80
|
+
targetParent[stateKey] = value;
|
|
81
|
+
targetParent.update(changesValue, {
|
|
82
|
+
...options,
|
|
76
83
|
preventUpdate: options.preventHoistElementUpdate,
|
|
77
|
-
|
|
84
|
+
skipOverwrite: options.replace
|
|
78
85
|
});
|
|
79
86
|
};
|
|
80
87
|
const updateDependentState = (state, obj, options) => {
|
package/index.js
CHANGED
package/{utils.js → inherit.js}
RENAMED
|
@@ -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,37 +26,44 @@ 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
|
|
31
|
-
const
|
|
32
|
-
let stateKey =
|
|
33
|
-
if (!
|
|
33
|
+
export const findInheritedState = (element, parent, options = {}) => {
|
|
34
|
+
const ref = element.__ref
|
|
35
|
+
let stateKey = ref.__state
|
|
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
|
|
46
|
+
return getChildStateInKey(stateKey, parentState, options)
|
|
47
|
+
}
|
|
41
48
|
|
|
42
|
-
|
|
43
|
-
|
|
49
|
+
export const createInheritedState = (element, parent) => {
|
|
50
|
+
const ref = element.__ref
|
|
51
|
+
const inheritedState = findInheritedState(element, parent)
|
|
52
|
+
if (!inheritedState) return element.state
|
|
44
53
|
|
|
45
|
-
if (is(
|
|
46
|
-
return deepClone(
|
|
47
|
-
} else if (is(
|
|
48
|
-
|
|
49
|
-
return { value:
|
|
54
|
+
if (is(inheritedState)('object', 'array')) {
|
|
55
|
+
return deepClone(inheritedState, IGNORE_STATE_PARAMS)
|
|
56
|
+
} else if (is(inheritedState)('string', 'number')) {
|
|
57
|
+
ref.__stateType = 'string'
|
|
58
|
+
return { value: inheritedState }
|
|
50
59
|
}
|
|
51
60
|
|
|
52
61
|
console.warn(stateKey, 'is not present. Replacing with', {})
|
|
53
62
|
}
|
|
54
63
|
|
|
55
64
|
export const checkIfInherits = (element) => {
|
|
56
|
-
const
|
|
57
|
-
const stateKey =
|
|
65
|
+
const ref = element.__ref
|
|
66
|
+
const stateKey = ref.__state
|
|
58
67
|
if (!stateKey || isNot(stateKey)('number', 'string')) return false
|
|
59
68
|
return true
|
|
60
69
|
}
|
|
@@ -64,3 +73,17 @@ export const isState = function (state) {
|
|
|
64
73
|
const keys = Object.keys(state)
|
|
65
74
|
return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
|
|
66
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
|
@@ -64,27 +64,30 @@ export const add = function (value, options = {}) {
|
|
|
64
64
|
const state = this
|
|
65
65
|
if (isArray(state)) {
|
|
66
66
|
state.push(value)
|
|
67
|
-
state.update(state.parse(), {
|
|
67
|
+
state.update(state.parse(), { replace: true, ...options })
|
|
68
|
+
}
|
|
69
|
+
else if (isObject(state)) {
|
|
70
|
+
const key = Object.keys(state).length
|
|
71
|
+
state.update({ [key]: value }, options)
|
|
68
72
|
}
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
export const toggle = function (key, options = {}) {
|
|
72
76
|
const state = this
|
|
73
|
-
state[key]
|
|
74
|
-
state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options })
|
|
77
|
+
state.update({ [key]: !state[key] }, options)
|
|
75
78
|
}
|
|
76
79
|
|
|
77
80
|
export const remove = function (key, options = {}) {
|
|
78
81
|
const state = this
|
|
79
82
|
if (isArray(state)) removeFromArray(state, key)
|
|
80
83
|
if (isObject(state)) removeFromObject(state, key)
|
|
81
|
-
return state.update(state.parse(), {
|
|
84
|
+
return state.update(state.parse(), { replace: true, ...options })
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
export const apply = function (func, options = {}) {
|
|
85
88
|
const state = this
|
|
86
89
|
if (isFunction(func)) {
|
|
87
90
|
func(state)
|
|
88
|
-
return state.update(state, {
|
|
91
|
+
return state.update(state, { replace: true, ...options })
|
|
89
92
|
}
|
|
90
93
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.123",
|
|
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": "feb0d24eb5924f0f9ebea660a93afffc7d6a2059"
|
|
30
30
|
}
|
package/updateState.js
CHANGED
|
@@ -4,9 +4,13 @@ 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 } from './
|
|
7
|
+
import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit'
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const STATE_UPDATE_OPTIONS = {
|
|
10
|
+
preventHoistElementUpdate: true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
|
|
10
14
|
const state = this
|
|
11
15
|
const element = state.__element
|
|
12
16
|
|
|
@@ -48,24 +52,27 @@ const applyOverwrite = (state, obj, options) => {
|
|
|
48
52
|
|
|
49
53
|
const hoistStateUpdate = (state, obj, options) => {
|
|
50
54
|
const element = state.__element
|
|
51
|
-
const
|
|
52
|
-
const stateKey =
|
|
55
|
+
const { parent, __ref: ref } = element
|
|
56
|
+
const stateKey = ref.__state
|
|
53
57
|
if (!stateKey) return
|
|
54
58
|
|
|
55
59
|
const asksForInherit = checkIfInherits(element)
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
|
|
60
|
+
const inheritedState = findInheritedState(element, parent, { returnParent: true })
|
|
61
|
+
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation
|
|
59
62
|
if (!shouldPropagateState) return
|
|
60
|
-
|
|
63
|
+
|
|
64
|
+
const isStringState = (ref.__stateType === 'string')
|
|
61
65
|
const value = isStringState ? state.value : state.parse()
|
|
62
66
|
const passedValue = isStringState ? state.value : obj
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state)
|
|
69
|
+
const changesValue = createChangesByKey(stateKey, passedValue)
|
|
70
|
+
const targetParent = findGrandParentState || parent.state
|
|
71
|
+
if (options.replace) targetParent[stateKey] = value
|
|
72
|
+
targetParent.update(changesValue, {
|
|
73
|
+
...options,
|
|
67
74
|
preventUpdate: options.preventHoistElementUpdate,
|
|
68
|
-
|
|
75
|
+
skipOverwrite: options.replace
|
|
69
76
|
})
|
|
70
77
|
}
|
|
71
78
|
|