Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add NitroModules.box(...) to support using Nitro Modules from any Runtime/Worklets context #138

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

mrousavy
Copy link
Owner

While Nitro is runtime-agnostic and all HybridObjects can be used from a any Runtime, some threading/worklet libraries (like react-native-worklets-core) do not yet support copying over HybridObjects as they use newer JSI APIs like jsi::NativeState.

While those APIs are not yet available in react-native-worklets-core, you can still use every Nitro Hybrid Object in a separate Runtime/Worklet context by just boxing it yourself:

const something = NitroModules.createHybridObject<Something>('Something')
const boxed = NitroModules.box(something)
const context = Worklets.createContext('DummyContext')
context.runAsync(() => {
  'worklet'
  const unboxed = boxed.unbox()
  console.log(unboxed.name) // --> "Something"
})

The BoxedHybridObject<T> is implemented as a jsi::HostObject, which can be used from any runtime.

This is slower than just efficiently copying over the jsi::NativeState (plus it's prototype chain), so we'll work on implementing a truly native and automatic solution next - cc @hannojg @chrfalch.

@mrousavy mrousavy merged commit 1128d6a into main Sep 17, 2024
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant