From 5a302b2f93e4495f70a90279cc16798288238cf1 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Sun, 23 Jun 2024 12:12:43 +0200 Subject: [PATCH] Implement `install()` for Runtime dispatcher --- .../cpp/turbomodule/NativeNitroModules.cpp | 16 ++++++++++++++-- .../cpp/turbomodule/NativeNitroModules.hpp | 4 ++++ .../src/NativeNitro.ts | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp index a8a06c107..bca3c4bd3 100644 --- a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp +++ b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp @@ -7,15 +7,27 @@ #include "NativeNitroModules.hpp" #include "TestHybridObject.hpp" +#include "Dispatcher.hpp" +#include "CallInvokerDispatcher.hpp" namespace facebook::react { -NativeNitroModules::NativeNitroModules(std::shared_ptr jsInvoker) : NativeNitroCxxSpec(jsInvoker) { } +using namespace margelo; + +NativeNitroModules::NativeNitroModules(std::shared_ptr jsInvoker) : NativeNitroCxxSpec(jsInvoker), _callInvoker(jsInvoker) { +} NativeNitroModules::~NativeNitroModules() { } +void NativeNitroModules::install(jsi::Runtime& runtime) { + // Installs the global Dispatcher mechanism into this Runtime. + // This allows creating Promises and calling back to JS. + auto dispatcher = std::make_shared(_callInvoker); + Dispatcher::installRuntimeGlobalDispatcher(runtime, dispatcher); +} + jsi::Object NativeNitroModules::createTestHybridObject(jsi::Runtime &runtime) { - auto hybrid = std::make_shared(); + auto hybrid = std::make_shared(); return jsi::Object::createFromHostObject(runtime, hybrid); } diff --git a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.hpp b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.hpp index ba9d31ed8..afb0a2ee4 100644 --- a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.hpp +++ b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.hpp @@ -25,7 +25,11 @@ class NativeNitroModules : public NativeNitroCxxSpec { NativeNitroModules(std::shared_ptr jsInvoker); ~NativeNitroModules(); + void install(jsi::Runtime& runtime); jsi::Object createTestHybridObject(jsi::Runtime& runtime); + +private: + std::shared_ptr _callInvoker; }; } // namespace facebook::react diff --git a/packages/react-native-nitro-modules/src/NativeNitro.ts b/packages/react-native-nitro-modules/src/NativeNitro.ts index 4dbd68b05..21a34b17f 100644 --- a/packages/react-native-nitro-modules/src/NativeNitro.ts +++ b/packages/react-native-nitro-modules/src/NativeNitro.ts @@ -3,8 +3,10 @@ import { TurboModuleRegistry } from 'react-native'; import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes'; export interface Spec extends TurboModule { + install(): void; createTestHybridObject(): UnsafeObject; } export const NitroModules = TurboModuleRegistry.getEnforcing('NitroModulesCxx'); +NitroModules.install();