mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: A previous version of this experiment saw crashes on iOS once rolled out. Switching to WeakObject to make sure we're not accessing invalid references, and setting up the ability to experiment with this on iOS. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D41552667 fbshipit-source-id: dc0c54edc2ad18c1947941119ffd50038a47c5f6
66 lines
2.0 KiB
C++
66 lines
2.0 KiB
C++
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "TurboModule.h"
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
TurboModule::TurboModule(
|
|
std::string name,
|
|
std::shared_ptr<CallInvoker> jsInvoker)
|
|
: name_(std::move(name)), jsInvoker_(std::move(jsInvoker)) {}
|
|
|
|
jsi::Value TurboModule::get(
|
|
jsi::Runtime &runtime,
|
|
const jsi::PropNameID &propName,
|
|
const MethodMetadata &meta) {
|
|
auto result = jsi::Function::createFromHostFunction(
|
|
runtime,
|
|
propName,
|
|
static_cast<unsigned int>(meta.argCount),
|
|
[this, meta](
|
|
jsi::Runtime &rt,
|
|
const jsi::Value &thisVal,
|
|
const jsi::Value *args,
|
|
size_t count) { return meta.invoker(rt, *this, args, count); });
|
|
// If we have a JS wrapper, cache the result of this lookup
|
|
// We don't cache misses, to allow for methodMap_ to dynamically be extended
|
|
if (jsRepresentation_) {
|
|
jsRepresentation_->lock(runtime).asObject(runtime).setProperty(
|
|
runtime, propName, result);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void TurboModule::emitDeviceEvent(
|
|
jsi::Runtime &runtime,
|
|
const std::string &eventName,
|
|
ArgFactory argFactory) {
|
|
jsInvoker_->invokeAsync([&runtime, eventName, argFactory]() {
|
|
jsi::Value emitter =
|
|
runtime.global().getProperty(runtime, "__rctDeviceEventEmitter");
|
|
if (!emitter.isUndefined()) {
|
|
jsi::Object emitterObject = emitter.asObject(runtime);
|
|
// TODO: consider caching these
|
|
jsi::Function emitFunction =
|
|
emitterObject.getPropertyAsFunction(runtime, "emit");
|
|
std::vector<jsi::Value> args;
|
|
args.emplace_back(
|
|
jsi::String::createFromAscii(runtime, eventName.c_str()));
|
|
if (argFactory) {
|
|
argFactory(runtime, args);
|
|
}
|
|
emitFunction.callWithThis(
|
|
runtime, emitterObject, (const jsi::Value *)args.data(), args.size());
|
|
}
|
|
});
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|