- From: Vuk <notifications@github.com>
- Date: Tue, 27 Aug 2024 05:21:47 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1323/2312414536@github.com>
@ricea I really appreciate you taking the time to answer my question.
Sorry again for posting the question here, but this is so specific I really doubted anyone on StackOverflow would answer it.
Your example really helped me to understand some things better after which I re-read the spec one more time.
Your example actually gave me some inspiration, and I think I have thought up a simpler way to do it, what do you think about:
```javascript
function MyTransform() {
let identityTransform = new TransformStream();
const readable = identityTransform.readable;
const identityWritter = identityTransform.writable.getWriter();
const writable = new WritableStream({
async write(chunk) {
await identityWritter.ready;
const foundTextIndex = chunk.indexOf('text to replace');
if (foundTextIndex === -1) {
identityWritter.write(chunk);
return;
}
await identityWritter.ready;
// Enqueue text before the part that needs to be replaced
identityWritter.write(chunk.slice(0, foundTextIndex));
// Enqueue the text from the second source
// this is the part I'm not sure for which how to handle if we are adding
// chunks to the readable sides internal queue even if it's full
for await (const chunk of streamForReplacement) {
await identityWritter.ready;
identityWritter.write(chunk);
}
await identityWritter.ready;
// Enqueue text after replacement
identityWritter.write(chunk.slice(foundTextIndex + 'text to replace'.length));
},
close() {
identityWritter.close();
},
abort(err) {
identityWritter.error(err);
},
});
return { readable, writable };
}
```
This way I'm using the identity transforms writter in order to respect the backpressure, since we can await `identityWritter.ready;` to know if we can write again
--
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/issues/1323#issuecomment-2312414536
You are receiving this because you are subscribed to this thread.
Message ID: <whatwg/streams/issues/1323/2312414536@github.com>
Received on Tuesday, 27 August 2024 12:21:51 UTC