From aafddd8eed0f3fc7c7228c2db4718ba54f3fc522 Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Fri, 24 Jan 2025 11:04:14 +0100 Subject: [PATCH] fix(useToast): add in queue and improve unique ids Resolves #2686 --- src/runtime/composables/useToast.ts | 35 +++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/runtime/composables/useToast.ts b/src/runtime/composables/useToast.ts index 14abd58a3f..0c4d8de71a 100644 --- a/src/runtime/composables/useToast.ts +++ b/src/runtime/composables/useToast.ts @@ -1,3 +1,4 @@ +import { ref, nextTick } from 'vue' import { useState } from '#imports' import type { ToastProps } from '../types' @@ -8,20 +9,40 @@ export interface Toast extends Omit { export function useToast() { const toasts = useState('toasts', () => []) + const maxToasts = 5 + const running = ref(false) + const queue: Toast[] = [] - function add(toast: Partial): Toast { + const generateId = () => `${Date.now()}-${Math.random().toString(36).slice(2, 9)}` + + async function processQueue() { + if (running.value || queue.length === 0) { + return + } + + running.value = true + + while (queue.length > 0) { + const toast = queue.shift()! + + await nextTick() + + toasts.value = [...toasts.value, toast].slice(-maxToasts) + } + + running.value = false + } + + async function add(toast: Partial): Promise { const body = { - id: new Date().getTime().toString(), + id: generateId(), open: true, ...toast } - const index = toasts.value.findIndex((t: Toast) => t.id === body.id) - if (index === -1) { - toasts.value.push(body) - } + queue.push(body) - toasts.value = toasts.value.slice(-5) + await processQueue() return body }