mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add enum example to Android/iOS rn-tester TurboModule
Summary: Add enum example to Android/iOS rn-tester TurboModule Changelog: [General][Added] - Add enum example to Android/iOS rn-tester TurboModule Reviewed By: javache Differential Revision: D40619020 fbshipit-source-id: a113c5c78bcff3275e80d11bce8d0e6421b0b97d
This commit is contained in:
committed by
Facebook GitHub Bot
parent
fd91748146
commit
02e4fcd825
@@ -30,6 +30,7 @@ flow/
|
||||
|
||||
[options]
|
||||
emoji=true
|
||||
enums=true
|
||||
|
||||
exact_by_default=true
|
||||
exact_empty_objects=true
|
||||
|
||||
@@ -30,6 +30,7 @@ flow/
|
||||
|
||||
[options]
|
||||
emoji=true
|
||||
enums=true
|
||||
|
||||
exact_by_default=true
|
||||
exact_empty_objects=true
|
||||
|
||||
@@ -743,6 +743,7 @@ rn_library(
|
||||
"//xplat/js:node_modules__anser",
|
||||
"//xplat/js:node_modules__base64_19js",
|
||||
"//xplat/js:node_modules__event_19target_19shim",
|
||||
"//xplat/js:node_modules__flow_19enums_19runtime",
|
||||
"//xplat/js:node_modules__invariant",
|
||||
"//xplat/js:node_modules__memoize_19one",
|
||||
"//xplat/js:node_modules__nullthrows",
|
||||
|
||||
@@ -13,6 +13,11 @@ import type {RootTag, TurboModule} from '../RCTExport';
|
||||
|
||||
import * as TurboModuleRegistry from '../TurboModuleRegistry';
|
||||
|
||||
export enum EnumInt {
|
||||
A = 23,
|
||||
B = 42,
|
||||
}
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
// Exported methods.
|
||||
+getConstants: () => {|
|
||||
@@ -22,6 +27,7 @@ export interface Spec extends TurboModule {
|
||||
|};
|
||||
+voidFunc: () => void;
|
||||
+getBool: (arg: boolean) => boolean;
|
||||
+getEnum?: (arg: EnumInt) => EnumInt;
|
||||
+getNumber: (arg: number) => number;
|
||||
+getString: (arg: string) => string;
|
||||
+getArray: (arg: Array<any>) => Array<any>;
|
||||
|
||||
+12
@@ -31,6 +31,16 @@ static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool(
|
||||
->getBool(rt, args[0].getBool()));
|
||||
}
|
||||
|
||||
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getEnum(
|
||||
jsi::Runtime &rt,
|
||||
TurboModule &turboModule,
|
||||
const jsi::Value *args,
|
||||
size_t count) {
|
||||
return jsi::Value(
|
||||
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
|
||||
->getEnum(rt, args[0].getNumber()));
|
||||
}
|
||||
|
||||
static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber(
|
||||
jsi::Runtime &rt,
|
||||
TurboModule &turboModule,
|
||||
@@ -119,6 +129,8 @@ NativeSampleTurboCxxModuleSpecJSI::NativeSampleTurboCxxModuleSpecJSI(
|
||||
0, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_voidFunc};
|
||||
methodMap_["getBool"] = MethodMetadata{
|
||||
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool};
|
||||
methodMap_["getEnum"] = MethodMetadata{
|
||||
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getEnum};
|
||||
methodMap_["getNumber"] = MethodMetadata{
|
||||
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber};
|
||||
methodMap_["getString"] = MethodMetadata{
|
||||
|
||||
@@ -23,6 +23,7 @@ class JSI_EXPORT NativeSampleTurboCxxModuleSpecJSI : public TurboModule {
|
||||
public:
|
||||
virtual void voidFunc(jsi::Runtime &rt) = 0;
|
||||
virtual bool getBool(jsi::Runtime &rt, bool arg) = 0;
|
||||
virtual double getEnum(jsi::Runtime &rt, double arg) = 0;
|
||||
virtual double getNumber(jsi::Runtime &rt, double arg) = 0;
|
||||
virtual jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) = 0;
|
||||
virtual jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) = 0;
|
||||
|
||||
@@ -26,6 +26,10 @@ bool SampleTurboCxxModule::getBool(jsi::Runtime &rt, bool arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
double SampleTurboCxxModule::getEnum(jsi::Runtime &rt, double arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
double SampleTurboCxxModule::getNumber(jsi::Runtime &rt, double arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ class SampleTurboCxxModule : public NativeSampleTurboCxxModuleSpecJSI {
|
||||
|
||||
void voidFunc(jsi::Runtime &rt) override;
|
||||
bool getBool(jsi::Runtime &rt, bool arg) override;
|
||||
double getEnum(jsi::Runtime &rt, double arg) override;
|
||||
double getNumber(jsi::Runtime &rt, double arg) override;
|
||||
jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) override;
|
||||
jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) override;
|
||||
|
||||
+3
@@ -66,6 +66,9 @@ public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaMo
|
||||
@ReactMethod(isBlockingSynchronousMethod = true)
|
||||
public abstract boolean getBool(boolean arg);
|
||||
|
||||
@ReactMethod(isBlockingSynchronousMethod = true)
|
||||
public abstract double getEnum(double arg);
|
||||
|
||||
protected abstract Map<String, Object> getTypedExportedConstants();
|
||||
|
||||
@Override
|
||||
|
||||
+15
@@ -36,6 +36,18 @@ __hostFunction_NativeSampleTurboModuleSpecJSI_getBool(
|
||||
rt, BooleanKind, "getBool", "(Z)Z", args, count, cachedMethodId);
|
||||
}
|
||||
|
||||
static facebook::jsi::Value
|
||||
__hostFunction_NativeSampleTurboModuleSpecJSI_getEnum(
|
||||
facebook::jsi::Runtime &rt,
|
||||
TurboModule &turboModule,
|
||||
const facebook::jsi::Value *args,
|
||||
size_t count) {
|
||||
static jmethodID cachedMethodId = nullptr;
|
||||
return static_cast<JavaTurboModule &>(turboModule)
|
||||
.invokeJavaMethod(
|
||||
rt, NumberKind, "getEnum", "(D)D", args, count, cachedMethodId);
|
||||
}
|
||||
|
||||
static facebook::jsi::Value
|
||||
__hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
|
||||
facebook::jsi::Runtime &rt,
|
||||
@@ -195,6 +207,9 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(
|
||||
methodMap_["getBool"] =
|
||||
MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};
|
||||
|
||||
methodMap_["getEnum"] =
|
||||
MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum};
|
||||
|
||||
methodMap_["getNumber"] = MethodMetadata{
|
||||
1, __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};
|
||||
|
||||
|
||||
@@ -48,6 +48,14 @@ public class SampleTurboModule extends NativeSampleTurboModuleSpec {
|
||||
return arg;
|
||||
}
|
||||
|
||||
@DoNotStrip
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
public double getEnum(double arg) {
|
||||
log("getEnum", arg, arg);
|
||||
return arg;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Map<String, Object> getTypedExportedConstants() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
- (void)voidFunc;
|
||||
- (NSNumber *)getBool:(BOOL)arg;
|
||||
- (NSNumber *)getEnum:(double)arg;
|
||||
- (NSNumber *)getNumber:(double)arg;
|
||||
- (NSString *)getString:(NSString *)arg;
|
||||
- (NSArray<id<NSObject>> *)getArray:(NSArray *)arg;
|
||||
|
||||
@@ -30,6 +30,16 @@ static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getBoo
|
||||
.invokeObjCMethod(rt, BooleanKind, "getBool", @selector(getBool:), args, count);
|
||||
}
|
||||
|
||||
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum(
|
||||
facebook::jsi::Runtime &rt,
|
||||
TurboModule &turboModule,
|
||||
const facebook::jsi::Value *args,
|
||||
size_t count)
|
||||
{
|
||||
return static_cast<ObjCTurboModule &>(turboModule)
|
||||
.invokeObjCMethod(rt, NumberKind, "getEnum", @selector(getEnum:), args, count);
|
||||
}
|
||||
|
||||
static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
|
||||
facebook::jsi::Runtime &rt,
|
||||
TurboModule &turboModule,
|
||||
@@ -136,6 +146,7 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const ObjCTurboMo
|
||||
{
|
||||
methodMap_["voidFunc"] = MethodMetadata{0, __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc};
|
||||
methodMap_["getBool"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};
|
||||
methodMap_["getEnum"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum};
|
||||
methodMap_["getNumber"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};
|
||||
methodMap_["getString"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getString};
|
||||
methodMap_["getArray"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArray};
|
||||
|
||||
@@ -74,6 +74,11 @@ RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getBool : (BOOL)arg)
|
||||
return @(arg);
|
||||
}
|
||||
|
||||
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getEnum : (double)arg)
|
||||
{
|
||||
return @(arg);
|
||||
}
|
||||
|
||||
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
|
||||
{
|
||||
return @(arg);
|
||||
|
||||
+10
@@ -39,6 +39,12 @@ std::vector<CxxModule::Method> SampleTurboCxxModuleLegacyImpl::getMethods() {
|
||||
return getBool(xplat::jsArgAsBool(args, 0));
|
||||
},
|
||||
CxxModule::SyncTag),
|
||||
CxxModule::Method(
|
||||
"getEnum",
|
||||
[this](folly::dynamic args) {
|
||||
return getEnum(xplat::jsArgAsDouble(args, 0));
|
||||
},
|
||||
CxxModule::SyncTag),
|
||||
CxxModule::Method(
|
||||
"getNumber",
|
||||
[this](folly::dynamic args) {
|
||||
@@ -114,6 +120,10 @@ bool SampleTurboCxxModuleLegacyImpl::getBool(bool arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
double SampleTurboCxxModuleLegacyImpl::getEnum(double arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
double SampleTurboCxxModuleLegacyImpl::getNumber(double arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ class SampleTurboCxxModuleLegacyImpl
|
||||
// API
|
||||
void voidFunc();
|
||||
bool getBool(bool arg);
|
||||
double getEnum(double arg);
|
||||
double getNumber(double arg);
|
||||
std::string getString(const std::string &arg);
|
||||
folly::dynamic getArray(const folly::dynamic &arg);
|
||||
|
||||
@@ -119,6 +119,7 @@
|
||||
"anser": "^1.4.9",
|
||||
"base64-js": "^1.1.2",
|
||||
"event-target-shim": "^5.0.1",
|
||||
"flow-enums-runtime": "^0.0.6",
|
||||
"invariant": "^2.2.4",
|
||||
"jest-environment-node": "^29.2.1",
|
||||
"jsc-android": "^250230.2.1",
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
|
||||
import NativeSampleTurboModule from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
|
||||
import {EnumInt} from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
|
||||
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
|
||||
import {
|
||||
StyleSheet,
|
||||
@@ -57,6 +58,10 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
|
||||
getConstants: () => NativeSampleTurboModule.getConstants(),
|
||||
voidFunc: () => NativeSampleTurboModule.voidFunc(),
|
||||
getBool: () => NativeSampleTurboModule.getBool(true),
|
||||
getEnum: () =>
|
||||
NativeSampleTurboModule.getEnum
|
||||
? NativeSampleTurboModule.getEnum(EnumInt.A)
|
||||
: null,
|
||||
getNumber: () => NativeSampleTurboModule.getNumber(99.95),
|
||||
getString: () => NativeSampleTurboModule.getString('Hello'),
|
||||
getArray: () =>
|
||||
@@ -80,6 +85,7 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
|
||||
| 'callback'
|
||||
| 'getArray'
|
||||
| 'getBool'
|
||||
| 'getEnum'
|
||||
| 'getConstants'
|
||||
| 'getNumber'
|
||||
| 'getObject'
|
||||
@@ -120,6 +126,7 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
|
||||
| 'callback'
|
||||
| 'getArray'
|
||||
| 'getBool'
|
||||
| 'getEnum'
|
||||
| 'getConstants'
|
||||
| 'getNumber'
|
||||
| 'getObject'
|
||||
|
||||
@@ -4909,6 +4909,11 @@ flow-bin@^0.190.1:
|
||||
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.190.1.tgz#59ccbc9aaa2515fe32acc66117a05e7ef6a11061"
|
||||
integrity sha512-5c9/6eEkMTTfdNuK2WwssrKfsUXKMUXlZVJZnrlWiqJpDSVc70/Smwyi9sXict9k/oq0f+Mo5wVH0d7peBYREg==
|
||||
|
||||
flow-enums-runtime@^0.0.6:
|
||||
version "0.0.6"
|
||||
resolved "https://registry.yarnpkg.com/flow-enums-runtime/-/flow-enums-runtime-0.0.6.tgz#5bb0cd1b0a3e471330f4d109039b7eba5cb3e787"
|
||||
integrity sha512-3PYnM29RFXwvAN6Pc/scUfkI7RwhQ/xqyLUyPNlXUp9S40zI8nup9tUSrTLSVnWGBN38FNiGWbwZOB6uR4OGdw==
|
||||
|
||||
flow-parser@0.*, flow-parser@^0.185.0:
|
||||
version "0.185.0"
|
||||
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.185.0.tgz#56bde60805bad19b2934ebfc50c9485e5c5424f9"
|
||||
|
||||
Reference in New Issue
Block a user