@domql/state 2.3.122 → 2.3.123

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
@@ -12,16 +12,17 @@ export const createState = function (element, parent, opts) {
12
12
 
13
13
  const objectizeState = checkForTypes(element)
14
14
  if (objectizeState === false) return parent.state || {}
15
- else element.state = objectizeState
15
+ else element.state = deepClone(objectizeState, IGNORE_STATE_PARAMS)
16
16
 
17
17
  const whatInitReturns = triggerEventOn('stateInit', element)
18
18
  if (whatInitReturns === false) return element.state
19
19
 
20
20
  if (checkIfInherits(element)) {
21
- element.state = createInheritedState(element, parent) || {}
21
+ const inheritedState = createInheritedState(element, parent)
22
+ element.state = inheritedState || {}
22
23
  }
23
24
 
24
- const dependentState = applyDependentState(element, state)
25
+ const dependentState = applyDependentState(element, element.state)
25
26
  if (dependentState) element.state = dependentState
26
27
 
27
28
  // NOTE: Only true when 'onlyResolveExtends' option is set to true
@@ -33,14 +33,15 @@ const createState = function(element, parent, opts) {
33
33
  if (objectizeState === false)
34
34
  return parent.state || {};
35
35
  else
36
- element.state = objectizeState;
36
+ element.state = (0, import_utils.deepClone)(objectizeState, import_ignore.IGNORE_STATE_PARAMS);
37
37
  const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element);
38
38
  if (whatInitReturns === false)
39
39
  return element.state;
40
40
  if ((0, import_inherit.checkIfInherits)(element)) {
41
- element.state = (0, import_inherit.createInheritedState)(element, parent) || {};
41
+ const inheritedState = (0, import_inherit.createInheritedState)(element, parent);
42
+ element.state = inheritedState || {};
42
43
  }
43
- const dependentState = applyDependentState(element, state);
44
+ const dependentState = applyDependentState(element, element.state);
44
45
  if (dependentState)
45
46
  element.state = dependentState;
46
47
  if (skip)
@@ -49,8 +50,8 @@ const createState = function(element, parent, opts) {
49
50
  (0, import_event.triggerEventOn)("stateCreated", element);
50
51
  return element.state;
51
52
  };
52
- const applyDependentState = (element, state2) => {
53
- const { __ref: ref } = state2;
53
+ const applyDependentState = (element, state) => {
54
+ const { __ref: ref } = state;
54
55
  if (!ref)
55
56
  return;
56
57
  const dependentState = (0, import_utils.deepClone)(ref, import_ignore.IGNORE_STATE_PARAMS);
@@ -59,42 +60,42 @@ const applyDependentState = (element, state2) => {
59
60
  return dependentState;
60
61
  };
61
62
  const checkForTypes = (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);
63
+ const { state, __ref: ref } = element;
64
+ if ((0, import_utils.isFunction)(state)) {
65
+ ref.__state = state;
66
+ return (0, import_utils.exec)(state, element);
66
67
  }
67
- if ((0, import_utils.is)(state2)("string", "number")) {
68
- ref.__state = state2;
68
+ if ((0, import_utils.is)(state)("string", "number")) {
69
+ ref.__state = state;
69
70
  return {};
70
71
  }
71
- if (state2 === true) {
72
+ if (state === true) {
72
73
  ref.__state = element.key;
73
74
  return {};
74
75
  }
75
- if (state2) {
76
+ if (state) {
76
77
  ref.__hasRootState = true;
77
- return state2;
78
+ return state;
78
79
  }
79
80
  return false;
80
81
  };
81
82
  const applyMethods = (element) => {
82
- const state2 = element.state;
83
+ const state = element.state;
83
84
  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;
85
+ state.clean = import_methods.clean;
86
+ state.parse = import_methods.parse;
87
+ state.destroy = import_methods.destroy;
88
+ state.update = import_updateState.updateState;
89
+ state.rootUpdate = import_methods.rootUpdate;
90
+ state.create = createState;
91
+ state.add = import_methods.add;
92
+ state.toggle = import_methods.toggle;
93
+ state.remove = import_methods.remove;
94
+ state.apply = import_methods.apply;
95
+ state.parent = element.parent.state;
96
+ state.__element = element;
97
+ state.__children = {};
98
+ state.__root = ref.__root ? ref.__root.state : state;
99
+ if (state.parent)
100
+ state.parent.__children[element.key] = state;
100
101
  };
@@ -19,6 +19,7 @@ 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,
22
23
  createInheritedState: () => createInheritedState,
23
24
  findInheritedState: () => findInheritedState,
24
25
  getChildStateInKey: () => getChildStateInKey,
@@ -27,7 +28,10 @@ __export(inherit_exports, {
27
28
  });
28
29
  module.exports = __toCommonJS(inherit_exports);
29
30
  var import_utils = require("@domql/utils");
31
+ var import_ignore = require("./ignore");
30
32
  const getParentStateInKey = (stateKey2, parentState) => {
33
+ if (!stateKey2.includes("../"))
34
+ return;
31
35
  const arr = stateKey2.split("../");
32
36
  const arrLength = arr.length - 1;
33
37
  for (let i = 0; i < arrLength; i++) {
@@ -37,7 +41,7 @@ const getParentStateInKey = (stateKey2, parentState) => {
37
41
  }
38
42
  return parentState;
39
43
  };
40
- const getChildStateInKey = (stateKey2, parentState) => {
44
+ const getChildStateInKey = (stateKey2, parentState, options) => {
41
45
  const arr = stateKey2.split("/");
42
46
  const arrLength = arr.length - 1;
43
47
  for (let i = 0; i < arrLength; i++) {
@@ -50,29 +54,32 @@ const getChildStateInKey = (stateKey2, parentState) => {
50
54
  } else
51
55
  return;
52
56
  }
57
+ if (options.returnParent)
58
+ return parentState;
53
59
  return parentState[stateKey2];
54
60
  };
55
- const findInheritedState = (element, parent) => {
61
+ const findInheritedState = (element, parent, options = {}) => {
56
62
  const ref = element.__ref;
57
63
  let stateKey2 = ref.__state;
58
- if (!stateKey2 || (0, import_utils.isNot)(stateKey2)("number", "string"))
59
- return element.state;
64
+ if (!checkIfInherits(element))
65
+ return;
60
66
  let parentState = parent.state;
61
- if (stateKey2.includes("../")) {
62
- parentState = getParentStateInKey(stateKey2, parent.state);
67
+ const findGrandParentState = getParentStateInKey(stateKey2, parent.state);
68
+ if (findGrandParentState) {
69
+ parentState = findGrandParentState;
63
70
  stateKey2 = stateKey2.replaceAll("../", "");
64
71
  }
65
72
  if (!parentState)
66
73
  return;
67
- return getChildStateInKey(stateKey2, parentState);
74
+ return getChildStateInKey(stateKey2, parentState, options);
68
75
  };
69
76
  const createInheritedState = (element, parent) => {
70
77
  const ref = element.__ref;
71
78
  const inheritedState = findInheritedState(element, parent);
72
79
  if (!inheritedState)
73
- return;
80
+ return element.state;
74
81
  if ((0, import_utils.is)(inheritedState)("object", "array")) {
75
- return (0, import_utils.deepClone)(inheritedState);
82
+ return (0, import_utils.deepClone)(inheritedState, import_ignore.IGNORE_STATE_PARAMS);
76
83
  } else if ((0, import_utils.is)(inheritedState)("string", "number")) {
77
84
  ref.__stateType = "string";
78
85
  return { value: inheritedState };
@@ -92,3 +99,16 @@ const isState = function(state) {
92
99
  const keys = Object.keys(state);
93
100
  return (0, import_utils.arrayContainsOtherArray)(keys, ["update", "parse", "clean", "create", "parent", "rootUpdate"]);
94
101
  };
102
+ const createChangesByKey = (path, value) => {
103
+ if (!path) {
104
+ return value || {};
105
+ }
106
+ const keys = path.split("/");
107
+ let obj = {};
108
+ let ref = obj;
109
+ keys.forEach((key, index) => {
110
+ ref[key] = index === keys.length - 1 ? value || {} : {};
111
+ ref = ref[key];
112
+ });
113
+ return obj;
114
+ };
@@ -84,13 +84,15 @@ const add = function(value, options = {}) {
84
84
  const state = this;
85
85
  if ((0, import_utils.isArray)(state)) {
86
86
  state.push(value);
87
- state.update(state.parse(), { skipOverwrite: true, ...options });
87
+ state.update(state.parse(), { replace: true, ...options });
88
+ } else if ((0, import_utils.isObject)(state)) {
89
+ const key = Object.keys(state).length;
90
+ state.update({ [key]: value }, options);
88
91
  }
89
92
  };
90
93
  const toggle = function(key, options = {}) {
91
94
  const state = this;
92
- state[key] = !state[key];
93
- state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options });
95
+ state.update({ [key]: !state[key] }, options);
94
96
  };
95
97
  const remove = function(key, options = {}) {
96
98
  const state = this;
@@ -98,12 +100,12 @@ const remove = function(key, options = {}) {
98
100
  (0, import_utils.removeFromArray)(state, key);
99
101
  if ((0, import_utils.isObject)(state))
100
102
  (0, import_utils.removeFromObject)(state, key);
101
- return state.update(state.parse(), { skipOverwrite: true, ...options });
103
+ return state.update(state.parse(), { replace: true, ...options });
102
104
  };
103
105
  const apply = function(func, options = {}) {
104
106
  const state = this;
105
107
  if ((0, import_utils.isFunction)(func)) {
106
108
  func(state);
107
- return state.update(state, { skipOverwrite: true, ...options });
109
+ return state.update(state, { replace: true, ...options });
108
110
  }
109
111
  };
@@ -26,7 +26,10 @@ var import_event = require("@domql/event");
26
26
  var import_ignore = require("./ignore");
27
27
  var import_utils = require("@domql/utils");
28
28
  var import_inherit = require("./inherit");
29
- const updateState = function(obj, options = {}) {
29
+ const STATE_UPDATE_OPTIONS = {
30
+ preventHoistElementUpdate: true
31
+ };
32
+ const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
30
33
  const state = this;
31
34
  const element = state.__element;
32
35
  if (!state.__element)
@@ -63,19 +66,22 @@ const hoistStateUpdate = (state, obj, options) => {
63
66
  if (!stateKey)
64
67
  return;
65
68
  const asksForInherit = (0, import_inherit.checkIfInherits)(element);
66
- const inheritedState = (0, import_inherit.findInheritedState)(element, parent);
69
+ const inheritedState = (0, import_inherit.findInheritedState)(element, parent, { returnParent: true });
67
70
  const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation;
68
71
  if (!shouldPropagateState)
69
72
  return;
70
- console.log(inheritedState);
71
73
  const isStringState = ref.__stateType === "string";
72
74
  const value = isStringState ? state.value : state.parse();
73
75
  const passedValue = isStringState ? state.value : obj;
74
- inheritedState[stateKey] = value;
75
- inheritedState.update({ [stateKey]: passedValue }, {
76
- skipOverwrite: true,
76
+ const findGrandParentState = (0, import_inherit.getParentStateInKey)(stateKey, parent.state);
77
+ const changesValue = (0, import_inherit.createChangesByKey)(stateKey, passedValue);
78
+ const targetParent = findGrandParentState || parent.state;
79
+ if (options.replace)
80
+ targetParent[stateKey] = value;
81
+ targetParent.update(changesValue, {
82
+ ...options,
77
83
  preventUpdate: options.preventHoistElementUpdate,
78
- ...options
84
+ skipOverwrite: options.replace
79
85
  });
80
86
  };
81
87
  const updateDependentState = (state, obj, options) => {
package/inherit.js CHANGED
@@ -1,8 +1,10 @@
1
1
  'use strict'
2
2
 
3
3
  import { arrayContainsOtherArray, deepClone, is, isNot, isObjectLike } from '@domql/utils'
4
+ import { IGNORE_STATE_PARAMS } from './ignore'
4
5
 
5
6
  export const getParentStateInKey = (stateKey, parentState) => {
7
+ if (!stateKey.includes('../')) return
6
8
  const arr = stateKey.split('../')
7
9
  const arrLength = arr.length - 1
8
10
  for (let i = 0; i < arrLength; i++) {
@@ -12,7 +14,7 @@ export const getParentStateInKey = (stateKey, parentState) => {
12
14
  return parentState
13
15
  }
14
16
 
15
- export const getChildStateInKey = (stateKey, parentState) => {
17
+ export const getChildStateInKey = (stateKey, parentState, options) => {
16
18
  const arr = stateKey.split('/')
17
19
  const arrLength = arr.length - 1
18
20
  for (let i = 0; i < arrLength; i++) {
@@ -24,30 +26,33 @@ export const getChildStateInKey = (stateKey, parentState) => {
24
26
  parentState = childInParent
25
27
  } else return
26
28
  }
29
+ if (options.returnParent) return parentState
27
30
  return parentState[stateKey]
28
31
  }
29
32
 
30
- export const findInheritedState = (element, parent) => {
33
+ export const findInheritedState = (element, parent, options = {}) => {
31
34
  const ref = element.__ref
32
35
  let stateKey = ref.__state
33
- if (!stateKey || isNot(stateKey)('number', 'string')) return element.state
36
+ if (!checkIfInherits(element)) return
34
37
 
35
38
  let parentState = parent.state
36
- if (stateKey.includes('../')) {
37
- parentState = getParentStateInKey(stateKey, parent.state)
39
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state)
40
+ if (findGrandParentState) {
41
+ parentState = findGrandParentState
38
42
  stateKey = stateKey.replaceAll('../', '')
39
43
  }
44
+
40
45
  if (!parentState) return
41
- return getChildStateInKey(stateKey, parentState)
46
+ return getChildStateInKey(stateKey, parentState, options)
42
47
  }
43
48
 
44
49
  export const createInheritedState = (element, parent) => {
45
50
  const ref = element.__ref
46
51
  const inheritedState = findInheritedState(element, parent)
47
- if (!inheritedState) return
52
+ if (!inheritedState) return element.state
48
53
 
49
54
  if (is(inheritedState)('object', 'array')) {
50
- return deepClone(inheritedState)
55
+ return deepClone(inheritedState, IGNORE_STATE_PARAMS)
51
56
  } else if (is(inheritedState)('string', 'number')) {
52
57
  ref.__stateType = 'string'
53
58
  return { value: inheritedState }
@@ -68,3 +73,17 @@ export const isState = function (state) {
68
73
  const keys = Object.keys(state)
69
74
  return arrayContainsOtherArray(keys, ['update', 'parse', 'clean', 'create', 'parent', 'rootUpdate'])
70
75
  }
76
+
77
+ export const createChangesByKey = (path, value) => {
78
+ if (!path) {
79
+ return value || {};
80
+ }
81
+ const keys = path.split('/');
82
+ let obj = {};
83
+ let ref = obj;
84
+ keys.forEach((key, index) => {
85
+ ref[key] = index === keys.length - 1 ? value || {} : {};
86
+ ref = ref[key];
87
+ });
88
+ return obj;
89
+ }
package/methods.js CHANGED
@@ -64,27 +64,30 @@ export const add = function (value, options = {}) {
64
64
  const state = this
65
65
  if (isArray(state)) {
66
66
  state.push(value)
67
- state.update(state.parse(), { skipOverwrite: true, ...options })
67
+ state.update(state.parse(), { replace: true, ...options })
68
+ }
69
+ else if (isObject(state)) {
70
+ const key = Object.keys(state).length
71
+ state.update({ [key]: value }, options)
68
72
  }
69
73
  }
70
74
 
71
75
  export const toggle = function (key, options = {}) {
72
76
  const state = this
73
- state[key] = !state[key]
74
- state.update({ [key]: !state[key] }, { skipOverwrite: true, ...options })
77
+ state.update({ [key]: !state[key] }, options)
75
78
  }
76
79
 
77
80
  export const remove = function (key, options = {}) {
78
81
  const state = this
79
82
  if (isArray(state)) removeFromArray(state, key)
80
83
  if (isObject(state)) removeFromObject(state, key)
81
- return state.update(state.parse(), { skipOverwrite: true, ...options })
84
+ return state.update(state.parse(), { replace: true, ...options })
82
85
  }
83
86
 
84
87
  export const apply = function (func, options = {}) {
85
88
  const state = this
86
89
  if (isFunction(func)) {
87
90
  func(state)
88
- return state.update(state, { skipOverwrite: true, ...options })
91
+ return state.update(state, { replace: true, ...options })
89
92
  }
90
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.3.122",
3
+ "version": "2.3.123",
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": "e599b1dc425a72bd3ec989b909f47bb51bf4dbac"
29
+ "gitHead": "feb0d24eb5924f0f9ebea660a93afffc7d6a2059"
30
30
  }
package/updateState.js CHANGED
@@ -4,9 +4,13 @@ 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, findInheritedState } from './inherit'
7
+ import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit'
8
8
 
9
- export const updateState = function (obj, options = {}) {
9
+ const STATE_UPDATE_OPTIONS = {
10
+ preventHoistElementUpdate: true
11
+ }
12
+
13
+ export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
10
14
  const state = this
11
15
  const element = state.__element
12
16
 
@@ -53,20 +57,22 @@ const hoistStateUpdate = (state, obj, options) => {
53
57
  if (!stateKey) return
54
58
 
55
59
  const asksForInherit = checkIfInherits(element)
56
- const inheritedState = findInheritedState(element, parent)
60
+ const inheritedState = findInheritedState(element, parent, { returnParent: true })
57
61
  const shouldPropagateState = asksForInherit && inheritedState && !options.stopStatePropagation
58
62
  if (!shouldPropagateState) return
59
- console.log(inheritedState)
60
63
 
61
64
  const isStringState = (ref.__stateType === 'string')
62
65
  const value = isStringState ? state.value : state.parse()
63
66
  const passedValue = isStringState ? state.value : obj
64
67
 
65
- inheritedState[stateKey] = value
66
- inheritedState.update({ [stateKey]: passedValue }, {
67
- skipOverwrite: true,
68
+ const findGrandParentState = getParentStateInKey(stateKey, parent.state)
69
+ const changesValue = createChangesByKey(stateKey, passedValue)
70
+ const targetParent = findGrandParentState || parent.state
71
+ if (options.replace) targetParent[stateKey] = value
72
+ targetParent.update(changesValue, {
73
+ ...options,
68
74
  preventUpdate: options.preventHoistElementUpdate,
69
- ...options
75
+ skipOverwrite: options.replace
70
76
  })
71
77
  }
72
78