Files
react-native/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h
T
Christoph Purrer d07575b1c6 react-native code-gen > Add a C++ only TurboModule example (for Android/iOS/macOS/Windows) (#35138)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35138

Changelog:

[General][Added] - Add a C++ only TurboModule example (for Android/iOS/macOS/Windows)

react-native@0.69 introduced a new bridging layer to ease integration for pure C++ TurboModules using C++ std:: types directly instead of the lower level jsi:: types:
https://github.com/facebook/react-native/tree/v0.69.0/ReactCommon/react/bridging

This bridging layer can be used in JSI functions or more conveniently in C++ TurboModules.

Here is a example of an C++ only TurboModule which will work on Android and iOS and macOS/Windows (using microsoft/react-native-macos|windows) only using flow/TypeScript and standard C++ types.

C++ only TurboModules are very handy as they do not require to work with JSI APIs - instead std:: or custom C++ can by used.

Reviewed By: javache

Differential Revision: D39011736

fbshipit-source-id: 84c833d8540671fde8505f1aeb0265074b248730
2022-11-09 10:48:49 -08:00

69 lines
1.9 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.
*/
#pragma once
#if __has_include(<React-Codegen/AppSpecsJSI.h>) // CocoaPod headers on Apple
#include <React-Codegen/AppSpecsJSI.h>
#elif __has_include("AppSpecsJSI.h") // Cmake headers on Android
#include "AppSpecsJSI.h"
#else // BUCK headers
#include <AppSpecs/AppSpecsJSI.h>
#endif
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "NativeCxxModuleExample_ConstantsStruct.h"
#include "NativeCxxModuleExample_ObjectStruct.h"
#include "NativeCxxModuleExample_ValueStruct.h"
namespace facebook::react {
class NativeCxxModuleExample
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
public:
NativeCxxModuleExample(std::shared_ptr<CallInvoker> jsInvoker);
void getValueWithCallback(
jsi::Runtime &rt,
AsyncCallback<std::string> callback);
std::vector<std::optional<ObjectStruct>> getArray(
jsi::Runtime &rt,
std::vector<std::optional<ObjectStruct>> arg);
bool getBool(jsi::Runtime &rt, bool arg);
ConstantsStruct getConstants(jsi::Runtime &rt);
int32_t getEnum(jsi::Runtime &rt, int32_t arg);
std::map<std::string, std::optional<int32_t>> getMap(
jsi::Runtime &rt,
std::map<std::string, std::optional<int32_t>> arg);
double getNumber(jsi::Runtime &rt, double arg);
ObjectStruct getObject(jsi::Runtime &rt, ObjectStruct arg);
std::set<float> getSet(jsi::Runtime &rt, std::set<float> arg);
std::string getString(jsi::Runtime &rt, std::string arg);
std::string getUnion(jsi::Runtime &rt, float x, std::string y, jsi::Object z);
ValueStruct
getValue(jsi::Runtime &rt, double x, std::string y, ObjectStruct z);
AsyncPromise<std::string> getValueWithPromise(jsi::Runtime &rt, bool error);
void voidFunc(jsi::Runtime &rt);
};
} // namespace facebook::react