45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { z } from 'zod'
|
|
import { defineStreamingMethod, defineMethod, type RpcAnyMethod } from '../core'
|
|
|
|
const NotificationUnsubscribeParams = z.object({
|
|
subscriptionId: z
|
|
.unknown()
|
|
.transform((value) => (typeof value === 'string' && value.length > 0 ? value : ''))
|
|
.pipe(z.string().min(1, 'Missing subscriptionId'))
|
|
})
|
|
|
|
// Why: notifications.subscribe streams desktop notification events to mobile
|
|
// clients over WebSocket. The mobile client shows a local push notification
|
|
// for each event. This avoids requiring Firebase/APNs — the existing
|
|
// persistent WebSocket connection doubles as the push channel.
|
|
export const NOTIFICATION_METHODS: readonly RpcAnyMethod[] = [
|
|
defineStreamingMethod({
|
|
name: 'notifications.subscribe',
|
|
params: null,
|
|
handler: async (_params, { runtime }, emit) => {
|
|
await new Promise<void>((resolve) => {
|
|
const unsubscribe = runtime.onNotificationDispatched((event) => {
|
|
emit({ type: 'notification', ...event })
|
|
})
|
|
|
|
const subscriptionId = `notifications-${Date.now()}`
|
|
runtime.registerSubscriptionCleanup(subscriptionId, () => {
|
|
unsubscribe()
|
|
emit({ type: 'end' })
|
|
resolve()
|
|
})
|
|
|
|
emit({ type: 'ready', subscriptionId })
|
|
})
|
|
}
|
|
}),
|
|
defineMethod({
|
|
name: 'notifications.unsubscribe',
|
|
params: NotificationUnsubscribeParams,
|
|
handler: async (params, { runtime }) => {
|
|
runtime.cleanupSubscription(params.subscriptionId)
|
|
return { unsubscribed: true }
|
|
}
|
|
})
|
|
]
|