mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This provides various versions of SampleTurboModule, that are: * compatible with existing NativeModule * TurboModule compliant Variants: * RCTSampleTurboModule (traditional objc module) * RCTSampleTurboCxxModule (objc++ module using CxxModule) * SampleTurboModule (pure C++ impl of a TurboModule, no ObjC) As noted in some files, they need to be codegen'ed based on the `NativeSampleTurboModule.js` (Flow type). The codegen script is not yet usable in OSS (we'll work on it some time in H2 2019). For now, these files need to be manually synced with Flow type. Reviewed By: cpojer Differential Revision: D14932539 fbshipit-source-id: fb887192384e5e6e4dff4cac68b4e037a4783cd9
141 lines
3.8 KiB
C++
141 lines
3.8 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 "SampleTurboCxxModuleLegacyImpl.h"
|
|
|
|
#include <cxxreact/JsArgumentHelpers.h>
|
|
|
|
using namespace facebook::xplat::module;
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
SampleTurboCxxModuleLegacyImpl::SampleTurboCxxModuleLegacyImpl() {}
|
|
|
|
std::string SampleTurboCxxModuleLegacyImpl::getName() {
|
|
return "SampleTurboCxxModule_v2";
|
|
}
|
|
|
|
std::map<std::string, folly::dynamic> SampleTurboCxxModuleLegacyImpl::getConstants() {
|
|
return {
|
|
{"const1", true},
|
|
{"const2", 375},
|
|
{"const3", "something"},
|
|
};
|
|
};
|
|
|
|
std::vector<CxxModule::Method> SampleTurboCxxModuleLegacyImpl::getMethods() {
|
|
return {
|
|
CxxModule::Method(
|
|
"voidFunc",
|
|
[this](folly::dynamic args) {
|
|
voidFunc();
|
|
}),
|
|
CxxModule::Method(
|
|
"getBool",
|
|
[this](folly::dynamic args) {
|
|
return getBool(xplat::jsArgAsBool(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getNumber",
|
|
[this](folly::dynamic args) {
|
|
return getNumber(xplat::jsArgAsDouble(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getString",
|
|
[this](folly::dynamic args) {
|
|
return getString(xplat::jsArgAsString(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getString",
|
|
[this](folly::dynamic args) {
|
|
return getString(xplat::jsArgAsString(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getArray",
|
|
[this](folly::dynamic args) {
|
|
return getArray(xplat::jsArgAsArray(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getObject",
|
|
[this](folly::dynamic args) {
|
|
return getObject(xplat::jsArgAsObject(args, 0));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getValue",
|
|
[this](folly::dynamic args) {
|
|
return getValue(
|
|
xplat::jsArgAsDouble(args, 0),
|
|
xplat::jsArgAsString(args, 1),
|
|
xplat::jsArgAsObject(args, 2));
|
|
},
|
|
CxxModule::SyncTag),
|
|
CxxModule::Method(
|
|
"getValueWithCallback",
|
|
[this](folly::dynamic args, CxxModule::Callback callback) {
|
|
getValueWithCallback(callback);
|
|
}),
|
|
CxxModule::Method(
|
|
"getValueWithPromise",
|
|
[this](folly::dynamic args, CxxModule::Callback resolve, CxxModule::Callback reject) {
|
|
getValueWithPromise(xplat::jsArgAsBool(args, 0), resolve, reject);
|
|
}),
|
|
};
|
|
};
|
|
|
|
void SampleTurboCxxModuleLegacyImpl::voidFunc() {
|
|
// Do nothing.
|
|
}
|
|
|
|
bool SampleTurboCxxModuleLegacyImpl::getBool(bool arg) {
|
|
return arg;
|
|
}
|
|
|
|
double SampleTurboCxxModuleLegacyImpl::getNumber(double arg) {
|
|
return arg;
|
|
}
|
|
|
|
std::string SampleTurboCxxModuleLegacyImpl::getString(const std::string &arg) {
|
|
return arg;
|
|
}
|
|
|
|
folly::dynamic SampleTurboCxxModuleLegacyImpl::getArray(const folly::dynamic &arg) {
|
|
return arg;
|
|
}
|
|
|
|
folly::dynamic SampleTurboCxxModuleLegacyImpl::getObject(const folly::dynamic &arg) {
|
|
return arg;
|
|
}
|
|
|
|
folly::dynamic SampleTurboCxxModuleLegacyImpl::getValue(double x, const std::string &y, const folly::dynamic &z) {
|
|
return folly::dynamic::object
|
|
("x", x)
|
|
("y", y)
|
|
("z", z);
|
|
}
|
|
|
|
void SampleTurboCxxModuleLegacyImpl::getValueWithCallback(const CxxModule::Callback &callback) {
|
|
callback({"value from callback!"});
|
|
}
|
|
|
|
void SampleTurboCxxModuleLegacyImpl::getValueWithPromise(bool error, const CxxModule::Callback &resolve, const CxxModule::Callback &reject) {
|
|
if (!error) {
|
|
resolve({"result!"});
|
|
} else {
|
|
reject({folly::dynamic::object("message", "intentional promise rejection")});
|
|
}
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|