update
This commit is contained in:
18
frontend/src/live.js
Normal file
18
frontend/src/live.js
Normal file
@@ -0,0 +1,18 @@
|
||||
// Ein Kanal für die ganze Seite. Mehrere Komponenten lauschen mit, ohne
|
||||
// dass jede eine eigene Verbindung aufmacht.
|
||||
|
||||
let quelle = null
|
||||
const hoerer = new Set()
|
||||
|
||||
export function abonnieren(fn) {
|
||||
hoerer.add(fn)
|
||||
if (!quelle) {
|
||||
quelle = new EventSource('/api/events')
|
||||
quelle.onmessage = (ereignis) => {
|
||||
const nachricht = JSON.parse(ereignis.data)
|
||||
hoerer.forEach((h) => h(nachricht))
|
||||
}
|
||||
// Der Browser verbindet nach einem Abbruch von selbst neu
|
||||
}
|
||||
return () => hoerer.delete(fn)
|
||||
}
|
||||
Reference in New Issue
Block a user