Skip to content

Commit 7591948

Browse files
authored
Editorial changes to transferable streams explainer
* Make some language clearer. * Add more use cases (thanks to @MattiasBuelens). * Add a section about end-user benefit.
1 parent 0ebe4b0 commit 7591948

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

transferable-streams-explainer.md

+26-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
## Introduction
55

66
The streams APIs provide ubiquitous, interoperable primitives for creating, composing, and consuming streams of data. A
7-
natural thing to want to do with a stream is pass it off to a web worker. This provides a fluent primitive for
8-
offloading work onto another thread.
7+
natural thing to want to do with a stream is pass it off to a web worker. This provides a building block that is easy
8+
and natural to use for offloading work onto another thread. Once you can speak the language of streams, you can use that
9+
same language to make use of a Worker.
910

1011
This work will permit streams to be transferred between workers, frames and anywhere else that `postMessage()` can be
1112
used. Chunks can be anything which is cloneable by `postMessage()`.
1213

13-
Permit transferring a stream to other realms using `postMessage()`. Initially chunks enqueued in such a stream will
14-
always be cloned, ie. all data will be copied. Future work will extend the Streams APIs to support transferring objects
15-
(ie. zero copy).
14+
Initially chunks enqueued in such a stream will always be cloned, ie. all data will be copied. Future work will extend
15+
the Streams APIs to support transferring objects (ie. zero copy).
1616

1717
This is an example of JavaScript which will work once this is implemented:
1818

@@ -43,7 +43,10 @@ Note that `w.postMessage(rs)` would not work. Streams can only be _transferred_,
4343
stream will throw a DataCloneError.
4444

4545
Once a stream has been transferred with `postMessage()` the original stream is locked and cannot be read or written.
46-
This is similar to how ArrayBuffers are neutered after they are transferred.
46+
This is similar to how ArrayBuffers are neutered after they are transferred. However, the code of the underlying source
47+
or sink is still running in the original context. The benefits to user experience can be seen in [this demo of streaming
48+
digits of PI](https://glitch.com/edit/#!/streaming-pi?path=pi.js:1:0) (if you have a browser that supports transferable
49+
streams, you can see it live at https://streaming-pi.glitch.me/).
4750

4851
Transferable streams are also useful in constructing responses for a service worker. See
4952
https://gist.github.com/domenic/ea5ebedffcee27f552e103963cf8585c/ for an example.
@@ -66,7 +69,23 @@ https://gist.github.com/domenic/ea5ebedffcee27f552e103963cf8585c/ for an example
6669
## Use cases
6770

6871
* Performing expensive transformations off the main thread. Transcoding, for example.
69-
* Synthesizing responses from a service worker.
72+
* Synthesizing responses from a service worker. For example, generating a PDF from data in the DOM and streaming it to
73+
the service worker where it can then be downloaded as a file.
74+
* Processing a stream of data from an input device only accessible on the main thread. For example, you could use
75+
`MediaRecorder` to capture the audio of a user's microphone and/or the video of a user's camera, pipe the captured
76+
data through a off-thread `TransformStream` to transcode it into a hypothetical new experimental media format and
77+
then upload the resulting stream to a server.
78+
* Displaying a stream of data that is expensive to generate on a web page. Inverting the previous example: you
79+
download a stream of a video file encoded in an experimental media format, you transcode it to a natively supported
80+
format in a worker, and finally you transfer the resulting stream to the main thread to play back the video using
81+
`<video>` and `MediaSource`.
82+
83+
## End-user benefit
84+
85+
* By enabling developers to easily offload work onto other threads, this will increase the availability of responsive,
86+
stutter-free experiences to end users. For example, a page that transcoded video using a new, CPU-intensive codec
87+
could still respond snappily to user input by offloading the transcoding to another thread.
88+
* The power multiplier of streams and threads could unlock whole new applications.
7089

7190
## Alternatives
7291

0 commit comments

Comments
 (0)