Skip to content

Commit 87df5e2

Browse files
authored
Merge pull request #434 from jan-ivar/awaitready
Update examples to not await write(bytes)
2 parents f2fe7de + d914daf commit 87df5e2

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

index.bs

+28-19
Original file line numberDiff line numberDiff line change
@@ -2095,9 +2095,9 @@ following example datagrams are only sent if the transport is ready to send.
20952095
async function sendDatagrams(url, datagrams) {
20962096
const wt = new WebTransport(url);
20972097
const writer = wt.datagrams.writable.getWriter();
2098-
for (const datagram of datagrams) {
2098+
for (const bytes of datagrams) {
20992099
await writer.ready;
2100-
writer.write(datagram).catch(() => {});
2100+
writer.write(bytes).catch(() => {});
21012101
}
21022102
}
21032103
</pre>
@@ -2117,8 +2117,8 @@ async function sendFixedRate(url, createDatagram, ms = 100) {
21172117
const wt = new WebTransport(url);
21182118
await wt.ready;
21192119
const writer = wt.datagrams.writable.getWriter();
2120-
const datagram = createDatagram();
2121-
setInterval(() => writer.write(datagram).catch(() => {}), ms);
2120+
const bytes = createDatagram();
2121+
setInterval(() => writer.write(bytes).catch(() => {}), ms);
21222122
}
21232123
</pre>
21242124

@@ -2148,14 +2148,22 @@ Sending data as a one-way stream can be achieved by using the
21482148
{{WebTransport/createUnidirectionalStream}} function and the resulting stream's writer.
21492149

21502150
<pre class="example" highlight="js">
2151-
async function sendData(url, data) {
2151+
async function sendData(url, ...data) {
21522152
const wt = new WebTransport(url);
21532153
const writable = await wt.createUnidirectionalStream();
21542154
const writer = writable.getWriter();
2155-
await writer.write(data);
2155+
for (const bytes of data) {
2156+
await writer.ready;
2157+
writer.write(bytes).catch(() => {});
2158+
}
21562159
await writer.close();
21572160
}
21582161
</pre>
2162+
<div class="note">
2163+
<p>The streams spec
2164+
[discourages](https://streams.spec.whatwg.org/#example-manual-write-dont-await)
2165+
awaiting the promise from write().</p>
2166+
</div>
21592167

21602168
Encoding can also be done through pipes from a {{ReadableStream}}, for example using
21612169
{{TextEncoderStream}}.
@@ -2182,11 +2190,11 @@ and then consuming each {{WebTransportReceiveStream}} by iterating over its chun
21822190
async function receiveData(url, processTheData) {
21832191
const wt = new WebTransport(url);
21842192
for await (const readable of wt.incomingUnidirectionalStreams) {
2185-
// consume streams individually, reporting per-stream errors
2193+
// consume streams individually using IFFEs, reporting per-stream errors
21862194
((async () => {
21872195
try {
2188-
for await (const chunk of readable) {
2189-
processTheData(chunk);
2196+
for await (const bytes of readable) {
2197+
processTheData(bytes);
21902198
}
21912199
} catch (e) {
21922200
console.error(e);
@@ -2257,21 +2265,22 @@ connect.onclick = async () => {
22572265

22582266
sendData.onclick = async () => {
22592267
const form = document.forms.sending.elements;
2260-
const rawData = sending.data.value;
2261-
const data = new TextEncoder('utf-8').encode(rawData);
2268+
const data = sending.data.value;
2269+
const bytes = new TextEncoder('utf-8').encode(data);
22622270
try {
22632271
switch (form.sendtype.value) {
22642272
case 'datagram': {
2265-
await datagramWriter.write(data);
2266-
addToEventLog(&#96;Sent datagram: ${rawData}&#96;);
2273+
await datagramWriter.ready;
2274+
datagramWriter.write(bytes).catch(() => {});
2275+
addToEventLog(&#96;Sent datagram: ${data}&#96;);
22672276
break;
22682277
}
22692278
case 'unidi': {
22702279
const writable = await wt.createUnidirectionalStream();
22712280
const writer = writable.getWriter();
2272-
await writer.write(data);
2281+
writer.write(bytes).catch(() => {});
22732282
await writer.close();
2274-
addToEventLog(&#96;Sent a unidirectional stream with data: ${rawData}&#96;);
2283+
addToEventLog(&#96;Sent a unidirectional stream with data: ${data}&#96;);
22752284
break;
22762285
}
22772286
case 'bidi': {
@@ -2280,9 +2289,9 @@ sendData.onclick = async () => {
22802289
readFromIncomingStream(duplexStream.readable, n);
22812290

22822291
const writer = duplexStream.writable.getWriter();
2283-
await writer.write(data);
2292+
writer.write(bytes).catch(() => {});
22842293
await writer.close();
2285-
addToEventLog(&#96;Sent bidirectional stream #${n} with data: ${rawData}&#96;);
2294+
addToEventLog(&#96;Sent bidirectional stream #${n} with data: ${data}&#96;);
22862295
break;
22872296
}
22882297
}
@@ -2321,8 +2330,8 @@ async function acceptUnidirectionalStreams() {
23212330
async function readFromIncomingStream(readable, number) {
23222331
try {
23232332
const decoder = new TextDecoderStream('utf-8');
2324-
for await (const chunk of readable.pipeThrough(decoder)) {
2325-
addToEventLog(&#96;Received data on stream #${number}: ${chunk}&#96;);
2333+
for await (const data of readable.pipeThrough(decoder)) {
2334+
addToEventLog(&#96;Received data on stream #${number}: ${data}&#96;);
23262335
}
23272336
addToEventLog(&#96;Stream #${number} closed&#96;);
23282337
} catch (e) {

0 commit comments

Comments
 (0)