fix: apply the second code-review pass

- The shared write manager passes the server's json_module, which a
  manager never attached to a server doesn't inherit; streaming pushes
  lost it when they moved onto that manager.
- The inbox drops a list requested before a reset, so a slow response for
  the previous user can't fill the next user's inbox.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
zurdi
2026-09-23 13:31:14 +02:00
parent 3a10ccc84f
commit 45fbb4fb7c
4 changed files with 31 additions and 2 deletions

View File

@@ -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

View File

@@ -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"
)

View File

@@ -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));

View File

@@ -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;
},