Initial Commit
This commit is contained in:
67
www/src/lib/sse.ts
Normal file
67
www/src/lib/sse.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import type { Note, ShareInvitation } from '@/types'
|
||||
|
||||
export interface SSEHandlers {
|
||||
onNoteCreated: (note: Note) => void
|
||||
onNoteUpdated: (note: Note) => void
|
||||
onNoteDeleted: (data: { id: string }) => void
|
||||
onInvitationReceived?: (invitation: ShareInvitation) => void
|
||||
onError: (err: Event) => void
|
||||
}
|
||||
|
||||
export function createSSEClient(url: string, handlers: SSEHandlers): { close: () => void } {
|
||||
const eventSource = new EventSource(url)
|
||||
|
||||
eventSource.addEventListener('note_created', (event: MessageEvent) => {
|
||||
try {
|
||||
const note: Note = JSON.parse(event.data)
|
||||
handlers.onNoteCreated(note)
|
||||
} catch {
|
||||
handlers.onError(new Event('parse_error'))
|
||||
}
|
||||
})
|
||||
|
||||
eventSource.addEventListener('note_updated', (event: MessageEvent) => {
|
||||
try {
|
||||
const note: Note = JSON.parse(event.data)
|
||||
handlers.onNoteUpdated(note)
|
||||
} catch {
|
||||
handlers.onError(new Event('parse_error'))
|
||||
}
|
||||
})
|
||||
|
||||
eventSource.addEventListener('note_deleted', (event: MessageEvent) => {
|
||||
try {
|
||||
const data: { id: string } = JSON.parse(event.data)
|
||||
handlers.onNoteDeleted(data)
|
||||
} catch {
|
||||
handlers.onError(new Event('parse_error'))
|
||||
}
|
||||
})
|
||||
|
||||
eventSource.addEventListener('invitation_received', (event: MessageEvent) => {
|
||||
try {
|
||||
const raw = JSON.parse(event.data)
|
||||
const invitation: ShareInvitation = {
|
||||
id: raw.id,
|
||||
fromPublicKey: raw.from_public_key,
|
||||
toPublicKey: raw.to_public_key,
|
||||
encryptedGroupKey: raw.encrypted_group_key,
|
||||
iv: raw.iv,
|
||||
noteId: raw.note_id,
|
||||
noteTitle: raw.note_title,
|
||||
created_at: raw.created_at,
|
||||
}
|
||||
handlers.onInvitationReceived?.(invitation)
|
||||
} catch {
|
||||
handlers.onError(new Event('parse_error'))
|
||||
}
|
||||
})
|
||||
|
||||
eventSource.onerror = (err: Event) => {
|
||||
handlers.onError(err)
|
||||
}
|
||||
|
||||
return {
|
||||
close: () => eventSource.close(),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user