Skip to content

Commit

Permalink
Update thread-safe documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
citkane committed Jan 7, 2025
1 parent b11cac0 commit 253c3ec
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/api/ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ const searchIterator = new JSCallback(
{
returns: "bool",
args: ["ptr", "usize"],
threadsafe: true // Optional. Defaults to `false`
},
);

Expand All @@ -298,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 253c3ec

Please sign in to comment.