diff --git a/src/animations.js b/src/animations.js index 9a0196f..9eae7fc 100644 --- a/src/animations.js +++ b/src/animations.js @@ -80,7 +80,7 @@ class AnimationManager { * @returns {Promise} 动画完成Promise */ apply(element, animationName, options = {}) { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const animation = this.get(animationName); if (!animation) { resolve(); @@ -123,19 +123,15 @@ class AnimationManager { // 动画完成处理 anim.onfinish = () => { - // 应用最终状态 Object.assign(element.style, config.leave); - - // 清理 this.activeAnimations.delete(animId); - resolve(); }; - // 动画取消处理 + // 动画取消处理 — reject 让调用者感知取消 anim.oncancel = () => { this.activeAnimations.delete(animId); - resolve(); + reject(new Error(`Animation "${animationName}" was cancelled`)); }; }); } diff --git a/src/core.js b/src/core.js index 6d0220d..c1eda5d 100644 --- a/src/core.js +++ b/src/core.js @@ -79,6 +79,10 @@ class Toast { this.html = opts.html || ''; this.iconHTML = opts.iconHTML || ''; this.config = { ...DEFAULTS, ...opts }; + // 防止嵌套对象被多个 toast 实例共享引用 + if (opts.style && typeof opts.style === 'object') { + this.config.style = { ...opts.style }; + } this.el = null; this.barEl = null; this.rafId = null; @@ -991,13 +995,18 @@ const meToast = { index++; const msg = typeof message === 'string' ? message : (message?.message || ''); + const msgObj = (typeof message === 'object' && message !== null) ? message : {}; + const msgOnClose = msgObj.onClose; this._emit({ ...opts, - ...((typeof message === 'object' && message !== null) ? message : {}), + ...msgObj, message: msg, - duration: opts.duration || 3000, + duration: msgObj.duration || opts.duration || 3000, onClose: (toast) => { + if (typeof msgOnClose === 'function') { + try { msgOnClose(toast); } catch (_) {} + } if (typeof userOnClose === 'function') { try { userOnClose(toast); } catch (_) {} }