Skip to content

Commit

Permalink
Listen for AbortSignal in WritableStream algorithms (#160)
Browse files Browse the repository at this point in the history
When controller.signal is aborted, abort write operations so that the stream can close more quickly.

Resolves #152.
  • Loading branch information
reillyeon authored Feb 10, 2022
1 parent ce90c45 commit 881d4b9
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -854,9 +854,12 @@ <h3><dfn>writable</dfn> attribute</h3>
<li>If [=this=].{{SerialPort/[[state]]}} is not `"opened"`, return `null`.
<li>If [=this=].{{SerialPort/[[writeFatal]]}} is `true`, return `null`.
<li>Let |stream:WritableStream| be a [=new=] {{WritableStream}}.
<li>Let |writeAlgorithm| be the following steps, given |chunk|:
<li>Let |signal:AbortSignal| be |stream|'s [=WritableStream/signal=].
<li>
Let |writeAlgorithm| be the following steps, given |chunk|:
<ol>
<li>Let |promise:Promise| be [=a new promise=].
<li>Assert: |signal| is not [=AbortSignal/aborted=].
<li>
If |chunk| cannot be [=converted to an IDL value=] of type
{{BufferSource}}, reject |promise| with a {{TypeError}} and return
Expand Down Expand Up @@ -909,6 +912,9 @@ <h3><dfn>writable</dfn> attribute</h3>
Invoke the steps to [=handle closing the writable
stream=].
</ol>
<li>
If |signal| is [=AbortSignal/aborted=], [=reject=] |promise|
with |signal|'s [=AbortSignal/abort reason=].
</ol>
</ol>
<li>Return |promise|.
Expand All @@ -935,18 +941,6 @@ <h3><dfn>writable</dfn> attribute</h3>
<li>Return |promise|.
</ol>

<div class="note">
[[STREAMS]] specifies that |abortAlgorithm| will only be invoked after
the {{Promise}} returned by a previous invocation of |writeAlgorithm|
(if any) has resolved. This blocks abort on completion of the most
recent write operation. This could be fixed by passing an
{{AbortSignal}} to |writeAlgorithm|.

<p>
This enhancement is tracked in
<a href="https://github.com/whatwg/streams/issues/1015">whatwg/streams#1015</a>.
</div>

<li>
Let |closeAlgorithm| be the following steps:
<ol>
Expand All @@ -963,12 +957,14 @@ <h3><dfn>writable</dfn> attribute</h3>
<ol>
<li>
Invoke the steps to [=handle closing the writable stream=].
<li>[=Resolve=] |promise| with `undefined`.
<li>
If |signal| is [=AbortSignal/aborted=], [=reject=] |promise|
with |signal|'s [=AbortSignal/abort reason=].
<li>Otherwise, [=resolve=] |promise| with `undefined`.
</ol>
</ol>
<li>Return |promise|.
</ol>

<li>
[=WritableStream/Set up=] |stream| with
<a href="https://streams.spec.whatwg.org/#writablestream-set-up-writealgorithm">writeAlgorithm</a>
Expand All @@ -981,6 +977,14 @@ <h3><dfn>writable</dfn> attribute</h3>
set to [=this=].{{SerialPort/[[bufferSize]]}}, and
<a href="https://streams.spec.whatwg.org/#writablestream-set-up-sizealgorithm">sizeAlgorithm</a>
set to a byte-counting size algorithm.
<li>
[=AbortSignal/Add=] the following abort steps to |signal|:
<ol>
<li>
Cause any invocation of the operating system to write to the
port to return as soon as possible no matter how much data has
been written.
</ol>
<li>Set [=this=].{{SerialPort/[[writable]]}} to |stream|.
<li>Return |stream|.
</ol>
Expand Down

0 comments on commit 881d4b9

Please sign in to comment.