nowrap-eventstream
This commit is contained in:
parent
ecc8cffa0e
commit
4748ecb141
|
@ -6,7 +6,7 @@ import { ShowIfAuthenticated } from '../components/_shared/ShowIfAuthenticated'
|
|||
import { NotificationsPanel } from '../components/NotificationsPanel'
|
||||
import { createStore } from 'solid-js/store'
|
||||
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 { Author, Message, Reaction, Shout } from '../graphql/types.gen'
|
||||
|
||||
|
@ -56,7 +56,10 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
|||
const loadNotifications = async () => {
|
||||
const storage = await db()
|
||||
const notifications = await storage.getAll('notifications')
|
||||
console.log('[context.notifications] Loaded notifications:', notifications)
|
||||
|
||||
const totalUnreadCount = notifications.filter((notification) => !notification.read).length
|
||||
console.log('[context.notifications] Total unread count:', totalUnreadCount)
|
||||
|
||||
setUnreadNotificationsCount(totalUnreadCount)
|
||||
setNotificationEntities(
|
||||
|
@ -74,6 +77,8 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
|||
})
|
||||
|
||||
const storeNotification = async (notification: ServerNotification) => {
|
||||
console.log('[context.notifications] Storing notification:', notification)
|
||||
|
||||
const storage = await db()
|
||||
const tx = storage.transaction('notifications', 'readwrite')
|
||||
const store = tx.objectStore('notifications')
|
||||
|
@ -85,42 +90,38 @@ export const NotificationsProvider = (props: { children: JSX.Element }) => {
|
|||
|
||||
const [messageHandler, setMessageHandler] = createSignal<(m: Message) => void>()
|
||||
|
||||
createEffect(() => {
|
||||
createEffect(async () => {
|
||||
if (isAuthenticated()) {
|
||||
loadNotifications()
|
||||
|
||||
fetchEventSource('https://chat.discours.io/connect', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: 'Bearer ' + getToken()
|
||||
},
|
||||
onmessage(event) {
|
||||
const n: { kind: string; payload: any } = JSON.parse(event.data)
|
||||
if (n.kind === 'new_message') {
|
||||
messageHandler()(n.payload)
|
||||
} else {
|
||||
console.log('[context.notifications] Received notification:', n)
|
||||
storeNotification({
|
||||
kind: n.kind,
|
||||
payload: n.payload,
|
||||
timestamp: Date.now(),
|
||||
seen: false
|
||||
})
|
||||
}
|
||||
},
|
||||
onclose() {
|
||||
console.log('[context.notifications] sse connection closed by server')
|
||||
},
|
||||
onerror(err) {
|
||||
console.error('[context.notifications] sse connection closed by error', err)
|
||||
throw new Error() // NOTE: simple hack to close the connection
|
||||
const token = getToken()
|
||||
const eventSource = new EventSource(`https://chat.discours.io/connect/${token}`)
|
||||
|
||||
eventSource.onmessage = (event) => {
|
||||
const n: { kind: string; payload: any } = JSON.parse(event.data)
|
||||
if (n.kind === 'new_message') {
|
||||
messageHandler()(n.payload)
|
||||
} else {
|
||||
console.log('[context.notifications] Received notification:', n)
|
||||
storeNotification({
|
||||
kind: n.kind,
|
||||
payload: n.payload,
|
||||
timestamp: Date.now(),
|
||||
seen: false
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
eventSource.onerror = (err) => {
|
||||
console.error('[context.notifications] sse connection closed by error', err)
|
||||
eventSource.close()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const markNotificationAsRead = async (notification: ServerNotification) => {
|
||||
console.log('[context.notifications] Marking notification as read:', notification)
|
||||
|
||||
const storage = await db()
|
||||
await storage.put('notifications', { ...notification, seen: true })
|
||||
loadNotifications()
|
||||
|
|
Loading…
Reference in New Issue
Block a user