@domql/state 2.3.119 → 2.3.121
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 +36 -36
- package/dist/cjs/createState.js +56 -52
- package/dist/cjs/updateState.js +29 -29
- package/package.json +2 -2
- package/updateState.js +30 -30
package/createState.js
CHANGED
|
@@ -9,68 +9,68 @@ import { checkIfInherits, createInheritedState } from './utils'
|
|
|
9
9
|
|
|
10
10
|
export const createState = function (element, parent, opts) {
|
|
11
11
|
const skip = (opts && opts.skip) ? opts.skip : false
|
|
12
|
-
let { state, __ref: __elementRef } = element
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
const objectizeState = checkForTypes(element)
|
|
14
|
+
if (objectizeState === false) return parent.state || {}
|
|
15
|
+
else element.state = objectizeState
|
|
15
16
|
|
|
16
|
-
triggerEventOn('stateInit', element)
|
|
17
|
+
const whatInitReturns = triggerEventOn('stateInit', element)
|
|
18
|
+
if (whatInitReturns === false) return element.state
|
|
17
19
|
|
|
18
20
|
if (checkIfInherits(element)) {
|
|
19
|
-
|
|
21
|
+
element.state = createInheritedState(element, parent) || {}
|
|
20
22
|
}
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return {}
|
|
25
|
-
} else {
|
|
26
|
-
__elementRef.__hasRootState = true
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// reference other state
|
|
30
|
-
// TODO: check why __ref is assigned with element
|
|
31
|
-
// /docs/intro
|
|
32
|
-
const { __ref } = state
|
|
33
|
-
if (__ref) {
|
|
34
|
-
state = deepClone(__ref, IGNORE_STATE_PARAMS)
|
|
35
|
-
if (isObject(__ref.__depends)) {
|
|
36
|
-
__ref.__depends[element.key] = state
|
|
37
|
-
} else __ref.__depends = { [element.key]: state }
|
|
38
|
-
} else {
|
|
39
|
-
state = deepClone(state, IGNORE_STATE_PARAMS)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
element.state = state
|
|
24
|
+
const dependentState = applyDependentState(element, state)
|
|
25
|
+
if (dependentState) element.state = dependentState
|
|
43
26
|
|
|
44
27
|
// NOTE: Only true when 'onlyResolveExtends' option is set to true
|
|
45
|
-
if (skip) return state
|
|
28
|
+
if (skip) return element.state
|
|
46
29
|
|
|
47
|
-
applyMethods(element
|
|
30
|
+
applyMethods(element)
|
|
48
31
|
|
|
49
32
|
// trigger `on.stateCreated`
|
|
50
33
|
triggerEventOn('stateCreated', element)
|
|
51
34
|
|
|
52
|
-
return state
|
|
35
|
+
return element.state
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const applyDependentState = (element, state) => {
|
|
39
|
+
const { __ref: ref } = state
|
|
40
|
+
if (!ref) return
|
|
41
|
+
const dependentState = deepClone(ref, IGNORE_STATE_PARAMS)
|
|
42
|
+
const newDepends = { [element.key]: dependentState }
|
|
43
|
+
ref.__depends = isObject(ref.__depends)
|
|
44
|
+
? { ...ref.__depends, ...newDepends }
|
|
45
|
+
: newDepends
|
|
46
|
+
|
|
47
|
+
return dependentState
|
|
53
48
|
}
|
|
54
49
|
|
|
55
50
|
const checkForTypes = (element) => {
|
|
56
|
-
const { state, __ref:
|
|
51
|
+
const { state, __ref: ref } = element
|
|
57
52
|
if (isFunction(state)) {
|
|
58
|
-
|
|
53
|
+
ref.__state = state
|
|
59
54
|
return exec(state, element)
|
|
60
55
|
}
|
|
61
56
|
if (is(state)('string', 'number')) {
|
|
62
|
-
|
|
57
|
+
ref.__state = state
|
|
63
58
|
return {}
|
|
64
59
|
}
|
|
65
60
|
if (state === true) {
|
|
66
|
-
|
|
61
|
+
ref.__state = element.key
|
|
67
62
|
return {}
|
|
68
63
|
}
|
|
69
|
-
|
|
64
|
+
if (state) {
|
|
65
|
+
ref.__hasRootState = true
|
|
66
|
+
return state
|
|
67
|
+
}
|
|
68
|
+
return false
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
const applyMethods = (element
|
|
73
|
-
const
|
|
71
|
+
const applyMethods = (element) => {
|
|
72
|
+
const state = element.state
|
|
73
|
+
const ref = element.__ref
|
|
74
74
|
|
|
75
75
|
state.clean = clean
|
|
76
76
|
state.parse = parse
|
|
@@ -85,7 +85,7 @@ const applyMethods = (element, state) => {
|
|
|
85
85
|
state.parent = element.parent.state
|
|
86
86
|
state.__element = element
|
|
87
87
|
state.__children = {}
|
|
88
|
-
state.__root =
|
|
88
|
+
state.__root = ref.__root ? ref.__root.state : state
|
|
89
89
|
|
|
90
90
|
if (state.parent) state.parent.__children[element.key] = state
|
|
91
91
|
}
|
package/dist/cjs/createState.js
CHANGED
|
@@ -29,68 +29,72 @@ var import_updateState = require("./updateState");
|
|
|
29
29
|
var import_utils2 = require("./utils");
|
|
30
30
|
const createState = function(element, parent, opts) {
|
|
31
31
|
const skip = opts && opts.skip ? opts.skip : false;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const objectizeState = checkForTypes(element);
|
|
33
|
+
if (objectizeState === false)
|
|
34
|
+
return parent.state || {};
|
|
35
|
+
else
|
|
36
|
+
element.state = objectizeState;
|
|
37
|
+
const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element);
|
|
38
|
+
if (whatInitReturns === false)
|
|
39
|
+
return element.state;
|
|
35
40
|
if ((0, import_utils2.checkIfInherits)(element)) {
|
|
36
|
-
|
|
41
|
+
element.state = (0, import_utils2.createInheritedState)(element, parent) || {};
|
|
37
42
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return {};
|
|
42
|
-
} else {
|
|
43
|
-
__elementRef.__hasRootState = true;
|
|
44
|
-
}
|
|
45
|
-
const { __ref } = state;
|
|
46
|
-
if (__ref) {
|
|
47
|
-
state = (0, import_utils.deepClone)(__ref, import_ignore.IGNORE_STATE_PARAMS);
|
|
48
|
-
if ((0, import_utils.isObject)(__ref.__depends)) {
|
|
49
|
-
__ref.__depends[element.key] = state;
|
|
50
|
-
} else
|
|
51
|
-
__ref.__depends = { [element.key]: state };
|
|
52
|
-
} else {
|
|
53
|
-
state = (0, import_utils.deepClone)(state, import_ignore.IGNORE_STATE_PARAMS);
|
|
54
|
-
}
|
|
55
|
-
element.state = state;
|
|
43
|
+
const dependentState = applyDependentState(element, state);
|
|
44
|
+
if (dependentState)
|
|
45
|
+
element.state = dependentState;
|
|
56
46
|
if (skip)
|
|
57
|
-
return state;
|
|
58
|
-
applyMethods(element
|
|
47
|
+
return element.state;
|
|
48
|
+
applyMethods(element);
|
|
59
49
|
(0, import_event.triggerEventOn)("stateCreated", element);
|
|
60
|
-
return state;
|
|
50
|
+
return element.state;
|
|
51
|
+
};
|
|
52
|
+
const applyDependentState = (element, state2) => {
|
|
53
|
+
const { __ref: ref } = state2;
|
|
54
|
+
if (!ref)
|
|
55
|
+
return;
|
|
56
|
+
const dependentState = (0, import_utils.deepClone)(ref, import_ignore.IGNORE_STATE_PARAMS);
|
|
57
|
+
const newDepends = { [element.key]: dependentState };
|
|
58
|
+
ref.__depends = (0, import_utils.isObject)(ref.__depends) ? { ...ref.__depends, ...newDepends } : newDepends;
|
|
59
|
+
return dependentState;
|
|
61
60
|
};
|
|
62
61
|
const checkForTypes = (element) => {
|
|
63
|
-
const { state, __ref:
|
|
64
|
-
if ((0, import_utils.isFunction)(
|
|
65
|
-
|
|
66
|
-
return (0, import_utils.exec)(
|
|
62
|
+
const { state: state2, __ref: ref } = element;
|
|
63
|
+
if ((0, import_utils.isFunction)(state2)) {
|
|
64
|
+
ref.__state = state2;
|
|
65
|
+
return (0, import_utils.exec)(state2, element);
|
|
67
66
|
}
|
|
68
|
-
if ((0, import_utils.is)(
|
|
69
|
-
|
|
67
|
+
if ((0, import_utils.is)(state2)("string", "number")) {
|
|
68
|
+
ref.__state = state2;
|
|
70
69
|
return {};
|
|
71
70
|
}
|
|
72
|
-
if (
|
|
73
|
-
|
|
71
|
+
if (state2 === true) {
|
|
72
|
+
ref.__state = element.key;
|
|
74
73
|
return {};
|
|
75
74
|
}
|
|
76
|
-
|
|
75
|
+
if (state2) {
|
|
76
|
+
ref.__hasRootState = true;
|
|
77
|
+
return state2;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
77
80
|
};
|
|
78
|
-
const applyMethods = (element
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
81
|
+
const applyMethods = (element) => {
|
|
82
|
+
const state2 = element.state;
|
|
83
|
+
const ref = element.__ref;
|
|
84
|
+
state2.clean = import_methods.clean;
|
|
85
|
+
state2.parse = import_methods.parse;
|
|
86
|
+
state2.destroy = import_methods.destroy;
|
|
87
|
+
state2.update = import_updateState.updateState;
|
|
88
|
+
state2.rootUpdate = import_methods.rootUpdate;
|
|
89
|
+
state2.create = createState;
|
|
90
|
+
state2.add = import_methods.add;
|
|
91
|
+
state2.toggle = import_methods.toggle;
|
|
92
|
+
state2.remove = import_methods.remove;
|
|
93
|
+
state2.apply = import_methods.apply;
|
|
94
|
+
state2.parent = element.parent.state;
|
|
95
|
+
state2.__element = element;
|
|
96
|
+
state2.__children = {};
|
|
97
|
+
state2.__root = ref.__root ? ref.__root.state : state2;
|
|
98
|
+
if (state2.parent)
|
|
99
|
+
state2.parent.__children[element.key] = state2;
|
|
96
100
|
};
|
package/dist/cjs/updateState.js
CHANGED
|
@@ -29,8 +29,6 @@ var import_utils2 = require("./utils");
|
|
|
29
29
|
const updateState = function(obj, options = {}) {
|
|
30
30
|
const state = this;
|
|
31
31
|
const element = state.__element;
|
|
32
|
-
const parentState = element.parent.state;
|
|
33
|
-
state.parent = parentState;
|
|
34
32
|
if (!state.__element)
|
|
35
33
|
(0, import_report.report)("ElementOnStateIsNotDefined");
|
|
36
34
|
if (!options.preventInitStateUpdateListener) {
|
|
@@ -40,7 +38,7 @@ const updateState = function(obj, options = {}) {
|
|
|
40
38
|
}
|
|
41
39
|
applyOverwrite(state, obj, options);
|
|
42
40
|
hoistStateUpdate(state, obj, options);
|
|
43
|
-
|
|
41
|
+
updateDependentState(state, obj, options);
|
|
44
42
|
applyElementUpdate(state, obj, options);
|
|
45
43
|
if (!options.preventStateUpdateListener) {
|
|
46
44
|
(0, import_event.triggerEventOn)("stateUpdated", element, obj);
|
|
@@ -48,41 +46,43 @@ const updateState = function(obj, options = {}) {
|
|
|
48
46
|
return state;
|
|
49
47
|
};
|
|
50
48
|
const applyOverwrite = (state, obj, options) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
(0, import_utils.overwriteShallow)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
54
|
-
} else {
|
|
55
|
-
(0, import_utils.overwriteDeep)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
56
|
-
}
|
|
57
|
-
} else if (options.skipOverwrite === "merge") {
|
|
49
|
+
const { skipOverwrite, shallow } = options;
|
|
50
|
+
if (skipOverwrite === "merge") {
|
|
58
51
|
(0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!skipOverwrite) {
|
|
55
|
+
const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
|
|
56
|
+
overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
|
|
59
57
|
}
|
|
60
58
|
};
|
|
61
59
|
const hoistStateUpdate = (state, obj, options) => {
|
|
62
60
|
const element = state.__element;
|
|
63
61
|
const __elementRef = element.__ref;
|
|
64
62
|
const stateKey = __elementRef.__state;
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
if (!stateKey)
|
|
64
|
+
return;
|
|
65
|
+
const asksForInherit = (0, import_utils2.checkIfInherits)(element);
|
|
66
|
+
const shouldPropagateState = asksForInherit && !options.stopStatePropagation;
|
|
67
67
|
const parentState = element.parent.state;
|
|
68
|
-
if (shouldPropagateState)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
68
|
+
if (!shouldPropagateState)
|
|
69
|
+
return;
|
|
70
|
+
const isStringState = __elementRef.__stateType === "string";
|
|
71
|
+
const value = isStringState ? state.value : state.parse();
|
|
72
|
+
const passedValue = isStringState ? state.value : obj;
|
|
73
|
+
parentState[stateKey] = value;
|
|
74
|
+
parentState.update({ [stateKey]: passedValue }, {
|
|
75
|
+
skipOverwrite: true,
|
|
76
|
+
preventUpdate: options.preventHoistElementUpdate,
|
|
77
|
+
...options
|
|
78
|
+
});
|
|
79
79
|
};
|
|
80
|
-
const
|
|
81
|
-
if (state.__depends)
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
const updateDependentState = (state, obj, options) => {
|
|
81
|
+
if (!state.__depends)
|
|
82
|
+
return;
|
|
83
|
+
for (const el in state.__depends) {
|
|
84
|
+
const dependentState = state.__depends[el];
|
|
85
|
+
dependentState.clean().update(state.parse(), options);
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
88
|
const applyElementUpdate = (state, obj, options) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.121",
|
|
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": "7bb63fd5aab7bfa92c0aaa3a7205ff9a002fac1c"
|
|
30
30
|
}
|
package/updateState.js
CHANGED
|
@@ -9,8 +9,6 @@ import { checkIfInherits } from './utils'
|
|
|
9
9
|
export const updateState = function (obj, options = {}) {
|
|
10
10
|
const state = this
|
|
11
11
|
const element = state.__element
|
|
12
|
-
const parentState = element.parent.state
|
|
13
|
-
state.parent = parentState
|
|
14
12
|
|
|
15
13
|
if (!state.__element) report('ElementOnStateIsNotDefined')
|
|
16
14
|
|
|
@@ -23,7 +21,7 @@ export const updateState = function (obj, options = {}) {
|
|
|
23
21
|
|
|
24
22
|
hoistStateUpdate(state, obj, options)
|
|
25
23
|
|
|
26
|
-
|
|
24
|
+
updateDependentState(state, obj, options)
|
|
27
25
|
|
|
28
26
|
applyElementUpdate(state, obj, options)
|
|
29
27
|
|
|
@@ -35,14 +33,16 @@ export const updateState = function (obj, options = {}) {
|
|
|
35
33
|
}
|
|
36
34
|
|
|
37
35
|
const applyOverwrite = (state, obj, options) => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
} else {
|
|
42
|
-
overwriteDeep(state, obj, IGNORE_STATE_PARAMS)
|
|
43
|
-
}
|
|
44
|
-
} else if (options.skipOverwrite === 'merge') {
|
|
36
|
+
const { skipOverwrite, shallow } = options
|
|
37
|
+
|
|
38
|
+
if (skipOverwrite === 'merge') {
|
|
45
39
|
deepMerge(state, obj, IGNORE_STATE_PARAMS)
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!skipOverwrite) {
|
|
44
|
+
const overwriteFunc = shallow ? overwriteShallow : overwriteDeep
|
|
45
|
+
overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -50,30 +50,30 @@ const hoistStateUpdate = (state, obj, options) => {
|
|
|
50
50
|
const element = state.__element
|
|
51
51
|
const __elementRef = element.__ref
|
|
52
52
|
const stateKey = __elementRef.__state
|
|
53
|
-
|
|
54
|
-
const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation
|
|
55
|
-
const parentState = element.parent.state
|
|
53
|
+
if (!stateKey) return
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const passedValue = isStringState ? state.value : obj
|
|
55
|
+
const asksForInherit = checkIfInherits(element)
|
|
56
|
+
const shouldPropagateState = asksForInherit && !options.stopStatePropagation
|
|
57
|
+
const parentState = element.parent.state
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
59
|
+
if (!shouldPropagateState) return
|
|
60
|
+
const isStringState = (__elementRef.__stateType === 'string')
|
|
61
|
+
const value = isStringState ? state.value : state.parse()
|
|
62
|
+
const passedValue = isStringState ? state.value : obj
|
|
63
|
+
|
|
64
|
+
parentState[stateKey] = value
|
|
65
|
+
parentState.update({ [stateKey]: passedValue }, {
|
|
66
|
+
skipOverwrite: true,
|
|
67
|
+
preventUpdate: options.preventHoistElementUpdate,
|
|
68
|
+
...options
|
|
69
|
+
})
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
const
|
|
72
|
-
if (state.__depends)
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
72
|
+
const updateDependentState = (state, obj, options) => {
|
|
73
|
+
if (!state.__depends) return
|
|
74
|
+
for (const el in state.__depends) {
|
|
75
|
+
const dependentState = state.__depends[el]
|
|
76
|
+
dependentState.clean().update(state.parse(), options)
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
|