Files
orca/src/shared/ws-inbound-message-queue.ts
T
OrcaWinandOrcaWin 8916bf36aa fix(mobile): release queued inbound frames after transport teardown (#34777)
* fix(mobile): release queued inbound frames when transports close

* test(mobile): run relay inbound retention regressions in every runner

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-27 23:40:36 -07:00

46 lines
1.1 KiB
TypeScript

type InboundMessage = { raw: unknown }
export class WsInboundMessageQueue {
private readonly pending = new Set<InboundMessage>()
private tail: Promise<void> = Promise.resolve()
private disposed = false
constructor(
private readonly processMessage: (raw: unknown) => void | Promise<void>,
private readonly onError: (error: unknown) => void
) {}
enqueue(raw: unknown): Promise<void> {
if (this.disposed) {
return Promise.resolve()
}
const message = { raw }
this.pending.add(message)
this.tail = this.tail
.then(() => {
this.pending.delete(message)
if (this.disposed) {
return
}
const raw = message.raw
message.raw = undefined
return this.processMessage(raw)
})
.catch((error: unknown) => {
if (!this.disposed) {
this.onError(error)
}
})
return this.tail
}
dispose(): void {
this.disposed = true
// A blocked conversion must not retain every later frame until it finishes.
for (const message of this.pending) {
message.raw = undefined
}
this.pending.clear()
}
}