Skip to content

Commit

Permalink
Add WritableStream.write convenience method
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Jan 23, 2025
1 parent 2811932 commit aecfcfc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,7 @@ interface WritableStream {

readonly attribute boolean locked;

Promise<undefined> write(optional any chunk);
Promise<undefined> abort(optional any reason);
Promise<undefined> close();
WritableStreamDefaultWriter getWriter();
Expand Down Expand Up @@ -4270,6 +4271,17 @@ as seen for example in [[#example-ws-no-backpressure]].
<dd>
<p>Returns whether or not the writable stream is [=locked to a writer=].

<dt><code>await <var ignore>stream</var>.{{WritableStream/write(chunk)|write}}([ <var ignore>chunk</var> ])</code>
<dd>
<p>Writes a [=chunk=] of data to the stream just like the {{WritableStreamDefaultWriter/write()}}
method.

<p>This is a convenience method that first acquires a writer, then writes the chunk, and releases
the writer (regardless of the outcome of the write). The returned promise will fulfill if the
write was successful, or reject if the write failed. Additionally, it will reject with a
{{TypeError}} if the stream is currently [=locked to a writer|locked=] (without attempting to
write).

<dt><code>await <var ignore>stream</var>.{{WritableStream/abort(reason)|abort}}([ <var ignore>reason</var> ])</code>
<dd>
<p>[=abort a writable stream|Aborts=] the stream, signaling that the producer can no longer
Expand Down Expand Up @@ -4332,6 +4344,22 @@ as seen for example in [[#example-ws-no-backpressure]].
1. Return ! [$IsWritableStreamLocked$]([=this=]).
</div>

<div algorithm>
The <dfn id="ws-write" method for="WritableStream">write(|chunk|)</dfn> method steps are:

1. Let |result| be ? [$AcquireWritableStreamDefaultWriter$]([=this=]).
1. If |result| is an abrupt completion, return [=a promise rejected with=] |result|.\[[Value]].
1. Let |writer| be |result|.\[[Value]].
1. Let |writePromise| be ! [$WritableStreamDefaultWriterWrite$]([=this=], |chunk|).
1. Return the result of [=reacting=] to |writePromise|:
1. If |writePromise| is fulfilled with a value |v|, then:
1. Perform ! [$WritableStreamDefaultWriterRelease$]([=this=]).
1. Return |v|.
1. If |writePromise| is rejected with a value |r|, then:
1. Perform ! [$WritableStreamDefaultWriterRelease$]([=this=]).
1. Return [=a promise rejected with=] |r|.
</div>

<div algorithm>
The <dfn id="ws-abort" method for="WritableStream">abort(|reason|)</dfn> method steps are:

Expand Down

0 comments on commit aecfcfc

Please sign in to comment.