barthap / discovering-turbomodules

A step by step example of creating your own React Native TurboModule.
MIT License
101 stars 5 forks source link

[Doubt] - Async Operations #3

Open EduFrazao opened 2 years ago

EduFrazao commented 2 years ago

Hi @barthap . Thank you very mutch for sharing your discoverys. I'm trying to contribute with a project that you mention in your repo (react-native-quick-sqlite), implementing some async operations, but I have some doubts and problems that I couldn't overcome yeat.

I need to use turbo modules in order to use callinvoker, or I can use it with a regular JSI Non Turbo Module implementation?

barthap commented 2 years ago

Yes, Call invoker is essential when doing async operations. You can also see the discussion in #1 for some examples (and more links) on async stuff in JSI

You don't need TurboModules to use call invoker. On iOS, your native module should inherit from RCTBridgeModule, then you can just do:

- (void)setBridge:(RCTBridge *)bridge {
  _bridge = bridge;
  RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
  jsi::Runtime* jsiRuntime = (jsi::Runtime *)cxxBridge.runtime;
  auto callInvoker = bridge.jsCallInvoker;

On Android, it is a bit more complex, you have there a com.facebook.react.turbomodule.core.CallInvokerHolderImpl class, which is a fbjni hybrid class. So you need the fbjni library to unwrap the call invoker on the C++ side.