Re: [whatwg/streams] Allow stream reader cancel() method to return bytes from queue instead of discarding them. (#1147)

> > While it might be nice syntactically to have `cancel()` return any bytes left in the queue I think this can be polyfilled with,
> > ```js
> > const finalReadPromise = reader.read();
> > await reader.cancel();
> > const finalRead = await finalReadPromise;
> > ```
> 
> That's pretty clever! 😀 It should work in simple cases, although I'm not sure how well it would translate to more complex scenarios (e.g. pipe chains or cross-realm streams) where chunks are buffered in multiple streams.

I want exactly this behavior, but it does not work this way in current Chrome. "finalReadPromise" is immediately resolved with _{ value:undefined, done:true}_ and nothing is read from the port at all.
I send a lazy stream of 1 byte containing a sequence number every 50-100 ms.
I've got some results with this code but with 1-2% data loss.

```
setTimeout( () => {reader.cancel()} , 0);
data = await reader.read();
console.log( data);
```


-- 
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/1147#issuecomment-882969240

Received on Tuesday, 20 July 2021 01:13:19 UTC