UNPKG

react-atom-toast

Version:
69 lines (66 loc) 2.03 kB
import { __publicField, __spreadProps, __spreadValues } from './chunk-5USKE2QT.js'; import { Renderer } from './renderer.js'; var ToastQueue = class { constructor() { __publicField(this, "renderer"); __publicField(this, "toasts", []); this.renderer = new Renderer(this); } render() { this.renderer.render(this.toasts); } add(options) { var _a; const { maxCount, key } = options; if (key && this.toasts.some((t) => t.key === key)) { this.update(key, options); return; } const visibleToasts = this.toasts.filter((t) => t.open === true); if (maxCount && visibleToasts.length >= maxCount) { if (maxCount === 1) { for (let i = 0; i < visibleToasts.length - 1; i++) { this.remove(visibleToasts[i].key); } this.update(visibleToasts[visibleToasts.length - 1].key, __spreadProps(__spreadValues({}, options), { open: true })); return; } else { this.close((_a = visibleToasts[0]) == null ? void 0 : _a.key); } } const toastOptions = __spreadProps(__spreadValues({}, options), { key: options.key || Math.random().toString(36), open: true }); this.toasts.push(toastOptions); this.render(); } close(key) { if (!key) return; this.update(key, { open: false }); } closeAll() { this.toasts = this.toasts.map((toastOptions) => __spreadProps(__spreadValues({}, toastOptions), { open: false })); this.render(); } remove(key) { this.toasts = this.toasts.filter((toastOptions) => toastOptions.key !== key); this.render(); } removeAll() { this.toasts = []; this.render(); } update(key, options) { const index = this.toasts.findIndex((toastOptions) => toastOptions.key === key); if (index !== -1) { this.toasts[index] = __spreadProps(__spreadValues(__spreadValues({}, this.toasts[index]), options), { updateFlag: !this.toasts[index].updateFlag }); this.render(); } } }; export { ToastQueue };