Skip to content

Commit 9751935

Browse files
authored
Modern Event System: improve dispatching queue (facebook#18799)
1 parent 3e13d70 commit 9751935

14 files changed

+258
-179
lines changed

packages/legacy-events/EventPluginRegistry.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ import type {DispatchConfig} from './ReactSyntheticEventType';
1111
import type {
1212
AnyNativeEvent,
1313
PluginName,
14-
PluginModule,
14+
LegacyPluginModule,
15+
ModernPluginModule,
1516
} from './PluginModuleType';
1617

1718
import invariant from 'shared/invariant';
1819

19-
type NamesToPlugins = {[key: PluginName]: PluginModule<AnyNativeEvent>, ...};
20+
type NamesToPlugins = {
21+
[key: PluginName]:
22+
| LegacyPluginModule<AnyNativeEvent>
23+
| ModernPluginModule<AnyNativeEvent>,
24+
...,
25+
};
2026
type EventPluginOrder = null | Array<PluginName>;
2127

2228
/**
@@ -84,7 +90,9 @@ function recomputePluginOrdering(): void {
8490
*/
8591
function publishEventForPlugin(
8692
dispatchConfig: DispatchConfig,
87-
pluginModule: PluginModule<AnyNativeEvent>,
93+
pluginModule:
94+
| LegacyPluginModule<AnyNativeEvent>
95+
| ModernPluginModule<AnyNativeEvent>,
8896
eventName: string,
8997
): boolean {
9098
invariant(
@@ -128,7 +136,9 @@ function publishEventForPlugin(
128136
*/
129137
function publishRegistrationName(
130138
registrationName: string,
131-
pluginModule: PluginModule<AnyNativeEvent>,
139+
pluginModule:
140+
| LegacyPluginModule<AnyNativeEvent>
141+
| ModernPluginModule<AnyNativeEvent>,
132142
eventName: string,
133143
): void {
134144
invariant(
@@ -243,7 +253,7 @@ export function injectEventPluginsByName(
243253
}
244254

245255
export function injectEventPlugins(
246-
eventPlugins: [PluginModule<AnyNativeEvent>],
256+
eventPlugins: [ModernPluginModule<AnyNativeEvent>],
247257
): void {
248258
for (let i = 0; i < eventPlugins.length; i++) {
249259
const pluginModule = eventPlugins[i];

packages/legacy-events/PluginModuleType.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type PluginName = string;
2222

2323
export type EventSystemFlags = number;
2424

25-
export type PluginModule<NativeEvent> = {
25+
export type LegacyPluginModule<NativeEvent> = {
2626
eventTypes: EventTypes,
2727
extractEvents: (
2828
topLevelType: TopLevelType,
@@ -34,3 +34,32 @@ export type PluginModule<NativeEvent> = {
3434
) => ?ReactSyntheticEvent,
3535
tapMoveThreshold?: number,
3636
};
37+
38+
export type DispatchQueueItemPhaseEntry = {|
39+
instance: null | Fiber,
40+
listener: Function,
41+
currentTarget: EventTarget,
42+
|};
43+
44+
export type DispatchQueueItemPhase = Array<DispatchQueueItemPhaseEntry>;
45+
46+
export type DispatchQueueItem = {|
47+
event: ReactSyntheticEvent,
48+
capture: DispatchQueueItemPhase,
49+
bubble: DispatchQueueItemPhase,
50+
|};
51+
52+
export type DispatchQueue = Array<DispatchQueueItem>;
53+
54+
export type ModernPluginModule<NativeEvent> = {
55+
eventTypes: EventTypes,
56+
extractEvents: (
57+
dispatchQueue: DispatchQueue,
58+
topLevelType: TopLevelType,
59+
targetInst: null | Fiber,
60+
nativeTarget: NativeEvent,
61+
nativeEventTarget: null | EventTarget,
62+
eventSystemFlags: number,
63+
container: null | EventTarget,
64+
) => void,
65+
};

packages/legacy-events/ReactSyntheticEventType.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ export type ReactSyntheticEvent = {|
4141
) => ReactSyntheticEvent,
4242
isPersistent: () => boolean,
4343
isPropagationStopped: () => boolean,
44-
_dispatchInstances: null | Array<Fiber | null> | Fiber,
45-
_dispatchListeners: null | Array<Function> | Function,
46-
_dispatchCurrentTargets: null | Array<EventTarget>,
44+
_dispatchInstances?: null | Array<Fiber | null> | Fiber,
45+
_dispatchListeners?: null | Array<Function> | Function,
4746
_targetInst: Fiber,
4847
type: string,
4948
currentTarget: null | EventTarget,

packages/legacy-events/SyntheticEvent.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/* eslint valid-typeof: 0 */
99

1010
import invariant from 'shared/invariant';
11+
import {enableModernEventSystem} from 'shared/ReactFeatureFlags';
1112

1213
const EVENT_POOL_SIZE = 10;
1314

@@ -76,9 +77,10 @@ function SyntheticEvent(
7677
this.dispatchConfig = dispatchConfig;
7778
this._targetInst = targetInst;
7879
this.nativeEvent = nativeEvent;
79-
this._dispatchListeners = null;
80-
this._dispatchInstances = null;
81-
this._dispatchCurrentTargets = null;
80+
if (!enableModernEventSystem) {
81+
this._dispatchListeners = null;
82+
this._dispatchInstances = null;
83+
}
8284

8385
const Interface = this.constructor.Interface;
8486
for (const propName in Interface) {
@@ -186,9 +188,10 @@ Object.assign(SyntheticEvent.prototype, {
186188
this.nativeEvent = null;
187189
this.isDefaultPrevented = functionThatReturnsFalse;
188190
this.isPropagationStopped = functionThatReturnsFalse;
189-
this._dispatchListeners = null;
190-
this._dispatchInstances = null;
191-
this._dispatchCurrentTargets = null;
191+
if (!enableModernEventSystem) {
192+
this._dispatchListeners = null;
193+
this._dispatchInstances = null;
194+
}
192195
if (__DEV__) {
193196
Object.defineProperty(
194197
this,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {DOMTopLevelEventType} from 'legacy-events/TopLevelEventTypes';
1212
import type {ElementListenerMap} from '../client/ReactDOMComponentTree';
1313
import type {EventSystemFlags} from './EventSystemFlags';
1414
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
15-
import type {PluginModule} from 'legacy-events/PluginModuleType';
15+
import type {LegacyPluginModule} from 'legacy-events/PluginModuleType';
1616
import type {ReactSyntheticEvent} from 'legacy-events/ReactSyntheticEventType';
1717
import type {TopLevelType} from 'legacy-events/TopLevelEventTypes';
1818
import forEachAccumulated from 'legacy-events/forEachAccumulated';
@@ -191,9 +191,10 @@ function extractPluginEvents(
191191
eventSystemFlags: EventSystemFlags,
192192
): Array<ReactSyntheticEvent> | ReactSyntheticEvent | null {
193193
let events = null;
194-
for (let i = 0; i < plugins.length; i++) {
194+
const legacyPlugins = ((plugins: any): Array<LegacyPluginModule<Event>>);
195+
for (let i = 0; i < legacyPlugins.length; i++) {
195196
// Not every plugin in the ordering may be loaded at runtime.
196-
const possiblePlugin: PluginModule<AnyNativeEvent> = plugins[i];
197+
const possiblePlugin = legacyPlugins[i];
197198
if (possiblePlugin) {
198199
const extractedEvents = possiblePlugin.extractEvents(
199200
topLevelType,

0 commit comments

Comments
 (0)