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

This is more along the lines of moving the deckchairs around than actually moving closer to the C API, but fwiw here's an alternative:

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

      readRepeatedly().catch(e => controller.error(e));

      function readOnce(buffer, offset, size) {
        return socket.read(buffer, offset, size)
            .then((bytesRead) => {
              if (bytesRead == 0) {
                controller.close();
              } else {
                // The internal queue size has changed, so propagate
                // the backpressure signal to the underlying source.
                socket.setTCPWindowSize(Math.max(0, controller.desiredSize));
                readRepeatedly();
              }
              return bytesRead;
            });
      }

      function readRepeatedly() {
        // 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;
          readOnce(v.buffer, v.byteOffset, b.byteLength)
              .then(bytesRead => controller.byobRequest.respond(bytesRead));
        } else {
          const buffer = new ArrayBuffer(DEFAULT_WINDOW_SIZE);
          readOnce(buffer, 0, DEFAULT_WINDOW_SIZE)
              .then(bytesRead => controller.enqueue(
                  new Uint8Array(buffer, 0, bytesRead)));
        }
      }
    }

I've renamed readInto() to read() and made it return a Promise. It also returns 0 for EOF rather than throwing an exception. I feel like this is closer in spirit to the C API.

If we were going to be closer to the C API we could call read() in a loop until it throws an EWOULDWAIT exception, and then call select() as in your version. But I suspect that would be really hard to read.

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

Received on Friday, 5 August 2016 07:41:53 UTC