Skip to content

Commit

Permalink
Use paperTopLevelNameDeprecated in generated EventEmitters if defined (
Browse files Browse the repository at this point in the history
…#42812)

Summary:
Pull Request resolved: #42812

There is a way of defining events where you specify additional string type parameter in the EventHandler in the spec. This additional type parameter is an overridden top level event name, that can be completely unrelated to the event handler name.
More context here D16042065.

Let's say we have
```
onLegacyStyleEvent?: ?BubblingEventHandler<LegacyStyleEvent, 'alternativeLegacyName'>
```
This will produce the following entry in the view config:
```
topAlternativeLegacyName: {
  phasedRegistrationNames: {
    captured: 'onLegacyStyleEventCapture',
    bubbled: 'onLegacyStyleEvent'
  }
}
```
This means that React expects `topAlternativeLegacyName`.
But the generated EventEmitter looks like this:
```
void RNTMyNativeViewEventEmitter::onLegacyStyleEvent(OnLegacyStyleEvent $event) const {
  dispatchEvent("legacyStyleEvent", [$event=std::move($event)](jsi::Runtime &runtime) {
    auto $payload = jsi::Object(runtime);
    $payload.setProperty(runtime, "string", $event.string);
    return $payload;
  });
}
```
The native component will emit `legacyStyleEvent` (`topLegacyStyleEvent` after normalization) that React will not be able to handle.

This issue only happens on iOS because Android doesn't use EventEmitter currently.

To address this issue we'll use `paperTopLevelNameDeprecated` for the generated EventEmitters if it is defined.

Changelog: [iOS][Fixed] - Fixed support for event name override in component specs.

Reviewed By: cortinico, mdvacca, cipolleschi

Differential Revision: D53310654

fbshipit-source-id: 018d5b11d8d36e2ecf900b9d8d6fe3e2ed71f80b
  • Loading branch information
dmytrorykun authored and facebook-github-bot committed Feb 13, 2024
1 parent 4d07aae commit 6974697
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Object {
namespace facebook::react {
void EventPropsNativeComponentViewEventEmitter::onChange(OnChange $event) const {
dispatchEvent(\\"change\\", [$event=std::move($event)](jsi::Runtime &runtime) {
dispatchEvent(\\"paperDirectName\\", [$event=std::move($event)](jsi::Runtime &runtime) {
auto $payload = jsi::Object(runtime);
$payload.setProperty(runtime, \\"value\\", $event.value);
$payload.setProperty(runtime, \\"source\\", $event.source);
Expand All @@ -211,7 +211,7 @@ void EventPropsNativeComponentViewEventEmitter::onEventDirect(OnEventDirect $eve
void EventPropsNativeComponentViewEventEmitter::onEventDirectWithPaperName(OnEventDirectWithPaperName $event) const {
dispatchEvent(\\"eventDirectWithPaperName\\", [$event=std::move($event)](jsi::Runtime &runtime) {
dispatchEvent(\\"paperDirectName\\", [$event=std::move($event)](jsi::Runtime &runtime) {
auto $payload = jsi::Object(runtime);
$payload.setProperty(runtime, \\"value\\", $event.value);
return $payload;
Expand All @@ -220,7 +220,7 @@ void EventPropsNativeComponentViewEventEmitter::onEventDirectWithPaperName(OnEve
void EventPropsNativeComponentViewEventEmitter::onOrientationChange(OnOrientationChange $event) const {
dispatchEvent(\\"orientationChange\\", [$event=std::move($event)](jsi::Runtime &runtime) {
dispatchEvent(\\"paperBubblingName\\", [$event=std::move($event)](jsi::Runtime &runtime) {
auto $payload = jsi::Object(runtime);
$payload.setProperty(runtime, \\"orientation\\", toString($event.orientation));
return $payload;
Expand All @@ -238,7 +238,7 @@ void EventPropsNativeComponentViewEventEmitter::onEnd(OnEnd $event) const {
void EventPropsNativeComponentViewEventEmitter::onEventBubblingWithPaperName(OnEventBubblingWithPaperName $event) const {
dispatchEvent(\\"eventBubblingWithPaperName\\", [](jsi::Runtime &runtime) {
dispatchEvent(\\"paperBubblingName\\", [](jsi::Runtime &runtime) {
auto $payload = jsi::Object(runtime);
return $payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,10 @@ function generateEvent(
// In order to migrate to this new system we have to support the current
// naming scheme. We should delete this once we are able to control this name
// throughout the system.
const dispatchEventName = `${event.name[2].toLowerCase()}${event.name.slice(
3,
)}`;
const dispatchEventName =
event.paperTopLevelNameDeprecated != null
? event.paperTopLevelNameDeprecated
: `${event.name[2].toLowerCase()}${event.name.slice(3)}`;

if (event.typeAnnotation.argument) {
const implementation = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ Map {
namespace facebook::react {
void InterfaceOnlyComponentEventEmitter::onChange(OnChange $event) const {
dispatchEvent(\\"change\\", [$event=std::move($event)](jsi::Runtime &runtime) {
dispatchEvent(\\"paperChange\\", [$event=std::move($event)](jsi::Runtime &runtime) {
auto $payload = jsi::Object(runtime);
$payload.setProperty(runtime, \\"value\\", $event.value);
return $payload;
Expand All @@ -370,7 +370,7 @@ void InterfaceOnlyComponentEventEmitter::onChange(OnChange $event) const {
void InterfaceOnlyComponentEventEmitter::onDirectChange(OnDirectChange $event) const {
dispatchEvent(\\"directChange\\", [$event=std::move($event)](jsi::Runtime &runtime) {
dispatchEvent(\\"paperDirectChange\\", [$event=std::move($event)](jsi::Runtime &runtime) {
auto $payload = jsi::Object(runtime);
$payload.setProperty(runtime, \\"value\\", $event.value);
return $payload;
Expand Down

0 comments on commit 6974697

Please sign in to comment.