Skip to content

Commit 77754ae

Browse files
authored
Decouple event priority list from event name list (#20760)
* Remove some dead code * Decouple event priority list from event name list * Fix lint
1 parent b5bac18 commit 77754ae

File tree

4 files changed

+86
-102
lines changed

4 files changed

+86
-102
lines changed

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

Lines changed: 12 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @flow
88
*/
99

10-
import type {EventPriority} from 'shared/ReactTypes';
1110
import type {DOMEventName} from './DOMEventNames';
1211

1312
import {registerTwoPhaseEvent} from './EventRegistry';
@@ -17,7 +16,6 @@ import {
1716
ANIMATION_START,
1817
TRANSITION_END,
1918
} from './DOMEventNames';
20-
import {DiscreteEvent, ContinuousEvent, DefaultEvent} from 'shared/ReactTypes';
2119

2220
import {enableCreateEventHandleAPI} from 'shared/ReactFeatureFlags';
2321

@@ -26,19 +24,8 @@ export const topLevelEventsToReactNames: Map<
2624
string | null,
2725
> = new Map();
2826

29-
const eventPriorities = new Map();
30-
31-
// We store most of the events in this module in pairs of two strings so we can re-use
32-
// the code required to apply the same logic for event prioritization and that of the
33-
// SimpleEventPlugin. This complicates things slightly, but the aim is to reduce code
34-
// duplication (for which there would be quite a bit). For the events that are not needed
35-
// for the SimpleEventPlugin (otherDiscreteEvents) we process them separately as an
36-
// array of top level events.
37-
38-
// Lastly, we ignore prettier so we can keep the formatting sane.
39-
4027
// prettier-ignore
41-
const discreteEventPairsForSimpleEventPlugin = [
28+
const simpleEventPluginNames = [
4229
('cancel': DOMEventName), 'cancel',
4330
('click': DOMEventName), 'click',
4431
('close': DOMEventName), 'close',
@@ -73,27 +60,7 @@ const discreteEventPairsForSimpleEventPlugin = [
7360
('touchend': DOMEventName), 'touchEnd',
7461
('touchstart': DOMEventName), 'touchStart',
7562
('volumechange': DOMEventName), 'volumeChange',
76-
];
77-
78-
const otherDiscreteEvents: Array<DOMEventName> = [
79-
'change',
80-
'selectionchange',
81-
'textInput',
82-
'compositionstart',
83-
'compositionend',
84-
'compositionupdate',
85-
];
86-
87-
if (enableCreateEventHandleAPI) {
88-
// Special case: these two events don't have on* React handler
89-
// and are only accessible via the createEventHandle API.
90-
topLevelEventsToReactNames.set('beforeblur', null);
91-
topLevelEventsToReactNames.set('afterblur', null);
92-
otherDiscreteEvents.push('beforeblur', 'afterblur');
93-
}
9463

95-
// prettier-ignore
96-
const continuousPairsForSimpleEventPlugin: Array<string | DOMEventName> = [
9764
('drag': DOMEventName), 'drag',
9865
('dragenter': DOMEventName), 'dragEnter',
9966
('dragexit': DOMEventName), 'dragExit',
@@ -109,10 +76,7 @@ const continuousPairsForSimpleEventPlugin: Array<string | DOMEventName> = [
10976
('toggle': DOMEventName), 'toggle',
11077
('touchmove': DOMEventName), 'touchMove',
11178
('wheel': DOMEventName), 'wheel',
112-
];
11379

114-
// prettier-ignore
115-
const defaultPairsForSimpleEventPlugin: Array<string | DOMEventName> = [
11680
('abort': DOMEventName), 'abort',
11781
(ANIMATION_END: DOMEventName), 'animationEnd',
11882
(ANIMATION_ITERATION: DOMEventName), 'animationIteration',
@@ -140,6 +104,13 @@ const defaultPairsForSimpleEventPlugin: Array<string | DOMEventName> = [
140104
('waiting': DOMEventName), 'waiting',
141105
];
142106

107+
if (enableCreateEventHandleAPI) {
108+
// Special case: these two events don't have on* React handler
109+
// and are only accessible via the createEventHandle API.
110+
topLevelEventsToReactNames.set('beforeblur', null);
111+
topLevelEventsToReactNames.set('afterblur', null);
112+
}
113+
143114
/**
144115
* Turns
145116
* ['abort', ...]
@@ -152,75 +123,19 @@ const defaultPairsForSimpleEventPlugin: Array<string | DOMEventName> = [
152123
*
153124
* and registers them.
154125
*/
155-
function registerSimplePluginEventsAndSetTheirPriorities(
156-
eventTypes: Array<DOMEventName | string>,
157-
priority: EventPriority,
158-
): void {
126+
export function registerSimpleEvents() {
159127
// As the event types are in pairs of two, we need to iterate
160128
// through in twos. The events are in pairs of two to save code
161129
// and improve init perf of processing this array, as it will
162130
// result in far fewer object allocations and property accesses
163131
// if we only use three arrays to process all the categories of
164132
// instead of tuples.
165-
for (let i = 0; i < eventTypes.length; i += 2) {
166-
const topEvent = ((eventTypes[i]: any): DOMEventName);
167-
const event = ((eventTypes[i + 1]: any): string);
133+
for (let i = 0; i < simpleEventPluginNames.length; i += 2) {
134+
const topEvent = ((simpleEventPluginNames[i]: any): DOMEventName);
135+
const event = ((simpleEventPluginNames[i + 1]: any): string);
168136
const capitalizedEvent = event[0].toUpperCase() + event.slice(1);
169137
const reactName = 'on' + capitalizedEvent;
170-
eventPriorities.set(topEvent, priority);
171138
topLevelEventsToReactNames.set(topEvent, reactName);
172139
registerTwoPhaseEvent(reactName, [topEvent]);
173140
}
174141
}
175-
176-
function setEventPriorities(
177-
eventTypes: Array<DOMEventName>,
178-
priority: EventPriority,
179-
): void {
180-
for (let i = 0; i < eventTypes.length; i++) {
181-
eventPriorities.set(eventTypes[i], priority);
182-
}
183-
}
184-
185-
export function getEventPriorityForPluginSystem(
186-
domEventName: DOMEventName,
187-
): EventPriority {
188-
const priority = eventPriorities.get(domEventName);
189-
// Default to a DefaultEvent. Note: we might
190-
// want to warn if we can't detect the priority
191-
// for the event.
192-
return priority === undefined ? DefaultEvent : priority;
193-
}
194-
195-
export function getEventPriorityForListenerSystem(
196-
type: DOMEventName,
197-
): EventPriority {
198-
const priority = eventPriorities.get(type);
199-
if (priority !== undefined) {
200-
return priority;
201-
}
202-
if (__DEV__) {
203-
console.warn(
204-
'The event "%s" provided to createEventHandle() does not have a known priority type.' +
205-
' This is likely a bug in React.',
206-
type,
207-
);
208-
}
209-
return DefaultEvent;
210-
}
211-
212-
export function registerSimpleEvents() {
213-
registerSimplePluginEventsAndSetTheirPriorities(
214-
discreteEventPairsForSimpleEventPlugin,
215-
DiscreteEvent,
216-
);
217-
registerSimplePluginEventsAndSetTheirPriorities(
218-
continuousPairsForSimpleEventPlugin,
219-
ContinuousEvent,
220-
);
221-
registerSimplePluginEventsAndSetTheirPriorities(
222-
defaultPairsForSimpleEventPlugin,
223-
DefaultEvent,
224-
);
225-
setEventPriorities(otherDiscreteEvents, DiscreteEvent);
226-
}

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

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {AnyNativeEvent} from '../events/PluginModuleType';
1111
import type {FiberRoot} from 'react-reconciler/src/ReactInternalTypes';
1212
import type {Container, SuspenseInstance} from '../client/ReactDOMHostConfig';
1313
import type {DOMEventName} from '../events/DOMEventNames';
14+
import type {EventPriority} from 'shared/ReactTypes';
1415

1516
// Intentionally not named imports because Rollup would use dynamic dispatch for
1617
// CommonJS interop named imports.
@@ -44,7 +45,6 @@ import {
4445
enableNewReconciler,
4546
} from 'shared/ReactFeatureFlags';
4647
import {ContinuousEvent, DefaultEvent, DiscreteEvent} from 'shared/ReactTypes';
47-
import {getEventPriorityForPluginSystem} from './DOMEventProperties';
4848
import {dispatchEventForPluginEventSystem} from './DOMPluginEventSystem';
4949
import {
5050
flushDiscreteUpdatesIfNeeded,
@@ -339,3 +339,76 @@ export function attemptToDispatchEvent(
339339
// We're not blocked on anything.
340340
return null;
341341
}
342+
343+
function getEventPriorityForPluginSystem(
344+
domEventName: DOMEventName,
345+
): EventPriority {
346+
switch (domEventName) {
347+
// Used by SimpleEventPlugin:
348+
case 'cancel':
349+
case 'click':
350+
case 'close':
351+
case 'contextmenu':
352+
case 'copy':
353+
case 'cut':
354+
case 'auxclick':
355+
case 'dblclick':
356+
case 'dragend':
357+
case 'dragstart':
358+
case 'drop':
359+
case 'focusin':
360+
case 'focusout':
361+
case 'input':
362+
case 'invalid':
363+
case 'keydown':
364+
case 'keypress':
365+
case 'keyup':
366+
case 'mousedown':
367+
case 'mouseup':
368+
case 'paste':
369+
case 'pause':
370+
case 'play':
371+
case 'pointercancel':
372+
case 'pointerdown':
373+
case 'pointerup':
374+
case 'ratechange':
375+
case 'reset':
376+
case 'seeked':
377+
case 'submit':
378+
case 'touchcancel':
379+
case 'touchend':
380+
case 'touchstart':
381+
case 'volumechange':
382+
// Used by polyfills:
383+
// eslint-disable-next-line no-fallthrough
384+
case 'change':
385+
case 'selectionchange':
386+
case 'textInput':
387+
case 'compositionstart':
388+
case 'compositionend':
389+
case 'compositionupdate':
390+
// Only enableCreateEventHandleAPI:
391+
// eslint-disable-next-line no-fallthrough
392+
case 'beforeblur':
393+
case 'afterblur':
394+
return DiscreteEvent;
395+
case 'drag':
396+
case 'dragenter':
397+
case 'dragexit':
398+
case 'dragleave':
399+
case 'dragover':
400+
case 'mousemove':
401+
case 'mouseout':
402+
case 'mouseover':
403+
case 'pointermove':
404+
case 'pointerout':
405+
case 'pointerover':
406+
case 'scroll':
407+
case 'toggle':
408+
case 'touchmove':
409+
case 'wheel':
410+
return ContinuousEvent;
411+
default:
412+
return DefaultEvent;
413+
}
414+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
12-
import type {EventPriority} from 'shared/ReactTypes';
1312
import type {DOMEventName} from './DOMEventNames';
1413

1514
export type DispatchConfig = {|
@@ -19,7 +18,6 @@ export type DispatchConfig = {|
1918
captured: null | string,
2019
|},
2120
registrationName?: string,
22-
eventPriority?: EventPriority,
2321
|};
2422

2523
type BaseSyntheticEvent = {

packages/react-native-renderer/src/legacy-events/ReactSyntheticEventType.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010

1111
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
12-
import type {EventPriority} from 'shared/ReactTypes';
1312
import type {TopLevelType} from './TopLevelEventTypes';
1413

1514
export type DispatchConfig = {|
@@ -19,7 +18,6 @@ export type DispatchConfig = {|
1918
captured: null | string,
2019
|},
2120
registrationName?: string,
22-
eventPriority: EventPriority,
2321
|};
2422

2523
export type CustomDispatchConfig = {|

0 commit comments

Comments
 (0)