Repository navigation
Expand file tree
/
Copy pathuseServerSentEvents.ts
More file actions
78 lines (65 loc) · 1.99 KB
/
Copy pathuseServerSentEvents.ts
File metadata and controls
78 lines (65 loc) · 1.99 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { useEffect, useRef } from 'react'
type EventListener = (data: any) => void
export class SharedEventSource {
private static instance: SharedEventSource | null = null
private es: EventSource | null = null
private listeners: Map<string, Set<EventListener>> = new Map()
private constructor() {
this.connect()
}
static getInstance(): SharedEventSource {
if (!SharedEventSource.instance) {
SharedEventSource.instance = new SharedEventSource()
}
return SharedEventSource.instance
}
private connect() {
this.es = new EventSource('/api/events', { withCredentials: true })
this.es.onerror = () => {
this.es?.close()
this.es = null
setTimeout(() => this.connect(), 5000)
}
}
subscribe(eventType: string, listener: EventListener): () => void {
if (!this.listeners.has(eventType)) {
this.listeners.set(eventType, new Set())
this.setupListener(eventType)
}
this.listeners.get(eventType)!.add(listener)
return () => {
this.listeners.get(eventType)?.delete(listener)
if (this.listeners.get(eventType)?.size === 0) {
this.listeners.delete(eventType)
if (this.listeners.size === 0) {
this.close()
SharedEventSource.instance = null
}
}
}
}
private setupListener(eventType: string) {
if (!this.es) return
this.es.addEventListener(eventType, (e: MessageEvent) => {
const data = JSON.parse(e.data)
this.listeners.get(eventType)?.forEach(listener => listener(data))
})
}
private close() {
this.es?.close()
this.es = null
}
}
export function useServerSentEvents(eventType: string, listener: EventListener) {
const listenerRef = useRef(listener)
useEffect(() => {
listenerRef.current = listener
}, [listener])
useEffect(() => {
const sse = SharedEventSource.getInstance()
const unsubscribe = sse.subscribe(eventType, (data) => {
listenerRef.current(data)
})
return unsubscribe
}, [eventType])
}