Add Java Turbo Module Event Emitter example (#44906)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/44906

Shows a proof of concept how '*strongly typed Turbo Module scoped*' `EventEmitters` can be used in a Java Turbo Module.

## Changelog:

[Android] [Added] - Add Java Turbo Module Event Emitter example

Reviewed By: javache

Differential Revision: D57530807

fbshipit-source-id: 04261d8885760f0e3b3c8c1931e0d56a5d33a0df
This commit is contained in:
Christoph Purrer
2024-06-28 09:41:02 -07:00
committed by Facebook GitHub Bot
parent dba25fa966
commit 84a9f5e6c8
16 changed files with 131 additions and 21 deletions
@@ -9,6 +9,7 @@
*/
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
import type {EventSubscription} from 'react-native/Libraries/vendor/emitter/EventEmitter';
import styles from './TurboModuleExampleCommon';
import * as React from 'react';
@@ -68,6 +69,7 @@ type ErrorExamples =
class SampleTurboModuleExample extends React.Component<{||}, State> {
static contextType: React$Context<RootTag> = RootTagContext;
eventSubscriptions: EventSubscription[] = [];
state: State = {
testResults: {},
@@ -218,6 +220,30 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
'The JSI bindings for SampleTurboModule are not installed.',
);
}
this.eventSubscriptions.push(
NativeSampleTurboModule.onPress(value => console.log('onPress: ()')),
);
this.eventSubscriptions.push(
NativeSampleTurboModule.onClick(value =>
console.log(`onClick: (${value})`),
),
);
this.eventSubscriptions.push(
NativeSampleTurboModule.onChange(value =>
console.log(`onChange: (${JSON.stringify(value)})`),
),
);
this.eventSubscriptions.push(
NativeSampleTurboModule.onSubmit(value =>
console.log(`onSubmit: (${JSON.stringify(value)})`),
),
);
}
componentWillUnmount() {
for (const subscription of this.eventSubscriptions) {
subscription.remove();
}
}
render(): React.Node {