diff --git a/src/index.js b/src/index.js index 82fe54b..2268f1c 100644 --- a/src/index.js +++ b/src/index.js @@ -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() {} diff --git a/src/utils.js b/src/utils.js index f21c291..3dae653 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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') {