Re: [whatwg/streams] Support teeing readable byte streams (#1114)

@MattiasBuelens commented on this pull request.



> +          if (forBranch2 === true) {
+            if (canceled1 === false) {
+              const clonedChunk = CloneArrayBufferView(chunk);
+              ReadableByteStreamControllerEnqueue(branch1._controller, clonedChunk);
+            }
+            if (canceled2 === true) {
+              chunk = new Uint8Array(chunk.buffer, chunk.byteOffset, 0);
+            }
+            ReadableByteStreamControllerRespondWithNewView(branch2._controller, chunk);
+          } else {
+            if (canceled2 === false) {
+              const clonedChunk = CloneArrayBufferView(chunk);
+              ReadableByteStreamControllerEnqueue(branch2._controller, clonedChunk);
+            }
+            if (canceled1 === true) {
+              chunk = new Uint8Array(chunk.buffer, chunk.byteOffset, 0);

> Eh, seems OK. Your fault for not returning the promise to signal you were still planning to use `byobRequest`.

Huh, hang on. We don't wait until `source.pull()` resolves/rejects before we call `source.cancel()`. So even *if* you returned a promise from `pull()`, you'd still have to provide your own `cancel()` method which ensures that the previous `pull()` promise stops early and doesn't use `byobRequest` anymore *after* `cancel()` settles.

I guess that makes sense. We have the same thing for `ReadableByteStreamController.enqueue()`: if the stream's state is `"closed"`, it throws a `TypeError`. So if the `pull()` method still uses it after the stream has been cancelled, it'll (hopefully) propagate the error to the returned promise and the stream will ignore that promise's result (because the state is no longer `"readable"`).

> Well, the readInto() example doesn't cancel the stream. I guess maybe that's my suggestion (and perhaps even the current intent of the spec): if you cancel the stream with a BYOB request pending, you don't expect to get your buffer back. You should have waited to cancel until the outstanding BYOB request completed. How bad would that be?

Hmm, that's also true. That would change `ReadableStreamBYOBReadResult` slightly, since `value` can now be `undefined` if the read resolves as a result of canceling the stream.

But really, what *would* a developer still want to do with the buffer from the `read()` result after they've cancelled the stream? Is there ever a case where you start a BYOB read, then change your mind and cancel it *but still want your buffer back*? 🤔

I'll try it out on the reference implementation, see where it leads us. But I'm *definitely* going to move it to a separate PR now! 😁 

-- 
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/pull/1114#discussion_r605958213

Received on Thursday, 1 April 2021 21:33:04 UTC