@domql/state 2.5.53 → 2.5.56

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.
@@ -19,16 +19,24 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var inherit_exports = {};
20
20
  __export(inherit_exports, {
21
21
  checkIfInherits: () => checkIfInherits,
22
- createChangesByKey: () => createChangesByKey,
23
22
  createInheritedState: () => createInheritedState,
23
+ createNestedObjectByKeyPath: () => createNestedObjectByKeyPath,
24
24
  findInheritedState: () => findInheritedState,
25
25
  getChildStateInKey: () => getChildStateInKey,
26
26
  getParentStateInKey: () => getParentStateInKey,
27
+ getRootStateInKey: () => getRootStateInKey,
27
28
  isState: () => isState
28
29
  });
29
30
  module.exports = __toCommonJS(inherit_exports);
30
31
  var import_utils = require("@domql/utils");
31
32
  var import_ignore = require("./ignore");
33
+ const getRootStateInKey = (stateKey, parentState) => {
34
+ if (!stateKey.includes("~/"))
35
+ return;
36
+ const arr = stateKey.split("~/");
37
+ if (arr.length > 1)
38
+ return parentState.__root;
39
+ };
32
40
  const getParentStateInKey = (stateKey, parentState) => {
33
41
  if (!stateKey.includes("../"))
34
42
  return;
@@ -66,11 +74,17 @@ const findInheritedState = (element, parent, options = {}) => {
66
74
  let stateKey = ref.__state;
67
75
  if (!checkIfInherits(element))
68
76
  return;
77
+ const rootState = getRootStateInKey(stateKey, parent.state);
69
78
  let parentState = parent.state;
70
- const findGrandParentState = getParentStateInKey(stateKey, parent.state);
71
- if (findGrandParentState) {
72
- parentState = findGrandParentState;
73
- stateKey = stateKey.replaceAll("../", "");
79
+ if (rootState) {
80
+ parentState = rootState;
81
+ stateKey = stateKey.replaceAll("~/", "");
82
+ } else {
83
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state);
84
+ if (findGrandParentState) {
85
+ parentState = findGrandParentState;
86
+ stateKey = stateKey.replaceAll("../", "");
87
+ }
74
88
  }
75
89
  if (!parentState)
76
90
  return;
@@ -101,7 +115,7 @@ const isState = function(state) {
101
115
  return false;
102
116
  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;
103
117
  };
104
- const createChangesByKey = (path, value) => {
118
+ const createNestedObjectByKeyPath = (path, value) => {
105
119
  if (!path) {
106
120
  return value || {};
107
121
  }
@@ -112,7 +112,7 @@ const add = function(value, options = {}) {
112
112
  const state = this;
113
113
  if ((0, import_utils.isArray)(state)) {
114
114
  state.push(value);
115
- state.update(state.parse(), { overwrite: "replace", ...options });
115
+ state.update(state.parse(), { overwrite: true, ...options });
116
116
  } else if ((0, import_utils.isObject)(state)) {
117
117
  const key = Object.keys(state).length;
118
118
  state.update({ [key]: value }, options);
@@ -90,9 +90,10 @@ const hoistStateUpdate = (state, obj, options) => {
90
90
  const isStringState = stateType === "string" || stateType === "number" || stateType === "boolean";
91
91
  const value = isStringState ? state.value : state.parse();
92
92
  const passedValue = isStringState ? state.value : obj;
93
+ const findRootState = (0, import_inherit.getRootStateInKey)(stateKey, parent.state);
93
94
  const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
94
- const changesValue = (0, import_inherit.createChangesByKey)(stateKey, passedValue);
95
- const targetParent = findGrandParentState || parent.state;
95
+ const changesValue = (0, import_inherit.createNestedObjectByKeyPath)(stateKey, passedValue);
96
+ const targetParent = findRootState || findGrandParentState || parent.state;
96
97
  if (options.replace)
97
98
  (0, import_utils.overwriteDeep)(targetParent, changesValue || value);
98
99
  targetParent.update(changesValue, {
package/inherit.js CHANGED
@@ -3,6 +3,12 @@
3
3
  import { deepCloneWithExtnd, is, isObjectLike, isUndefined } from '@domql/utils'
4
4
  import { IGNORE_STATE_PARAMS } from './ignore'
5
5
 
6
+ export const getRootStateInKey = (stateKey, parentState) => {
7
+ if (!stateKey.includes('~/')) return
8
+ const arr = stateKey.split('~/')
9
+ if (arr.length > 1) return parentState.__root
10
+ }
11
+
6
12
  export const getParentStateInKey = (stateKey, parentState) => {
7
13
  if (!stateKey.includes('../')) return
8
14
  const arr = stateKey.split('../')
@@ -39,11 +45,18 @@ export const findInheritedState = (element, parent, options = {}) => {
39
45
  let stateKey = ref.__state
40
46
  if (!checkIfInherits(element)) return
41
47
 
48
+ const rootState = getRootStateInKey(stateKey, parent.state)
42
49
  let parentState = parent.state
43
- const findGrandParentState = getParentStateInKey(stateKey, parent.state)
44
- if (findGrandParentState) {
45
- parentState = findGrandParentState
46
- stateKey = stateKey.replaceAll('../', '')
50
+
51
+ if (rootState) {
52
+ parentState = rootState
53
+ stateKey = stateKey.replaceAll('~/', '')
54
+ } else {
55
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state)
56
+ if (findGrandParentState) {
57
+ parentState = findGrandParentState
58
+ stateKey = stateKey.replaceAll('../', '')
59
+ }
47
60
  }
48
61
 
49
62
  if (!parentState) return
@@ -91,7 +104,7 @@ export const isState = function (state) {
91
104
  // return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
92
105
  }
93
106
 
94
- export const createChangesByKey = (path, value) => {
107
+ export const createNestedObjectByKeyPath = (path, value) => {
95
108
  if (!path) {
96
109
  return value || {}
97
110
  }
package/methods.js CHANGED
@@ -88,7 +88,7 @@ export const add = function (value, options = {}) {
88
88
  const state = this
89
89
  if (isArray(state)) {
90
90
  state.push(value)
91
- state.update(state.parse(), { overwrite: 'replace', ...options })
91
+ state.update(state.parse(), { overwrite: true, ...options })
92
92
  } else if (isObject(state)) {
93
93
  const key = Object.keys(state).length
94
94
  state.update({ [key]: value }, options)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.5.53",
3
+ "version": "2.5.56",
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": "aafcffa2ebe3d9c2fb96bd3adcb05325b62e68ff"
34
+ "gitHead": "a832fd0b6d8c39f16e6c7b2b61ab6c3dccd1f539"
35
35
  }
package/updateState.js CHANGED
@@ -4,7 +4,7 @@ import { report } from '@domql/report'
4
4
  import { triggerEventOnUpdate } from '@domql/event'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
  import { deepMerge, merge, overwriteDeep, overwriteShallow } from '@domql/utils'
7
- import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit'
7
+ import { checkIfInherits, createNestedObjectByKeyPath, findInheritedState, getParentStateInKey, getRootStateInKey } from './inherit'
8
8
 
9
9
  const STATE_UPDATE_OPTIONS = {
10
10
  overwrite: true,
@@ -78,10 +78,11 @@ const hoistStateUpdate = (state, obj, options) => {
78
78
  const value = isStringState ? state.value : state.parse()
79
79
  const passedValue = isStringState ? state.value : obj
80
80
 
81
+ const findRootState = getRootStateInKey(stateKey, parent.state)
81
82
  const findGrandParentState = getParentStateInKey(stateKey, parent.state)
82
- const changesValue = createChangesByKey(stateKey, passedValue)
83
- const targetParent = findGrandParentState || parent.state
84
- if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createChangesByKey
83
+ const changesValue = createNestedObjectByKeyPath(stateKey, passedValue)
84
+ const targetParent = findRootState || findGrandParentState || parent.state
85
+ if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createNestedObjectByKeyPath
85
86
  targetParent.update(changesValue, {
86
87
  execStateFunction: false,
87
88
  isHoisted: true,