mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
30 lines
646 B
TypeScript
30 lines
646 B
TypeScript
|
|
import type { ToastData } from '$lib/types';
|
||
|
|
import { writable } from 'svelte/store';
|
||
|
|
|
||
|
|
// Custom store
|
||
|
|
const newToast = () => {
|
||
|
|
const { subscribe, update } = writable([]);
|
||
|
|
|
||
|
|
function send(message: string, { duration = 2000, type = 'INFO' } = {}) {
|
||
|
|
const id = Math.floor(Math.random() * 1000);
|
||
|
|
const newMessage = {
|
||
|
|
id,
|
||
|
|
duration,
|
||
|
|
type,
|
||
|
|
message
|
||
|
|
};
|
||
|
|
update((store) => [...store, newMessage]);
|
||
|
|
}
|
||
|
|
|
||
|
|
function remove(id: number) {
|
||
|
|
update((store) => {
|
||
|
|
const newStore = store.filter((item: ToastData) => item.id !== id);
|
||
|
|
return [...newStore];
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return { subscribe, send, remove };
|
||
|
|
};
|
||
|
|
|
||
|
|
export const toast = newToast();
|