Skip to content

Commit

Permalink
Turbo Module EventEmitters as functions (#44886)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44886

## Changelog:

[General] [Added] - Turbo Module EventEmitters as functions

Reviewed By: javache

Differential Revision: D58429202

fbshipit-source-id: c56793d216f5ecf981e62d3b004f715110903945
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Jun 12, 2024
1 parent 6a3a305 commit 42f136d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 18 deletions.
4 changes: 1 addition & 3 deletions packages/react-native/Libraries/Types/CodegenTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ type DefaultTypes = number | boolean | string | $ReadOnlyArray<string>;
// eslint-disable-next-line no-unused-vars
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = ?Type;

export type EventEmitter<T> = {
addListener(handler: (T) => mixed): EventSubscription,
};
export type EventEmitter<T> = (handler: (T) => mixed) => EventSubscription;
Original file line number Diff line number Diff line change
Expand Up @@ -8133,9 +8133,7 @@ export type UnsafeObject = $FlowFixMe;
export type UnsafeMixed = mixed;
type DefaultTypes = number | boolean | string | $ReadOnlyArray<string>;
export type WithDefault<Type: DefaultTypes, Value: ?Type | string> = ?Type;
export type EventEmitter<T> = {
addListener(handler: (T) => mixed): EventSubscription,
};
export type EventEmitter<T> = (handler: (T) => mixed) => EventSubscription;
"
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ class AsyncEventEmitter : public IAsyncEventEmitter {
jsi::Object get(
jsi::Runtime& rt,
const std::shared_ptr<CallInvoker>& 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)})`),
),
);
Expand Down

0 comments on commit 42f136d

Please sign in to comment.