WritableStream()
The WritableStream()
constructor creates
a new WritableStream
object instance.
Syntax
new WritableStream(underlyingSink)
new WritableStream(underlyingSink, queuingStrategy)
Parameters
underlyingSink
optional: An object containing methods and properties that define how the constructed stream instance will behave.
underlyingSink
can contain the following:start(controller)
optional- : This is a method, called immediately when the object is constructed. The
contents of this method are defined by the developer, and should aim to get access
to the underlying sink. If this process is to be done asynchronously, it can
return a promise to signal success or failure. The
controller
parameter passed to this method is aWritableStreamDefaultController
. This can be used by the developer to control the stream during set up.
- : This is a method, called immediately when the object is constructed. The
contents of this method are defined by the developer, and should aim to get access
to the underlying sink. If this process is to be done asynchronously, it can
return a promise to signal success or failure. The
write(chunk, controller)
optional- : This method, also defined by the developer, will be called when a new chunk of
data (specified in the
chunk
parameter) is ready to be written to the underlying sink. It can return a promise to signal success or failure of the write operation. Thecontroller
parameter passed to this method is aWritableStreamDefaultController
that can be used by the developer to control the stream as more chunks are submitted for writing. This method will be called only after previous writes have succeeded, and never after the stream is closed or aborted (see below).
- : This method, also defined by the developer, will be called when a new chunk of
data (specified in the
close(controller)
optional- : This method, also defined by the developer, will be called if the app signals
that it has finished writing chunks to the stream. The contents should do whatever
is necessary to finalize writes to the underlying sink, and release access to it.
If this process is asynchronous, it can return a promise to signal success or
failure. This method will be called only after all queued-up writes have
succeeded. The
controller
parameter passed to this method is aWritableStreamDefaultController
, which can be used to control the stream at the end of writing.
- : This method, also defined by the developer, will be called if the app signals
that it has finished writing chunks to the stream. The contents should do whatever
is necessary to finalize writes to the underlying sink, and release access to it.
If this process is asynchronous, it can return a promise to signal success or
failure. This method will be called only after all queued-up writes have
succeeded. The
abort(reason)
optional- : This method, also defined by the developer, will be called if the app signals
that it wishes to abruptly close the stream and put it in an errored state. It can
clean up any held resources, much like
close()
, butabort()
will be called even if writes are queued up — those chunks will be thrown away. If this process is asynchronous, it can return a promise to signal success or failure. Thereason
parameter contains a string describing why the stream was aborted.
- : This method, also defined by the developer, will be called if the app signals
that it wishes to abruptly close the stream and put it in an errored state. It can
clean up any held resources, much like
queuingStrategy
optional: An object that optionally defines a queuing strategy for the stream. This takes two parameters:
highWaterMark
- : A non-negative integer — this defines the total number of chunks that can be contained in the internal queue before backpressure is applied.
size(chunk)
- : A method containing a parameter
chunk
— this indicates the size to use for each chunk, in bytes.
- : A method containing a parameter
Note: You could define your own custom
queuingStrategy
, or use an instance ofByteLengthQueuingStrategy
orCountQueuingStrategy
for this object value. If noqueuingStrategy
is supplied, the default used is the same as aCountQueuingStrategy
with a high water mark of 1.
Return value
An instance of the WritableStream
object.