From 3bba3b49f0504df4e197e019af5f68210cf4bcda Mon Sep 17 00:00:00 2001 From: Okiki Date: Fri, 30 Aug 2024 09:29:30 +0000 Subject: [PATCH] chore: move withDisposal to disposal.ts Signed-off-by: Okiki --- disposal.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ streams.ts | 51 +-------------------------------------------------- 2 files changed, 49 insertions(+), 50 deletions(-) create mode 100644 disposal.ts diff --git a/disposal.ts b/disposal.ts new file mode 100644 index 0000000..c56cfa8 --- /dev/null +++ b/disposal.ts @@ -0,0 +1,48 @@ +/** + * An interface representing a `ReadableStream` with added disposal capabilities. + * This interface extends the `ReadableStream` and includes methods for both + * synchronous and asynchronous disposal of the stream. + * + * @template T - The type of data in the `ReadableStream`. + */ +export interface ReadableStreamWithDisposal extends ReadableStream, AsyncDisposable, Disposable { + /** + * Synchronously disposes of the `ReadableStream`, canceling it and releasing resources. + */ + [Symbol.dispose](reason?: unknown): void; + + /** + * Asynchronously disposes of the `ReadableStream`, canceling it and releasing resources. + * @returns A promise that resolves when the disposal is complete. + */ + [Symbol.asyncDispose](reason?: unknown): Promise; +} + +/** + * Wraps a ReadableStream and adds Symbol.dispose and Symbol.asyncDispose methods + * for proper cleanup of resources. + * + * @template T - The type of data in the ReadableStream. + * @param stream - The ReadableStream to wrap. + * @returns A new ReadableStream with disposal support. + */ +export function withDisposal(stream: ReadableStream): ReadableStreamWithDisposal { + return Object.assign(stream, { + /** + * Synchronous disposal of the ReadableStream. + * This cancels the stream and releases resources. + */ + [Symbol.dispose](reason?: unknown) { + stream.cancel(reason); + }, + + /** + * Asynchronous disposal of the ReadableStream. + * This cancels the stream and releases resources asynchronously. + * @returns A promise that resolves when the disposal is complete. + */ + async [Symbol.asyncDispose](reason?: unknown) { + await stream.cancel(reason); + } + }); +} diff --git a/streams.ts b/streams.ts index 117f111..c3fd5d3 100644 --- a/streams.ts +++ b/streams.ts @@ -256,53 +256,4 @@ export function splitByStream( const falseStream = source.pipeThrough(falseTransformer); return [trueStream, falseStream]; -} - -/** - * An interface representing a `ReadableStream` with added disposal capabilities. - * This interface extends the `ReadableStream` and includes methods for both - * synchronous and asynchronous disposal of the stream. - * - * @template T - The type of data in the `ReadableStream`. - */ -export interface ReadableStreamWithDisposal extends ReadableStream, AsyncDisposable, Disposable { - /** - * Synchronously disposes of the `ReadableStream`, canceling it and releasing resources. - */ - [Symbol.dispose](reason?: unknown): void; - - /** - * Asynchronously disposes of the `ReadableStream`, canceling it and releasing resources. - * @returns A promise that resolves when the disposal is complete. - */ - [Symbol.asyncDispose](reason?: unknown): Promise; -} - -/** - * Wraps a ReadableStream and adds Symbol.dispose and Symbol.asyncDispose methods - * for proper cleanup of resources. - * - * @template T - The type of data in the ReadableStream. - * @param stream - The ReadableStream to wrap. - * @returns A new ReadableStream with disposal support. - */ -export function withDisposal(stream: ReadableStream): ReadableStreamWithDisposal { - return Object.assign(stream, { - /** - * Synchronous disposal of the ReadableStream. - * This cancels the stream and releases resources. - */ - [Symbol.dispose](reason?: unknown) { - stream.cancel(reason); - }, - - /** - * Asynchronous disposal of the ReadableStream. - * This cancels the stream and releases resources asynchronously. - * @returns A promise that resolves when the disposal is complete. - */ - async [Symbol.asyncDispose](reason?: unknown) { - await stream.cancel(reason); - } - }); -} +} \ No newline at end of file -- 2.51.2