Re: [whatwg/streams] Add a realtime mode to ReadableStream.tee (Issue #1186)

In https://github.com/whatwg/streams/issues/1157#issuecomment-893728969, we discussed and prototyped a "synchronized" variant of `tee()`. That variant would only pull from the original stream if *both* branches are pulling, to make sure that both branches see the same chunks at the same time (by slowing down the faster reader).

If I understand correctly, this "realtime" variant is slightly different. It would pull from the original stream if *at least one* of the two branches is pulling, and only enqueue the received chunk to branches that *are actively pulling*. That way, we avoid overfilling the queue of the slower branch by "skipping" chunks.

What should happen if one branch is only *slightly* slower than the other? That is:
1. Branch 1 starts pulling. `tee()` starts pulling from the original stream.
2. We receive a chunk from the original stream. Only branch 1 is pulling, so we only enqueue it to that branch.
3. Branch 2 starts pulling, but only *slightly* after we did the previous step.

I see two options:
* Treat this as a new pull. Thus, `tee()` starts pulling the next chunk from the original stream, which will definitely go to branch 2. (If branch 1 manages to catch up in the meantime and also starts pulling, we'll also enqueue it to branch 1.)
* Store the previous chunk, and enqueue it when branch 2 (eventually) starts pulling. This chunk can be overwritten when branch 1 pulls in more chunks in the meantime. However, if it doesn't get overwritten, then this previous chunk may be very old by the time branch 2 pulls it.

Or perhaps some combination of these two, with some sort of "max age" to decide between enqueuing the previous chunk or pulling a new chunk? 🤷

-- 
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/1186#issuecomment-970363813

Received on Tuesday, 16 November 2021 15:07:18 UTC