Skip to content

Commit

Permalink
added awaits to domql events
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoloza committed Feb 3, 2025
1 parent 54bd1ba commit aee2183
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/element/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export function setNodeStyles (params = {}) {

export function remove (opts) {
const element = this
const beforeUpdateReturns = triggerEventOn('beforeRemove', element, opts)
if (beforeUpdateReturns === false) return element
const beforeRemoveReturns = triggerEventOn('beforeRemove', element, opts)
if (beforeRemoveReturns === false) return element
if (isFunction(element.node.remove)) element.node.remove()
else if (!isProduction()) {
console.warn('This item cant be removed')
Expand Down
6 changes: 3 additions & 3 deletions packages/element/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const reset = (options) => {
})
}

export const set = function (params, options = {}, el) {
export const set = async function (params, options = {}, el) {
const element = el || this
const { __ref: ref } = element

Expand All @@ -42,11 +42,11 @@ export const set = function (params, options = {}, el) {

if (ref.__noCollectionDifference || (__contentRef && __contentRef.__cached && deepContains(params, content))) {
if (!options.preventBeforeUpdateListener && !options.preventListeners) {
const beforeUpdateReturns = triggerEventOnUpdate('beforeUpdate', params, element, options)
const beforeUpdateReturns = await triggerEventOnUpdate('beforeUpdate', params, element, options)
if (beforeUpdateReturns === false) return element
}
if (content?.update) content.update()
if (!options.preventUpdateListener) triggerEventOn('update', element, options)
if (!options.preventUpdateListener) await triggerEventOn('update', element, options)
return
}

Expand Down
21 changes: 10 additions & 11 deletions packages/element/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const UPDATE_DEFAULT_OPTIONS = {
excludes: METHODS_EXL
}

export const update = function (params = {}, opts) {
export const update = async function (params = {}, opts) {
const calleeElementCache = opts?.calleeElement
const options = deepClone(isObject(opts) ? deepMerge(opts, UPDATE_DEFAULT_OPTIONS) : UPDATE_DEFAULT_OPTIONS, { exclude: ['calleeElement'] })
options.calleeElement = calleeElementCache
Expand All @@ -54,7 +54,7 @@ export const update = function (params = {}, opts) {
const [snapshotOnCallee, calleeElement, snapshotHasUpdated] = captureSnapshot(element, options)
if (snapshotHasUpdated) return

if (!options.preventListeners) triggerEventOnUpdate('startUpdate', params, element, options)
if (!options.preventListeners) await triggerEventOnUpdate('startUpdate', params, element, options)

if (preventInheritAtCurrentState && preventInheritAtCurrentState.__element === element) return
if (!excludes) merge(options, UPDATE_DEFAULT_OPTIONS)
Expand All @@ -63,7 +63,7 @@ export const update = function (params = {}, opts) {
params = { text: params }
}

const inheritState = inheritStateUpdates(element, options)
const inheritState = await inheritStateUpdates(element, options)
if (inheritState === false) return

const ifFails = checkIfOnUpdate(element, parent, options)
Expand All @@ -77,11 +77,14 @@ export const update = function (params = {}, opts) {
}

if (!options.preventBeforeUpdateListener && !options.preventListeners) {
const beforeUpdateReturns = triggerEventOnUpdate('beforeUpdate', params, element, options)
const beforeUpdateReturns = await triggerEventOnUpdate('beforeUpdate', params, element, options)
if (beforeUpdateReturns === false) return element
}

// apply new updates
overwriteDeep(element, params, { exclude: METHODS_EXL })

// exec updates
throughExecProps(element)
throughUpdatedExec(element, { ignore: UPDATE_DEFAULT_OPTIONS })
throughUpdatedDefine(element)
Expand Down Expand Up @@ -110,10 +113,6 @@ export const update = function (params = {}, opts) {
for (const param in element) {
const prop = element[param]

if (element.Iframe && element.Overlay && element.ComponentFrameCanvas && param === 'Iframe') {
console.log('isframe')
}

if (!Object.hasOwnProperty.call(element, param)) continue

const isInPreventUpdate = isArray(preventUpdate) && preventUpdate.includes(param)
Expand Down Expand Up @@ -262,7 +261,7 @@ const checkIfOnUpdate = (element, parent, options) => {
* @param {boolean} [options.preventStateUpdateListener] - If true, prevent the 'stateUpdate' event listener.
* @returns {boolean} - If returns false, it breaks the update function
*/
const inheritStateUpdates = (element, options) => {
const inheritStateUpdates = async (element, options) => {
const { __ref: ref } = element
const stateKey = ref.__state
const { parent, state } = element
Expand Down Expand Up @@ -290,7 +289,7 @@ const inheritStateUpdates = (element, options) => {

// Trigger on.beforeStateUpdate event
if (!options.preventBeforeStateUpdateListener && !options.preventListeners) {
const initStateReturns = triggerEventOnUpdate('beforeStateUpdate', keyInParentState, element, options)
const initStateReturns = await triggerEventOnUpdate('beforeStateUpdate', keyInParentState, element, options)
if (initStateReturns === false) return element
}

Expand All @@ -299,7 +298,7 @@ const inheritStateUpdates = (element, options) => {

// Trigger on.stateUpdate event
if (!options.preventStateUpdateListener && !options.preventListeners) {
triggerEventOnUpdate('stateUpdate', newState.parse(), element, options)
await triggerEventOnUpdate('stateUpdate', newState.parse(), element, options)
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/state/updateState.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const STATE_UPDATE_OPTIONS = {
execStateFunction: true
}

export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
export const updateState = async function (obj, options = STATE_UPDATE_OPTIONS) {
const state = this
const element = state.__element

Expand All @@ -28,7 +28,7 @@ export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
} else if (options.preventInheritAtCurrentState) return

if (!options.preventBeforeStateUpdateListener) {
const beforeStateUpdateReturns = triggerEventOnUpdate('beforeStateUpdate', obj, element, options)
const beforeStateUpdateReturns = await triggerEventOnUpdate('beforeStateUpdate', obj, element, options)
if (beforeStateUpdateReturns === false) return element
}

Expand All @@ -41,7 +41,7 @@ export const updateState = function (obj, options = STATE_UPDATE_OPTIONS) {
applyElementUpdate(state, obj, options)

if (!options.preventStateUpdateListener) {
triggerEventOnUpdate('stateUpdate', obj, element, options)
await triggerEventOnUpdate('stateUpdate', obj, element, options)
}

return state
Expand Down

0 comments on commit aee2183

Please sign in to comment.