@domql/state 2.4.0 → 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.
@@ -8,8 +8,10 @@ import { updateState } from './updateState'
8
8
  import { checkIfInherits, createInheritedState } from './inherit'
9
9
 
10
10
  export const createState = function (element, parent, options) {
11
- const skipApplyMethods = Boolean(options && options.skipApplyMethods)
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`
@@ -112,5 +108,6 @@ const applyMethods = (element) => {
112
108
  Object.setPrototypeOf(state, proto)
113
109
  }
114
110
 
115
- if (state.parent) state.parent.__children[element.key] = state
111
+ if (state.parent && state.parent.__children)
112
+ state.parent.__children[element.key] = state
116
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/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("./createState"), module.exports);
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/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  export * from './ignore'
4
- export * from './createState'
4
+ export * from './create'
5
5
  export * from './updateState'
6
6
  export * from './methods'
7
7
  export * from './inherit'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.4.0",
3
+ "version": "2.4.1",
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": "d01a7237a0065a5a439a03062064076c5fdc91b7"
34
+ "gitHead": "86251c1d94b9e87288cc9e84fd9badb6461b2973"
35
35
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 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.