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

I'm trying to simulate a situation where the reading speed is higher than the data flow speed.

The following code uses an Array as the underlying source, and once we reach the end of the array, we don't do controller.close(). We call readAll() which non-stoppingly tries to read data from the stream. Then, after a while (setTimeout), we append more data to the array (I think we are appending data to the same memory allocation, right?).

`
function createReadableDataStream(source: number[]) {
    let position = 0;
    
    return new ReadableStream({
        type: "bytes",

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

function readAll(readableStream: ReadableStream) {
    const reader = readableStream.getReader();
    return pump();

    function pump() {
        return reader.read()
            .then(
                ( { value, done } ) => {
                    console.log(`chunk just read: ${value}`);
                    return pump();
                }
            )
            .catch(
                ( error: Error ) => {
                    console.log("Read failes. Error info: " + error.message);
                }
            );
    }
}

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

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

To my understanding, for some time, the internal queue is empty and pull() will do nothing, and the pump() will continue to return itself (i.e. continue to try reading something). But once we append more data to the array, the stream should be "resumed". So I expected the following output:

`
1
2
3
4
5
// wait for about 3s
11
12
13
`

But what I actually got was only number 1-5, which means nothing happened after I appended data to the underlying source array.

Could anyone explain what's wrong with the 

-- 
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

Received on Thursday, 8 August 2019 21:05:27 UTC