A queuing strategy determines how a ReadableStream measures queued chunks and applies its highWaterMark. CountQueuingStrategy counts JavaScript values; ByteQueuingStrategy counts total bytes. Byte measurement is more predictable when chunks vary from a few bytes to several kilobytes. section 10.4.2
Use the strategy together with desiredSize to interpret backpressure:
- Positive desiredSize means the queue has room; zero means capacity; a negative value means the queue exceeds its limit. section 10.4.2
- When desiredSize is zero or negative, the runtime stops calling pull(), provided production remains inside pull(). section 10.4.2
- Extracting the controller and enqueueing from an independent feed bypasses that protection, allowing an unbounded queue unless the feed is paused or otherwise coordinated. section 10.4.2
const readable = new ReadableStream({
pull(controller) {
controller.enqueue(getNextChunk());
}
}, new CountQueuingStrategy({ highWaterMark: 10 })); // #AA CountQueuingStrategy with highWaterMark 10 limits the queue by distinct JavaScript values while pull() supplies chunks. section 10.4.2
