title | slug | page-type | browser-compat |
---|---|---|---|
ReadableStreamDefaultController |
Web/API/ReadableStreamDefaultController |
web-api-interface |
api.ReadableStreamDefaultController |
{{APIRef("Streams")}}{{AvailableInWorkers}}
The ReadableStreamDefaultController
interface of the Streams API represents a controller allowing control of a {{domxref("ReadableStream")}}'s state and internal queue. Default controllers are for streams that are not byte streams.
None. ReadableStreamDefaultController
instances are created automatically during ReadableStream
construction.
- {{domxref("ReadableStreamDefaultController.desiredSize")}} {{ReadOnlyInline}}
- : Returns the desired size required to fill the stream's internal queue.
- {{domxref("ReadableStreamDefaultController.close()")}}
- : Closes the associated stream.
- {{domxref("ReadableStreamDefaultController.enqueue()")}}
- : Enqueues a given chunk in the associated stream.
- {{domxref("ReadableStreamDefaultController.error()")}}
- : Causes any future interactions with the associated stream to error.
In the following simple example, a custom ReadableStream
is created using a constructor (see our Simple random stream example for the full code). The start()
function generates a random string of text every second and enqueues it into the stream. A cancel()
function is also provided to stop the generation if {{domxref("ReadableStream.cancel()")}} is called for any reason.
Note that a {{domxref("ReadableStreamDefaultController")}} object is provided as the parameter of the start()
and pull()
functions.
When a button is pressed, the generation is stopped, the stream is closed using {{domxref("ReadableStreamDefaultController.close()")}}, and another function is run, which reads the data back out of the stream.
let interval;
const stream = new ReadableStream({
start(controller) {
interval = setInterval(() => {
let string = randomChars();
// Add the string to the stream
controller.enqueue(string);
// show it on the screen
let listItem = document.createElement("li");
listItem.textContent = string;
list1.appendChild(listItem);
}, 1000);
button.addEventListener("click", () => {
clearInterval(interval);
fetchStream();
controller.close();
});
},
pull(controller) {
// We don't really need a pull in this example
},
cancel() {
// This is called if the reader cancels,
// so we should stop generating strings
clearInterval(interval);
},
});
{{Specifications}}
{{Compat}}
- Streams API concepts
- Using readable streams
- {{domxref("ReadableStream")}}
- WHATWG Stream Visualizer, for a basic visualization of readable, writable, and transform streams.
- Web-streams-polyfill or sd-streams - polyfills