mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
8662d9d3b0
Summary: Some ObjC NativeModules conform to `RCTInvalidating` protocol and implements `invalidate` method. This is typically used to clean things up during bridge teardown or reload. In TurboModule system the invalidate method can be replaced by pure destructors, but for backward compatibility, we call existing invalidate method on each module during teardown. This also cleans up all existing LongLivedObject's. Reviewed By: mdvacca, RSNara Differential Revision: D15365655 fbshipit-source-id: 802844b39b5b7adb54970ea541f4d744bbf9e480
74 lines
2.0 KiB
C++
74 lines
2.0 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "TurboModuleBinding.h"
|
|
|
|
#include <string>
|
|
|
|
#include <cxxreact/SystraceSection.h>
|
|
#include <jsireact/LongLivedObject.h>
|
|
|
|
using namespace facebook;
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/**
|
|
* Public API to install the TurboModule system.
|
|
*/
|
|
TurboModuleBinding::TurboModuleBinding(const TurboModuleProviderFunctionType &moduleProvider)
|
|
: moduleProvider_(moduleProvider) {}
|
|
|
|
void TurboModuleBinding::install(
|
|
jsi::Runtime &runtime,
|
|
std::shared_ptr<TurboModuleBinding> binding) {
|
|
runtime.global().setProperty(
|
|
runtime,
|
|
"__turboModuleProxy",
|
|
jsi::Function::createFromHostFunction(
|
|
runtime,
|
|
jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"),
|
|
1,
|
|
[binding](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
|
|
return binding->jsProxy(rt, thisVal, args, count);
|
|
}));
|
|
}
|
|
|
|
void TurboModuleBinding::invalidate() const {
|
|
LongLivedObjectCollection::get().clear();
|
|
}
|
|
|
|
std::shared_ptr<TurboModule> TurboModuleBinding::getModule(const std::string &name) {
|
|
std::shared_ptr<TurboModule> module = nullptr;
|
|
{
|
|
SystraceSection s("TurboModuleBinding::getModule", "module", name);
|
|
module = moduleProvider_(name);
|
|
}
|
|
return module;
|
|
}
|
|
|
|
jsi::Value TurboModuleBinding::jsProxy(
|
|
jsi::Runtime& runtime,
|
|
const jsi::Value& thisVal,
|
|
const jsi::Value* args,
|
|
size_t count) {
|
|
if (count != 1) {
|
|
throw std::invalid_argument("TurboModuleBinding::jsProxy arg count must be 1");
|
|
}
|
|
std::string moduleName = args[0].getString(runtime).utf8(runtime);
|
|
std::shared_ptr<TurboModule> module = getModule(moduleName);
|
|
|
|
if (module == nullptr) {
|
|
return jsi::Value::null();
|
|
}
|
|
|
|
return jsi::Object::createFromHostObject(runtime, std::move(module));
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|