From fb5068b0405892dd6088a66c9a267f5cfb0b4e58 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 20 Sep 2023 17:27:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=BC=82=E6=AD=A5=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E7=9A=84=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 26 +++++++++----------------- src/utils.js | 15 ++++++++++----- 2 files changed, 19 insertions(+), 22 deletions(-) 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') {