From 42f136d00d0917bad8928839dea01e99bb0a1c98 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Tue, 11 Jun 2024 23:35:36 -0700 Subject: [PATCH] Turbo Module EventEmitters as functions (#44886) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44886 ## Changelog: [General] [Added] - Turbo Module EventEmitters as functions Reviewed By: javache Differential Revision: D58429202 fbshipit-source-id: c56793d216f5ecf981e62d3b004f715110903945 --- .../react-native/Libraries/Types/CodegenTypes.js | 4 +--- .../__tests__/__snapshots__/public-api-test.js.snap | 4 +--- .../ReactCommon/react/bridging/EventEmitter.h | 5 +---- .../react/bridging/tests/BridgingTest.cpp | 2 +- .../TurboModule/NativeCxxModuleExampleExample.js | 12 +++++------- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/packages/react-native/Libraries/Types/CodegenTypes.js b/packages/react-native/Libraries/Types/CodegenTypes.js index 444b1869a76..cf53ec01547 100644 --- a/packages/react-native/Libraries/Types/CodegenTypes.js +++ b/packages/react-native/Libraries/Types/CodegenTypes.js @@ -42,6 +42,4 @@ type DefaultTypes = number | boolean | string | $ReadOnlyArray; // eslint-disable-next-line no-unused-vars export type WithDefault = ?Type; -export type EventEmitter = { - addListener(handler: (T) => mixed): EventSubscription, -}; +export type EventEmitter = (handler: (T) => mixed) => EventSubscription; diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 5301bad9c1d..429782e34af 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -8133,9 +8133,7 @@ export type UnsafeObject = $FlowFixMe; export type UnsafeMixed = mixed; type DefaultTypes = number | boolean | string | $ReadOnlyArray; export type WithDefault = ?Type; -export type EventEmitter = { - addListener(handler: (T) => mixed): EventSubscription, -}; +export type EventEmitter = (handler: (T) => mixed) => EventSubscription; " `; diff --git a/packages/react-native/ReactCommon/react/bridging/EventEmitter.h b/packages/react-native/ReactCommon/react/bridging/EventEmitter.h index e2c8977302f..acfb6a3152d 100644 --- a/packages/react-native/ReactCommon/react/bridging/EventEmitter.h +++ b/packages/react-native/ReactCommon/react/bridging/EventEmitter.h @@ -104,10 +104,7 @@ class AsyncEventEmitter : public IAsyncEventEmitter { jsi::Object get( jsi::Runtime& rt, const std::shared_ptr& jsInvoker) const override { - auto result = jsi::Object(rt); - result.setProperty( - rt, "addListener", bridging::toJs(rt, listen_, jsInvoker)); - return result; + return bridging::toJs(rt, listen_, jsInvoker); } private: diff --git a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp index 512562c29d8..01263a3d6b9 100644 --- a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp +++ b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp @@ -445,7 +445,7 @@ void addEventSubscription( [lastEvent = lastEvent](const EventType& event) { *lastEvent = event; }, invoker); eventSubscriptionsWithListener.emplace_back(std::make_pair( - jsi::Object(eventEmitterJs.getPropertyAsFunction(rt, "addListener") + jsi::Object(eventEmitterJs.asFunction(rt) .callWithThis(rt, eventEmitterJs, listenJs) .asObject(rt)), std::move(lastEvent))); diff --git a/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js b/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js index f28b2ac3232..5d67efddbd3 100644 --- a/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js +++ b/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js @@ -266,22 +266,20 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> { } if (NativeCxxModuleExample) { this.eventSubscriptions.push( - NativeCxxModuleExample.onPress.addListener(value => - console.log('onPress: ()'), - ), + NativeCxxModuleExample.onPress(value => console.log('onPress: ()')), ); this.eventSubscriptions.push( - NativeCxxModuleExample.onClick.addListener(value => + NativeCxxModuleExample.onClick(value => console.log(`onClick: (${value})`), ), ); this.eventSubscriptions.push( - NativeCxxModuleExample.onChange.addListener(value => - console.log(`onChange: (${JSON.stringify(value)})`), + NativeCxxModuleExample.onChange(value => + console.log(`onChange: ${JSON.stringify(value)})`), ), ); this.eventSubscriptions.push( - NativeCxxModuleExample.onSubmit.addListener(value => + NativeCxxModuleExample.onSubmit(value => console.log(`onSubmit: (${JSON.stringify(value)})`), ), );