fix: make session resume atomic across restarts
This commit is contained in:
@@ -5,6 +5,10 @@ export class FakeEventSource {
|
||||
onerror: (() => void) | null = null;
|
||||
closed = false;
|
||||
private listeners = new Map<string, Set<(e: { data: string; lastEventId: string }) => void>>();
|
||||
private historicalListeners = new Map<
|
||||
string,
|
||||
Set<(e: { data: string; lastEventId: string }) => void>
|
||||
>();
|
||||
constructor(public url: string) {
|
||||
FakeEventSource.instances.push(this);
|
||||
}
|
||||
@@ -17,9 +21,16 @@ export class FakeEventSource {
|
||||
const data = JSON.stringify(obj);
|
||||
for (const h of this.listeners.get(type) ?? []) h({ data, lastEventId });
|
||||
}
|
||||
/** Simulate an event already queued before removeEventListener/close completed. */
|
||||
emitQueuedNamed(type: string, obj: unknown, lastEventId = "") {
|
||||
const data = JSON.stringify(obj);
|
||||
for (const h of this.historicalListeners.get(type) ?? []) h({ data, lastEventId });
|
||||
}
|
||||
addEventListener(type: string, handler: (e: { data: string; lastEventId: string }) => void) {
|
||||
if (!this.listeners.has(type)) this.listeners.set(type, new Set());
|
||||
if (!this.historicalListeners.has(type)) this.historicalListeners.set(type, new Set());
|
||||
this.listeners.get(type)!.add(handler);
|
||||
this.historicalListeners.get(type)!.add(handler);
|
||||
}
|
||||
removeEventListener(type: string, handler: (e: { data: string; lastEventId: string }) => void) {
|
||||
this.listeners.get(type)?.delete(handler);
|
||||
|
||||
Reference in New Issue
Block a user