wrapped-sse
This commit is contained in:
parent
9c263b697e
commit
121cbfdbdd
|
@ -6,7 +6,7 @@ import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
|
||||||
import { NotificationsPanel } from '../components/NotificationsPanel'
|
import { NotificationsPanel } from '../components/NotificationsPanel'
|
||||||
import { createStore } from 'solid-js/store'
|
import { createStore } from 'solid-js/store'
|
||||||
import { IDBPDatabase, openDB } from 'idb'
|
import { IDBPDatabase, openDB } from 'idb'
|
||||||
// import { fetchEventSource } from '@microsoft/fetch-event-source'
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
||||||
import { getToken } from '../graphql/privateGraphQLClient'
|
import { getToken } from '../graphql/privateGraphQLClient'
|
||||||
import { Author, Message, Reaction, Shout } from '../graphql/types.gen'
|
import { Author, Message, Reaction, Shout } from '../graphql/types.gen'
|
||||||
|
|
||||||
|
@ -90,19 +90,23 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
||||||
|
|
||||||
const [messageHandler, setMessageHandler] = createSignal<(m: Message) => void>()
|
const [messageHandler, setMessageHandler] = createSignal<(m: Message) => void>()
|
||||||
|
|
||||||
createEffect(() => {
|
createEffect(async () => {
|
||||||
if (isAuthenticated()) {
|
if (isAuthenticated()) {
|
||||||
loadNotifications()
|
loadNotifications()
|
||||||
|
|
||||||
const token = getToken()
|
await fetchEventSource('https://chat.discours.io/connect', {
|
||||||
const eventSource = new EventSource(`https://connect.discours.io/${token}`)
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
eventSource.onmessage = (event) => {
|
'Content-Type': 'application/json',
|
||||||
console.log('[context.notifications] Received event:', event)
|
Authorization: 'Bearer ' + getToken()
|
||||||
|
},
|
||||||
|
onmessage(event) {
|
||||||
const n: { kind: string; payload: any } = JSON.parse(event.data)
|
const n: { kind: string; payload: any } = JSON.parse(event.data)
|
||||||
if (n.kind === 'new_message') {
|
if (n.kind === 'new_message') {
|
||||||
|
console.log('[context.notifications] Received message:', n)
|
||||||
messageHandler()(n.payload)
|
messageHandler()(n.payload)
|
||||||
} else {
|
} else {
|
||||||
|
console.log('[context.notifications] Received notification:', n)
|
||||||
storeNotification({
|
storeNotification({
|
||||||
kind: n.kind,
|
kind: n.kind,
|
||||||
payload: n.payload,
|
payload: n.payload,
|
||||||
|
@ -110,12 +114,15 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
||||||
seen: false
|
seen: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
onclose() {
|
||||||
eventSource.onerror = (err) => {
|
console.log('[context.notifications] sse connection closed by server')
|
||||||
|
},
|
||||||
|
onerror(err) {
|
||||||
console.error('[context.notifications] sse connection closed by error', err)
|
console.error('[context.notifications] sse connection closed by error', err)
|
||||||
eventSource.close()
|
throw new Error() // NOTE: simple hack to close the connection
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user