refactor: update notification handling to support multiple thread IDs

This commit is contained in:
Aj Wazzan
2025-05-20 23:37:44 -07:00
parent 906c032ca0
commit e4ef39a220
2 changed files with 34 additions and 19 deletions

View File

@@ -38,18 +38,33 @@ export const NotificationProvider = ({ headers }: { headers: Record<string, stri
},
host: import.meta.env.VITE_PUBLIC_BACKEND_URL!,
onMessage: async (message: MessageEvent<string>) => {
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);
}
},
});

View File

@@ -85,21 +85,21 @@ export default class extends WorkerEntrypoint<typeof env> {
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}`);
}