- From: Mattias Buelens <notifications@github.com>
- Date: Tue, 08 Sep 2020 06:22:01 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1030/688861815@github.com>
A bit late to the party, but I gave it a go. 😁
This is basically a regular pipe loop, but the `writer` can be switched out in the middle.
```javascript
let counter = 0;
let readable = new ReadableStream(/* ... */); // this is probably an input argument
let writable = createOutputStream(counter);
let reader = readable.getReader();
let writer = writable.getWriter();
try {
while (true) {
await writer.ready;
const { done, value } = await reader.read();
if (done) {
// all chunks have been read, we're done
writer.close();
break;
}
if (shouldSplit(value)) {
// create and switch to a new output stream
writer.close().catch(() => {});
writer.releaseLock();
counter++;
writable = createOutputStream(counter);
writer = writable.getWriter();
}
// write chunk to current output stream
writer.write(value).catch(() => {});
}
} catch (e) {
// read or write failed, propagate to the other end
reader.cancel(e).catch(() => {});
writer.abort(e).catch(() => {});
} finally {
// just in case someone else wants to use the readable afterwards
reader.releaseLock();
writer.releaseLock();
}
function shouldSplit(chunk) {
return string.includes(thePattern);
}
function createOutputStream(counter) {
const transform = new TransformStream();
transform.readable
.pipeThrough(createMyRemainingTransforms())
.pipeTo(createMyWritableFileStream(counter));
return transform.writable;
// equivalent, but less obvious:
const { readable, writable } = createMyRemainingTransforms();
readable.pipeTo(createMyWritableFileStream(counter));
return writable;
}
```
There's probably a way to replace the loop with some clever `.pipeTo()` calls, but I'll leave that as an exercise for the reader. 😛
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1030#issuecomment-688861815
Received on Tuesday, 8 September 2020 13:22:13 UTC