Re: [whatwg/streams] Add WritableStream.write convenience method (PR #1339)

@MattiasBuelens commented on this pull request.



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

We don't need to wait for the `writePromise` to resolve or reject before releasing the lock. Instead, we should release it *immediately*.
```suggestion
 1. Let |writePromise| be ! [$WritableStreamDefaultWriterWrite$](|writer|, |chunk|).
 1. Perform ! [$WritableStreamDefaultWriterRelease$](|writer|).
 1. Return |writePromise|.
```
This works, because [WritableStreamDefaultWriterRelease](https://streams.spec.whatwg.org/#writable-stream-default-writer-release) does not reject any pending write requests.

This also resolves https://github.com/whatwg/streams/pull/1339#issuecomment-2611228239.

> @@ -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=]).

It might be worth introducing a new abstract op `WritableStreamWrite(stream, chunk)` for doing a "writer-less write", similar to [`WritableStreamAbort`](https://streams.spec.whatwg.org/#writable-stream-abort) and [`WritableStreamClose`](https://streams.spec.whatwg.org/#writable-stream-close). We could then rework [`WritableStreamDefaultWriterWrite`](https://streams.spec.whatwg.org/#writable-stream-default-writer-write) to use the same abstract op, similar to how [`WritableStreamDefaultWriterAbort`](https://streams.spec.whatwg.org/#writable-stream-default-writer-abort) delegates to `WritableStreamAbort`.

(We may have to take `chunkSize` as an extra argument though, since [step 5 of `WritableStreamDefaultWriterWrite`](https://streams.spec.whatwg.org/commit-snapshots/2811932a34b5cfdb0a6e33ad204f747ee9c71292/#writable-stream-default-writer-write) might be difficult to implement otherwise.)

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/1339#pullrequestreview-2572175799
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/streams/pull/1339/review/2572175799@github.com>

Received on Friday, 24 January 2025 09:55:05 UTC