Skip to content

Commit eeffb08

Browse files
author
Peter Steinberger
committed
fix(ui): retire stale Gateway event-log observers
1 parent fdcb321 commit eeffb08

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎ui/src/app/gateway-store.observers.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,58 @@ describe("application gateway observer ownership", () => {
193193
expect(third).toHaveBeenCalledExactlyOnceWith(gateway.eventLog);
194194
});
195195

196+
it.each(["replace", "stop"] as const)(
197+
"retires remaining event-log observers when the first observer %ss its client",
198+
(action) => {
199+
const { gateway, clients, current } = createGatewayStore();
200+
const stale = vi.fn();
201+
const delivered = vi.fn();
202+
gateway.subscribeEventLog(() => {
203+
if (action === "replace") {
204+
gateway.connect();
205+
} else {
206+
gateway.stop();
207+
}
208+
});
209+
gateway.subscribeEventLog(stale);
210+
gateway.subscribeEvents(delivered);
211+
gateway.start();
212+
const retired = current();
213+
retired.opts.onHello?.(HELLO);
214+
215+
retired.opts.onEvent?.(createGatewayEvent(1));
216+
217+
expect(stale).not.toHaveBeenCalled();
218+
expect(delivered).not.toHaveBeenCalled();
219+
expect(gateway.eventLog).toHaveLength(1);
220+
expect(clients).toHaveLength(action === "replace" ? 2 : 1);
221+
expect(gateway.snapshot.phase).toBe(action === "replace" ? "reconnecting" : "stopped");
222+
},
223+
);
224+
225+
it("retires an older event-log snapshot after a reentrant event is recorded", () => {
226+
const { gateway, current } = createGatewayStore();
227+
const observed: number[] = [];
228+
let nested = false;
229+
gateway.subscribeEventLog(() => {
230+
if (!nested) {
231+
nested = true;
232+
current().opts.onEvent?.(createGatewayEvent(2));
233+
}
234+
});
235+
gateway.subscribeEventLog((events) => observed.push(events.length));
236+
gateway.start();
237+
current().opts.onHello?.(HELLO);
238+
239+
current().opts.onEvent?.(createGatewayEvent(1));
240+
241+
expect(observed).toEqual([2]);
242+
expect(gateway.eventLog.map((event) => event.payload)).toEqual([
243+
{ text: "event-2" },
244+
{ text: "event-1" },
245+
]);
246+
});
247+
196248
it("never logs a presence event after its snapshot observer replaces the client", () => {
197249
const { gateway, current } = createGatewayStore();
198250
const logged = vi.fn();

‎ui/src/app/gateway-store.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,14 @@ export function createApplicationGateway(
235235
settings = patchSettings(patch, { selectGateway });
236236
};
237237
const recordGatewayEvent = (event: Parameters<GatewayEventListener>[0]) => {
238+
const eventClient = client;
238239
if (event.event === "presence") {
239240
const entries = readPresenceEntries(event.payload);
240241
if (entries) {
241242
const selfUser = resolveSelfPresenceUser(entries, client?.instanceId);
242243
// A live connection owns its authenticated identity until onClose. Older
243244
// gateways can omit still-connected clients after presence TTL pruning.
244245
if (selfUser && !sameSelfUser(snapshot.selfUser, selfUser)) {
245-
const eventClient = client;
246246
setSnapshot({ ...snapshot, selfUser });
247247
// A presence observer can replace its client before this event reaches the log.
248248
if (!isCurrentClient(eventClient)) {
@@ -255,7 +255,9 @@ export function createApplicationGateway(
255255
0,
256256
250,
257257
);
258-
notifyGatewayObservers(eventLogListeners, eventLog, "event");
258+
const ownsEventLog = (current: readonly EventLogEntry[]) =>
259+
current === eventLog && isCurrentClient(eventClient);
260+
notifyGatewayObservers(eventLogListeners, eventLog, "event", ownsEventLog);
259261
};
260262

261263
const connect = (overrides: ApplicationGatewayConnectOptions = {}) => {

0 commit comments

Comments
 (0)