- From: Simon Chan <notifications@github.com>
- Date: Fri, 10 Jun 2022 21:27:55 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1217/1152852831@github.com>
If we are talking about API design (which means the consumer needs to explicitly release the buffer), I think it's already possible like this:
```ts
declare function pullFromSource<T>(): Promise<T>;
declare function releaseToSource<T>(value: T): void;
interface ReusableData<T> {
    value: T;
    release: () => void;
}
class ReusableReadableStream<T> extends ReadableStream<ReusableData<T>>{
    constructor() {
        super({
            async pull(controller) {
                const value = await pullFromSource<T>();
                controller.enqueue({
                    value, release: () => {
                        releaseToSource(value);
                    }
                });
            }
        })
    }
}
const stream = new ReusableReadableStream<Uint8Array>();
stream.pipeTo(new WritableStream({
    write(chunk) {
        // do things to chunk
        console.log(chunk.value);
        chunk.release();
    }
}))
```
The consumer needs to use `chunk.value`, so it knows it's special, and remembers to call `release()`.
If we don't want the consumer to learn new things, is it possible to let GC release the buffer?
-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1217#issuecomment-1152852831
You are receiving this because you are subscribed to this thread.
Message ID: <whatwg/streams/issues/1217/1152852831@github.com>
Received on Saturday, 11 June 2022 04:28:06 UTC