Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WritableStream#write convenience method #1339

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved

1. Let |result| be ? [$AcquireWritableStreamDefaultWriter$]([=this=]).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth introducing a new abstract op WritableStreamWrite(stream, chunk) for doing a "writer-less write", similar to WritableStreamAbort and WritableStreamClose. We could then rework WritableStreamDefaultWriterWrite to use the same abstract op, similar to how WritableStreamDefaultWriterAbort delegates to WritableStreamAbort.

(We may have to take chunkSize as an extra argument though, since step 5 of WritableStreamDefaultWriterWrite might be difficult to implement otherwise.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that if I can get implementer interest :)

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$](|writer|, |chunk|).
1. Return the result of [=reacting=] to |writePromise|:
1. If |writePromise| is fulfilled with a value |v|, then:
1. Perform ! [$WritableStreamDefaultWriterRelease$](|writer|).
1. Return |v|.
1. If |writePromise| is rejected with a value |r|, then:
1. Perform ! [$WritableStreamDefaultWriterRelease$](|writer|).
1. Return [=a promise rejected with=] |r|.
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
</div>

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

Expand Down
Loading