[whatwg/streams] Make the internal queue observable (#1167)

Since `desiredSize` exists, and is updated internally (being a property of the stream's controller), wouldn't exposing the queue for [obviously read-only] observation, on a stream itself, be a safe and useful feature?

By observability of the queue I don't just mean arbitrary stream consumers being able to read its queue size, but also ability to register notifications about *changes* to the size of the queue.

The first one is arguably achievable with some `ObservableReadableStream` (inapt name, since we're only talking about observing the queue, but for the sake of example) subclass:

```javascript
class ObservableReadableStream extends ReadableStream {
    constructor(underlyingSource, queuingStrategy) {
        super({ ...underlyingSource, start: controller => {
            Object.defineProperty(this, "desiredSize", { get: () => controller.desiredSize });
            underlyingSource.start(controller);
        });
    }
}
``` 

Implementing event notifications for when the queue changes size, would be more involved -- the only way I can think of is to plug into whatever would be calling `controller.enqueue` for a specific stream, for when the queue grows in size, but also into `read` calls on obtained reader, which seems to be complicated by the fact one doesn't necessarily have control over the class of reader returned by `getReader` and that a reader may also be obtained with a `new ReadableStream...Reader(stream)`. Point being, I am not at all certain "monkey patching" readable streams even by subclassing is elegant or simple.

Since a queue is a limited resource, allowing its (again, read-only) introspection from arbitrary locations can't hurt but may save a lot of boilerplating (subclassing for "ObservableReadableStream"?) on part of applications which want to make decisions based on queue sizes, or in the very least expose these changing quantity to "advanced" users.

Am I onto something, or have I missed something very obvious?

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

Received on Friday, 24 September 2021 12:28:53 UTC