@domql/state 2.3.120 → 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 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
- state = element.state = checkForTypes(element)
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
- state = element.state = createInheritedState(element, parent) || {}
21
+ element.state = createInheritedState(element, parent) || {}
20
22
  }
21
23
 
22
- if (!state) {
23
- if (parent && parent.state) return parent.state
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, state)
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: __elementRef } = element
51
+ const { state, __ref: ref } = element
57
52
  if (isFunction(state)) {
58
- __elementRef.__state = state
53
+ ref.__state = state
59
54
  return exec(state, element)
60
55
  }
61
56
  if (is(state)('string', 'number')) {
62
- __elementRef.__state = state
57
+ ref.__state = state
63
58
  return {}
64
59
  }
65
60
  if (state === true) {
66
- __elementRef.__state = element.key
61
+ ref.__state = element.key
67
62
  return {}
68
63
  }
69
- return state
64
+ if (state) {
65
+ ref.__hasRootState = true
66
+ return state
67
+ }
68
+ return false
70
69
  }
71
70
 
72
- const applyMethods = (element, state) => {
73
- const __elementRef = element.__ref
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 = __elementRef.__root ? __elementRef.__root.state : state
88
+ state.__root = ref.__root ? ref.__root.state : state
89
89
 
90
90
  if (state.parent) state.parent.__children[element.key] = state
91
91
  }
@@ -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
- let { state, __ref: __elementRef } = element;
33
- state = element.state = checkForTypes(element);
34
- (0, import_event.triggerEventOn)("stateInit", element);
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
- state = element.state = (0, import_utils2.createInheritedState)(element, parent) || {};
41
+ element.state = (0, import_utils2.createInheritedState)(element, parent) || {};
37
42
  }
38
- if (!state) {
39
- if (parent && parent.state)
40
- return parent.state;
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, state);
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: __elementRef } = element;
64
- if ((0, import_utils.isFunction)(state)) {
65
- __elementRef.__state = state;
66
- return (0, import_utils.exec)(state, element);
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)(state)("string", "number")) {
69
- __elementRef.__state = state;
67
+ if ((0, import_utils.is)(state2)("string", "number")) {
68
+ ref.__state = state2;
70
69
  return {};
71
70
  }
72
- if (state === true) {
73
- __elementRef.__state = element.key;
71
+ if (state2 === true) {
72
+ ref.__state = element.key;
74
73
  return {};
75
74
  }
76
- return state;
75
+ if (state2) {
76
+ ref.__hasRootState = true;
77
+ return state2;
78
+ }
79
+ return false;
77
80
  };
78
- const applyMethods = (element, state) => {
79
- const __elementRef = element.__ref;
80
- state.clean = import_methods.clean;
81
- state.parse = import_methods.parse;
82
- state.destroy = import_methods.destroy;
83
- state.update = import_updateState.updateState;
84
- state.rootUpdate = import_methods.rootUpdate;
85
- state.create = createState;
86
- state.add = import_methods.add;
87
- state.toggle = import_methods.toggle;
88
- state.remove = import_methods.remove;
89
- state.apply = import_methods.apply;
90
- state.parent = element.parent.state;
91
- state.__element = element;
92
- state.__children = {};
93
- state.__root = __elementRef.__root ? __elementRef.__root.state : state;
94
- if (state.parent)
95
- state.parent.__children[element.key] = state;
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
  };
@@ -38,7 +38,7 @@ const updateState = function(obj, options = {}) {
38
38
  }
39
39
  applyOverwrite(state, obj, options);
40
40
  hoistStateUpdate(state, obj, options);
41
- applyDependentState(state, obj, options);
41
+ updateDependentState(state, obj, options);
42
42
  applyElementUpdate(state, obj, options);
43
43
  if (!options.preventStateUpdateListener) {
44
44
  (0, import_event.triggerEventOn)("stateUpdated", element, obj);
@@ -60,27 +60,29 @@ const hoistStateUpdate = (state, obj, options) => {
60
60
  const element = state.__element;
61
61
  const __elementRef = element.__ref;
62
62
  const stateKey = __elementRef.__state;
63
- const hasParentState = (0, import_utils2.checkIfInherits)(element);
64
- const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation;
63
+ if (!stateKey)
64
+ return;
65
+ const asksForInherit = (0, import_utils2.checkIfInherits)(element);
66
+ const shouldPropagateState = asksForInherit && !options.stopStatePropagation;
65
67
  const parentState = element.parent.state;
66
- if (shouldPropagateState) {
67
- const isStringState = __elementRef.__stateType === "string";
68
- const value = isStringState ? state.value : state.parse();
69
- const passedValue = isStringState ? state.value : obj;
70
- parentState[stateKey] = value;
71
- parentState.update({ [stateKey]: passedValue }, {
72
- skipOverwrite: true,
73
- preventUpdate: options.preventHoistElementUpdate,
74
- ...options
75
- });
76
- }
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
+ });
77
79
  };
78
- const applyDependentState = (state, obj, options) => {
79
- if (state.__depends) {
80
- for (const el in state.__depends) {
81
- const findElement = state.__depends[el];
82
- findElement.clean().update(state.parse(), options);
83
- }
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);
84
86
  }
85
87
  };
86
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.120",
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": "6e932a813b8e6ee0a57a40ebc6dc757cc44e6427"
29
+ "gitHead": "7bb63fd5aab7bfa92c0aaa3a7205ff9a002fac1c"
30
30
  }
package/updateState.js CHANGED
@@ -21,7 +21,7 @@ export const updateState = function (obj, options = {}) {
21
21
 
22
22
  hoistStateUpdate(state, obj, options)
23
23
 
24
- applyDependentState(state, obj, options)
24
+ updateDependentState(state, obj, options)
25
25
 
26
26
  applyElementUpdate(state, obj, options)
27
27
 
@@ -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
- const hasParentState = checkIfInherits(element)
54
- const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation
55
- const parentState = element.parent.state
53
+ if (!stateKey) return
56
54
 
57
- if (shouldPropagateState) {
58
- const isStringState = (__elementRef.__stateType === 'string')
59
- const value = isStringState ? state.value : state.parse()
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
- parentState[stateKey] = value
63
- parentState.update({ [stateKey]: passedValue }, {
64
- skipOverwrite: true,
65
- preventUpdate: options.preventHoistElementUpdate,
66
- ...options
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 applyDependentState = (state, obj, options) => {
72
- if (state.__depends) {
73
- for (const el in state.__depends) {
74
- const findElement = state.__depends[el]
75
- findElement.clean().update(state.parse(), options)
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