Files
react-native/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js
T
Christoph Purrer 9320174df4 Cxx TurboModules > Add example to return a JS function from Cxx to JS (#41385)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41385

Changelog: Internal

Adding a Cxx TM example which adds a listener and returns a subscription to remove that listener from the TM.

You should be able to use this with React Hooks - https://legacy.reactjs.org/docs/hooks-reference.html

E.g.

```
useEffect(() => {
  const subscription =  NativeCxxModuleExample.setValueCallbackWithSubscription(
          callbackValue => // use it
        );
  return subscription;
});
```

Reviewed By: shwanton

Differential Revision: D50473063

fbshipit-source-id: 4e9b92aeccff1771eb4ffad6bdaa20ba7f18435f
2023-11-09 11:38:54 -08:00

94 lines
2.5 KiB
JavaScript

/**
* 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.
*
* @flow strict-local
* @format
*/
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import {TurboModuleRegistry} from 'react-native';
export enum EnumInt {
IA = 23,
IB = 42,
}
export enum EnumFloat {
FA = 1.23,
FB = 4.56,
}
export enum EnumNone {
NA,
NB,
}
export enum EnumStr {
SA = 's---a',
SB = 's---b',
}
export type UnionFloat = 1.44 | 2.88 | 5.76;
export type UnionString = 'One' | 'Two' | 'Three';
export type UnionObject = {value: number} | {low: string};
export type ConstantsStruct = {|
const1: boolean,
const2: number,
const3: string,
|};
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
export type ValueStruct = {|
x: number,
y: string,
z: ObjectStruct,
|};
export type CustomHostObject = {};
export interface Spec extends TurboModule {
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
+getBool: (arg: boolean) => boolean;
+getConstants: () => ConstantsStruct;
+getCustomEnum: (arg: EnumInt) => EnumInt;
+getCustomHostObject: () => CustomHostObject;
+consumeCustomHostObject: (customHostObject: CustomHostObject) => string;
+getNumEnum: (arg: EnumInt) => EnumFloat;
+getStrEnum: (arg: EnumNone) => EnumStr;
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};
+getNumber: (arg: number) => number;
+getObject: (arg: ObjectStruct) => ObjectStruct;
+getSet: (arg: Array<number>) => Array<number>;
+getString: (arg: string) => string;
+getUnion: (x: UnionFloat, y: UnionString, z: UnionObject) => string;
+getValue: (x: number, y: string, z: ObjectStruct) => ValueStruct;
+getValueWithCallback: (callback: (value: string) => void) => void;
+setValueCallbackWithSubscription: (
callback: (value: string) => void,
) => () => void;
+getValueWithPromise: (error: boolean) => Promise<string>;
+getWithWithOptionalArgs: (optionalArg?: boolean) => ?boolean;
+voidFunc: () => void;
+emitCustomDeviceEvent: (eventName: string) => void;
+voidFuncThrows: () => void;
+getObjectThrows: (arg: ObjectStruct) => ObjectStruct;
+promiseThrows: () => Promise<void>;
+voidFuncAssert: () => void;
+getObjectAssert: (arg: ObjectStruct) => ObjectStruct;
+promiseAssert: () => Promise<void>;
}
export default (TurboModuleRegistry.get<Spec>(
'NativeCxxModuleExampleCxx',
): ?Spec);