Skip to content

Commit c7920fa

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Match dispatchEvent return contract on dispatchTrustedEvent (#56761)
Summary: Pull Request resolved: #56761 Aligns `dispatchTrustedEvent` and the underlying `[INTERNAL_DISPATCH_METHOD_KEY]` "protected" symbol method on `EventTarget` with the public `EventTarget.dispatchEvent` contract: both now return `boolean` (`!event.defaultPrevented`) instead of `void`, so callers can tell whether the event's default action was canceled. Existing call sites ignore the return value, so this is a non-breaking widening. Changelog: [Internal] Reviewed By: javache Differential Revision: D104651715 fbshipit-source-id: 0cc837adb705fc3b34ee4363cd5873176dac25cb
1 parent 645f846 commit c7920fa

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

packages/react-native/src/private/webapis/dom/events/EventTarget.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,14 @@ export default class EventTarget {
243243
}
244244

245245
/**
246-
* This is "protected" method to dispatch trusted events.
246+
* This is "protected" method to dispatch trusted events. Mirrors the
247+
* `dispatchEvent` return contract: returns `false` if the event was
248+
* canceled (i.e. `event.defaultPrevented`), otherwise `true`.
247249
*/
248250
// $FlowExpectedError[unsupported-syntax]
249-
[INTERNAL_DISPATCH_METHOD_KEY](event: Event): void {
251+
[INTERNAL_DISPATCH_METHOD_KEY](event: Event): boolean {
250252
dispatch(this, event);
253+
return !event.defaultPrevented;
251254
}
252255
}
253256

packages/react-native/src/private/webapis/dom/events/internals/EventTargetInternals.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ export const INTERNAL_DISPATCH_METHOD_KEY: symbol = Symbol(
4848
);
4949

5050
/**
51-
* Dispatches a trusted event to the given event target.
51+
* Dispatches a trusted event to the given event target. Mirrors the
52+
* `dispatchEvent` method on `EventTarget`: returns `false` if the event
53+
* was canceled (i.e. `event.defaultPrevented`), otherwise `true`.
5254
*
5355
* This should only be used by the runtime to dispatch native events to
5456
* JavaScript.
5557
*/
5658
export function dispatchTrustedEvent(
5759
eventTarget: EventTarget,
5860
event: Event,
59-
): void {
61+
): boolean {
6062
setIsTrusted(event, true);
6163

6264
// $FlowExpectedError[prop-missing]

0 commit comments

Comments
 (0)