diff --git a/backend/handler/socket_handler.py b/backend/handler/socket_handler.py index c38c725c7..45cc6d7f9 100644 --- a/backend/handler/socket_handler.py +++ b/backend/handler/socket_handler.py @@ -35,8 +35,9 @@ class SocketHandler: # each job in a new loop. loop = asyncio.get_running_loop() if self._write_manager is None or self._write_manager_loop is not loop: + # Only a manager attached to a server inherits its JSON encoder. self._write_manager = socketio.AsyncRedisManager( - REDIS_URL, channel=self.channel, write_only=True + REDIS_URL, channel=self.channel, write_only=True, json=json_module ) self._write_manager_loop = loop return self._write_manager diff --git a/backend/tests/handler/test_socket_handler.py b/backend/tests/handler/test_socket_handler.py index 0d3aac632..143b9a806 100644 --- a/backend/tests/handler/test_socket_handler.py +++ b/backend/tests/handler/test_socket_handler.py @@ -22,7 +22,11 @@ class TestEmitToUser: await handler.emit_to_user(5, "notifications:read", {"ids": None}) - assert make.call_args.kwargs == {"channel": "test-channel", "write_only": True} + assert make.call_args.kwargs == { + "channel": "test-channel", + "write_only": True, + "json": socket_handler_module.json_module, + } manager.emit.assert_awaited_once_with( "notifications:read", {"ids": None}, room="user:5" ) diff --git a/frontend/src/v2/stores/notificationInbox.test.ts b/frontend/src/v2/stores/notificationInbox.test.ts index 527ccf246..7e23c6a7b 100644 --- a/frontend/src/v2/stores/notificationInbox.test.ts +++ b/frontend/src/v2/stores/notificationInbox.test.ts @@ -55,6 +55,24 @@ describe("notificationInbox", () => { expect(inbox.unreadCount).toBe(1); }); + it("drops a list that arrives after a reset", async () => { + let answer!: (value: unknown) => void; + getNotifications.mockReturnValue( + new Promise((resolve) => { + answer = resolve; + }), + ); + const inbox = storeNotificationInbox(); + + const pending = inbox.fetch(); + inbox.reset(); + answer({ data: [notification(1)] }); + await pending; + + expect(inbox.notifications).toEqual([]); + expect(inbox.loaded).toBe(false); + }); + it("puts a pushed notification first, once", () => { const inbox = storeNotificationInbox(); inbox.receive(notification(1)); diff --git a/frontend/src/v2/stores/notificationInbox.ts b/frontend/src/v2/stores/notificationInbox.ts index 8fa78ff16..20f01f8c1 100644 --- a/frontend/src/v2/stores/notificationInbox.ts +++ b/frontend/src/v2/stores/notificationInbox.ts @@ -8,6 +8,9 @@ import type { } from "@/__generated__"; import notificationApi from "@/services/api/notification"; +// Bumped by reset(), so a list requested for the previous user is dropped. +let generation = 0; + // No ids stands for every notification, as it does in the API. type NotificationIds = NotificationIdsPayload["ids"]; @@ -24,7 +27,9 @@ export default defineStore("v2NotificationInbox", { actions: { async fetch() { + const requested = generation; const { data } = await notificationApi.getNotifications(); + if (requested !== generation) return; this.notifications = data; this.loaded = true; }, @@ -91,6 +96,7 @@ export default defineStore("v2NotificationInbox", { }, reset() { + generation += 1; this.notifications = []; this.loaded = false; },