-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcast.ts
38 lines (36 loc) · 1.07 KB
/
broadcast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class BroadcastJSNotification {
name: string;
object: unknown;
constructor(name: string, object = null) {
this.name = name
this.object = object
}
}
class NotificationManager {
observers: Map<string, (object: unknown) => void>
constructor() {
this.observers = new Map()
}
get default(): NotificationManager {
if (typeof globalThis.BroadcastJS_Shared_Instance == "undefined") {
globalThis.BroadcastJS_Shared_Instance = new NotificationManager()
}
return globalThis.BroadcastJS_Shared_Instance
}
addObserver(name: string, callback: (object: unknown) => void, reference = null) {
this.observers.set(`${name}, ${reference}`, callback)
}
removeObserver(name: string, reference = null) {
this.observers.delete(`${name}, ${reference}`)
}
post(notification: BroadcastJSNotification) {
const name = notification.name
for (const n of this.observers.keys()) {
if (n.split(",")[0] == name) {
this.observers.get(n)(notification.object)
}
}
}
}
export { BroadcastJSNotification as Notification }
export const NotificationCenter = new NotificationManager()