- From: Mattias Buelens <notifications@github.com>
- Date: Tue, 11 May 2021 08:11:39 -0700
- To: whatwg/streams <streams@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/streams/issues/1014/838651602@github.com>
I don't think this part is correct:
> When a source blocks inside `pull()`, it is not possible to cancel the ReadableStream until the promise returned by `pull()` settles.
As far as I know, we don't actually wait for the pending `pull()` to settle before we call `cancel()` on the source.
```javascript
var rs = new ReadableStream({
async pull(c) {
console.log("source pull");
await new Promise(r => setTimeout(r, 1000));
console.log("source enqueue");
c.enqueue("a");
console.log("source pull done");
},
cancel(reason) {
console.log("source cancel", reason);
}
}, { highWaterMark: 0 });
var reader = rs.getReader();
reader.read().then((result) => console.log("read", result));
setTimeout(() => reader.cancel("hi"), 100);
```
This logs:
```
source pull
source cancel hi
read {value: undefined, done: true}
source enqueue
```
The source receives the `cancel()` call immediately, and the pending `read()` is fulfilled right away. When `pull()` eventually calls `enqueue()`, it'll throw an error (which the stream silently handles, since it has already become canceled):
```
TypeError: Failed to execute 'enqueue' on 'ReadableStreamDefaultController': Cannot enqueue a chunk into a closed readable stream
```
This also means my previous suggestion for passing an `AbortSignal` to `pull()` isn't really necessary. You can already do this yourself using `cancel()`:
```javascript
var rs = new ReadableStream({
start() {
this._controller = new AbortController();
},
async pull(c) {
const response = await fetch("./more/data/please", {
signal: this._controller.signal
});
const data = await response.json();
c.enqueue(data);
},
cancel(reason) {
this._controller.abort();
}
}, { highWaterMark: 0 });
```
--
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/1014#issuecomment-838651602
Received on Tuesday, 11 May 2021 15:11:52 UTC