Re: [whatwg/streams] Add a Random Values Generator Demonstration (#844)

ricea commented on this pull request.



> +function createRandomValuesStream(numberOfBytes = 10, valueInterval = 1000, maxValues = null) {
+    const cqs = new CountQueuingStrategy({ highWaterMark: 4 });
+    const readableStream = new ReadableStream({
+        totalEnqueuedItemsCount: 0,
+        interval: null,
+
+        start(controller) {
+            logStatusText('`start` method of the readable stream called')
+            this.startValueInterval(controller);
+        },
+
+        /**
+         * Starting the random values generation again after a certain period
+         * @param {*} controller 
+         */
+        async pull(controller) {

This is unnecessarily complicated. You don't need to track `desiredSize` yourself. As long as you have called `enqueue()` you can rely on the ReadableStream to call you back when there is no backpressure.

It would be simpler to do something like

```javascript
pull(controller) {
  if (maxValues && this.totalEnqueuedItemsCount >= maxValues) {
    controller.close();
    return;
  }
  let resolve;
  const promise = new Promise(r => {
    resolve = r;
  });
  setTimeout(() => {
    controller.enqueue(randomValuesUint8Array(20));
    ++this.totalEnqueuedItemsCount;
    resolve();
  }, valueInterval);
  return promise;
}
```

(untested).

-- 
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/844#pullrequestreview-69910652

Received on Tuesday, 17 October 2017 14:51:46 UTC