Re: [whatwg/streams] What would happen if the data stream "pauses" then continues (see mode detail below) (#1008)

If `pull()` does not return a promise and it does not call `enqueue()`, then the stream will not call `pull()` again until `enqueue()` is called. In your case: after reaching the end of the array, the next `pull()` call does not enqueue any more chunks, so the stream will stop calling `pull()`.

One solution would be to have `pull()` return a promise, and always enqueue at least one chunk before resolving that promise. For example:
```javascript
function createReadableDataStream(source: number[]) {
    let nextAppendResolve;
    let nextAppendPromise = new Promise((resolve) => {
        nextAppendResolve = resolve;
    });
    let append = (chunks) => {
        source.push(...chunks);
        nextAppendResolve();
        nextAppendPromise = new Promise((resolve) => {
            nextAppendResolve = resolve;
        });
    };

    let position = 0;
    let stream = new ReadableStream({
        type: "bytes",

        autoAllocateChunkSize: CHUNK_SIZE,
    
        async pull(controller: ReadableByteStreamController) {
            // when reaching the end of the current array, don't close the stream
            // the source array may be appended later
            while (position >= source.length) {
                await nextAppendPromise;
            }
            const view = controller.byobRequest.view;
            view[0] = source[position++];
            controller.byobRequest.respond(1);
        }
    });

    return { stream, append };
}

let streamdata: number[] = new Array(5);
for (let i = 0; i < streamdata.length; i++) {
    streamdata[i] = i + 1;
}
let { stream, append } = createReadableDataStream(streamdata);
readAll(stream);

// append more data to the underlying source
setTimeout(
    () => {
        append([11, 12, 13]);
    },
    3000
);
```

-- 
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/1008#issuecomment-519717244

Received on Thursday, 8 August 2019 23:00:20 UTC