domql 1.6.7 → 1.6.9

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "domql",
3
3
  "description": "DOM rendering Javascript framework at early stage.",
4
- "version": "1.6.7",
4
+ "version": "1.6.9",
5
5
  "repository": "https://github.com/domql/domql",
6
6
  "publishConfig": {
7
7
  "registry": "https://registry.npmjs.org"
@@ -1,12 +1,23 @@
1
1
  'use strict'
2
2
 
3
3
  import { triggerEventOn } from '@domql/event'
4
- import { is, isObject, exec, isFunction, isUndefined, arrayContainsOtherArray, isObjectLike, isArray, removeFromArray, removeFromObject } from '@domql/utils'
4
+ import {
5
+ is,
6
+ isObject,
7
+ exec,
8
+ isFunction,
9
+ arrayContainsOtherArray,
10
+ isObjectLike,
11
+ isArray,
12
+ removeFromArray,
13
+ removeFromObject,
14
+ isNot
15
+ } from '@domql/utils'
5
16
  import { deepClone, overwriteShallow, overwriteDeep } from '../utils'
6
17
  import { create } from '.'
7
18
 
8
19
  export const IGNORE_STATE_PARAMS = [
9
- 'update', 'parse', 'clean', 'create', 'destroy', 'remove', 'apply', 'rootUpdate',
20
+ 'update', 'parse', 'clean', 'create', 'destroy', 'add', 'remove', 'apply', 'rootUpdate',
10
21
  'parent', '__element', '__depends', '__ref', '__children', '__root'
11
22
  ]
12
23
 
@@ -73,15 +84,7 @@ export const updateState = function (obj, options = {}) {
73
84
  const parentState = element.parent.state
74
85
  state.parent = parentState
75
86
 
76
- for (const param in state) {
77
- if (isUndefined(state[param])) {
78
- delete state[param]
79
- }
80
- }
81
-
82
- if (!state.__element) {
83
- create(element, element.parent)
84
- }
87
+ if (!state.__element && options.createElementFallback) { create(element, element.parent) }
85
88
 
86
89
  const initStateUpdateReturns = triggerEventOn('initStateUpdated', element, obj)
87
90
  if (initStateUpdateReturns === false) return element
@@ -99,9 +102,20 @@ export const updateState = function (obj, options = {}) {
99
102
  if (shouldPropagateState) {
100
103
  const isStringState = (__elementRef.__stateType === 'string')
101
104
  const value = isStringState ? state.value : state.parse()
105
+ const passedValue = isStringState ? state.value : obj
106
+
102
107
  parentState[stateKey] = value
103
- parentState.update(value, { skipOverwrite: true, ...options })
104
- return state
108
+ parentState.update({ [stateKey]: passedValue }, {
109
+ skipOverwrite: true,
110
+ preventUpdate: options.preventHoistElementUpdate,
111
+ ...options
112
+ })
113
+
114
+ if (!options.preventUpdateListener) {
115
+ triggerEventOn('stateUpdated', element, value)
116
+ }
117
+
118
+ if (!options.preventHoistElementUpdate) return state
105
119
  }
106
120
 
107
121
  if (!options.preventUpdate) {
@@ -124,18 +138,27 @@ export const updateState = function (obj, options = {}) {
124
138
  return state
125
139
  }
126
140
 
127
- export const remove = function (key, options) {
141
+ export const add = function (value, options = {}) {
142
+ const state = this
143
+ if (isArray(state)) {
144
+ state.push(value)
145
+ console.log(state)
146
+ state.update(state.parse(), { skipOverwrite: true, ...options })
147
+ }
148
+ }
149
+
150
+ export const remove = function (key, options = {}) {
128
151
  const state = this
129
152
  if (isArray(state)) removeFromArray(state, key)
130
153
  if (isObject(state)) removeFromObject(state, key)
131
- return state.update(state, { skipOverwrite: true, options })
154
+ return state.update(state.parse(), { skipOverwrite: true, ...options })
132
155
  }
133
156
 
134
- export const apply = function (func, options) {
157
+ export const apply = function (func, options = {}) {
135
158
  const state = this
136
159
  if (isFunction(func)) {
137
160
  func(state)
138
- return state.update(state, { skipOverwrite: true, options })
161
+ return state.update(state, { skipOverwrite: true, ...options })
139
162
  }
140
163
  }
141
164
 
@@ -167,7 +190,7 @@ const getChildStateInKey = (stateKey, parentState) => {
167
190
  const createInheritedState = function (element, parent) {
168
191
  const __elementRef = element.__ref
169
192
  let stateKey = __elementRef.__state
170
- if (!stateKey) return element.state
193
+ if (!stateKey || isNot(stateKey)('number', 'string')) return element.state
171
194
 
172
195
  let parentState = parent.state
173
196
  if (stateKey.includes('../')) {
@@ -194,7 +217,10 @@ export const createState = function (element, parent, opts) {
194
217
  const skip = (opts && opts.skip) ? opts.skip : false
195
218
  let { state, __ref: __elementRef } = element
196
219
 
197
- if (isFunction(state)) element.state = exec(state, element)
220
+ if (isFunction(state)) {
221
+ __elementRef.__state = state
222
+ state = element.state = exec(state, element)
223
+ }
198
224
 
199
225
  if (is(state)('string', 'number')) {
200
226
  __elementRef.__state = state
@@ -258,6 +284,7 @@ const applyMethods = (element, state) => {
258
284
  state.update = updateState
259
285
  state.rootUpdate = rootUpdate
260
286
  state.create = createState
287
+ state.add = add
261
288
  state.remove = remove
262
289
  state.apply = apply
263
290
  state.parent = element.parent.state
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { window } from '@domql/globals'
4
- import { isFunction, isNumber, isObject, isString } from '@domql/utils'
4
+ import { exec, isFunction, isNumber, isObject, isString } from '@domql/utils'
5
5
  import { applyEvent, triggerEventOn } from '@domql/event'
6
6
  import { isMethod } from '@domql/methods'
7
7
  import { createSnapshotId } from '@domql/key'
@@ -158,13 +158,21 @@ const checkIfOnUpdate = (element, options) => {
158
158
  const inheritStateUpdates = (element, options) => {
159
159
  const { __ref } = element
160
160
  const stateKey = __ref.__state
161
- const { parent } = element
161
+ const { parent, state } = element
162
+
163
+ if (options.preventUpdateTriggerStateUpdate) return
162
164
 
163
165
  if (!stateKey && !__ref.__hasRootState) {
164
166
  element.state = (parent && parent.state) || {}
165
167
  return
166
168
  }
167
169
 
170
+ if (isFunction(stateKey)) {
171
+ const execState = exec(stateKey, element)
172
+ state.update(execState, { preventUpdateTriggerStateUpdate: true, ...options })
173
+ return false
174
+ }
175
+
168
176
  const parentState = (parent && parent.state) || {}
169
177
  const keyInParentState = parentState[stateKey]
170
178