Re: [whatwg/streams] Fix makeReadableBackpressureByteSocketStream to drain all data (#504)

Ah, I understand better now. OK, hmm. I wonder if there's a way to fix this while also making our socket API more like the low-level version. (I am already a little unhappy that setTCPWindowSize is fictional.) I guess that is in a large part what the PR is trying to do.

Maybe one way of getting a bit less complexity is to pretend we had a `socket.select2()` that returned a promise, instead of an EventTarget. The translation between low-level polling in C and a higher-level event loop language is tricky to capture, but I think that might be a better mapping. (Is `select2()` a good name, or better to stick with something schematic like `waitForReadable()`?)

Concretely, here is what I am thinking:

```js
    start(controller) {
      socket.setTCPWindowSize(Math.max(0, controller.desiredSize));

      socket.onend = () => controller.close();

      readRepeatedly().catch(e => {
        if (e.code === "EOF") {
          controller.close();
        } else {
          controller.error(e)
        }
      });
      
      function readRepeatedly() {
        return socket.select2().then(() => {
          // Since the socket can become readable even when there’s
          // no pending BYOB requests, we need to handle both cases.
          if (controller.byobRequest) {
            const v = controller.byobRequest.view;
            const bytesRead = socket.readInto(v.buffer, v.byteOffset, v.byteLength);
            controller.byobRequest.respond(bytesRead);
          } else {
            const buffer = new ArrayBuffer(DEFAULT_WINDOW_SIZE);
            const bytesRead = socket.readInto(buffer, 0, DEFAULT_WINDOW_SIZE);
            controller.enqueue(new Uint8Array(buffer, 0, bytesRead));
          }

          // The internal queue size has changed, so propagate
          // the backpressure signal to the underlying source.
          socket.setTCPWindowSize(Math.max(0, controller.desiredSize));
          
          return readRepeatedly();
        });
      }
    },
```

---
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/504#issuecomment-237770834

Received on Friday, 5 August 2016 06:55:17 UTC