优化异步回调的触发

master
yutent 2023-09-20 17:27:15 +08:00
parent 4f4e8970ca
commit fb5068b040
2 changed files with 19 additions and 22 deletions

View File

@ -259,7 +259,7 @@ export class Component extends HTMLElement {
disconnectedCallback() {
if (this.keepAlive) {
this.#asyncCallSafety(this.deactivated)
nextTick(_ => this.deactivated())
} else {
this[__children__]?.setConnected(false)
@ -276,7 +276,7 @@ export class Component extends HTMLElement {
window.wkitd.deassign(this)
}
}
this.#asyncCallSafety(this.unmounted)
nextTick(_ => this.unmounted())
}
}
// 监听属性变化
@ -385,12 +385,14 @@ export class Component extends HTMLElement {
if (this.$animate?.immediate) {
this.$animate()
}
if (this.keepAlive) {
this.#asyncCallSafety(this.activated)
}
this.#asyncCallSafety(this.mounted)
nextTick(_ => {
if (this.keepAlive) {
this.activated()
}
this.mounted()
})
} else {
this.#asyncCallSafety(this.updated, props)
nextTick(_ => this.updated(props))
}
}
@ -412,16 +414,6 @@ export class Component extends HTMLElement {
}
}
#asyncCallSafety(callback, arg) {
nextTick(_ => {
try {
callback.call(this, arg)
} catch (err) {
console.error(err)
}
})
}
// 几个生命周期回调
created() {}
mounted() {}

View File

@ -24,11 +24,14 @@ export const nextTick = (function () {
let bool = false
function callback() {
let n = queue.length
for (let i = 0; i < n; i++) {
queue[i]()
while (queue.length > 0) {
let fn = queue.shift()
try {
fn()
} catch (err) {
console.error(err)
}
}
queue = queue.slice(n)
}
new MutationObserver(callback).observe(node, { characterData: true })
@ -86,7 +89,9 @@ export function bind(dom, type = '', selector, fn, phase = true) {
let host = isWc ? dom : null
if (!dom || !type) {
return console.error('call bind: argument error')
return console.error(
"Argument Error: function bind's arg 1 must be a document obejct"
)
}
if (typeof selector === 'function') {