@domql/state 3.1.2 → 3.2.7

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/methods.js CHANGED
@@ -25,33 +25,33 @@ export const parse = function () {
25
25
  if (isObject(state)) {
26
26
  const obj = {}
27
27
  for (const param in state) {
28
- if (!STATE_METHODS.includes(param)) {
28
+ if (!STATE_METHODS.has(param)) {
29
29
  obj[param] = state[param]
30
30
  }
31
31
  }
32
32
  return obj
33
33
  } else if (isArray(state)) {
34
- return state.filter(item => !STATE_METHODS.includes(item))
34
+ return state.filter(item => !STATE_METHODS.has(item))
35
35
  }
36
36
  }
37
37
 
38
- export const clean = async function (options = {}) {
38
+ export const clean = function (options = {}) {
39
39
  const state = this
40
40
  for (const param in state) {
41
41
  if (
42
- !STATE_METHODS.includes(param) &&
43
- Object.hasOwnProperty.call(state, param)
42
+ !STATE_METHODS.has(param) &&
43
+ Object.prototype.hasOwnProperty.call(state, param)
44
44
  ) {
45
45
  delete state[param]
46
46
  }
47
47
  }
48
48
  if (!options.preventStateUpdate) {
49
- await state.update(state, { replace: true, ...options })
49
+ state.update(state, { replace: true, ...options })
50
50
  }
51
51
  return state
52
52
  }
53
53
 
54
- export const destroy = async function (options = {}) {
54
+ export const destroy = function (options = {}) {
55
55
  const state = this
56
56
  const element = state.__element
57
57
 
@@ -86,99 +86,98 @@ export const destroy = async function (options = {}) {
86
86
  }
87
87
  }
88
88
 
89
- await element.state.update({}, { isHoisted: true, ...options })
89
+ element.state.update({}, { isHoisted: true, ...options })
90
90
  return element.state
91
91
  }
92
92
 
93
- export const parentUpdate = async function (obj, options = {}) {
93
+ export const parentUpdate = function (obj, options = {}) {
94
94
  const state = this
95
95
  if (!state || !state.parent) return
96
- return await state.parent.update(obj, { isHoisted: true, ...options })
96
+ return state.parent.update(obj, { isHoisted: true, ...options })
97
97
  }
98
98
 
99
- export const rootUpdate = async function (obj, options = {}) {
99
+ export const rootUpdate = function (obj, options = {}) {
100
100
  const state = this
101
101
  if (!state) return
102
102
  const rootState = state.__element.__ref.root.state
103
- return await rootState.update(obj, { isHoisted: false, ...options })
103
+ return rootState.update(obj, { isHoisted: false, ...options })
104
104
  }
105
105
 
106
- export const add = async function (value, options = {}) {
106
+ export const add = function (value, options = {}) {
107
107
  const state = this
108
108
  if (isArray(state)) {
109
109
  state.push(value)
110
- await state.update(state.parse(), { overwrite: true, ...options })
110
+ state.update(state.parse(), { overwrite: true, ...options })
111
111
  } else if (isObject(state)) {
112
112
  const key = Object.keys(state).length
113
- await state.update({ [key]: value }, options)
113
+ state.update({ [key]: value }, options)
114
114
  }
115
115
  }
116
116
 
117
- export const toggle = async function (key, options = {}) {
117
+ export const toggle = function (key, options = {}) {
118
118
  const state = this
119
- await state.update({ [key]: !state[key] }, options)
119
+ state.update({ [key]: !state[key] }, options)
120
120
  }
121
121
 
122
- export const remove = async function (key, options = {}) {
122
+ export const remove = function (key, options = {}) {
123
123
  const state = this
124
124
  if (isArray(state)) removeFromArray(state, key)
125
- if (isObject(state)) removeFromObject(state, key)
125
+ else if (isObject(state)) removeFromObject(state, key)
126
126
  if (options.applyReset) {
127
- return await state.set(state.parse(), { replace: true, ...options })
127
+ return state.set(state.parse(), { replace: true, ...options })
128
128
  }
129
- return await state.update({}, options)
129
+ return state.update({}, options)
130
130
  }
131
131
 
132
- export const set = async function (val, options = {}) {
132
+ export const set = function (val, options = {}) {
133
133
  const state = this
134
134
  const value = deepClone(val)
135
- const cleanState = await state.clean({ preventStateUpdate: true, ...options })
136
- return await cleanState.update(value, { replace: true, ...options })
135
+ const cleanState = state.clean({ preventStateUpdate: true, ...options })
136
+ return cleanState.update(value, { replace: true, ...options })
137
137
  }
138
138
 
139
- export const setByPath = async function (path, val, options = {}) {
139
+ export const setByPath = function (path, val, options = {}) {
140
140
  const state = this
141
141
  const value = deepClone(val)
142
- setInObjectByPath(state, path, val)
142
+ if (!options.preventReplace) setInObjectByPath(state, path, val)
143
143
  const update = createNestedObject(path, value)
144
144
  if (options.preventStateUpdate) return update
145
- return await state.update(update, options)
145
+ return state.update(update, options)
146
146
  }
147
147
 
148
- export const setPathCollection = async function (changes, options = {}) {
148
+ export const setPathCollection = function (changes, options = {}) {
149
149
  const state = this
150
- const update = await changes.reduce(async (promise, change) => {
151
- const acc = await promise
150
+ const update = changes.reduce((acc, change) => {
152
151
  if (change[0] === 'update') {
153
152
  const result = setByPath.call(state, change[1], change[2], {
154
153
  preventStateUpdate: true
155
154
  })
156
155
  return overwriteDeep(acc, result)
157
156
  } else if (change[0] === 'delete') {
158
- await removeByPath.call(state, change[1], {
157
+ removeByPath.call(state, change[1], {
159
158
  ...options,
160
159
  preventUpdate: true
161
160
  })
162
161
  }
163
162
  return acc
164
- }, Promise.resolve({}))
163
+ }, {})
165
164
 
166
165
  return state.update(update, options)
167
166
  }
168
167
 
169
- export const removeByPath = async function (path, options = {}) {
168
+ export const removeByPath = function (path, options = {}) {
170
169
  const state = this
171
170
  removeNestedKeyByPath(state, path)
172
171
  if (options.preventUpdate) return path
173
- return await state.update({}, options)
172
+ return state.update({}, options)
174
173
  }
175
174
 
176
- export const removePathCollection = async function (changes, options = {}) {
175
+ export const removePathCollection = function (changes, options = {}) {
177
176
  const state = this
178
- changes.forEach(async item => {
179
- await removeByPath(item, { preventUpdate: true })
180
- })
181
- return await state.update({}, options)
177
+ for (let i = 0; i < changes.length; i++) {
178
+ removeByPath(changes[i], { preventUpdate: true })
179
+ }
180
+ return state.update({}, options)
182
181
  }
183
182
 
184
183
  export const getByPath = function (path, options = {}) {
@@ -186,54 +185,54 @@ export const getByPath = function (path, options = {}) {
186
185
  return getInObjectByPath(state, path)
187
186
  }
188
187
 
189
- export const reset = async function (options = {}) {
188
+ export const reset = function (options = {}) {
190
189
  const state = this
191
190
  const value = deepClone(state.parse())
192
- return await state.set(value, { replace: true, ...options })
191
+ return state.set(value, { replace: true, ...options })
193
192
  }
194
193
 
195
- export const apply = async function (func, options = {}) {
194
+ export const apply = function (func, options = {}) {
196
195
  const state = this
197
196
  if (isFunction(func)) {
198
197
  const value = func(state)
199
- return await state.update(value, { replace: true, ...options })
198
+ return state.update(value, { replace: true, ...options })
200
199
  }
201
200
  }
202
201
 
203
- export const applyReplace = async function (func, options = {}) {
202
+ export const applyReplace = function (func, options = {}) {
204
203
  const state = this
205
204
  if (isFunction(func)) {
206
205
  const value = func(state)
207
- return await state.replace(value, options)
206
+ return state.replace(value, options)
208
207
  }
209
208
  }
210
209
 
211
- export const applyFunction = async function (func, options = {}) {
210
+ export const applyFunction = function (func, options = {}) {
212
211
  const state = this
213
212
  if (isFunction(func)) {
214
- await func(state)
215
- return await state.update(state.parse(), { replace: true, ...options })
213
+ func(state)
214
+ return state.update(state.parse(), { replace: true, ...options })
216
215
  }
217
216
  }
218
217
 
219
- export const quietUpdate = async function (obj, options = {}) {
218
+ export const quietUpdate = function (obj, options = {}) {
220
219
  const state = this
221
- return await state.update(obj, { preventUpdate: true, ...options })
220
+ return state.update(obj, { preventUpdate: true, ...options })
222
221
  }
223
222
 
224
- export const replace = async function (obj, options = {}) {
223
+ export const replace = function (obj, options = {}) {
225
224
  const state = this
226
225
 
227
226
  for (const param in obj) {
228
227
  state[param] = obj[param]
229
228
  }
230
229
 
231
- return await state.update(obj, options)
230
+ return state.update(obj, options)
232
231
  }
233
232
 
234
- export const quietReplace = async function (obj, options = {}) {
233
+ export const quietReplace = function (obj, options = {}) {
235
234
  const state = this
236
- return await state.replace(obj, { preventUpdate: true, ...options })
235
+ return state.replace(obj, { preventUpdate: true, ...options })
237
236
  }
238
237
 
239
238
  export const keys = function (obj, options = {}) {
package/package.json CHANGED
@@ -1,36 +1,48 @@
1
1
  {
2
2
  "name": "@domql/state",
3
- "version": "3.1.2",
3
+ "version": "3.2.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
- "module": "index.js",
7
- "main": "index.js",
8
- "unpkg": "dist/iife/index.js",
9
- "jsdelivr": "dist/iife/index.js",
6
+ "module": "./dist/esm/index.js",
7
+ "main": "./dist/cjs/index.js",
8
+ "unpkg": "./dist/iife/index.js",
9
+ "jsdelivr": "./dist/iife/index.js",
10
10
  "exports": {
11
11
  ".": {
12
- "default": "./dist/esm/index.js",
13
12
  "import": "./dist/esm/index.js",
14
- "require": "./dist/cjs/index.js"
13
+ "require": "./dist/cjs/index.js",
14
+ "browser": "./dist/iife/index.js",
15
+ "default": "./dist/esm/index.js"
16
+ },
17
+ "./*.js": {
18
+ "import": "./dist/esm/*.js",
19
+ "require": "./dist/cjs/*.js",
20
+ "default": "./dist/esm/*.js"
21
+ },
22
+ "./*": {
23
+ "import": "./dist/esm/*.js",
24
+ "require": "./dist/cjs/*.js",
25
+ "default": "./dist/esm/*.js"
15
26
  }
16
27
  },
17
28
  "source": "index.js",
18
29
  "files": [
19
- "*.js",
20
- "dist"
30
+ "dist",
31
+ "*.js"
21
32
  ],
22
33
  "scripts": {
23
34
  "copy:package:cjs": "cp ../../build/package-cjs.json dist/cjs/package.json",
24
- "build:esm": "npx esbuild *.js --target=es2017 --format=esm --outdir=dist/esm",
25
- "build:cjs": "npx esbuild *.js --target=node16 --format=cjs --outdir=dist/cjs",
26
- "build:iife": "npx esbuild *.js --target=node16 --format=iife --outdir=dist/iife",
27
- "build": "npx rimraf -I dist; npm run build:cjs; npm run build:esm",
28
- "prepublish": "npx rimraf -I dist && npm run build && npm run copy:package:cjs"
35
+ "build:esm": "cross-env NODE_ENV=$NODE_ENV esbuild *.js --target=es2020 --format=esm --outdir=dist/esm --define:process.env.NODE_ENV=process.env.NODE_ENV",
36
+ "build:cjs": "cross-env NODE_ENV=$NODE_ENV esbuild *.js --target=node18 --format=cjs --outdir=dist/cjs --define:process.env.NODE_ENV=process.env.NODE_ENV",
37
+ "build:iife": "cross-env NODE_ENV=$NODE_ENV esbuild index.js --bundle --target=es2020 --format=iife --global-name=DomqlState --outfile=dist/iife/index.js --define:process.env.NODE_ENV=process.env.NODE_ENV",
38
+ "build": "node ../../build/build.js",
39
+ "prepublish": "npm run build && npm run copy:package:cjs"
29
40
  },
30
41
  "dependencies": {
31
- "@domql/event": "^3.1.2",
32
- "@domql/report": "^3.1.2",
33
- "@domql/utils": "^3.1.2"
42
+ "@domql/report": "^3.2.3",
43
+ "@domql/utils": "^3.2.3"
34
44
  },
35
- "gitHead": "429b36616aa04c8587a26ce3c129815115e35897"
45
+ "gitHead": "9fc1b79b41cdc725ca6b24aec64920a599634681",
46
+ "browser": "./dist/iife/index.js",
47
+ "sideEffects": false
36
48
  }
package/updateState.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  import { report } from '@domql/report'
4
- import { triggerEventOnUpdate } from '@domql/event'
5
4
  import {
6
5
  checkIfInherits,
7
6
  createNestedObjectByKeyPath,
@@ -10,7 +9,8 @@ import {
10
9
  getRootStateInKey,
11
10
  merge,
12
11
  overwriteDeep,
13
- overwriteState
12
+ overwriteState,
13
+ triggerEventOnUpdate
14
14
  } from '@domql/utils'
15
15
 
16
16
  const STATE_UPDATE_OPTIONS = {
@@ -21,10 +21,7 @@ const STATE_UPDATE_OPTIONS = {
21
21
  execStateFunction: true
22
22
  }
23
23
 
24
- export const updateState = async function (
25
- obj,
26
- options = STATE_UPDATE_OPTIONS
27
- ) {
24
+ export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
28
25
  const state = this
29
26
  const element = state.__element
30
27
 
@@ -38,7 +35,7 @@ export const updateState = async function (
38
35
  } else if (options.preventInheritAtCurrentState) return
39
36
 
40
37
  if (!options.preventBeforeStateUpdateListener) {
41
- const beforeStateUpdateReturns = await triggerEventOnUpdate(
38
+ const beforeStateUpdateReturns = triggerEventOnUpdate(
42
39
  'beforeStateUpdate',
43
40
  obj,
44
41
  element,
@@ -48,21 +45,21 @@ export const updateState = async function (
48
45
  }
49
46
 
50
47
  overwriteState(state, obj, options)
51
- const updateIsHoisted = await hoistStateUpdate(state, obj, options)
48
+ const updateIsHoisted = hoistStateUpdate(state, obj, options)
52
49
  if (updateIsHoisted) return state
53
50
 
54
- await updateDependentState(state, obj, options)
51
+ updateDependentState(state, obj, options)
55
52
 
56
- await applyElementUpdate(state, obj, options)
53
+ applyElementUpdate(state, obj, options)
57
54
 
58
55
  if (!options.preventStateUpdateListener) {
59
- await triggerEventOnUpdate('stateUpdate', obj, element, options)
56
+ triggerEventOnUpdate('stateUpdate', obj, element, options)
60
57
  }
61
58
 
62
59
  return state
63
60
  }
64
61
 
65
- export const hoistStateUpdate = async (state, obj, options) => {
62
+ export const hoistStateUpdate = (state, obj, options) => {
66
63
  const element = state.__element
67
64
  const { parent, __ref: ref } = element
68
65
 
@@ -88,7 +85,7 @@ export const hoistStateUpdate = async (state, obj, options) => {
88
85
  const changesValue = createNestedObjectByKeyPath(stateKey, passedValue)
89
86
  const targetParent = findRootState || findGrandParentState || parent.state
90
87
  if (options.replace) overwriteDeep(targetParent, changesValue || value) // check with createNestedObjectByKeyPath
91
- await targetParent.update(changesValue, {
88
+ targetParent.update(changesValue, {
92
89
  execStateFunction: false,
93
90
  isHoisted: true,
94
91
  preventUpdate: options.preventHoistElementUpdate,
@@ -98,24 +95,24 @@ export const hoistStateUpdate = async (state, obj, options) => {
98
95
  const hasNotUpdated =
99
96
  options.preventUpdate !== true || !options.preventHoistElementUpdate
100
97
  if (!options.preventStateUpdateListener && hasNotUpdated) {
101
- await triggerEventOnUpdate('stateUpdate', obj, element, options)
98
+ triggerEventOnUpdate('stateUpdate', obj, element, options)
102
99
  }
103
100
  return true
104
101
  }
105
102
 
106
- const updateDependentState = async (state, obj, options) => {
103
+ const updateDependentState = (state, obj, options) => {
107
104
  if (!state.__depends) return
108
105
  for (const el in state.__depends) {
109
106
  const dependentState = state.__depends[el]
110
- const cleanState = await dependentState.clean()
111
- await cleanState.update(state.parse(), options)
107
+ const cleanState = dependentState.clean()
108
+ cleanState.update(state.parse(), options)
112
109
  }
113
110
  }
114
111
 
115
- const applyElementUpdate = async (state, obj, options) => {
112
+ const applyElementUpdate = (state, obj, options) => {
116
113
  const element = state.__element
117
114
  if (options.preventUpdate !== true) {
118
- await element.update(
115
+ element.update(
119
116
  {},
120
117
  {
121
118
  ...options,
@@ -123,7 +120,7 @@ const applyElementUpdate = async (state, obj, options) => {
123
120
  }
124
121
  )
125
122
  } else if (options.preventUpdate === 'recursive') {
126
- await element.update(
123
+ element.update(
127
124
  {},
128
125
  {
129
126
  ...options,
@@ -1,4 +0,0 @@
1
- {
2
- "type": "commonjs",
3
- "main": "index.js"
4
- }