import { isArray } from './utils'; interface NoticeQueueOptions { /** 间隔时间 */ interval?: number } export default class NoticeQueue { interval: number; queue: Function[]; timer?: number; paused: boolean; handler?: { close: Function }; constructor(options: NoticeQueueOptions) { this.interval = 3000 || options.interval; this.queue = []; this.paused = false; } push(part: Function[] | Function) { if (!part) return if(isArray(part)) { this.queue = this.queue.concat(part) } else { // @ts-ignore:忽略part 为Function的报错 this.queue.push(part) } return this } start() { if (this.paused) return // 直接执行第一个(队列遍历) const preHandler = this.handler; if (this.queue.length) { const fn = this.queue.shift(); // todo: notification.close 函数没有异步,当队列中有多个数据时,前面的还未完全消失,后面的提示就出现 this.handler = fn(); } if (preHandler) { // 先销毁fn之前的行为 if ('close' in preHandler && typeof preHandler.close == 'function') { preHandler.close(); } } this.timer = window.setTimeout(() => { this.start() }, this.interval); return this } pause() { this.paused = true; // 去除定时遍历 clearTimeout(this.timer); return this } clearQueue() { this.queue = []; } }