Files
skt-vuetify-templates/src/services/http-toast.ts
T
skytek_xinliang 71683482e1 refactor: ky
2026-05-07 11:17:30 +08:00

31 lines
918 B
TypeScript

// 全域 HTTP Toast 事件(Playground 專用)
//
// 目的:
// - 讓 HTTP hooks 能用「事件」通知 UI 顯示錯誤提示,不需直接依賴 Pinia
// - 預設只用於「非阻斷」錯誤(例如 500 / 網路中斷),避免導頁打斷使用者
export const HTTP_TOAST_EVENT = 'sk-playground:http-toast'
export type HttpToastLevel = 'info' | 'warning' | 'error'
export type HttpToastDetail = {
level: HttpToastLevel
message: string
dedupeKey?: string
}
let lastKey = ''
let lastAt = 0
export function emitHttpToast(detail: HttpToastDetail) {
const now = Date.now()
const key = detail.dedupeKey ?? `${detail.level}:${detail.message}`
// 500ms 內同樣訊息不重複噴(避免同頁多支 API 一起爆)
if (key === lastKey && now - lastAt < 500) return
lastKey = key
lastAt = now
window.dispatchEvent(new CustomEvent<HttpToastDetail>(HTTP_TOAST_EVENT, { detail }))
}