ReadableStream()
constructor creates and returns a readable stream object from the given handlers.
var readableStream = new ReadableStream(underlyingSource[, queuingStrategy]);
underlyingSource
can contain the following:
controller
parameter passed to this method is a
ReadableStreamDefaultController
或
ReadableByteStreamController
, depending on the value of the
type
property. This can be used by the developer to control the stream during set up.
pull()
returns a promise, then it won't be called again until that promise fulfills; if the promise rejects, the stream will become errored. The
controller
parameter passed to this method is a
ReadableStreamDefaultController
或
ReadableByteStreamController
, depending on the value of the
type
property. This can be used by the developer to control the stream as more chunks are fetched.
ReadableStream.cancel()
is called). The contents should do whatever is necessary to release access to the stream source. If this process is asynchronous, it can return a promise to signal success or failure. The
reason
parameter contains a
DOMString
describing why the stream was cancelled.
"bytes"
, the passed controller object will be a
ReadableByteStreamController
capable of handling a BYOB (bring your own buffer)/byte stream. If it is not included, the passed controller will be a
ReadableStreamDefaultController
.
autoAllocateChunkSize
with a positive integer value to turn on the stream's auto-allocation feature. With this turned on, the stream implementation will automatically allocate an
ArrayBuffer
with a size of the given integer, and the consumer can also use a default reader.
A non-negative integer — this defines the total number of chunks that can be contained in the internal queue before backpressure is applied.
chunk
— this indicates the size to use for each chunk, in bytes.
注意
: You could define your own custom
queuingStrategy
, or use an instance of
ByteLengthQueuingStrategy
or
CountQueuingStrategy
for this object value. If no
queuingStrategy
is supplied, the default used is the same as a
CountQueuingStrategy
with a high water mark of 1.
An instance of the
ReadableStream
对象。
"bytes"
nor
undefined
.
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()
fuction is also provided to stop the generation if
ReadableStream.cancel()
is called for any reason.
When a button is pressed, the generation is stopped, the stream is closed using
ReadableStreamDefaultController.close()
, and another function is run, which reads the data back out of the stream.
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', function() {
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);
}
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
流
The definition of 'ReadableStream()' in that specification. |
实时标准 | 初始定义。 |
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
ReadableStream()
构造函数
|
Chrome 43 | Edge 79 |
Firefox
65
|
IE No | Opera 30 | Safari 10.1 | WebView Android 43 | Chrome Android 43 |
Firefox Android
65
|
Opera Android 30 | Safari iOS 10.3 | Samsung Internet Android 4.0 |
完整支持
不支持
实验。期望将来行为有所改变。
用户必须明确启用此特征。
ReadableStream
ReadableStream()
Body.body
ByteLengthQueuingStrategy
CountQueuingStrategy
ReadableByteStreamController
ReadableStreamBYOBReader
ReadableStreamBYOBRequest
ReadableStreamDefaultController
ReadableStreamDefaultReader
WindowOrWorkerGlobalScope.fetch()
WritableStream
WritableStreamDefaultController
WritableStreamDefaultWriter