@domql/state 2.5.52 → 2.5.56
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/dist/cjs/inherit.js +20 -6
- package/dist/cjs/methods.js +1 -1
- package/dist/cjs/updateState.js +8 -5
- package/inherit.js +18 -5
- package/methods.js +1 -1
- package/package.json +2 -2
- package/updateState.js +9 -7
package/dist/cjs/inherit.js
CHANGED
|
@@ -19,16 +19,24 @@ 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,
|
|
23
22
|
createInheritedState: () => createInheritedState,
|
|
23
|
+
createNestedObjectByKeyPath: () => createNestedObjectByKeyPath,
|
|
24
24
|
findInheritedState: () => findInheritedState,
|
|
25
25
|
getChildStateInKey: () => getChildStateInKey,
|
|
26
26
|
getParentStateInKey: () => getParentStateInKey,
|
|
27
|
+
getRootStateInKey: () => getRootStateInKey,
|
|
27
28
|
isState: () => isState
|
|
28
29
|
});
|
|
29
30
|
module.exports = __toCommonJS(inherit_exports);
|
|
30
31
|
var import_utils = require("@domql/utils");
|
|
31
32
|
var import_ignore = require("./ignore");
|
|
33
|
+
const getRootStateInKey = (stateKey, parentState) => {
|
|
34
|
+
if (!stateKey.includes("~/"))
|
|
35
|
+
return;
|
|
36
|
+
const arr = stateKey.split("~/");
|
|
37
|
+
if (arr.length > 1)
|
|
38
|
+
return parentState.__root;
|
|
39
|
+
};
|
|
32
40
|
const getParentStateInKey = (stateKey, parentState) => {
|
|
33
41
|
if (!stateKey.includes("../"))
|
|
34
42
|
return;
|
|
@@ -66,11 +74,17 @@ const findInheritedState = (element, parent, options = {}) => {
|
|
|
66
74
|
let stateKey = ref.__state;
|
|
67
75
|
if (!checkIfInherits(element))
|
|
68
76
|
return;
|
|
77
|
+
const rootState = getRootStateInKey(stateKey, parent.state);
|
|
69
78
|
let parentState = parent.state;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
if (rootState) {
|
|
80
|
+
parentState = rootState;
|
|
81
|
+
stateKey = stateKey.replaceAll("~/", "");
|
|
82
|
+
} else {
|
|
83
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state);
|
|
84
|
+
if (findGrandParentState) {
|
|
85
|
+
parentState = findGrandParentState;
|
|
86
|
+
stateKey = stateKey.replaceAll("../", "");
|
|
87
|
+
}
|
|
74
88
|
}
|
|
75
89
|
if (!parentState)
|
|
76
90
|
return;
|
|
@@ -101,7 +115,7 @@ const isState = function(state) {
|
|
|
101
115
|
return false;
|
|
102
116
|
return state.update && state.parse && state.clean && state.create && state.parent && state.destroy && state.rootUpdate && state.parentUpdate && state.toggle && state.add && state.apply && state.__element && state.__children;
|
|
103
117
|
};
|
|
104
|
-
const
|
|
118
|
+
const createNestedObjectByKeyPath = (path, value) => {
|
|
105
119
|
if (!path) {
|
|
106
120
|
return value || {};
|
|
107
121
|
}
|
package/dist/cjs/methods.js
CHANGED
|
@@ -112,7 +112,7 @@ const add = function(value, options = {}) {
|
|
|
112
112
|
const state = this;
|
|
113
113
|
if ((0, import_utils.isArray)(state)) {
|
|
114
114
|
state.push(value);
|
|
115
|
-
state.update(state.parse(), { overwrite:
|
|
115
|
+
state.update(state.parse(), { overwrite: true, ...options });
|
|
116
116
|
} else if ((0, import_utils.isObject)(state)) {
|
|
117
117
|
const key = Object.keys(state).length;
|
|
118
118
|
state.update({ [key]: value }, options);
|
package/dist/cjs/updateState.js
CHANGED
|
@@ -64,13 +64,15 @@ const applyOverwrite = (state, obj, options) => {
|
|
|
64
64
|
const { overwrite } = options;
|
|
65
65
|
if (!overwrite)
|
|
66
66
|
return;
|
|
67
|
-
const shallow = overwrite === "shallow";
|
|
67
|
+
const shallow = overwrite === "shallow" || overwrite === "shallow-once";
|
|
68
68
|
const merge2 = overwrite === "merge";
|
|
69
69
|
if (merge2) {
|
|
70
70
|
(0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
71
71
|
return;
|
|
72
72
|
}
|
|
73
73
|
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
|
|
74
|
+
if (options.overwrite === "shallow-once")
|
|
75
|
+
options.overwrite = true;
|
|
74
76
|
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
75
77
|
};
|
|
76
78
|
const hoistStateUpdate = (state, obj, options) => {
|
|
@@ -88,17 +90,18 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
88
90
|
const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
|
|
89
91
|
const value = isStringState ? state.value : state.parse();
|
|
90
92
|
const passedValue = isStringState ? state.value : obj;
|
|
93
|
+
const findRootState = (0, import_inherit.getRootStateInKey)(stateKey, parent.state);
|
|
91
94
|
const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
|
|
92
|
-
const changesValue = (0, import_inherit.
|
|
93
|
-
const targetParent = findGrandParentState || parent.state;
|
|
95
|
+
const changesValue = (0, import_inherit.createNestedObjectByKeyPath)(stateKey, passedValue);
|
|
96
|
+
const targetParent = findRootState || findGrandParentState || parent.state;
|
|
94
97
|
if (options.replace)
|
|
95
98
|
(0, import_utils.overwriteDeep)(targetParent, changesValue || value);
|
|
96
99
|
targetParent.update(changesValue, {
|
|
97
100
|
execStateFunction: false,
|
|
98
101
|
isHoisted: true,
|
|
99
|
-
...options,
|
|
100
102
|
preventUpdate: options.preventHoistElementUpdate,
|
|
101
|
-
overwrite: !options.replace
|
|
103
|
+
overwrite: !options.replace,
|
|
104
|
+
...options
|
|
102
105
|
});
|
|
103
106
|
const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
|
|
104
107
|
if (!options.preventStateUpdateListener && hasNotUpdated) {
|
package/inherit.js
CHANGED
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
import { deepCloneWithExtnd, is, isObjectLike, isUndefined } from '@domql/utils'
|
|
4
4
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
5
5
|
|
|
6
|
+
export const getRootStateInKey = (stateKey, parentState) => {
|
|
7
|
+
if (!stateKey.includes('~/')) return
|
|
8
|
+
const arr = stateKey.split('~/')
|
|
9
|
+
if (arr.length > 1) return parentState.__root
|
|
10
|
+
}
|
|
11
|
+
|
|
6
12
|
export const getParentStateInKey = (stateKey, parentState) => {
|
|
7
13
|
if (!stateKey.includes('../')) return
|
|
8
14
|
const arr = stateKey.split('../')
|
|
@@ -39,11 +45,18 @@ export const findInheritedState = (element, parent, options = {}) => {
|
|
|
39
45
|
let stateKey = ref.__state
|
|
40
46
|
if (!checkIfInherits(element)) return
|
|
41
47
|
|
|
48
|
+
const rootState = getRootStateInKey(stateKey, parent.state)
|
|
42
49
|
let parentState = parent.state
|
|
43
|
-
|
|
44
|
-
if (
|
|
45
|
-
parentState =
|
|
46
|
-
stateKey = stateKey.replaceAll('
|
|
50
|
+
|
|
51
|
+
if (rootState) {
|
|
52
|
+
parentState = rootState
|
|
53
|
+
stateKey = stateKey.replaceAll('~/', '')
|
|
54
|
+
} else {
|
|
55
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state)
|
|
56
|
+
if (findGrandParentState) {
|
|
57
|
+
parentState = findGrandParentState
|
|
58
|
+
stateKey = stateKey.replaceAll('../', '')
|
|
59
|
+
}
|
|
47
60
|
}
|
|
48
61
|
|
|
49
62
|
if (!parentState) return
|
|
@@ -91,7 +104,7 @@ export const isState = function (state) {
|
|
|
91
104
|
// return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
|
|
92
105
|
}
|
|
93
106
|
|
|
94
|
-
export const
|
|
107
|
+
export const createNestedObjectByKeyPath = (path, value) => {
|
|
95
108
|
if (!path) {
|
|
96
109
|
return value || {}
|
|
97
110
|
}
|
package/methods.js
CHANGED
|
@@ -88,7 +88,7 @@ export const add = function (value, options = {}) {
|
|
|
88
88
|
const state = this
|
|
89
89
|
if (isArray(state)) {
|
|
90
90
|
state.push(value)
|
|
91
|
-
state.update(state.parse(), { overwrite:
|
|
91
|
+
state.update(state.parse(), { overwrite: true, ...options })
|
|
92
92
|
} else if (isObject(state)) {
|
|
93
93
|
const key = Object.keys(state).length
|
|
94
94
|
state.update({ [key]: value }, options)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.56",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"@domql/report": "latest",
|
|
32
32
|
"@domql/utils": "latest"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "a832fd0b6d8c39f16e6c7b2b61ab6c3dccd1f539"
|
|
35
35
|
}
|
package/updateState.js
CHANGED
|
@@ -4,7 +4,7 @@ import { report } from '@domql/report'
|
|
|
4
4
|
import { triggerEventOnUpdate } from '@domql/event'
|
|
5
5
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
6
|
import { deepMerge, merge, overwriteDeep, overwriteShallow } from '@domql/utils'
|
|
7
|
-
import { checkIfInherits,
|
|
7
|
+
import { checkIfInherits, createNestedObjectByKeyPath, findInheritedState, getParentStateInKey, getRootStateInKey } from './inherit'
|
|
8
8
|
|
|
9
9
|
const STATE_UPDATE_OPTIONS = {
|
|
10
10
|
overwrite: true,
|
|
@@ -49,7 +49,7 @@ const applyOverwrite = (state, obj, options) => {
|
|
|
49
49
|
const { overwrite } = options
|
|
50
50
|
if (!overwrite) return
|
|
51
51
|
|
|
52
|
-
const shallow = overwrite === 'shallow'
|
|
52
|
+
const shallow = overwrite === 'shallow' || overwrite === 'shallow-once'
|
|
53
53
|
const merge = overwrite === 'merge'
|
|
54
54
|
|
|
55
55
|
if (merge) {
|
|
@@ -58,6 +58,7 @@ const applyOverwrite = (state, obj, options) => {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
const overwriteFunc = shallow ? overwriteShallow : overwriteDeep
|
|
61
|
+
if (options.overwrite === 'shallow-once') options.overwrite = true
|
|
61
62
|
overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
|
|
62
63
|
}
|
|
63
64
|
|
|
@@ -77,16 +78,17 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
77
78
|
const value = isStringState ? state.value : state.parse()
|
|
78
79
|
const passedValue = isStringState ? state.value : obj
|
|
79
80
|
|
|
81
|
+
const findRootState = getRootStateInKey(stateKey, parent.state)
|
|
80
82
|
const findGrandParentState = getParentStateInKey(stateKey, parent.state)
|
|
81
|
-
const changesValue =
|
|
82
|
-
const targetParent = findGrandParentState || parent.state
|
|
83
|
-
if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with
|
|
83
|
+
const changesValue = createNestedObjectByKeyPath(stateKey, passedValue)
|
|
84
|
+
const targetParent = findRootState || findGrandParentState || parent.state
|
|
85
|
+
if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createNestedObjectByKeyPath
|
|
84
86
|
targetParent.update(changesValue, {
|
|
85
87
|
execStateFunction: false,
|
|
86
88
|
isHoisted: true,
|
|
87
|
-
...options,
|
|
88
89
|
preventUpdate: options.preventHoistElementUpdate,
|
|
89
|
-
overwrite: !options.replace
|
|
90
|
+
overwrite: !options.replace,
|
|
91
|
+
...options
|
|
90
92
|
})
|
|
91
93
|
const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate
|
|
92
94
|
if (!options.preventStateUpdateListener && hasNotUpdated) {
|