@domql/state 2.3.152 → 2.4.1
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 → create.js} +22 -10
- package/dist/cjs/create.js +124 -0
- package/dist/cjs/createState.js +18 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/inherit.js +1 -2
- package/index.js +1 -1
- package/inherit.js +15 -3
- package/package.json +3 -3
- package/LICENSE +0 -21
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
import { triggerEventOn } from '@domql/event'
|
|
4
|
-
import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
|
|
4
|
+
import { deepClone, exec, is, isArray, isFunction, isObject } from '@domql/utils'
|
|
5
5
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
6
6
|
import { add, apply, clean, destroy, parentUpdate, parse, remove, rootUpdate, set, toggle } from './methods'
|
|
7
7
|
import { updateState } from './updateState'
|
|
8
8
|
import { checkIfInherits, createInheritedState } from './inherit'
|
|
9
9
|
|
|
10
10
|
export const createState = function (element, parent, options) {
|
|
11
|
-
|
|
11
|
+
element.state = applyInitialState(element, parent, options)
|
|
12
|
+
}
|
|
12
13
|
|
|
14
|
+
export const applyInitialState = function (element, parent, options) {
|
|
13
15
|
const objectizeState = checkForTypes(element)
|
|
14
16
|
if (objectizeState === false) return parent.state || {}
|
|
15
17
|
else element.state = deepClone(objectizeState, IGNORE_STATE_PARAMS)
|
|
@@ -25,12 +27,6 @@ export const createState = function (element, parent, options) {
|
|
|
25
27
|
const dependentState = applyDependentState(element, element.state)
|
|
26
28
|
if (dependentState) element.state = dependentState
|
|
27
29
|
|
|
28
|
-
// NOTE: Only true when 'onlyResolveExtends' option is set to true
|
|
29
|
-
if (skipApplyMethods) {
|
|
30
|
-
if (element.parent && element.parent.state) { element.state.parent = element.parent.state } else { element.state.parent = {} }
|
|
31
|
-
return element.state
|
|
32
|
-
}
|
|
33
|
-
|
|
34
30
|
applyMethods(element)
|
|
35
31
|
|
|
36
32
|
// trigger `on.stateCreated`
|
|
@@ -72,6 +68,17 @@ const checkForTypes = (element) => {
|
|
|
72
68
|
return false
|
|
73
69
|
}
|
|
74
70
|
|
|
71
|
+
const addProtoToArray = (state, proto) => {
|
|
72
|
+
for (const key in proto) {
|
|
73
|
+
Object.defineProperty(state, key, {
|
|
74
|
+
value: proto[key],
|
|
75
|
+
enumerable: false, // Set this to true if you want the method to appear in for...in loops
|
|
76
|
+
configurable: true, // Set this to true if you want to allow redefining/removing the property later
|
|
77
|
+
writable: true // Set this to true if you want to allow changing the function later
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
75
82
|
const applyMethods = (element) => {
|
|
76
83
|
const state = element.state
|
|
77
84
|
const ref = element.__ref
|
|
@@ -95,7 +102,12 @@ const applyMethods = (element) => {
|
|
|
95
102
|
__root: ref.__root ? ref.__root.state : state
|
|
96
103
|
}
|
|
97
104
|
|
|
98
|
-
|
|
105
|
+
if (isArray(state)) {
|
|
106
|
+
addProtoToArray(state, proto)
|
|
107
|
+
} else {
|
|
108
|
+
Object.setPrototypeOf(state, proto)
|
|
109
|
+
}
|
|
99
110
|
|
|
100
|
-
if (state.parent
|
|
111
|
+
if (state.parent && state.parent.__children)
|
|
112
|
+
state.parent.__children[element.key] = state
|
|
101
113
|
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var create_exports = {};
|
|
20
|
+
__export(create_exports, {
|
|
21
|
+
applyInitialState: () => applyInitialState,
|
|
22
|
+
createState: () => createState
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(create_exports);
|
|
25
|
+
var import_event = require("@domql/event");
|
|
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");
|
|
31
|
+
const createState = function(element, parent, options) {
|
|
32
|
+
element.state = applyInitialState(element, parent, options);
|
|
33
|
+
};
|
|
34
|
+
const applyInitialState = function(element, parent, options) {
|
|
35
|
+
const objectizeState = checkForTypes(element);
|
|
36
|
+
if (objectizeState === false)
|
|
37
|
+
return parent.state || {};
|
|
38
|
+
else
|
|
39
|
+
element.state = (0, import_utils.deepClone)(objectizeState, import_ignore.IGNORE_STATE_PARAMS);
|
|
40
|
+
const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element, options);
|
|
41
|
+
if (whatInitReturns === false)
|
|
42
|
+
return element.state;
|
|
43
|
+
if ((0, import_inherit.checkIfInherits)(element)) {
|
|
44
|
+
const inheritedState = (0, import_inherit.createInheritedState)(element, parent);
|
|
45
|
+
element.state = inheritedState || {};
|
|
46
|
+
}
|
|
47
|
+
const dependentState = applyDependentState(element, element.state);
|
|
48
|
+
if (dependentState)
|
|
49
|
+
element.state = dependentState;
|
|
50
|
+
applyMethods(element);
|
|
51
|
+
(0, import_event.triggerEventOn)("stateCreated", element);
|
|
52
|
+
return element.state;
|
|
53
|
+
};
|
|
54
|
+
const applyDependentState = (element, state) => {
|
|
55
|
+
const { __ref: ref } = state;
|
|
56
|
+
if (!ref)
|
|
57
|
+
return;
|
|
58
|
+
const dependentState = (0, import_utils.deepClone)(ref, import_ignore.IGNORE_STATE_PARAMS);
|
|
59
|
+
const newDepends = { [element.key]: dependentState };
|
|
60
|
+
ref.__depends = (0, import_utils.isObject)(ref.__depends) ? { ...ref.__depends, ...newDepends } : newDepends;
|
|
61
|
+
return dependentState;
|
|
62
|
+
};
|
|
63
|
+
const checkForTypes = (element) => {
|
|
64
|
+
const { state, __ref: ref } = element;
|
|
65
|
+
if ((0, import_utils.isFunction)(state)) {
|
|
66
|
+
ref.__state = state;
|
|
67
|
+
return (0, import_utils.exec)(state, element);
|
|
68
|
+
}
|
|
69
|
+
if ((0, import_utils.is)(state)("string", "number")) {
|
|
70
|
+
ref.__state = state;
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
if (state === true) {
|
|
74
|
+
ref.__state = element.key;
|
|
75
|
+
return {};
|
|
76
|
+
}
|
|
77
|
+
if (state) {
|
|
78
|
+
ref.__hasRootState = true;
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
};
|
|
83
|
+
const addProtoToArray = (state, proto) => {
|
|
84
|
+
for (const key in proto) {
|
|
85
|
+
Object.defineProperty(state, key, {
|
|
86
|
+
value: proto[key],
|
|
87
|
+
enumerable: false,
|
|
88
|
+
// Set this to true if you want the method to appear in for...in loops
|
|
89
|
+
configurable: true,
|
|
90
|
+
// Set this to true if you want to allow redefining/removing the property later
|
|
91
|
+
writable: true
|
|
92
|
+
// Set this to true if you want to allow changing the function later
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const applyMethods = (element) => {
|
|
97
|
+
const state = element.state;
|
|
98
|
+
const ref = element.__ref;
|
|
99
|
+
const proto = {
|
|
100
|
+
clean: import_methods.clean.bind(state),
|
|
101
|
+
parse: import_methods.parse.bind(state),
|
|
102
|
+
destroy: import_methods.destroy.bind(state),
|
|
103
|
+
update: import_updateState.updateState.bind(state),
|
|
104
|
+
rootUpdate: import_methods.rootUpdate.bind(state),
|
|
105
|
+
parentUpdate: import_methods.parentUpdate.bind(state),
|
|
106
|
+
create: createState.bind(state),
|
|
107
|
+
add: import_methods.add.bind(state),
|
|
108
|
+
toggle: import_methods.toggle.bind(state),
|
|
109
|
+
remove: import_methods.remove.bind(state),
|
|
110
|
+
apply: import_methods.apply.bind(state),
|
|
111
|
+
set: import_methods.set.bind(state),
|
|
112
|
+
parent: element.parent.state,
|
|
113
|
+
__element: element,
|
|
114
|
+
__children: {},
|
|
115
|
+
__root: ref.__root ? ref.__root.state : state
|
|
116
|
+
};
|
|
117
|
+
if ((0, import_utils.isArray)(state)) {
|
|
118
|
+
addProtoToArray(state, proto);
|
|
119
|
+
} else {
|
|
120
|
+
Object.setPrototypeOf(state, proto);
|
|
121
|
+
}
|
|
122
|
+
if (state.parent && state.parent.__children)
|
|
123
|
+
state.parent.__children[element.key] = state;
|
|
124
|
+
};
|
package/dist/cjs/createState.js
CHANGED
|
@@ -85,6 +85,19 @@ const checkForTypes = (element) => {
|
|
|
85
85
|
}
|
|
86
86
|
return false;
|
|
87
87
|
};
|
|
88
|
+
const addProtoToArray = (state, proto) => {
|
|
89
|
+
for (const key in proto) {
|
|
90
|
+
Object.defineProperty(state, key, {
|
|
91
|
+
value: proto[key],
|
|
92
|
+
enumerable: false,
|
|
93
|
+
// Set this to true if you want the method to appear in for...in loops
|
|
94
|
+
configurable: true,
|
|
95
|
+
// Set this to true if you want to allow redefining/removing the property later
|
|
96
|
+
writable: true
|
|
97
|
+
// Set this to true if you want to allow changing the function later
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
};
|
|
88
101
|
const applyMethods = (element) => {
|
|
89
102
|
const state = element.state;
|
|
90
103
|
const ref = element.__ref;
|
|
@@ -106,7 +119,11 @@ const applyMethods = (element) => {
|
|
|
106
119
|
__children: {},
|
|
107
120
|
__root: ref.__root ? ref.__root.state : state
|
|
108
121
|
};
|
|
109
|
-
|
|
122
|
+
if ((0, import_utils.isArray)(state)) {
|
|
123
|
+
addProtoToArray(state, proto);
|
|
124
|
+
} else {
|
|
125
|
+
Object.setPrototypeOf(state, proto);
|
|
126
|
+
}
|
|
110
127
|
if (state.parent)
|
|
111
128
|
state.parent.__children[element.key] = state;
|
|
112
129
|
};
|
package/dist/cjs/index.js
CHANGED
|
@@ -16,7 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
16
16
|
var state_exports = {};
|
|
17
17
|
module.exports = __toCommonJS(state_exports);
|
|
18
18
|
__reExport(state_exports, require("./ignore"), module.exports);
|
|
19
|
-
__reExport(state_exports, require("./
|
|
19
|
+
__reExport(state_exports, require("./create"), module.exports);
|
|
20
20
|
__reExport(state_exports, require("./updateState"), module.exports);
|
|
21
21
|
__reExport(state_exports, require("./methods"), module.exports);
|
|
22
22
|
__reExport(state_exports, require("./inherit"), module.exports);
|
package/dist/cjs/inherit.js
CHANGED
|
@@ -99,8 +99,7 @@ const checkIfInherits = (element) => {
|
|
|
99
99
|
const isState = function(state) {
|
|
100
100
|
if (!(0, import_utils.isObjectLike)(state))
|
|
101
101
|
return false;
|
|
102
|
-
|
|
103
|
-
return (0, import_utils.arrayContainsOtherArray)(keys, ["update", "parse", "clean", "create", "parent", "rootUpdate"]);
|
|
102
|
+
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;
|
|
104
103
|
};
|
|
105
104
|
const createChangesByKey = (path, value) => {
|
|
106
105
|
if (!path) {
|
package/index.js
CHANGED
package/inherit.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { deepClone, is, isNot, isObjectLike } from '@domql/utils'
|
|
4
4
|
import { IGNORE_STATE_PARAMS } from './ignore'
|
|
5
5
|
|
|
6
6
|
export const getParentStateInKey = (stateKey, parentState) => {
|
|
@@ -74,8 +74,20 @@ export const checkIfInherits = (element) => {
|
|
|
74
74
|
|
|
75
75
|
export const isState = function (state) {
|
|
76
76
|
if (!isObjectLike(state)) return false
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
return state.update &&
|
|
78
|
+
state.parse &&
|
|
79
|
+
state.clean &&
|
|
80
|
+
state.create &&
|
|
81
|
+
state.parent &&
|
|
82
|
+
state.destroy &&
|
|
83
|
+
state.rootUpdate &&
|
|
84
|
+
state.parentUpdate &&
|
|
85
|
+
state.toggle &&
|
|
86
|
+
state.add &&
|
|
87
|
+
state.apply &&
|
|
88
|
+
state.__element &&
|
|
89
|
+
state.__children
|
|
90
|
+
// return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
|
|
79
91
|
}
|
|
80
92
|
|
|
81
93
|
export const createChangesByKey = (path, value) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/state",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"dist"
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
|
-
"copy:package:cjs": "cp
|
|
22
|
+
"copy:package:cjs": "cp ../../build/package-cjs.json dist/cjs/package.json",
|
|
23
23
|
"build:esm": "npx esbuild *.js --target=es2019 --format=esm --outdir=dist/esm",
|
|
24
24
|
"build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
|
|
25
25
|
"build:iife": "npx esbuild *.js --target=node16 --format=iife --outdir=dist/iife",
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"@domql/report": "latest",
|
|
32
32
|
"@domql/utils": "latest"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "86251c1d94b9e87288cc9e84fd9badb6461b2973"
|
|
35
35
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2016 symbo.ls
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|