Skip to content

Commit 42f136d

Browse files
Turbo Module EventEmitters as functions (#44886)
Summary: Pull Request resolved: #44886 ## Changelog: [General] [Added] - Turbo Module EventEmitters as functions Reviewed By: javache Differential Revision: D58429202 fbshipit-source-id: c56793d216f5ecf981e62d3b004f715110903945
1 parent 6a3a305 commit 42f136d

File tree

5 files changed

+9
-18
lines changed

5 files changed

+9
-18
lines changed

packages/react-native/Libraries/Types/CodegenTypes.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,4 @@ type DefaultTypes = number | boolean | string | $ReadOnlyArray<string>;
4242
// eslint-disable-next-line no-unused-vars
4343
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = ?Type;
4444

45-
export type EventEmitter<T> = {
46-
addListener(handler: (T) => mixed): EventSubscription,
47-
};
45+
export type EventEmitter<T> = (handler: (T) => mixed) => EventSubscription;

packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap

+1-3
Original file line numberDiff line numberDiff line change
@@ -8133,9 +8133,7 @@ export type UnsafeObject = $FlowFixMe;
81338133
export type UnsafeMixed = mixed;
81348134
type DefaultTypes = number | boolean | string | $ReadOnlyArray<string>;
81358135
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = ?Type;
8136-
export type EventEmitter<T> = {
8137-
addListener(handler: (T) => mixed): EventSubscription,
8138-
};
8136+
export type EventEmitter<T> = (handler: (T) => mixed) => EventSubscription;
81398137
"
81408138
`;
81418139

packages/react-native/ReactCommon/react/bridging/EventEmitter.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ class AsyncEventEmitter : public IAsyncEventEmitter {
104104
jsi::Object get(
105105
jsi::Runtime& rt,
106106
const std::shared_ptr<CallInvoker>& jsInvoker) const override {
107-
auto result = jsi::Object(rt);
108-
result.setProperty(
109-
rt, "addListener", bridging::toJs(rt, listen_, jsInvoker));
110-
return result;
107+
return bridging::toJs(rt, listen_, jsInvoker);
111108
}
112109

113110
private:

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ void addEventSubscription(
445445
[lastEvent = lastEvent](const EventType& event) { *lastEvent = event; },
446446
invoker);
447447
eventSubscriptionsWithListener.emplace_back(std::make_pair(
448-
jsi::Object(eventEmitterJs.getPropertyAsFunction(rt, "addListener")
448+
jsi::Object(eventEmitterJs.asFunction(rt)
449449
.callWithThis(rt, eventEmitterJs, listenJs)
450450
.asObject(rt)),
451451
std::move(lastEvent)));

packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -266,22 +266,20 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
266266
}
267267
if (NativeCxxModuleExample) {
268268
this.eventSubscriptions.push(
269-
NativeCxxModuleExample.onPress.addListener(value =>
270-
console.log('onPress: ()'),
271-
),
269+
NativeCxxModuleExample.onPress(value => console.log('onPress: ()')),
272270
);
273271
this.eventSubscriptions.push(
274-
NativeCxxModuleExample.onClick.addListener(value =>
272+
NativeCxxModuleExample.onClick(value =>
275273
console.log(`onClick: (${value})`),
276274
),
277275
);
278276
this.eventSubscriptions.push(
279-
NativeCxxModuleExample.onChange.addListener(value =>
280-
console.log(`onChange: (${JSON.stringify(value)})`),
277+
NativeCxxModuleExample.onChange(value =>
278+
console.log(`onChange: ${JSON.stringify(value)})`),
281279
),
282280
);
283281
this.eventSubscriptions.push(
284-
NativeCxxModuleExample.onSubmit.addListener(value =>
282+
NativeCxxModuleExample.onSubmit(value =>
285283
console.log(`onSubmit: (${JSON.stringify(value)})`),
286284
),
287285
);

0 commit comments

Comments
 (0)