diff --git a/apps/mail/components/party.tsx b/apps/mail/components/party.tsx index 89db262ea..10b057841 100644 --- a/apps/mail/components/party.tsx +++ b/apps/mail/components/party.tsx @@ -38,18 +38,33 @@ export const NotificationProvider = ({ headers }: { headers: Record) => { - console.warn('party message', message); - const [threadId, type] = message.data.split(':'); - if (type === 'end') { - labelsDebouncer.call(); - await queryClient.invalidateQueries({ - queryKey: trpc.mail.get.queryKey({ id: threadId }), - }); - threadsDebouncer.call(); - console.warn('refetched threads'); - } else if (type === 'start') { - threadsDebouncer.call(); - console.warn('refetched threads'); + try { + console.warn('party message', message); + const { threadIds, type } = JSON.parse(message.data); + if (type === 'refresh') { + labelsDebouncer.call(); + await Promise.all( + threadIds.map(async (threadId: string) => { + await queryClient.invalidateQueries({ + queryKey: trpc.mail.get.queryKey({ id: threadId }), + }); + }), + ); + console.warn('refetched labels & threads', threadIds); + } else if (type === 'list') { + threadsDebouncer.call(); + labelsDebouncer.call(); + await Promise.all( + threadIds.map(async (threadId: string) => { + await queryClient.invalidateQueries({ + queryKey: trpc.mail.get.queryKey({ id: threadId }), + }); + }), + ); + console.warn('refetched threads, added', threadIds); + } + } catch (error) { + console.error('error parsing party message', error); } }, }); diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts index e7eb475f5..638b11957 100644 --- a/apps/server/src/main.ts +++ b/apps/server/src/main.ts @@ -85,21 +85,21 @@ export default class extends WorkerEntrypoint { public async notifyUser({ connectionId, - threadId, + threadIds, type, }: { connectionId: string; - threadId: string; - type: 'start' | 'end'; + threadIds: string[]; + type: 'refresh' | 'list'; }) { - console.log(`Notifying user ${connectionId} for thread ${threadId} with type ${type}`); + console.log(`Notifying user ${connectionId} for threads ${threadIds} with type ${type}`); const durableObject = env.DURABLE_MAILBOX.idFromName(`${connectionId}`); if (env.DURABLE_MAILBOX.get(durableObject)) { const stub = env.DURABLE_MAILBOX.get(durableObject); if (stub) { - console.log(`Broadcasting message for thread ${threadId} with type ${type}`); - await stub.broadcast(threadId + ':' + type); - console.log(`Successfully broadcasted message for thread ${threadId}`); + console.log(`Broadcasting message for thread ${threadIds} with type ${type}`); + await stub.broadcast(JSON.stringify({ threadIds, type })); + console.log(`Successfully broadcasted message for thread ${threadIds}`); } else { console.log(`No stub found for connection ${connectionId}`); }