@domql/state 2.3.125 → 2.3.126

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
@@ -7,14 +7,14 @@ import { add, apply, clean, destroy, parse, remove, rootUpdate, set, toggle } fr
7
7
  import { updateState } from './updateState'
8
8
  import { checkIfInherits, createInheritedState } from './inherit'
9
9
 
10
- export const createState = function (element, parent, opts) {
11
- const skip = (opts && opts.skip) ? opts.skip : false
10
+ export const createState = function (element, parent, options) {
11
+ const skip = (options && options.skip) ? options.skip : false
12
12
 
13
13
  const objectizeState = checkForTypes(element)
14
14
  if (objectizeState === false) return parent.state || {}
15
15
  else element.state = deepClone(objectizeState, IGNORE_STATE_PARAMS)
16
16
 
17
- const whatInitReturns = triggerEventOn('stateInit', element)
17
+ const whatInitReturns = triggerEventOn('stateInit', element, options)
18
18
  if (whatInitReturns === false) return element.state
19
19
 
20
20
  if (checkIfInherits(element)) {
@@ -27,14 +27,14 @@ var import_ignore = require("./ignore");
27
27
  var import_methods = require("./methods");
28
28
  var import_updateState = require("./updateState");
29
29
  var import_inherit = require("./inherit");
30
- const createState = function(element, parent, opts) {
31
- const skip = opts && opts.skip ? opts.skip : false;
30
+ const createState = function(element, parent, options) {
31
+ const skip = options && options.skip ? options.skip : false;
32
32
  const objectizeState = checkForTypes(element);
33
33
  if (objectizeState === false)
34
34
  return parent.state || {};
35
35
  else
36
36
  element.state = (0, import_utils.deepClone)(objectizeState, import_ignore.IGNORE_STATE_PARAMS);
37
- const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element);
37
+ const whatInitReturns = (0, import_event.triggerEventOn)("stateInit", element, options);
38
38
  if (whatInitReturns === false)
39
39
  return element.state;
40
40
  if ((0, import_inherit.checkIfInherits)(element)) {
@@ -53,13 +53,18 @@ const clean = function(options = {}) {
53
53
  }
54
54
  }
55
55
  if (!options.preventStateUpdate) {
56
- state.update(state, { replace: true, skipOverwrite: true, options });
56
+ state.update(state, { replace: true, options });
57
57
  }
58
58
  return state;
59
59
  };
60
- const destroy = function() {
60
+ const destroy = function(options = {}) {
61
61
  const state = this;
62
62
  const element = state.__element;
63
+ const stateKey = element.__ref.__state;
64
+ if ((0, import_utils.isString)(stateKey)) {
65
+ element.parent.state.remove(stateKey, { isHoisted: true, ...options });
66
+ return element.state;
67
+ }
63
68
  delete element.state;
64
69
  element.state = state.parent;
65
70
  if (state.parent) {
@@ -73,7 +78,7 @@ const destroy = function() {
73
78
  }
74
79
  }
75
80
  }
76
- element.state.update();
81
+ element.state.update({}, { isHoisted: true, ...options });
77
82
  return element.state;
78
83
  };
79
84
  const rootUpdate = function(obj, options = {}) {
@@ -88,7 +93,7 @@ const add = function(value, options = {}) {
88
93
  const state = this;
89
94
  if ((0, import_utils.isArray)(state)) {
90
95
  state.push(value);
91
- state.update(state.parse(), { replace: true, ...options });
96
+ state.update(state.parse(), { overwrite: "replace", ...options });
92
97
  } else if ((0, import_utils.isObject)(state)) {
93
98
  const key = Object.keys(state).length;
94
99
  state.update({ [key]: value }, options);
@@ -100,7 +105,6 @@ const toggle = function(key, options = {}) {
100
105
  };
101
106
  const remove = function(key, options = {}) {
102
107
  const state = this;
103
- console.log(state);
104
108
  if ((0, import_utils.isArray)(state))
105
109
  (0, import_utils.removeFromArray)(state, key);
106
110
  if ((0, import_utils.isObject)(state))
@@ -109,13 +113,12 @@ const remove = function(key, options = {}) {
109
113
  };
110
114
  const set = function(value, options = {}) {
111
115
  const state = this;
112
- state.clean({ preventStateUpdate: true });
113
- return state.update(value, { replace: true, ...options });
116
+ return state.clean({ preventStateUpdate: true }).update(value, { replace: true, ...options });
114
117
  };
115
118
  const apply = function(func, options = {}) {
116
119
  const state = this;
117
120
  if ((0, import_utils.isFunction)(func)) {
118
121
  func(state);
119
- return state.update(state, { replace: true, ...options });
122
+ return state.update(state, { overwrite: "replace", ...options });
120
123
  }
121
124
  };
@@ -27,12 +27,17 @@ var import_ignore = require("./ignore");
27
27
  var import_utils = require("@domql/utils");
28
28
  var import_inherit = require("./inherit");
29
29
  const STATE_UPDATE_OPTIONS = {
30
+ overwrite: true,
30
31
  preventHoistElementUpdate: false,
31
- updateByState: true
32
+ updateByState: true,
33
+ execStateFunction: true,
34
+ stateFunctionOverwrite: "replace"
32
35
  };
33
36
  const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
34
37
  const state = this;
35
38
  const element = state.__element;
39
+ if (!options.updateByState)
40
+ (0, import_utils.merge)(options, STATE_UPDATE_OPTIONS);
36
41
  if (!state.__element)
37
42
  (0, import_report.report)("ElementOnStateIsNotDefined");
38
43
  if (options.preventInheritAtCurrentState === true) {
@@ -40,31 +45,33 @@ const updateState = function(obj, options = STATE_UPDATE_OPTIONS) {
40
45
  } else if (options.preventInheritAtCurrentState)
41
46
  return;
42
47
  if (!options.preventInitStateUpdateListener) {
43
- const initStateUpdateReturns = (0, import_event.triggerEventOn)("initStateUpdated", element, obj);
48
+ const initStateUpdateReturns = (0, import_event.triggerEventOnUpdate)("initStateUpdated", obj, element, options);
44
49
  if (initStateUpdateReturns === false)
45
50
  return element;
46
51
  }
47
52
  applyOverwrite(state, obj, options);
48
- const updateIsHousted = hoistStateUpdate(state, obj, options);
49
- if (updateIsHousted)
53
+ const updateIsHoisted = hoistStateUpdate(state, obj, options);
54
+ if (updateIsHoisted)
50
55
  return state;
51
56
  updateDependentState(state, obj, options);
52
57
  applyElementUpdate(state, obj, options);
53
58
  if (!options.preventStateUpdateListener) {
54
- (0, import_event.triggerEventOn)("stateUpdated", element, obj);
59
+ (0, import_event.triggerEventOnUpdate)("stateUpdated", obj, element, options);
55
60
  }
56
61
  return state;
57
62
  };
58
63
  const applyOverwrite = (state, obj, options) => {
59
- const { skipOverwrite, shallow } = options;
60
- if (skipOverwrite === "merge") {
64
+ const { overwrite } = options;
65
+ if (!overwrite)
66
+ return;
67
+ const shallow = overwrite === "shallow";
68
+ const merge2 = overwrite === "merge";
69
+ if (merge2) {
61
70
  (0, import_utils.deepMerge)(state, obj, import_ignore.IGNORE_STATE_PARAMS);
62
71
  return;
63
72
  }
64
- if (!skipOverwrite) {
65
- const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
66
- overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
67
- }
73
+ const overwriteFunc = shallow ? import_utils.overwriteShallow : import_utils.overwriteDeep;
74
+ overwriteFunc(state, obj, import_ignore.IGNORE_STATE_PARAMS);
68
75
  };
69
76
  const hoistStateUpdate = (state, obj, options) => {
70
77
  const element = state.__element;
@@ -86,10 +93,17 @@ const hoistStateUpdate = (state, obj, options) => {
86
93
  if (options.replace)
87
94
  targetParent[stateKey] = value;
88
95
  targetParent.update(changesValue, {
96
+ execStateFunction: false,
97
+ stateFunctionOverwrite: false,
98
+ isHoisted: true,
89
99
  ...options,
90
100
  preventUpdate: options.preventHoistElementUpdate,
91
- skipOverwrite: options.replace
101
+ overwrite: !options.replace
92
102
  });
103
+ const hasNotUpdated = !options.preventUpdate || !options.preventHoistElementUpdate;
104
+ if (!options.preventStateUpdateListener && hasNotUpdated) {
105
+ (0, import_event.triggerEventOnUpdate)("stateUpdated", obj, element, options);
106
+ }
93
107
  return true;
94
108
  };
95
109
  const updateDependentState = (state, obj, options) => {
package/methods.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- import { isArray, isFunction, isObject, removeFromArray, removeFromObject } from '@domql/utils'
3
+ import { isArray, isFunction, isObject, isString, removeFromArray, removeFromObject } from '@domql/utils'
4
4
 
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
6
 
@@ -27,14 +27,21 @@ export const clean = function (options = {}) {
27
27
  }
28
28
  }
29
29
  if (!options.preventStateUpdate) {
30
- state.update(state, { replace: true, skipOverwrite: true, options })
30
+ state.update(state, { replace: true, options })
31
31
  }
32
32
  return state
33
33
  }
34
34
 
35
- export const destroy = function () {
35
+ export const destroy = function (options = {}) {
36
36
  const state = this
37
37
  const element = state.__element
38
+
39
+ const stateKey = element.__ref.__state
40
+ if (isString(stateKey)) {
41
+ element.parent.state.remove(stateKey, { isHoisted: true, ...options })
42
+ return element.state
43
+ }
44
+
38
45
  delete element.state
39
46
  element.state = state.parent
40
47
 
@@ -51,7 +58,7 @@ export const destroy = function () {
51
58
  }
52
59
  }
53
60
 
54
- element.state.update()
61
+ element.state.update({}, { isHoisted: true, ...options })
55
62
  return element.state
56
63
  }
57
64
 
@@ -67,7 +74,7 @@ export const add = function (value, options = {}) {
67
74
  const state = this
68
75
  if (isArray(state)) {
69
76
  state.push(value)
70
- state.update(state.parse(), { replace: true, ...options })
77
+ state.update(state.parse(), { overwrite: 'replace', ...options })
71
78
  } else if (isObject(state)) {
72
79
  const key = Object.keys(state).length
73
80
  state.update({ [key]: value }, options)
@@ -81,7 +88,6 @@ export const toggle = function (key, options = {}) {
81
88
 
82
89
  export const remove = function (key, options = {}) {
83
90
  const state = this
84
- console.log(state)
85
91
  if (isArray(state)) removeFromArray(state, key)
86
92
  if (isObject(state)) removeFromObject(state, key)
87
93
  return state.update(state.parse(), { replace: true, ...options })
@@ -89,14 +95,14 @@ export const remove = function (key, options = {}) {
89
95
 
90
96
  export const set = function (value, options = {}) {
91
97
  const state = this
92
- state.clean({ preventStateUpdate: true })
93
- return state.update(value, { replace: true, ...options })
98
+ return state.clean({ preventStateUpdate: true })
99
+ .update(value, { replace: true, ...options })
94
100
  }
95
101
 
96
102
  export const apply = function (func, options = {}) {
97
103
  const state = this
98
104
  if (isFunction(func)) {
99
105
  func(state)
100
- return state.update(state, { replace: true, ...options })
106
+ return state.update(state, { overwrite: 'replace', ...options })
101
107
  }
102
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.3.125",
3
+ "version": "2.3.126",
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": "c62e01157f488334d07a8bbd67f892d617d48f06"
29
+ "gitHead": "6329de71d2e831d0efd9d59e765567aa8c563481"
30
30
  }
package/updateState.js CHANGED
@@ -1,63 +1,65 @@
1
1
  'use strict'
2
2
 
3
3
  import { report } from '@domql/report'
4
- import { triggerEventOn } from '@domql/event'
4
+ import { triggerEventOnUpdate } from '@domql/event'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
- import { deepMerge, overwriteDeep, overwriteShallow } from '@domql/utils'
6
+ import { deepMerge, merge, overwriteDeep, overwriteShallow } from '@domql/utils'
7
7
  import { checkIfInherits, createChangesByKey, findInheritedState, getParentStateInKey } from './inherit'
8
8
 
9
9
  const STATE_UPDATE_OPTIONS = {
10
+ overwrite: true,
10
11
  preventHoistElementUpdate: false,
11
- updateByState: true
12
+ updateByState: true,
13
+ execStateFunction: true,
14
+ stateFunctionOverwrite: 'replace'
12
15
  }
13
16
 
14
17
  export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
15
18
  const state = this
16
19
  const element = state.__element
17
20
 
21
+ if (!options.updateByState) merge(options, STATE_UPDATE_OPTIONS)
22
+
18
23
  if (!state.__element) report('ElementOnStateIsNotDefined')
19
24
  if (options.preventInheritAtCurrentState === true) {
20
25
  options.preventInheritAtCurrentState = state
21
26
  } else if (options.preventInheritAtCurrentState) return
22
27
 
23
28
  if (!options.preventInitStateUpdateListener) {
24
- const initStateUpdateReturns = triggerEventOn('initStateUpdated', element, obj)
29
+ const initStateUpdateReturns = triggerEventOnUpdate('initStateUpdated', obj, element, options)
25
30
  if (initStateUpdateReturns === false) return element
26
31
  }
27
32
 
28
33
  applyOverwrite(state, obj, options)
29
34
 
30
- const updateIsHousted = hoistStateUpdate(state, obj, options)
31
- if (updateIsHousted) return state
35
+ const updateIsHoisted = hoistStateUpdate(state, obj, options)
36
+ if (updateIsHoisted) return state
32
37
 
33
38
  updateDependentState(state, obj, options)
34
39
 
35
40
  applyElementUpdate(state, obj, options)
36
41
 
37
42
  if (!options.preventStateUpdateListener) {
38
- triggerEventOn('stateUpdated', element, obj)
43
+ triggerEventOnUpdate('stateUpdated', obj, element, options)
39
44
  }
40
45
 
41
46
  return state
42
47
  }
43
48
 
44
49
  const applyOverwrite = (state, obj, options) => {
45
- const { skipOverwrite, shallow } = options
50
+ const { overwrite } = options
51
+ if (!overwrite) return
52
+
53
+ const shallow = overwrite === 'shallow'
54
+ const merge = overwrite === 'merge'
46
55
 
47
- if (skipOverwrite === 'merge') {
56
+ if (merge) {
48
57
  deepMerge(state, obj, IGNORE_STATE_PARAMS)
49
58
  return
50
59
  }
51
60
 
52
- // if (skipOverwrite === 'skipOverwrite') {
53
- // deepMerge(state, obj, IGNORE_STATE_PARAMS)
54
- // return
55
- // }
56
-
57
- if (!skipOverwrite) {
58
- const overwriteFunc = shallow ? overwriteShallow : overwriteDeep
59
- overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
60
- }
61
+ const overwriteFunc = shallow ? overwriteShallow : overwriteDeep
62
+ overwriteFunc(state, obj, IGNORE_STATE_PARAMS)
61
63
  }
62
64
 
63
65
  const hoistStateUpdate = (state, obj, options) => {
@@ -80,10 +82,17 @@ const hoistStateUpdate = (state, obj, options) => {
80
82
  const targetParent = findGrandParentState || parent.state
81
83
  if (options.replace) targetParent[stateKey] = value
82
84
  targetParent.update(changesValue, {
85
+ execStateFunction: false,
86
+ stateFunctionOverwrite: false,
87
+ isHoisted: true,
83
88
  ...options,
84
89
  preventUpdate: options.preventHoistElementUpdate,
85
- skipOverwrite: options.replace
90
+ overwrite: !options.replace
86
91
  })
92
+ const hasNotUpdated = !options.preventUpdate || !options.preventHoistElementUpdate
93
+ if (!options.preventStateUpdateListener && hasNotUpdated) {
94
+ triggerEventOnUpdate('stateUpdated', obj, element, options)
95
+ }
87
96
  return true
88
97
  }
89
98