@domql/state 2.3.120 → 2.3.122

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
@@ -5,72 +5,72 @@ import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
  import { add, apply, clean, destroy, parse, remove, rootUpdate, toggle } from './methods'
7
7
  import { updateState } from './updateState'
8
- import { checkIfInherits, createInheritedState } from './utils'
8
+ import { checkIfInherits, createInheritedState } from './inherit'
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
  }
@@ -26,71 +26,75 @@ var import_utils = require("@domql/utils");
26
26
  var import_ignore = require("./ignore");
27
27
  var import_methods = require("./methods");
28
28
  var import_updateState = require("./updateState");
29
- var import_utils2 = require("./utils");
29
+ var import_inherit = require("./inherit");
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);
35
- if ((0, import_utils2.checkIfInherits)(element)) {
36
- state = element.state = (0, import_utils2.createInheritedState)(element, parent) || {};
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;
40
+ if ((0, import_inherit.checkIfInherits)(element)) {
41
+ element.state = (0, import_inherit.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
  };
package/dist/cjs/index.js CHANGED
@@ -19,4 +19,4 @@ __reExport(state_exports, require("./ignore"), module.exports);
19
19
  __reExport(state_exports, require("./createState"), module.exports);
20
20
  __reExport(state_exports, require("./updateState"), module.exports);
21
21
  __reExport(state_exports, require("./methods"), module.exports);
22
- __reExport(state_exports, require("./utils"), module.exports);
22
+ __reExport(state_exports, require("./inherit"), module.exports);
@@ -16,18 +16,19 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var utils_exports = {};
20
- __export(utils_exports, {
19
+ var inherit_exports = {};
20
+ __export(inherit_exports, {
21
21
  checkIfInherits: () => checkIfInherits,
22
22
  createInheritedState: () => createInheritedState,
23
+ findInheritedState: () => findInheritedState,
23
24
  getChildStateInKey: () => getChildStateInKey,
24
25
  getParentStateInKey: () => getParentStateInKey,
25
26
  isState: () => isState
26
27
  });
27
- module.exports = __toCommonJS(utils_exports);
28
+ module.exports = __toCommonJS(inherit_exports);
28
29
  var import_utils = require("@domql/utils");
29
- const getParentStateInKey = (stateKey, parentState) => {
30
- const arr = stateKey.split("../");
30
+ const getParentStateInKey = (stateKey2, parentState) => {
31
+ const arr = stateKey2.split("../");
31
32
  const arrLength = arr.length - 1;
32
33
  for (let i = 0; i < arrLength; i++) {
33
34
  if (!parentState.parent)
@@ -36,48 +37,52 @@ const getParentStateInKey = (stateKey, parentState) => {
36
37
  }
37
38
  return parentState;
38
39
  };
39
- const getChildStateInKey = (stateKey, parentState) => {
40
- const arr = stateKey.split("/");
40
+ const getChildStateInKey = (stateKey2, parentState) => {
41
+ const arr = stateKey2.split("/");
41
42
  const arrLength = arr.length - 1;
42
43
  for (let i = 0; i < arrLength; i++) {
43
44
  const childKey = arr[i];
44
45
  const grandChildKey = arr[i + 1];
45
46
  const childInParent = parentState[childKey];
46
47
  if (childInParent && childInParent[grandChildKey]) {
47
- stateKey = grandChildKey;
48
+ stateKey2 = grandChildKey;
48
49
  parentState = childInParent;
49
50
  } else
50
51
  return;
51
52
  }
52
- return parentState[stateKey];
53
+ return parentState[stateKey2];
53
54
  };
54
- const createInheritedState = (element, parent) => {
55
- const __elementRef = element.__ref;
56
- let stateKey = __elementRef.__state;
57
- if (!stateKey || (0, import_utils.isNot)(stateKey)("number", "string"))
55
+ const findInheritedState = (element, parent) => {
56
+ const ref = element.__ref;
57
+ let stateKey2 = ref.__state;
58
+ if (!stateKey2 || (0, import_utils.isNot)(stateKey2)("number", "string"))
58
59
  return element.state;
59
60
  let parentState = parent.state;
60
- if (stateKey.includes("../")) {
61
- parentState = getParentStateInKey(stateKey, parent.state);
62
- stateKey = stateKey.replaceAll("../", "");
61
+ if (stateKey2.includes("../")) {
62
+ parentState = getParentStateInKey(stateKey2, parent.state);
63
+ stateKey2 = stateKey2.replaceAll("../", "");
63
64
  }
64
65
  if (!parentState)
65
66
  return;
66
- const keyInParentState = getChildStateInKey(stateKey, parentState);
67
- if (!keyInParentState)
67
+ return getChildStateInKey(stateKey2, parentState);
68
+ };
69
+ const createInheritedState = (element, parent) => {
70
+ const ref = element.__ref;
71
+ const inheritedState = findInheritedState(element, parent);
72
+ if (!inheritedState)
68
73
  return;
69
- if ((0, import_utils.is)(keyInParentState)("object", "array")) {
70
- return (0, import_utils.deepClone)(keyInParentState);
71
- } else if ((0, import_utils.is)(keyInParentState)("string", "number")) {
72
- __elementRef.__stateType = "string";
73
- return { value: keyInParentState };
74
+ if ((0, import_utils.is)(inheritedState)("object", "array")) {
75
+ return (0, import_utils.deepClone)(inheritedState);
76
+ } else if ((0, import_utils.is)(inheritedState)("string", "number")) {
77
+ ref.__stateType = "string";
78
+ return { value: inheritedState };
74
79
  }
75
80
  console.warn(stateKey, "is not present. Replacing with", {});
76
81
  };
77
82
  const checkIfInherits = (element) => {
78
- const __elementRef = element.__ref;
79
- const stateKey = __elementRef.__state;
80
- if (!stateKey || (0, import_utils.isNot)(stateKey)("number", "string"))
83
+ const ref = element.__ref;
84
+ const stateKey2 = ref.__state;
85
+ if (!stateKey2 || (0, import_utils.isNot)(stateKey2)("number", "string"))
81
86
  return false;
82
87
  return true;
83
88
  };
@@ -25,7 +25,7 @@ var import_report = require("@domql/report");
25
25
  var import_event = require("@domql/event");
26
26
  var import_ignore = require("./ignore");
27
27
  var import_utils = require("@domql/utils");
28
- var import_utils2 = require("./utils");
28
+ var import_inherit = require("./inherit");
29
29
  const updateState = function(obj, options = {}) {
30
30
  const state = this;
31
31
  const element = state.__element;
@@ -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);
@@ -58,29 +58,32 @@ const applyOverwrite = (state, obj, options) => {
58
58
  };
59
59
  const hoistStateUpdate = (state, obj, options) => {
60
60
  const element = state.__element;
61
- const __elementRef = element.__ref;
62
- const stateKey = __elementRef.__state;
63
- const hasParentState = (0, import_utils2.checkIfInherits)(element);
64
- const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation;
65
- 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
- }
61
+ const { parent, __ref: ref } = element;
62
+ const stateKey = ref.__state;
63
+ if (!stateKey)
64
+ return;
65
+ const asksForInherit = (0, import_inherit.checkIfInherits)(element);
66
+ const inheritedState = (0, import_inherit.findInheritedState)(element, parent);
67
+ const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
68
+ if (!shouldPropagateState)
69
+ return;
70
+ console.log(inheritedState);
71
+ const isStringState = ref.__stateType === "string";
72
+ const value = isStringState ? state.value : state.parse();
73
+ const passedValue = isStringState ? state.value : obj;
74
+ inheritedState[stateKey] = value;
75
+ inheritedState.update({ [stateKey]: passedValue }, {
76
+ skipOverwrite: true,
77
+ preventUpdate: options.preventHoistElementUpdate,
78
+ ...options
79
+ });
77
80
  };
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
- }
81
+ const updateDependentState = (state, obj, options) => {
82
+ if (!state.__depends)
83
+ return;
84
+ for (const el in state.__depends) {
85
+ const dependentState = state.__depends[el];
86
+ dependentState.clean().update(state.parse(), options);
84
87
  }
85
88
  };
86
89
  const applyElementUpdate = (state, obj, options) => {
package/index.js CHANGED
@@ -4,4 +4,4 @@ export * from './ignore'
4
4
  export * from './createState'
5
5
  export * from './updateState'
6
6
  export * from './methods'
7
- export * from './utils'
7
+ export * from './inherit'
@@ -27,9 +27,9 @@ export const getChildStateInKey = (stateKey, parentState) => {
27
27
  return parentState[stateKey]
28
28
  }
29
29
 
30
- export const createInheritedState = (element, parent) => {
31
- const __elementRef = element.__ref
32
- let stateKey = __elementRef.__state
30
+ export const findInheritedState = (element, parent) => {
31
+ const ref = element.__ref
32
+ let stateKey = ref.__state
33
33
  if (!stateKey || isNot(stateKey)('number', 'string')) return element.state
34
34
 
35
35
  let parentState = parent.state
@@ -38,23 +38,27 @@ export const createInheritedState = (element, parent) => {
38
38
  stateKey = stateKey.replaceAll('../', '')
39
39
  }
40
40
  if (!parentState) return
41
+ return getChildStateInKey(stateKey, parentState)
42
+ }
41
43
 
42
- const keyInParentState = getChildStateInKey(stateKey, parentState)
43
- if (!keyInParentState) return
44
+ export const createInheritedState = (element, parent) => {
45
+ const ref = element.__ref
46
+ const inheritedState = findInheritedState(element, parent)
47
+ if (!inheritedState) return
44
48
 
45
- if (is(keyInParentState)('object', 'array')) {
46
- return deepClone(keyInParentState)
47
- } else if (is(keyInParentState)('string', 'number')) {
48
- __elementRef.__stateType = 'string'
49
- return { value: keyInParentState }
49
+ if (is(inheritedState)('object', 'array')) {
50
+ return deepClone(inheritedState)
51
+ } else if (is(inheritedState)('string', 'number')) {
52
+ ref.__stateType = 'string'
53
+ return { value: inheritedState }
50
54
  }
51
55
 
52
56
  console.warn(stateKey, 'is not present. Replacing with', {})
53
57
  }
54
58
 
55
59
  export const checkIfInherits = (element) => {
56
- const __elementRef = element.__ref
57
- const stateKey = __elementRef.__state
60
+ const ref = element.__ref
61
+ const stateKey = ref.__state
58
62
  if (!stateKey || isNot(stateKey)('number', 'string')) return false
59
63
  return true
60
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.3.120",
3
+ "version": "2.3.122",
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": "e599b1dc425a72bd3ec989b909f47bb51bf4dbac"
30
30
  }
package/updateState.js CHANGED
@@ -4,7 +4,7 @@ import { report } from '@domql/report'
4
4
  import { triggerEventOn } from '@domql/event'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
  import { deepMerge, overwriteDeep, overwriteShallow } from '@domql/utils'
7
- import { checkIfInherits } from './utils'
7
+ import { checkIfInherits, findInheritedState } from './inherit'
8
8
 
9
9
  export const updateState = function (obj, options = {}) {
10
10
  const state = this
@@ -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
 
@@ -48,32 +48,33 @@ const applyOverwrite = (state, obj, options) => {
48
48
 
49
49
  const hoistStateUpdate = (state, obj, options) => {
50
50
  const element = state.__element
51
- const __elementRef = element.__ref
52
- const stateKey = __elementRef.__state
53
- const hasParentState = checkIfInherits(element)
54
- const shouldPropagateState = stateKey && hasParentState && !options.stopStatePropagation
55
- const parentState = element.parent.state
56
-
57
- if (shouldPropagateState) {
58
- const isStringState = (__elementRef.__stateType === 'string')
59
- const value = isStringState ? state.value : state.parse()
60
- const passedValue = isStringState ? state.value : obj
61
-
62
- parentState[stateKey] = value
63
- parentState.update({ [stateKey]: passedValue }, {
64
- skipOverwrite: true,
65
- preventUpdate: options.preventHoistElementUpdate,
66
- ...options
67
- })
68
- }
51
+ const { parent, __ref: ref } = element
52
+ const stateKey = ref.__state
53
+ if (!stateKey) return
54
+
55
+ const asksForInherit = checkIfInherits(element)
56
+ const inheritedState = findInheritedState(element, parent)
57
+ const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation
58
+ if (!shouldPropagateState) return
59
+ console.log(inheritedState)
60
+
61
+ const isStringState = (ref.__stateType === 'string')
62
+ const value = isStringState ? state.value : state.parse()
63
+ const passedValue = isStringState ? state.value : obj
64
+
65
+ inheritedState[stateKey] = value
66
+ inheritedState.update({ [stateKey]: passedValue }, {
67
+ skipOverwrite: true,
68
+ preventUpdate: options.preventHoistElementUpdate,
69
+ ...options
70
+ })
69
71
  }
70
72
 
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
- }
73
+ const updateDependentState = (state, obj, options) => {
74
+ if (!state.__depends) return
75
+ for (const el in state.__depends) {
76
+ const dependentState = state.__depends[el]
77
+ dependentState.clean().update(state.parse(), options)
77
78
  }
78
79
  }
79
80