4
4
## Introduction
5
5
6
6
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.
9
10
10
11
This work will permit streams to be transferred between workers, frames and anywhere else that ` postMessage() ` can be
11
12
used. Chunks can be anything which is cloneable by ` postMessage() ` .
12
13
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).
16
16
17
17
This is an example of JavaScript which will work once this is implemented:
18
18
@@ -43,7 +43,10 @@ Note that `w.postMessage(rs)` would not work. Streams can only be _transferred_,
43
43
stream will throw a DataCloneError.
44
44
45
45
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/ ).
47
50
48
51
Transferable streams are also useful in constructing responses for a service worker. See
49
52
https://gist.github.com/domenic/ea5ebedffcee27f552e103963cf8585c/ for an example.
@@ -66,7 +69,23 @@ https://gist.github.com/domenic/ea5ebedffcee27f552e103963cf8585c/ for an example
66
69
## Use cases
67
70
68
71
* 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.
70
89
71
90
## Alternatives
72
91
0 commit comments