@domql/state 2.3.125 → 2.3.127

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
@@ -3,18 +3,18 @@
3
3
  import { triggerEventOn } from '@domql/event'
4
4
  import { deepClone, exec, is, isFunction, isObject } from '@domql/utils'
5
5
  import { IGNORE_STATE_PARAMS } from './ignore'
6
- import { add, apply, clean, destroy, parse, remove, rootUpdate, set, toggle } from './methods'
6
+ import { add, apply, clean, destroy, parentUpdate, parse, remove, rootUpdate, set, toggle } from './methods'
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)) {
@@ -78,6 +78,7 @@ const applyMethods = (element) => {
78
78
  state.destroy = destroy
79
79
  state.update = updateState
80
80
  state.rootUpdate = rootUpdate
81
+ state.parentUpdate = parentUpdate
81
82
  state.create = createState
82
83
  state.add = add
83
84
  state.toggle = toggle
@@ -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)) {
@@ -87,6 +87,7 @@ const applyMethods = (element) => {
87
87
  state.destroy = import_methods.destroy;
88
88
  state.update = import_updateState.updateState;
89
89
  state.rootUpdate = import_methods.rootUpdate;
90
+ state.parentUpdate = import_methods.parentUpdate;
90
91
  state.create = createState;
91
92
  state.add = import_methods.add;
92
93
  state.toggle = import_methods.toggle;
@@ -33,6 +33,7 @@ const IGNORE_STATE_PARAMS = [
33
33
  "apply",
34
34
  "set",
35
35
  "rootUpdate",
36
+ "parentUpdate",
36
37
  "parent",
37
38
  "__element",
38
39
  "__depends",
@@ -22,6 +22,7 @@ __export(methods_exports, {
22
22
  apply: () => apply,
23
23
  clean: () => clean,
24
24
  destroy: () => destroy,
25
+ parentUpdate: () => parentUpdate,
25
26
  parse: () => parse,
26
27
  remove: () => remove,
27
28
  rootUpdate: () => rootUpdate,
@@ -53,13 +54,18 @@ const clean = function(options = {}) {
53
54
  }
54
55
  }
55
56
  if (!options.preventStateUpdate) {
56
- state.update(state, { replace: true, skipOverwrite: true, options });
57
+ state.update(state, { replace: true, options });
57
58
  }
58
59
  return state;
59
60
  };
60
- const destroy = function() {
61
+ const destroy = function(options = {}) {
61
62
  const state = this;
62
63
  const element = state.__element;
64
+ const stateKey = element.__ref.__state;
65
+ if ((0, import_utils.isString)(stateKey)) {
66
+ element.parent.state.remove(stateKey, { isHoisted: true, ...options });
67
+ return element.state;
68
+ }
63
69
  delete element.state;
64
70
  element.state = state.parent;
65
71
  if (state.parent) {
@@ -73,14 +79,19 @@ const destroy = function() {
73
79
  }
74
80
  }
75
81
  }
76
- element.state.update();
82
+ element.state.update({}, { isHoisted: true, ...options });
77
83
  return element.state;
78
84
  };
85
+ const parentUpdate = function(obj, options = {}) {
86
+ const state = this;
87
+ if (!state || !state.parent)
88
+ return;
89
+ return state.parent.update(obj, { isHoisted: true, ...options });
90
+ };
79
91
  const rootUpdate = function(obj, options = {}) {
80
92
  const state = this;
81
93
  if (!state)
82
94
  return;
83
- console.log(options);
84
95
  const rootState = state.__element.__ref.__root.state;
85
96
  return rootState.update(obj, options);
86
97
  };
@@ -88,7 +99,7 @@ const add = function(value, options = {}) {
88
99
  const state = this;
89
100
  if ((0, import_utils.isArray)(state)) {
90
101
  state.push(value);
91
- state.update(state.parse(), { replace: true, ...options });
102
+ state.update(state.parse(), { overwrite: "replace", ...options });
92
103
  } else if ((0, import_utils.isObject)(state)) {
93
104
  const key = Object.keys(state).length;
94
105
  state.update({ [key]: value }, options);
@@ -100,7 +111,6 @@ const toggle = function(key, options = {}) {
100
111
  };
101
112
  const remove = function(key, options = {}) {
102
113
  const state = this;
103
- console.log(state);
104
114
  if ((0, import_utils.isArray)(state))
105
115
  (0, import_utils.removeFromArray)(state, key);
106
116
  if ((0, import_utils.isObject)(state))
@@ -109,13 +119,12 @@ const remove = function(key, options = {}) {
109
119
  };
110
120
  const set = function(value, options = {}) {
111
121
  const state = this;
112
- state.clean({ preventStateUpdate: true });
113
- return state.update(value, { replace: true, ...options });
122
+ return state.clean({ preventStateUpdate: true }).update(value, { replace: true, ...options });
114
123
  };
115
124
  const apply = function(func, options = {}) {
116
125
  const state = this;
117
126
  if ((0, import_utils.isFunction)(func)) {
118
127
  func(state);
119
- return state.update(state, { replace: true, ...options });
128
+ return state.update(state, { overwrite: "replace", ...options });
120
129
  }
121
130
  };
@@ -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/ignore.js CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  export const IGNORE_STATE_PARAMS = [
4
4
  'update', 'parse', 'clean', 'create', 'destroy', 'add', 'toggle', 'remove', 'apply', 'set',
5
- 'rootUpdate', 'parent', '__element', '__depends', '__ref', '__children', '__root'
5
+ 'rootUpdate', 'parentUpdate', 'parent', '__element', '__depends', '__ref', '__children', '__root'
6
6
  ]
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,14 +58,19 @@ 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
 
65
+ export const parentUpdate = function (obj, options = {}) {
66
+ const state = this
67
+ if (!state || !state.parent) return
68
+ return state.parent.update(obj, { isHoisted: true, ...options })
69
+ }
70
+
58
71
  export const rootUpdate = function (obj, options = {}) {
59
72
  const state = this
60
73
  if (!state) return
61
- console.log(options)
62
74
  const rootState = (state.__element.__ref.__root).state
63
75
  return rootState.update(obj, options)
64
76
  }
@@ -67,7 +79,7 @@ export const add = function (value, options = {}) {
67
79
  const state = this
68
80
  if (isArray(state)) {
69
81
  state.push(value)
70
- state.update(state.parse(), { replace: true, ...options })
82
+ state.update(state.parse(), { overwrite: 'replace', ...options })
71
83
  } else if (isObject(state)) {
72
84
  const key = Object.keys(state).length
73
85
  state.update({ [key]: value }, options)
@@ -81,7 +93,6 @@ export const toggle = function (key, options = {}) {
81
93
 
82
94
  export const remove = function (key, options = {}) {
83
95
  const state = this
84
- console.log(state)
85
96
  if (isArray(state)) removeFromArray(state, key)
86
97
  if (isObject(state)) removeFromObject(state, key)
87
98
  return state.update(state.parse(), { replace: true, ...options })
@@ -89,14 +100,14 @@ export const remove = function (key, options = {}) {
89
100
 
90
101
  export const set = function (value, options = {}) {
91
102
  const state = this
92
- state.clean({ preventStateUpdate: true })
93
- return state.update(value, { replace: true, ...options })
103
+ return state.clean({ preventStateUpdate: true })
104
+ .update(value, { replace: true, ...options })
94
105
  }
95
106
 
96
107
  export const apply = function (func, options = {}) {
97
108
  const state = this
98
109
  if (isFunction(func)) {
99
110
  func(state)
100
- return state.update(state, { replace: true, ...options })
111
+ return state.update(state, { overwrite: 'replace', ...options })
101
112
  }
102
113
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "2.3.125",
3
+ "version": "2.3.127",
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": "8cefec8e0527a4cbd0ec964055435b00c28ec7c3"
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