Skip to content

Commit

Permalink
Add documentation for threadsafe option in FFI JSCallback (#15982)
Browse files Browse the repository at this point in the history
Co-authored-by: Don Isaac <[email protected]>
  • Loading branch information
citkane and DonIsaac authored Jan 8, 2025
1 parent 04b388e commit 84cf40b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/api/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ setTimeout(() => {

When you're done with a JSCallback, you should call `close()` to free the memory.

### Experimental thread-safe callbacks
`JSCallback` has experimental support for thread-safe callbacks. This will be needed if you pass a callback function into a different thread from it's instantiation context. You can enable it with the optional `threadsafe` option flag.
```ts
const searchIterator = new JSCallback(
(ptr, length) => /hello/.test(new CString(ptr, length)),
{
returns: "bool",
args: ["ptr", "usize"],
threadsafe: true, // Optional. Defaults to `false`
},
);
```
Be aware that there are still cases where this does not 100% work.

{% callout %}

**⚡️ Performance tip** — For a slight performance boost, directly pass `JSCallback.prototype.ptr` instead of the `JSCallback` object:
Expand Down

0 comments on commit 84cf40b

Please sign in to comment.