Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
994 B
Raw Blame
// SPDX-FileCopyrightText: 2024 LiveKit, Inc.
//
// SPDX-License-Identifier: Apache-2.0
import type {BaseStreamInfo, ByteStreamInfo, TextStreamInfo} from '../../types.ts';

class BaseStreamWriter<T, InfoType extends BaseStreamInfo> {
	protected writableStream: WritableStream<T>;

	protected defaultWriter: WritableStreamDefaultWriter<T>;

	protected onClose?: () => void;

	readonly info: InfoType;

	constructor(writableStream: WritableStream<T>, info: InfoType, onClose?: () => void) {
		this.writableStream = writableStream;
		this.defaultWriter = writableStream.getWriter();
		this.onClose = onClose;
		this.info = info;
	}

	write(chunk: T): Promise<void> {
		return this.defaultWriter.write(chunk);
	}

	async close() {
		await this.defaultWriter.close();
		this.defaultWriter.releaseLock();
		this.onClose?.();
	}
}

export class TextStreamWriter extends BaseStreamWriter<string, TextStreamInfo> {}

export class ByteStreamWriter extends BaseStreamWriter<Uint8Array, ByteStreamInfo> {}