44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { BaseStream, type StreamMessage } from '../../source/services/stream/base-stream.ts';
|
|
|
|
/** Minimal observable connection used by application and broadcaster tests. */
|
|
export class TestConnection extends BaseStream {
|
|
readonly messages: StreamMessage[] = [];
|
|
readonly closeCallbacks: Array<() => void> = [];
|
|
closed = false;
|
|
|
|
constructor(
|
|
readonly streaming: boolean,
|
|
readonly bidirectional: boolean,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
async send(message: StreamMessage): Promise<void> {
|
|
if (this.closed) {
|
|
throw new Error('connection is closed');
|
|
}
|
|
|
|
this.messages.push(message);
|
|
}
|
|
|
|
close(): void {
|
|
if (this.closed) {
|
|
return;
|
|
}
|
|
|
|
this.closed = true;
|
|
const callbacks = this.closeCallbacks.splice(0);
|
|
callbacks.forEach((callback) => callback());
|
|
}
|
|
|
|
onClose(callback: () => void): void {
|
|
if (this.closed) {
|
|
callback();
|
|
|
|
return;
|
|
}
|
|
|
|
this.closeCallbacks.push(callback);
|
|
}
|
|
}
|