Skip to content

Commit d1b61e6

Browse files
committed
Minimal implementation of known and unknown synthetic events
1 parent 6c016be commit d1b61e6

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

packages/react-dom/src/events/DOMPluginEventSystem.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
SHOULD_NOT_PROCESS_POLYFILL_EVENT_PLUGINS,
1616
} from './EventSystemFlags';
1717
import type {AnyNativeEvent} from './PluginModuleType';
18-
import type {ReactSyntheticEvent} from './ReactSyntheticEventType';
18+
import type {
19+
KnownReactSyntheticEvent,
20+
ReactSyntheticEvent,
21+
} from './ReactSyntheticEventType';
1922
import type {ElementListenerMapEntry} from '../client/ReactDOMComponentTree';
2023
import type {EventPriority} from 'shared/ReactTypes';
2124
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
@@ -917,15 +920,12 @@ function getLowestCommonAncestor(instA: Fiber, instB: Fiber): Fiber | null {
917920

918921
function accumulateEnterLeaveListenersForEvent(
919922
dispatchQueue: DispatchQueue,
920-
event: ReactSyntheticEvent,
923+
event: KnownReactSyntheticEvent,
921924
target: Fiber,
922925
common: Fiber | null,
923926
inCapturePhase: boolean,
924927
): void {
925928
const registrationName = event._reactName;
926-
if (registrationName === null) {
927-
return;
928-
}
929929
const listeners: Array<DispatchListener> = [];
930930

931931
let instance = target;
@@ -969,8 +969,8 @@ function accumulateEnterLeaveListenersForEvent(
969969
// phase event listeners.
970970
export function accumulateEnterLeaveTwoPhaseListeners(
971971
dispatchQueue: DispatchQueue,
972-
leaveEvent: ReactSyntheticEvent,
973-
enterEvent: null | ReactSyntheticEvent,
972+
leaveEvent: KnownReactSyntheticEvent,
973+
enterEvent: null | KnownReactSyntheticEvent,
974974
from: Fiber | null,
975975
to: Fiber | null,
976976
): void {

packages/react-dom/src/events/ReactSyntheticEventType.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1212
import type {EventPriority} from 'shared/ReactTypes';
1313
import type {DOMEventName} from './DOMEventNames';
14+
import { MAX_SIGNED_31_BIT_INT } from 'react-reconciler/src/MaxInts';
1415

1516
export type DispatchConfig = {|
1617
dependencies?: Array<DOMEventName>,
@@ -22,14 +23,20 @@ export type DispatchConfig = {|
2223
eventPriority?: EventPriority,
2324
|};
2425

25-
export type ReactSyntheticEvent = {|
26+
type BaseSyntheticEvent = {
2627
isPersistent: () => boolean,
2728
isPropagationStopped: () => boolean,
2829
_dispatchInstances?: null | Array<Fiber | null> | Fiber,
2930
_dispatchListeners?: null | Array<Function> | Function,
30-
_reactName: string | null,
3131
_targetInst: Fiber,
3232
nativeEvent: Event,
33+
target?: mixed,
34+
relatedTarget?: mixed,
3335
type: string,
3436
currentTarget: null | EventTarget,
35-
|};
37+
};
38+
39+
export type KnownReactSyntheticEvent = BaseSyntheticEvent & {_reactName: string};
40+
export type UnknownReactSyntheticEvent = BaseSyntheticEvent & {_reactName: null};
41+
42+
export type ReactSyntheticEvent = KnownReactSyntheticEvent | UnknownReactSyntheticEvent;

packages/react-dom/src/events/plugins/EnterLeaveEventPlugin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
isContainerMarkedAsRoot,
2626
} from '../../client/ReactDOMComponentTree';
2727
import {accumulateEnterLeaveTwoPhaseListeners} from '../DOMPluginEventSystem';
28+
import type {KnownReactSyntheticEvent} from '../ReactSyntheticEventType';
2829

2930
import {HostComponent, HostText} from 'react-reconciler/src/ReactWorkTags';
3031
import {getNearestMountedFiber} from 'react-reconciler/src/ReactFiberTreeReflection';
@@ -147,16 +148,17 @@ function extractEvents(
147148
leave.target = fromNode;
148149
leave.relatedTarget = toNode;
149150

150-
let enter = new SyntheticEvent(
151+
let enter: KnownReactSyntheticEvent | null = new SyntheticEvent(
151152
enterEventType,
152153
eventTypePrefix + 'enter',
153154
to,
154155
nativeEvent,
155156
nativeEventTarget,
156157
eventInterface,
157158
);
158-
enter.target = toNode;
159-
enter.relatedTarget = fromNode;
159+
// flow thinks `enter` could be `null` at this point
160+
((enter: any): KnownReactSyntheticEvent).target = toNode;
161+
((enter: any): KnownReactSyntheticEvent).relatedTarget = fromNode;
160162

161163
// If we are not processing the first ancestor, then we
162164
// should not process the same nativeEvent again, as we

0 commit comments

Comments
 (0)