Re: [whatwg/streams] Allow web devs to synchronize branches with tee()? (#1157)

Ah, I *think* I understand. Correct me if I'm wrong though!

Currently, `tee()` will pull a chunk from the original stream as soon as *one* of the two branches needs a new chunk. Once that chunk becomes available, we will enqueue it to both branches. If the other branch does not need a new chunk right now, that chunk will end up in its queue and potentially cause a build up.

With `tee({ synchronized: true })`, we will only pull a chunk from the original stream when *both* branches need a new chunk. Then, once that chunk becomes available, we enqueue it to both branches. (I'll leave the bikeshedding for later. 😛)

For example:
```javascript
const [branch1, branch2] = readable.tee({ synchronized: true });
const bufferedBranch1 = branch1.pipeThrough(new TransformStream({}, { highWaterMark: 3 }));
const bufferedBranch2 = branch2.pipeThrough(new TransformStream({}, { highWaterMark: 5 }));
```
With a regular `tee()`, we would pull 5 chunks from `readable` to fill `bufferedBranch2` up to its HWM. With the proposed `tee({ synchronized: true })`, we would only pull 3 chunks from `readable`, since after that `bufferedBranch1` will stop pulling from `branch1`. In other words: a regular tee pulls as many chunks as the branch with the *largest* total queue size can hold (and "overfilling" the other branch), whereas this "synchronized tee" only pulls as many as the branch with the *smallest* total queue size can hold (and "underfilling" the other branch).

This sounds pretty feasible to do in author code. I'll see if I can whip up something to experiment with, and see if that solves your problem. 👨‍🔬

> > Looks like you're running into much of the same issues [as WebCodecs did](https://github.com/w3c/webcodecs/blob/a044235116df03f3a97f574a1a546e45eff26346/explainer.md#integrating-with-whatwg-streams). 😕
> 
> Where does that document mention this issue?

That was assuming that you wanted to drop frames mid-stream, which was a misunderstanding on my part.

-- 
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/1157#issuecomment-893728969

Received on Thursday, 5 August 2021 19:33:09 UTC