Skip to content

Commit

Permalink
withTransferable
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Mar 11, 2024
1 parent d939e8f commit 9553554
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/postmessage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ const result = await workerApi.runScript({ script})
// result is now known to be ScriptResponse and you get autocomplete
```




## transferable

It is simple to send transferable objects when sending a message to the worker by adding a parameter to `postMessage`.
It is however more tricky to support transferable for return values without complicating simple use cases that do not need transferable.

If you have a method that can be called and it needs to return transferable then you must use object as a return value.
When returning such object, include `__transferable` key in the return value. It will not be in the data at the receiving end but will be taken out and passed to postMessage as a transferable parameter.
When returning such object in call to `withTransferable` before returning the value.
It will not be in the data at the receiving end, but will be taken out and passed to postMessage as the transferable parameter.

```js
return withTransferable({ entities, mainTime }, transferable)
```

0 comments on commit 9553554

Please sign in to comment.