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