From 25fb1b1d48f5ce54b1c8a28ff0c5a5b05687afe2 Mon Sep 17 00:00:00 2001 From: tianhao Date: Tue, 16 Jun 2026 13:00:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20resetTimerOnUpdate=20+=20onUpdate?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=20+=20queue=E5=8F=AF=E5=8F=96=E6=B6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants.js | 4 ++++ src/core.js | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/constants.js b/src/constants.js index e265193..b46cb54 100644 --- a/src/constants.js +++ b/src/constants.js @@ -43,6 +43,10 @@ export const DEFAULTS = Object.freeze({ onShow: null, onClose: null, onClick: null, + onUpdate: null, + + // 高级 + resetTimerOnUpdate: false, // 国际化 locale: 'zh-CN', diff --git a/src/core.js b/src/core.js index c1eda5d..341a553 100644 --- a/src/core.js +++ b/src/core.js @@ -446,6 +446,18 @@ class Toast { content.innerHTML = `${safeTitle}${safeMessage}`; } + // resetTimerOnUpdate: 更新内容后重置计时器 + if (this.config.resetTimerOnUpdate && this.config.duration > 0) { + cancelAnimationFrame(this.rafId); + this.startedAt = Date.now(); + this.remaining = this.config.duration; + this._startTimer(); + } + + if (typeof this.config.onUpdate === 'function') { + try { this.config.onUpdate(this); } catch (e) { console.error('onUpdate callback error:', e); } + } + Toast.trigger('afterUpdate', this); return this; } @@ -977,16 +989,17 @@ const meToast = { queue(messages, opts = {}) { if (!Array.isArray(messages)) { console.error('MeToast.queue: messages must be an array'); - return Promise.resolve(); + return { then: (fn) => Promise.resolve().then(fn), cancel: () => {} }; } - return new Promise((resolve) => { + let cancelled = false; + const promise = new Promise((resolve) => { let index = 0; const delay = opts.delay || 1000; const userOnClose = opts.onClose; const showNext = () => { - if (index >= messages.length) { + if (cancelled || index >= messages.length) { resolve(); return; } @@ -1017,6 +1030,10 @@ const meToast = { showNext(); }); + return { + then: (fn, rj) => promise.then(fn, rj), + cancel: () => { cancelled = true; }, + }; }, /**