@@ -2,9 +2,21 @@ import { writable } from "svelte/store"
2
2
3
3
const NOTIFICATION_TIMEOUT = 3000
4
4
5
+ interface Notification {
6
+ id : string
7
+ type : string
8
+ message : string
9
+ icon : string
10
+ dismissable : boolean
11
+ action : ( ( ) => void ) | null
12
+ actionMessage : string | null
13
+ wide : boolean
14
+ dismissTimeout : number
15
+ }
16
+
5
17
export const createNotificationStore = ( ) => {
6
- const timeoutIds = new Set ( )
7
- const _notifications = writable ( [ ] , ( ) => {
18
+ const timeoutIds = new Set < ReturnType < typeof setTimeout > > ( )
19
+ const _notifications = writable < Notification [ ] > ( [ ] , ( ) => {
8
20
return ( ) => {
9
21
// clear all the timers
10
22
timeoutIds . forEach ( timeoutId => {
@@ -21,7 +33,7 @@ export const createNotificationStore = () => {
21
33
}
22
34
23
35
const send = (
24
- message ,
36
+ message : string ,
25
37
{
26
38
type = "default" ,
27
39
icon = "" ,
@@ -30,7 +42,15 @@ export const createNotificationStore = () => {
30
42
actionMessage = null ,
31
43
wide = false ,
32
44
dismissTimeout = NOTIFICATION_TIMEOUT ,
33
- }
45
+ } : {
46
+ type ?: string
47
+ icon ?: string
48
+ autoDismiss ?: boolean
49
+ action ?: ( ( ) => void ) | null
50
+ actionMessage ?: string | null
51
+ wide ?: boolean
52
+ dismissTimeout ?: number
53
+ } = { }
34
54
) => {
35
55
if ( block ) {
36
56
return
@@ -60,7 +80,7 @@ export const createNotificationStore = () => {
60
80
}
61
81
}
62
82
63
- const dismissNotification = id => {
83
+ const dismissNotification = ( id : string ) => {
64
84
_notifications . update ( state => {
65
85
return state . filter ( n => n . id !== id )
66
86
} )
@@ -71,17 +91,18 @@ export const createNotificationStore = () => {
71
91
return {
72
92
subscribe,
73
93
send,
74
- info : msg => send ( msg , { type : "info" , icon : "Info" } ) ,
75
- error : msg =>
94
+ info : ( msg : string ) => send ( msg , { type : "info" , icon : "Info" } ) ,
95
+ error : ( msg : string ) =>
76
96
send ( msg , { type : "error" , icon : "Alert" , autoDismiss : false } ) ,
77
- warning : msg => send ( msg , { type : "warning" , icon : "Alert" } ) ,
78
- success : msg => send ( msg , { type : "success" , icon : "CheckmarkCircle" } ) ,
97
+ warning : ( msg : string ) => send ( msg , { type : "warning" , icon : "Alert" } ) ,
98
+ success : ( msg : string ) =>
99
+ send ( msg , { type : "success" , icon : "CheckmarkCircle" } ) ,
79
100
blockNotifications,
80
101
dismiss : dismissNotification ,
81
102
}
82
103
}
83
104
84
- function id ( ) {
105
+ function id ( ) : string {
85
106
return "_" + Math . random ( ) . toString ( 36 ) . slice ( 2 , 9 )
86
107
}
87
108
0 commit comments