Skip to content

Commit 0fa4356

Browse files
authored
Document how to use transferables with observables (#250)
1 parent 73c9daa commit 0fa4356

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

docs/usage-advanced.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ expose(function xorBuffer(buffer, value) {
4040

4141
Without `Transfer()` the buffers would be copied on every call and every return. Using `Transfer()` their ownership is transferred to the other thread instead only, to make sure it is accessible in a thread-safe way. This is a much faster operation.
4242

43+
You can use transferable objects with observables, too.
44+
45+
```js
46+
import { expose, Observable, Transfer } from "threads/worker"
47+
import { DataSource } from "./my-data-source"
48+
49+
expose(function streamBuffers() {
50+
return new Observable(observer => {
51+
const datasource = new DataSource()
52+
datasource.on("data", arrayBuffer => observer.next(Transfer(arrayBuffer)))
53+
return () => datasource.close()
54+
})
55+
})
56+
```
57+
4358
## Task queue
4459

4560
It is a fairly common use case to have a lot of work that needs to be done by workers, but is just too much to be run efficiently at once. You will need to schedule tasks and have them dispatched and run on workers in a controlled fashion.

0 commit comments

Comments
 (0)