@domql/state 2.5.185 → 2.5.187
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 +4 -4
- package/dist/cjs/create.js +4 -4
- package/dist/cjs/index.js +5 -5
- package/dist/cjs/inherit.js +1 -1
- package/dist/cjs/methods.js +1 -1
- package/dist/cjs/updateState.js +2 -2
- package/dist/esm/create.js +167 -0
- package/dist/esm/ignore.js +37 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/inherit.js +110 -0
- package/dist/esm/methods.js +244 -0
- package/dist/esm/updateState.js +133 -0
- package/index.js +5 -5
- package/inherit.js +1 -1
- package/methods.js +1 -1
- package/package.json +11 -10
- package/updateState.js +2 -2
package/create.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { triggerEventOn } from '@domql/event'
|
|
4
4
|
import { deepClone, exec, is, isArray, isFunction, isObject, isUndefined } from '@domql/utils'
|
|
5
|
-
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
5
|
+
import { IGNORE_STATE_PARAMS } from './ignore.js'
|
|
6
6
|
import {
|
|
7
7
|
add,
|
|
8
8
|
apply,
|
|
@@ -27,9 +27,9 @@ import {
|
|
|
27
27
|
getByPath,
|
|
28
28
|
keys,
|
|
29
29
|
values
|
|
30
|
-
} from './methods'
|
|
31
|
-
import { updateState } from './updateState'
|
|
32
|
-
import { checkIfInherits, createInheritedState } from './inherit'
|
|
30
|
+
} from './methods.js'
|
|
31
|
+
import { updateState } from './updateState.js'
|
|
32
|
+
import { checkIfInherits, createInheritedState } from './inherit.js'
|
|
33
33
|
|
|
34
34
|
export const createState = async function (element, parent, options) {
|
|
35
35
|
element.state = await applyInitialState(element, parent, options)
|
package/dist/cjs/create.js
CHANGED
|
@@ -24,10 +24,10 @@ __export(create_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(create_exports);
|
|
25
25
|
var import_event = require("@domql/event");
|
|
26
26
|
var import_utils = require("@domql/utils");
|
|
27
|
-
var import_ignore = require("./ignore");
|
|
28
|
-
var import_methods = require("./methods");
|
|
29
|
-
var import_updateState = require("./updateState");
|
|
30
|
-
var import_inherit = require("./inherit");
|
|
27
|
+
var import_ignore = require("./ignore.js");
|
|
28
|
+
var import_methods = require("./methods.js");
|
|
29
|
+
var import_updateState = require("./updateState.js");
|
|
30
|
+
var import_inherit = require("./inherit.js");
|
|
31
31
|
const createState = async function(element, parent, options) {
|
|
32
32
|
element.state = await applyInitialState(element, parent, options);
|
|
33
33
|
return element.state;
|
package/dist/cjs/index.js
CHANGED
|
@@ -15,8 +15,8 @@ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "defau
|
|
|
15
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
16
|
var state_exports = {};
|
|
17
17
|
module.exports = __toCommonJS(state_exports);
|
|
18
|
-
__reExport(state_exports, require("./ignore"), module.exports);
|
|
19
|
-
__reExport(state_exports, require("./create"), module.exports);
|
|
20
|
-
__reExport(state_exports, require("./updateState"), module.exports);
|
|
21
|
-
__reExport(state_exports, require("./methods"), module.exports);
|
|
22
|
-
__reExport(state_exports, require("./inherit"), module.exports);
|
|
18
|
+
__reExport(state_exports, require("./ignore.js"), module.exports);
|
|
19
|
+
__reExport(state_exports, require("./create.js"), module.exports);
|
|
20
|
+
__reExport(state_exports, require("./updateState.js"), module.exports);
|
|
21
|
+
__reExport(state_exports, require("./methods.js"), module.exports);
|
|
22
|
+
__reExport(state_exports, require("./inherit.js"), module.exports);
|
package/dist/cjs/inherit.js
CHANGED
|
@@ -29,7 +29,7 @@ __export(inherit_exports, {
|
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(inherit_exports);
|
|
31
31
|
var import_utils = require("@domql/utils");
|
|
32
|
-
var import_ignore = require("./ignore");
|
|
32
|
+
var import_ignore = require("./ignore.js");
|
|
33
33
|
const getRootStateInKey = (stateKey, parentState) => {
|
|
34
34
|
if (!stateKey.includes("~/"))
|
|
35
35
|
return;
|
package/dist/cjs/methods.js
CHANGED
|
@@ -44,7 +44,7 @@ __export(methods_exports, {
|
|
|
44
44
|
});
|
|
45
45
|
module.exports = __toCommonJS(methods_exports);
|
|
46
46
|
var import_utils = require("@domql/utils");
|
|
47
|
-
var import_ignore = require("./ignore");
|
|
47
|
+
var import_ignore = require("./ignore.js");
|
|
48
48
|
const parse = function() {
|
|
49
49
|
const state = this;
|
|
50
50
|
if ((0, import_utils.isObject)(state)) {
|
package/dist/cjs/updateState.js
CHANGED
|
@@ -23,9 +23,9 @@ __export(updateState_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(updateState_exports);
|
|
24
24
|
var import_report = require("@domql/report");
|
|
25
25
|
var import_event = require("@domql/event");
|
|
26
|
-
var import_ignore = require("./ignore");
|
|
26
|
+
var import_ignore = require("./ignore.js");
|
|
27
27
|
var import_utils = require("@domql/utils");
|
|
28
|
-
var import_inherit = require("./inherit");
|
|
28
|
+
var import_inherit = require("./inherit.js");
|
|
29
29
|
const STATE_UPDATE_OPTIONS = {
|
|
30
30
|
overwrite: true,
|
|
31
31
|
preventHoistElementUpdate: false,
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { triggerEventOn } from "@domql/event";
|
|
21
|
+
import { deepClone, exec, is, isArray, isFunction, isObject, isUndefined } from "@domql/utils";
|
|
22
|
+
import { IGNORE_STATE_PARAMS } from "./ignore.js";
|
|
23
|
+
import {
|
|
24
|
+
add,
|
|
25
|
+
apply,
|
|
26
|
+
applyFunction,
|
|
27
|
+
clean,
|
|
28
|
+
destroy,
|
|
29
|
+
parentUpdate,
|
|
30
|
+
parse,
|
|
31
|
+
remove,
|
|
32
|
+
rootUpdate,
|
|
33
|
+
set,
|
|
34
|
+
reset,
|
|
35
|
+
toggle,
|
|
36
|
+
replace,
|
|
37
|
+
quietUpdate,
|
|
38
|
+
quietReplace,
|
|
39
|
+
applyReplace,
|
|
40
|
+
setByPath,
|
|
41
|
+
setPathCollection,
|
|
42
|
+
removeByPath,
|
|
43
|
+
removePathCollection,
|
|
44
|
+
getByPath,
|
|
45
|
+
keys,
|
|
46
|
+
values
|
|
47
|
+
} from "./methods.js";
|
|
48
|
+
import { updateState } from "./updateState.js";
|
|
49
|
+
import { checkIfInherits, createInheritedState } from "./inherit.js";
|
|
50
|
+
const createState = async function(element, parent, options) {
|
|
51
|
+
element.state = await applyInitialState(element, parent, options);
|
|
52
|
+
return element.state;
|
|
53
|
+
};
|
|
54
|
+
const applyInitialState = async function(element, parent, options) {
|
|
55
|
+
const objectizeState = await checkForTypes(element);
|
|
56
|
+
if (objectizeState === false)
|
|
57
|
+
return parent.state || {};
|
|
58
|
+
else
|
|
59
|
+
element.state = objectizeState;
|
|
60
|
+
const whatInitReturns = triggerEventOn("stateInit", element, options);
|
|
61
|
+
if (whatInitReturns === false)
|
|
62
|
+
return element.state;
|
|
63
|
+
if (checkIfInherits(element)) {
|
|
64
|
+
const inheritedState = createInheritedState(element, parent);
|
|
65
|
+
element.state = isUndefined(inheritedState) ? {} : inheritedState;
|
|
66
|
+
}
|
|
67
|
+
const dependentState = applyDependentState(element, element.state || parent.state || {});
|
|
68
|
+
if (dependentState)
|
|
69
|
+
element.state = dependentState;
|
|
70
|
+
applyMethods(element);
|
|
71
|
+
triggerEventOn("stateCreated", element);
|
|
72
|
+
return element.state;
|
|
73
|
+
};
|
|
74
|
+
const applyDependentState = (element, state) => {
|
|
75
|
+
const { __ref, ref, __element } = state;
|
|
76
|
+
const origState = exec(__ref || ref || (__element == null ? void 0 : __element.state), element);
|
|
77
|
+
if (!origState)
|
|
78
|
+
return;
|
|
79
|
+
const dependentState = deepClone(origState, IGNORE_STATE_PARAMS);
|
|
80
|
+
const newDepends = { [element.key]: dependentState };
|
|
81
|
+
const __depends = isObject(origState.__depends) ? __spreadValues(__spreadValues({}, origState.__depends), newDepends) : newDepends;
|
|
82
|
+
if (Array.isArray(origState)) {
|
|
83
|
+
addProtoToArray(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), { __depends }));
|
|
84
|
+
} else {
|
|
85
|
+
Object.setPrototypeOf(origState, __spreadProps(__spreadValues({}, Object.getPrototypeOf(origState)), { __depends }));
|
|
86
|
+
}
|
|
87
|
+
return dependentState;
|
|
88
|
+
};
|
|
89
|
+
const checkForTypes = async (element) => {
|
|
90
|
+
const { state: orig, props, __ref: ref } = element;
|
|
91
|
+
const state = (props == null ? void 0 : props.state) || orig;
|
|
92
|
+
if (isFunction(state)) {
|
|
93
|
+
ref.__state = state;
|
|
94
|
+
return await exec(state, element);
|
|
95
|
+
} else if (is(state)("string", "number")) {
|
|
96
|
+
ref.__state = state;
|
|
97
|
+
return { value: state };
|
|
98
|
+
} else if (state === true) {
|
|
99
|
+
ref.__state = element.key;
|
|
100
|
+
return {};
|
|
101
|
+
} else if (state) {
|
|
102
|
+
ref.__hasRootState = true;
|
|
103
|
+
return state;
|
|
104
|
+
} else {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const addProtoToArray = (state, proto) => {
|
|
109
|
+
for (const key in proto) {
|
|
110
|
+
Object.defineProperty(state, key, {
|
|
111
|
+
value: proto[key],
|
|
112
|
+
enumerable: false,
|
|
113
|
+
// Set this to true if you want the method to appear in for...in loops
|
|
114
|
+
configurable: true,
|
|
115
|
+
// Set this to true if you want to allow redefining/removing the property later
|
|
116
|
+
writable: true
|
|
117
|
+
// Set this to true if you want to allow changing the function later
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const applyMethods = (element) => {
|
|
122
|
+
const state = element.state;
|
|
123
|
+
const ref = element.__ref;
|
|
124
|
+
const proto = {
|
|
125
|
+
clean: clean.bind(state),
|
|
126
|
+
parse: parse.bind(state),
|
|
127
|
+
destroy: destroy.bind(state),
|
|
128
|
+
update: updateState.bind(state),
|
|
129
|
+
rootUpdate: rootUpdate.bind(state),
|
|
130
|
+
parentUpdate: parentUpdate.bind(state),
|
|
131
|
+
create: createState.bind(state),
|
|
132
|
+
add: add.bind(state),
|
|
133
|
+
toggle: toggle.bind(state),
|
|
134
|
+
remove: remove.bind(state),
|
|
135
|
+
apply: apply.bind(state),
|
|
136
|
+
applyReplace: applyReplace.bind(state),
|
|
137
|
+
applyFunction: applyFunction.bind(state),
|
|
138
|
+
set: set.bind(state),
|
|
139
|
+
quietUpdate: quietUpdate.bind(state),
|
|
140
|
+
replace: replace.bind(state),
|
|
141
|
+
quietReplace: quietReplace.bind(state),
|
|
142
|
+
reset: reset.bind(state),
|
|
143
|
+
parent: element.parent.state || state,
|
|
144
|
+
setByPath: setByPath.bind(state),
|
|
145
|
+
setPathCollection: setPathCollection.bind(state),
|
|
146
|
+
removeByPath: removeByPath.bind(state),
|
|
147
|
+
removePathCollection: removePathCollection.bind(state),
|
|
148
|
+
getByPath: getByPath.bind(state),
|
|
149
|
+
keys: keys.bind(state),
|
|
150
|
+
values: values.bind(state),
|
|
151
|
+
__element: element,
|
|
152
|
+
__children: {},
|
|
153
|
+
root: ref.root ? ref.root.state : state
|
|
154
|
+
};
|
|
155
|
+
if (isArray(state)) {
|
|
156
|
+
addProtoToArray(state, proto);
|
|
157
|
+
} else {
|
|
158
|
+
Object.setPrototypeOf(state, proto);
|
|
159
|
+
}
|
|
160
|
+
if (state.parent && state.parent.__children) {
|
|
161
|
+
state.parent.__children[element.key] = state;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
export {
|
|
165
|
+
applyInitialState,
|
|
166
|
+
createState
|
|
167
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const IGNORE_STATE_PARAMS = [
|
|
2
|
+
"update",
|
|
3
|
+
"parse",
|
|
4
|
+
"clean",
|
|
5
|
+
"create",
|
|
6
|
+
"destroy",
|
|
7
|
+
"add",
|
|
8
|
+
"toggle",
|
|
9
|
+
"remove",
|
|
10
|
+
"apply",
|
|
11
|
+
"set",
|
|
12
|
+
"reset",
|
|
13
|
+
"replace",
|
|
14
|
+
"quietReplace",
|
|
15
|
+
"quietUpdate",
|
|
16
|
+
"applyReplace",
|
|
17
|
+
"applyFunction",
|
|
18
|
+
"keys",
|
|
19
|
+
"values",
|
|
20
|
+
"ref",
|
|
21
|
+
"rootUpdate",
|
|
22
|
+
"parentUpdate",
|
|
23
|
+
"parent",
|
|
24
|
+
"__element",
|
|
25
|
+
"__depends",
|
|
26
|
+
"__ref",
|
|
27
|
+
"__children",
|
|
28
|
+
"root",
|
|
29
|
+
"setByPath",
|
|
30
|
+
"setPathCollection",
|
|
31
|
+
"removeByPath",
|
|
32
|
+
"removePathCollection",
|
|
33
|
+
"getByPath"
|
|
34
|
+
];
|
|
35
|
+
export {
|
|
36
|
+
IGNORE_STATE_PARAMS
|
|
37
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { deepClone, is, isObjectLike, isUndefined } from "@domql/utils";
|
|
2
|
+
import { IGNORE_STATE_PARAMS } from "./ignore.js";
|
|
3
|
+
const getRootStateInKey = (stateKey, parentState) => {
|
|
4
|
+
if (!stateKey.includes("~/"))
|
|
5
|
+
return;
|
|
6
|
+
const arr = stateKey.split("~/");
|
|
7
|
+
if (arr.length > 1)
|
|
8
|
+
return parentState.root;
|
|
9
|
+
};
|
|
10
|
+
const getParentStateInKey = (stateKey, parentState) => {
|
|
11
|
+
if (!stateKey.includes("../"))
|
|
12
|
+
return;
|
|
13
|
+
const arr = stateKey.split("../");
|
|
14
|
+
const arrLength = arr.length - 1;
|
|
15
|
+
for (let i = 0; i < arrLength; i++) {
|
|
16
|
+
if (!parentState.parent)
|
|
17
|
+
return null;
|
|
18
|
+
parentState = parentState.parent;
|
|
19
|
+
}
|
|
20
|
+
return parentState;
|
|
21
|
+
};
|
|
22
|
+
const getChildStateInKey = (stateKey, parentState, options = {}) => {
|
|
23
|
+
const arr = stateKey.split("/");
|
|
24
|
+
const arrLength = arr.length - 1;
|
|
25
|
+
for (let i = 0; i < arrLength; i++) {
|
|
26
|
+
const childKey = arr[i];
|
|
27
|
+
const grandChildKey = arr[i + 1];
|
|
28
|
+
if (childKey === "__proto__" || grandChildKey === "__proto__")
|
|
29
|
+
return;
|
|
30
|
+
let childInParent = parentState[childKey];
|
|
31
|
+
if (!childInParent)
|
|
32
|
+
childInParent = parentState[childKey] = {};
|
|
33
|
+
if (!childInParent[grandChildKey])
|
|
34
|
+
childInParent[grandChildKey] = {};
|
|
35
|
+
stateKey = grandChildKey;
|
|
36
|
+
parentState = childInParent;
|
|
37
|
+
}
|
|
38
|
+
if (options.returnParent)
|
|
39
|
+
return parentState;
|
|
40
|
+
return parentState[stateKey];
|
|
41
|
+
};
|
|
42
|
+
const findInheritedState = (element, parent, options = {}) => {
|
|
43
|
+
const ref = element.__ref;
|
|
44
|
+
let stateKey = ref.__state;
|
|
45
|
+
if (!checkIfInherits(element))
|
|
46
|
+
return;
|
|
47
|
+
const rootState = getRootStateInKey(stateKey, parent.state);
|
|
48
|
+
let parentState = parent.state;
|
|
49
|
+
if (rootState) {
|
|
50
|
+
parentState = rootState;
|
|
51
|
+
stateKey = stateKey.replaceAll("~/", "");
|
|
52
|
+
} else {
|
|
53
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state);
|
|
54
|
+
if (findGrandParentState) {
|
|
55
|
+
parentState = findGrandParentState;
|
|
56
|
+
stateKey = stateKey.replaceAll("../", "");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!parentState)
|
|
60
|
+
return;
|
|
61
|
+
return getChildStateInKey(stateKey, parentState, options);
|
|
62
|
+
};
|
|
63
|
+
const createInheritedState = (element, parent) => {
|
|
64
|
+
const ref = element.__ref;
|
|
65
|
+
const inheritedState = findInheritedState(element, parent);
|
|
66
|
+
if (isUndefined(inheritedState))
|
|
67
|
+
return element.state;
|
|
68
|
+
if (is(inheritedState)("object", "array")) {
|
|
69
|
+
return deepClone(inheritedState, { exclude: IGNORE_STATE_PARAMS });
|
|
70
|
+
} else if (is(inheritedState)("string", "number", "boolean")) {
|
|
71
|
+
ref.__stateType = typeof inheritedState;
|
|
72
|
+
return { value: inheritedState };
|
|
73
|
+
}
|
|
74
|
+
console.warn(ref.__state, "is not present. Replacing with", {});
|
|
75
|
+
};
|
|
76
|
+
const checkIfInherits = (element) => {
|
|
77
|
+
const ref = element.__ref;
|
|
78
|
+
const stateKey = ref.__state;
|
|
79
|
+
if (stateKey && is(stateKey)("number", "string", "boolean"))
|
|
80
|
+
return true;
|
|
81
|
+
return false;
|
|
82
|
+
};
|
|
83
|
+
const isState = function(state) {
|
|
84
|
+
if (!isObjectLike(state))
|
|
85
|
+
return false;
|
|
86
|
+
return state.update && state.parse && state.clean && state.create && state.parent && state.destroy && state.rootUpdate && state.parentUpdate && state.keys && state.values && state.toggle && state.replace && state.quietUpdate && state.quietReplace && state.add && state.apply && state.applyReplace && state.setByPath && state.setPathCollection && state.removeByPath && state.removePathCollection && state.getByPath && state.applyFunction && state.__element && state.__children;
|
|
87
|
+
};
|
|
88
|
+
const createNestedObjectByKeyPath = (path, value) => {
|
|
89
|
+
if (!path) {
|
|
90
|
+
return value || {};
|
|
91
|
+
}
|
|
92
|
+
const keys = path.split("/");
|
|
93
|
+
const obj = {};
|
|
94
|
+
let ref = obj;
|
|
95
|
+
keys.forEach((key, index) => {
|
|
96
|
+
ref[key] = index === keys.length - 1 ? value || {} : {};
|
|
97
|
+
ref = ref[key];
|
|
98
|
+
});
|
|
99
|
+
return obj;
|
|
100
|
+
};
|
|
101
|
+
export {
|
|
102
|
+
checkIfInherits,
|
|
103
|
+
createInheritedState,
|
|
104
|
+
createNestedObjectByKeyPath,
|
|
105
|
+
findInheritedState,
|
|
106
|
+
getChildStateInKey,
|
|
107
|
+
getParentStateInKey,
|
|
108
|
+
getRootStateInKey,
|
|
109
|
+
isState
|
|
110
|
+
};
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
import {
|
|
18
|
+
isArray,
|
|
19
|
+
deepClone,
|
|
20
|
+
isFunction,
|
|
21
|
+
isObject,
|
|
22
|
+
isString,
|
|
23
|
+
removeFromArray,
|
|
24
|
+
removeFromObject,
|
|
25
|
+
overwriteDeep,
|
|
26
|
+
createNestedObject,
|
|
27
|
+
getInObjectByPath,
|
|
28
|
+
removeNestedKeyByPath,
|
|
29
|
+
setInObjectByPath
|
|
30
|
+
} from "@domql/utils";
|
|
31
|
+
import { IGNORE_STATE_PARAMS } from "./ignore.js";
|
|
32
|
+
const parse = function() {
|
|
33
|
+
const state = this;
|
|
34
|
+
if (isObject(state)) {
|
|
35
|
+
const obj = {};
|
|
36
|
+
for (const param in state) {
|
|
37
|
+
if (!IGNORE_STATE_PARAMS.includes(param)) {
|
|
38
|
+
obj[param] = state[param];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return obj;
|
|
42
|
+
} else if (isArray(state)) {
|
|
43
|
+
return state.filter((item) => !IGNORE_STATE_PARAMS.includes(item));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const clean = function(options = {}) {
|
|
47
|
+
const state = this;
|
|
48
|
+
for (const param in state) {
|
|
49
|
+
if (!IGNORE_STATE_PARAMS.includes(param) && Object.hasOwnProperty.call(state, param)) {
|
|
50
|
+
delete state[param];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!options.preventStateUpdate) {
|
|
54
|
+
state.update(state, __spreadValues({ replace: true }, options));
|
|
55
|
+
}
|
|
56
|
+
return state;
|
|
57
|
+
};
|
|
58
|
+
const destroy = function(options = {}) {
|
|
59
|
+
const state = this;
|
|
60
|
+
const element = state.__element;
|
|
61
|
+
const stateKey = element.__ref.__state;
|
|
62
|
+
if (isString(stateKey)) {
|
|
63
|
+
element.parent.state.remove(stateKey, __spreadValues({ isHoisted: true }, options));
|
|
64
|
+
return element.state;
|
|
65
|
+
}
|
|
66
|
+
delete element.state;
|
|
67
|
+
element.state = state.parent;
|
|
68
|
+
if (state.parent) {
|
|
69
|
+
delete state.parent.__children[element.key];
|
|
70
|
+
}
|
|
71
|
+
if (state.__children) {
|
|
72
|
+
for (const key in state.__children) {
|
|
73
|
+
const child = state.__children[key];
|
|
74
|
+
if (child.state) {
|
|
75
|
+
if (isArray(child.state)) {
|
|
76
|
+
Object.defineProperty(child.state, "parent", {
|
|
77
|
+
value: state.parent,
|
|
78
|
+
enumerable: false,
|
|
79
|
+
// Set this to true if you want the method to appear in for...in loops
|
|
80
|
+
configurable: true,
|
|
81
|
+
// Set this to true if you want to allow redefining/removing the property later
|
|
82
|
+
writable: true
|
|
83
|
+
// Set this to true if you want to allow changing the function later
|
|
84
|
+
});
|
|
85
|
+
} else {
|
|
86
|
+
Object.setPrototypeOf(child, { parent: state.parent });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
element.state.update({}, __spreadValues({ isHoisted: true }, options));
|
|
92
|
+
return element.state;
|
|
93
|
+
};
|
|
94
|
+
const parentUpdate = function(obj, options = {}) {
|
|
95
|
+
const state = this;
|
|
96
|
+
if (!state || !state.parent)
|
|
97
|
+
return;
|
|
98
|
+
return state.parent.update(obj, __spreadValues({ isHoisted: true }, options));
|
|
99
|
+
};
|
|
100
|
+
const rootUpdate = function(obj, options = {}) {
|
|
101
|
+
const state = this;
|
|
102
|
+
if (!state)
|
|
103
|
+
return;
|
|
104
|
+
const rootState = state.__element.__ref.root.state;
|
|
105
|
+
return rootState.update(obj, __spreadValues({ isHoisted: false }, options));
|
|
106
|
+
};
|
|
107
|
+
const add = function(value, options = {}) {
|
|
108
|
+
const state = this;
|
|
109
|
+
if (isArray(state)) {
|
|
110
|
+
state.push(value);
|
|
111
|
+
state.update(state.parse(), __spreadValues({ overwrite: true }, options));
|
|
112
|
+
} else if (isObject(state)) {
|
|
113
|
+
const key = Object.keys(state).length;
|
|
114
|
+
state.update({ [key]: value }, options);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const toggle = function(key, options = {}) {
|
|
118
|
+
const state = this;
|
|
119
|
+
state.update({ [key]: !state[key] }, options);
|
|
120
|
+
};
|
|
121
|
+
const remove = function(key, options = {}) {
|
|
122
|
+
const state = this;
|
|
123
|
+
if (isArray(state))
|
|
124
|
+
removeFromArray(state, key);
|
|
125
|
+
if (isObject(state))
|
|
126
|
+
removeFromObject(state, key);
|
|
127
|
+
if (options.applyReset)
|
|
128
|
+
return state.set(state.parse(), __spreadValues({ replace: true }, options));
|
|
129
|
+
return state.update();
|
|
130
|
+
};
|
|
131
|
+
const set = function(val, options = {}) {
|
|
132
|
+
const state = this;
|
|
133
|
+
const value = deepClone(val);
|
|
134
|
+
return state.clean(__spreadValues({ preventStateUpdate: true }, options)).update(value, __spreadValues({ replace: true }, options));
|
|
135
|
+
};
|
|
136
|
+
const setByPath = function(path, val, options = {}) {
|
|
137
|
+
const state = this;
|
|
138
|
+
const value = deepClone(val);
|
|
139
|
+
setInObjectByPath(state, path, val);
|
|
140
|
+
const update = createNestedObject(path, value);
|
|
141
|
+
if (options.preventUpdate)
|
|
142
|
+
return update;
|
|
143
|
+
return state.update(update, options);
|
|
144
|
+
};
|
|
145
|
+
const setPathCollection = function(changes, options = {}) {
|
|
146
|
+
const state = this;
|
|
147
|
+
const update = changes.reduce((acc, change) => {
|
|
148
|
+
const result = setByPath(...change.slice(1), { preventUpdate: true });
|
|
149
|
+
return overwriteDeep(acc, result);
|
|
150
|
+
}, {});
|
|
151
|
+
return state.update(update, options);
|
|
152
|
+
};
|
|
153
|
+
const removeByPath = function(path, options = {}) {
|
|
154
|
+
const state = this;
|
|
155
|
+
removeNestedKeyByPath(state, path);
|
|
156
|
+
if (options.preventUpdate)
|
|
157
|
+
return path;
|
|
158
|
+
return state.update({}, options);
|
|
159
|
+
};
|
|
160
|
+
const removePathCollection = function(changes, options = {}) {
|
|
161
|
+
const state = this;
|
|
162
|
+
changes.forEach((item) => {
|
|
163
|
+
removeByPath(item, { preventUpdate: true });
|
|
164
|
+
});
|
|
165
|
+
return state.update({}, options);
|
|
166
|
+
};
|
|
167
|
+
const getByPath = function(path, options = {}) {
|
|
168
|
+
const state = this;
|
|
169
|
+
return getInObjectByPath(state, path);
|
|
170
|
+
};
|
|
171
|
+
const reset = function(options = {}) {
|
|
172
|
+
const state = this;
|
|
173
|
+
const value = deepClone(state.parse());
|
|
174
|
+
return state.set(value, __spreadValues({ replace: true }, options));
|
|
175
|
+
};
|
|
176
|
+
const apply = function(func, options = {}) {
|
|
177
|
+
const state = this;
|
|
178
|
+
if (isFunction(func)) {
|
|
179
|
+
const value = func(state);
|
|
180
|
+
return state.update(value, __spreadValues({ replace: true }, options));
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
const applyReplace = function(func, options = {}) {
|
|
184
|
+
const state = this;
|
|
185
|
+
if (isFunction(func)) {
|
|
186
|
+
const value = func(state);
|
|
187
|
+
return state.replace(value, options);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const applyFunction = function(func, options = {}) {
|
|
191
|
+
const state = this;
|
|
192
|
+
if (isFunction(func)) {
|
|
193
|
+
func(state);
|
|
194
|
+
return state.update(state.parse(), __spreadValues({ replace: true }, options));
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
const quietUpdate = function(obj, options = {}) {
|
|
198
|
+
const state = this;
|
|
199
|
+
return state.update(obj, __spreadValues({ preventUpdate: true }, options));
|
|
200
|
+
};
|
|
201
|
+
const replace = function(obj, options = {}) {
|
|
202
|
+
const state = this;
|
|
203
|
+
for (const param in obj) {
|
|
204
|
+
state[param] = obj[param];
|
|
205
|
+
}
|
|
206
|
+
return state.update(obj, options);
|
|
207
|
+
};
|
|
208
|
+
const quietReplace = function(obj, options = {}) {
|
|
209
|
+
const state = this;
|
|
210
|
+
return state.replace(obj, __spreadValues({ preventUpdate: true }, options));
|
|
211
|
+
};
|
|
212
|
+
const keys = function(obj, options = {}) {
|
|
213
|
+
const state = this;
|
|
214
|
+
return Object.keys(state);
|
|
215
|
+
};
|
|
216
|
+
const values = function(obj, options = {}) {
|
|
217
|
+
const state = this;
|
|
218
|
+
return Object.values(state);
|
|
219
|
+
};
|
|
220
|
+
export {
|
|
221
|
+
add,
|
|
222
|
+
apply,
|
|
223
|
+
applyFunction,
|
|
224
|
+
applyReplace,
|
|
225
|
+
clean,
|
|
226
|
+
destroy,
|
|
227
|
+
getByPath,
|
|
228
|
+
keys,
|
|
229
|
+
parentUpdate,
|
|
230
|
+
parse,
|
|
231
|
+
quietReplace,
|
|
232
|
+
quietUpdate,
|
|
233
|
+
remove,
|
|
234
|
+
removeByPath,
|
|
235
|
+
removePathCollection,
|
|
236
|
+
replace,
|
|
237
|
+
reset,
|
|
238
|
+
rootUpdate,
|
|
239
|
+
set,
|
|
240
|
+
setByPath,
|
|
241
|
+
setPathCollection,
|
|
242
|
+
toggle,
|
|
243
|
+
values
|
|
244
|
+
};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { report } from "@domql/report";
|
|
21
|
+
import { triggerEventOnUpdate } from "@domql/event";
|
|
22
|
+
import { IGNORE_STATE_PARAMS } from "./ignore.js";
|
|
23
|
+
import { deepMerge, merge, overwriteDeep, overwriteShallow } from "@domql/utils";
|
|
24
|
+
import { checkIfInherits, createNestedObjectByKeyPath, findInheritedState, getParentStateInKey, getRootStateInKey } from "./inherit.js";
|
|
25
|
+
const STATE_UPDATE_OPTIONS = {
|
|
26
|
+
overwrite: true,
|
|
27
|
+
preventHoistElementUpdate: false,
|
|
28
|
+
updateByState: true,
|
|
29
|
+
isHoisted: true,
|
|
30
|
+
execStateFunction: true
|
|
31
|
+
};
|
|
32
|
+
const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
|
|
33
|
+
const state = this;
|
|
34
|
+
const element = state.__element;
|
|
35
|
+
if (options.onEach)
|
|
36
|
+
options.onEach(element, state, element.context, options);
|
|
37
|
+
if (!options.updateByState)
|
|
38
|
+
merge(options, STATE_UPDATE_OPTIONS);
|
|
39
|
+
if (!state.__element)
|
|
40
|
+
report("ElementOnStateIsNotDefined");
|
|
41
|
+
if (options.preventInheritAtCurrentState === true) {
|
|
42
|
+
options.preventInheritAtCurrentState = state;
|
|
43
|
+
} else if (options.preventInheritAtCurrentState)
|
|
44
|
+
return;
|
|
45
|
+
if (!options.preventBeforeStateUpdateListener) {
|
|
46
|
+
const beforeStateUpdateReturns = triggerEventOnUpdate("beforeStateUpdate", obj, element, options);
|
|
47
|
+
if (beforeStateUpdateReturns === false)
|
|
48
|
+
return element;
|
|
49
|
+
}
|
|
50
|
+
applyOverwrite(state, obj, options);
|
|
51
|
+
const updateIsHoisted = hoistStateUpdate(state, obj, options);
|
|
52
|
+
if (updateIsHoisted)
|
|
53
|
+
return state;
|
|
54
|
+
updateDependentState(state, obj, options);
|
|
55
|
+
applyElementUpdate(state, obj, options);
|
|
56
|
+
if (!options.preventStateUpdateListener) {
|
|
57
|
+
triggerEventOnUpdate("stateUpdate", obj, element, options);
|
|
58
|
+
}
|
|
59
|
+
return state;
|
|
60
|
+
};
|
|
61
|
+
const applyOverwrite = (state, obj, options) => {
|
|
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) => {
|
|
77
|
+
const element = state.__element;
|
|
78
|
+
const { parent, __ref: ref } = element;
|
|
79
|
+
const stateKey = ref == null ? void 0 : ref.__state;
|
|
80
|
+
const stateType = ref == null ? void 0 : ref.__stateType;
|
|
81
|
+
if (!stateKey)
|
|
82
|
+
return;
|
|
83
|
+
const asksForInherit = checkIfInherits(element);
|
|
84
|
+
const inheritedState = findInheritedState(element, parent, { returnParent: true });
|
|
85
|
+
const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
|
|
86
|
+
if (!shouldPropagateState)
|
|
87
|
+
return;
|
|
88
|
+
const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
|
|
89
|
+
const value = isStringState ? state.value : state.parse();
|
|
90
|
+
const passedValue = isStringState ? state.value : obj;
|
|
91
|
+
const findRootState = getRootStateInKey(stateKey, parent.state);
|
|
92
|
+
const findGrandParentState = getParentStateInKey(stateKey, parent.state);
|
|
93
|
+
const changesValue = createNestedObjectByKeyPath(stateKey, passedValue);
|
|
94
|
+
const targetParent = findRootState || findGrandParentState || parent.state;
|
|
95
|
+
if (options.replace)
|
|
96
|
+
overwriteDeep(targetParent, changesValue || value);
|
|
97
|
+
targetParent.update(changesValue, __spreadValues({
|
|
98
|
+
execStateFunction: false,
|
|
99
|
+
isHoisted: true,
|
|
100
|
+
preventUpdate: options.preventHoistElementUpdate,
|
|
101
|
+
overwrite: !options.replace
|
|
102
|
+
}, options));
|
|
103
|
+
const hasNotUpdated = options.preventUpdate !== true || !options.preventHoistElementUpdate;
|
|
104
|
+
if (!options.preventStateUpdateListener && hasNotUpdated) {
|
|
105
|
+
triggerEventOnUpdate("stateUpdate", obj, element, options);
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
};
|
|
109
|
+
const updateDependentState = (state, obj, options) => {
|
|
110
|
+
if (!state.__depends)
|
|
111
|
+
return;
|
|
112
|
+
for (const el in state.__depends) {
|
|
113
|
+
const dependentState = state.__depends[el];
|
|
114
|
+
dependentState.clean().update(state.parse(), options);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const applyElementUpdate = (state, obj, options) => {
|
|
118
|
+
const element = state.__element;
|
|
119
|
+
if (options.preventUpdate !== true) {
|
|
120
|
+
element.update({}, __spreadProps(__spreadValues({}, options), {
|
|
121
|
+
updateByState: true
|
|
122
|
+
}));
|
|
123
|
+
} else if (options.preventUpdate === "recursive") {
|
|
124
|
+
element.update({}, __spreadProps(__spreadValues({}, options), {
|
|
125
|
+
isHoisted: false,
|
|
126
|
+
updateByState: true,
|
|
127
|
+
preventUpdate: true
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
export {
|
|
132
|
+
updateState
|
|
133
|
+
};
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
export * from './ignore'
|
|
4
|
-
export * from './create'
|
|
5
|
-
export * from './updateState'
|
|
6
|
-
export * from './methods'
|
|
7
|
-
export * from './inherit'
|
|
3
|
+
export * from './ignore.js'
|
|
4
|
+
export * from './create.js'
|
|
5
|
+
export * from './updateState.js'
|
|
6
|
+
export * from './methods.js'
|
|
7
|
+
export * from './inherit.js'
|
package/inherit.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
import { deepClone, is, isObjectLike, isUndefined } from '@domql/utils'
|
|
4
|
-
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
4
|
+
import { IGNORE_STATE_PARAMS } from './ignore.js'
|
|
5
5
|
|
|
6
6
|
export const getRootStateInKey = (stateKey, parentState) => {
|
|
7
7
|
if (!stateKey.includes('~/')) return
|
package/methods.js
CHANGED
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.187",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"module": "
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"main": "index.js",
|
|
7
8
|
"unpkg": "dist/iife/index.js",
|
|
8
9
|
"jsdelivr": "dist/iife/index.js",
|
|
9
|
-
"main": "dist/esm/index.js",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
-
"
|
|
13
|
-
"
|
|
12
|
+
"default": "./dist/esm/index.js",
|
|
13
|
+
"import": "./dist/esm/index.js",
|
|
14
|
+
"require": "./dist/cjs/index.js"
|
|
14
15
|
}
|
|
15
16
|
},
|
|
16
17
|
"source": "index.js",
|
|
@@ -23,13 +24,13 @@
|
|
|
23
24
|
"build:esm": "npx esbuild *.js --target=es2017 --format=esm --outdir=dist/esm",
|
|
24
25
|
"build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
|
|
25
26
|
"build:iife": "npx esbuild *.js --target=node16 --format=iife --outdir=dist/iife",
|
|
26
|
-
"build": "npm run build:cjs",
|
|
27
|
+
"build": "rimraf -I dist; npm run build:cjs; npm run build:esm",
|
|
27
28
|
"prepublish": "rimraf -I dist && npm run build && npm run copy:package:cjs"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@domql/event": "^2.5.
|
|
31
|
-
"@domql/report": "^2.5.
|
|
32
|
-
"@domql/utils": "^2.5.
|
|
31
|
+
"@domql/event": "^2.5.187",
|
|
32
|
+
"@domql/report": "^2.5.186",
|
|
33
|
+
"@domql/utils": "^2.5.187"
|
|
33
34
|
},
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "c150bfbbdd51b19d25c93f10334d54175cea9d1d"
|
|
35
36
|
}
|
package/updateState.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import { report } from '@domql/report'
|
|
4
4
|
import { triggerEventOnUpdate } from '@domql/event'
|
|
5
|
-
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
5
|
+
import { IGNORE_STATE_PARAMS } from './ignore.js'
|
|
6
6
|
import { deepMerge, merge, overwriteDeep, overwriteShallow } from '@domql/utils'
|
|
7
|
-
import { checkIfInherits, createNestedObjectByKeyPath, findInheritedState, getParentStateInKey, getRootStateInKey } from './inherit'
|
|
7
|
+
import { checkIfInherits, createNestedObjectByKeyPath, findInheritedState, getParentStateInKey, getRootStateInKey } from './inherit.js'
|
|
8
8
|
|
|
9
9
|
const STATE_UPDATE_OPTIONS = {
|
|
10
10
|
overwrite: true,
|