mirror of
https://github.com/zoriya/nitro.git
synced 2026-10-13 00:05:23 +00:00
50 lines
1.6 KiB
C++
50 lines
1.6 KiB
C++
//
|
|
// NativeNitroModules.cpp
|
|
// NitroModules
|
|
//
|
|
// Created by Marc Rousavy on 21.06.24.
|
|
//
|
|
|
|
#include "NativeNitroModules.hpp"
|
|
#include "TestHybridObject.hpp"
|
|
#include "SwiftTestHybridObject.hpp"
|
|
#include "Dispatcher.hpp"
|
|
#include "CallInvokerDispatcher.hpp"
|
|
|
|
namespace facebook::react {
|
|
|
|
using namespace margelo;
|
|
|
|
NativeNitroModules::NativeNitroModules(std::shared_ptr<CallInvoker> 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<CallInvokerDispatcher>(_callInvoker);
|
|
Dispatcher::installRuntimeGlobalDispatcher(runtime, dispatcher);
|
|
}
|
|
|
|
jsi::Object NativeNitroModules::createTestHybridObject(jsi::Runtime &runtime) {
|
|
auto hybrid = std::make_shared<TestHybridObject>();
|
|
return jsi::Object::createFromHostObject(runtime, hybrid);
|
|
}
|
|
|
|
jsi::Object NativeNitroModules::createSwiftTestHybridObject(jsi::Runtime &runtime) {
|
|
// 1. Create Swift instance
|
|
auto swiftPart = NitroModules::SwiftTestHybridObjectSwift::init();
|
|
// 2. Create C++ HybridObject instance wrapping Swift instance
|
|
auto hybrid = std::make_shared<SwiftTestHybridObject>(swiftPart);
|
|
// 3. Pass the Swift instance a weak_ptr to the C++ instance so we can cache it
|
|
JSContext context {
|
|
.cppPart = hybrid
|
|
};
|
|
swiftPart.setJsContext(swift::Optional<JSContext>::some(context));
|
|
// 4. Return jsi::Object to JS
|
|
return jsi::Object::createFromHostObject(runtime, hybrid);
|
|
}
|
|
|
|
}
|