Files
react-native/packages/react-native-test-library/ios/OSSLibraryExampleSpecJSI.h
T
Pieter De Baets 5b5e150eaf Fix Cxx TurboModule methods not being cached (#46751)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46751

rubennorte noticed that some turbo module methods were not getting cached on the jsRepresentation as expected. This is caused by the react-native-codegen wrappers we generate overriding the `get` method and bypassing this caching layer. Instead they should be overriding `create` to lazily allocate new properties. To prevent this from re-occuring, I've made `get` final so no other overrides can happen.

## Changelog:

[General][Fixed] Properties for C++ TurboModules using codegen were not correctly cached
[General][Breaking] TurboModule::get is now a final method, override `create` to customize property lookup

Reviewed By: rubennorte

Differential Revision: D63665966

fbshipit-source-id: c614901c2f0d698147ec9ba36a4b592ed3d37652
2024-10-02 05:10:33 -07:00

68 lines
1.8 KiB
C++

/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GenerateModuleH.js
*/
#pragma once
#include <ReactCommon/TurboModule.h>
#include <react/bridging/Bridging.h>
namespace facebook::react {
class JSI_EXPORT NativeSampleModuleCxxSpecJSI : public TurboModule {
protected:
NativeSampleModuleCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker);
public:
virtual double getRandomNumber(jsi::Runtime &rt) = 0;
};
template <typename T>
class JSI_EXPORT NativeSampleModuleCxxSpec : public TurboModule {
public:
jsi::Value create(jsi::Runtime &rt, const jsi::PropNameID &propName) override {
return delegate_.create(rt, propName);
}
static constexpr std::string_view kModuleName = "NativeSampleModule";
protected:
NativeSampleModuleCxxSpec(std::shared_ptr<CallInvoker> jsInvoker)
: TurboModule(std::string{NativeSampleModuleCxxSpec::kModuleName}, jsInvoker),
delegate_(reinterpret_cast<T*>(this), jsInvoker) {}
private:
class Delegate : public NativeSampleModuleCxxSpecJSI {
public:
Delegate(T *instance, std::shared_ptr<CallInvoker> jsInvoker) :
NativeSampleModuleCxxSpecJSI(std::move(jsInvoker)), instance_(instance) {
}
double getRandomNumber(jsi::Runtime &rt) override {
static_assert(
bridging::getParameterCount(&T::getRandomNumber) == 1,
"Expected getRandomNumber(...) to have 1 parameters");
return bridging::callFromJs<double>(
rt, &T::getRandomNumber, jsInvoker_, instance_);
}
private:
friend class NativeSampleModuleCxxSpec;
T *instance_;
};
Delegate delegate_;
};
} // namespace facebook::react