Skip to content

Commit

Permalink
Add C++ Turbo Module Event Emitter example (#44810)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44810

Adds an example how to use the `EventEmitter` on a (C++) Turbo Module

## Changelog:

[General] [Added] - Add C++ Turbo Module Event Emitter example

Reviewed By: javache

Differential Revision: D57473949

fbshipit-source-id: 1a8d17fb83af4220ef12379e0102b5b2e233ed45
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Jun 12, 2024
1 parent 872d5d3 commit 6a3a305
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,14 @@ std::optional<bool> NativeCxxModuleExample::getWithWithOptionalArgs(
}

void NativeCxxModuleExample::voidFunc(jsi::Runtime& rt) {
// Nothing to do
// Emit some events
emitOnPress();
emitOnClick<std::string>("value from callback on click!");
emitOnChange(ObjectStruct{1, "two", std::nullopt});
emitOnSubmit(std::vector{
ObjectStruct{1, "two", std::nullopt},
ObjectStruct{3, "four", std::nullopt},
ObjectStruct{5, "six", std::nullopt}});
}

void NativeCxxModuleExample::setMenu(jsi::Runtime& rt, MenuItem menuItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
import type {EventEmitter} from 'react-native/Libraries/Types/CodegenTypes';

import {TurboModuleRegistry} from 'react-native';

Expand Down Expand Up @@ -76,6 +77,10 @@ export type CustomDeviceEvent = {
};

export interface Spec extends TurboModule {
+onPress: EventEmitter<void>;
+onClick: EventEmitter<string>;
+onChange: EventEmitter<ObjectStruct>;
+onSubmit: EventEmitter<ObjectStruct[]>;
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
+getBool: (arg: boolean) => boolean;
+getConstants: () => ConstantsStruct;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter';

import NativeCxxModuleExample, {
EnumInt,
Expand Down Expand Up @@ -72,6 +73,7 @@ type ErrorExamples =

class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
static contextType: React$Context<RootTag> = RootTagContext;
eventSubscriptions: EventSubscription[] = [];

state: State = {
testResults: {},
Expand Down Expand Up @@ -262,6 +264,34 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
'Cannot load this example because TurboModule is not configured.',
);
}
if (NativeCxxModuleExample) {
this.eventSubscriptions.push(
NativeCxxModuleExample.onPress.addListener(value =>
console.log('onPress: ()'),
),
);
this.eventSubscriptions.push(
NativeCxxModuleExample.onClick.addListener(value =>
console.log(`onClick: (${value})`),
),
);
this.eventSubscriptions.push(
NativeCxxModuleExample.onChange.addListener(value =>
console.log(`onChange: (${JSON.stringify(value)})`),
),
);
this.eventSubscriptions.push(
NativeCxxModuleExample.onSubmit.addListener(value =>
console.log(`onSubmit: (${JSON.stringify(value)})`),
),
);
}
}

componentWillUnmount() {
for (const subscription of this.eventSubscriptions) {
subscription.remove();
}
}

render(): React.Node {
Expand Down

0 comments on commit 6a3a305

Please sign in to comment.