优化异步回调的触发

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() { disconnectedCallback() {
if (this.keepAlive) { if (this.keepAlive) {
this.#asyncCallSafety(this.deactivated) nextTick(_ => this.deactivated())
} else { } else {
this[__children__]?.setConnected(false) this[__children__]?.setConnected(false)
@ -276,7 +276,7 @@ export class Component extends HTMLElement {
window.wkitd.deassign(this) window.wkitd.deassign(this)
} }
} }
this.#asyncCallSafety(this.unmounted) nextTick(_ => this.unmounted())
} }
} }
// 监听属性变化 // 监听属性变化
@ -385,12 +385,14 @@ export class Component extends HTMLElement {
if (this.$animate?.immediate) { if (this.$animate?.immediate) {
this.$animate() this.$animate()
} }
nextTick(_ => {
if (this.keepAlive) { if (this.keepAlive) {
this.#asyncCallSafety(this.activated) this.activated()
} }
this.#asyncCallSafety(this.mounted) this.mounted()
})
} else { } 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() {} created() {}
mounted() {} mounted() {}

View File

@ -24,11 +24,14 @@ export const nextTick = (function () {
let bool = false let bool = false
function callback() { function callback() {
let n = queue.length while (queue.length > 0) {
for (let i = 0; i < n; i++) { let fn = queue.shift()
queue[i]() try {
fn()
} catch (err) {
console.error(err)
}
} }
queue = queue.slice(n)
} }
new MutationObserver(callback).observe(node, { characterData: true }) 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 let host = isWc ? dom : null
if (!dom || !type) { 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') { if (typeof selector === 'function') {