From aecfcfc2bda01727d23528b09fd57cb86e10939b Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 24 Jan 2025 00:21:45 +0100 Subject: [PATCH 1/3] Add WritableStream.write convenience method --- index.bs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/index.bs b/index.bs index 43149ff9..3f16db20 100644 --- a/index.bs +++ b/index.bs @@ -4064,6 +4064,7 @@ interface WritableStream { readonly attribute boolean locked; + Promise write(optional any chunk); Promise abort(optional any reason); Promise close(); WritableStreamDefaultWriter getWriter(); @@ -4270,6 +4271,17 @@ as seen for example in [[#example-ws-no-backpressure]].

Returns whether or not the writable stream is [=locked to a writer=]. +

await stream.{{WritableStream/write(chunk)|write}}([ chunk ]) +
+

Writes a [=chunk=] of data to the stream just like the {{WritableStreamDefaultWriter/write()}} + method. + +

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). +

await stream.{{WritableStream/abort(reason)|abort}}([ reason ])

[=abort a writable stream|Aborts=] the stream, signaling that the producer can no longer @@ -4332,6 +4344,22 @@ as seen for example in [[#example-ws-no-backpressure]]. 1. Return ! [$IsWritableStreamLocked$]([=this=]). +

+ The write(|chunk|) 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|. +
+
The abort(|reason|) method steps are: From da646b81de986facecadaec953cc9baa7cb81277 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 24 Jan 2025 00:27:52 +0100 Subject: [PATCH 2/3] fix --- index.bs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.bs b/index.bs index 3f16db20..f429bf4c 100644 --- a/index.bs +++ b/index.bs @@ -4350,13 +4350,13 @@ as seen for example in [[#example-ws-no-backpressure]]. 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. 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$]([=this=]). + 1. Perform ! [$WritableStreamDefaultWriterRelease$](|writer|). 1. Return |v|. 1. If |writePromise| is rejected with a value |r|, then: - 1. Perform ! [$WritableStreamDefaultWriterRelease$]([=this=]). + 1. Perform ! [$WritableStreamDefaultWriterRelease$](|writer|). 1. Return [=a promise rejected with=] |r|.
From 8368fd8a32e4c00c196830d5eb99aa16d1eddd06 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 24 Jan 2025 11:20:51 +0100 Subject: [PATCH 3/3] fix --- index.bs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/index.bs b/index.bs index f429bf4c..550da0ca 100644 --- a/index.bs +++ b/index.bs @@ -4276,11 +4276,11 @@ as seen for example in [[#example-ws-no-backpressure]].

Writes a [=chunk=] of data to the stream just like the {{WritableStreamDefaultWriter/write()}} method. -

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). +

This is a convenience method that first acquires a writer, queues the chunk to be written, + releases the writer (without waiting on the write to complete), and returns a promise that + fulfills if the write was successful, or reject if the write failed. Additionally, if the stream + is currently [=locked to a writer|locked=], a promise rejecting with a {{TypeError}} is returned, + and no attempt to write will be made.

await stream.{{WritableStream/abort(reason)|abort}}([ reason ])
@@ -4351,13 +4351,8 @@ as seen for example in [[#example-ws-no-backpressure]]. 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|. + 1. Perform ! [$WritableStreamDefaultWriterRelease$](|writer|). + 1. Return |writePromise|.