Skip to content

Commit 9def56e

Browse files
authored
Refactor DOM plugin system to single module (#18025)
1 parent 2d6be75 commit 9def56e

19 files changed

+470
-403
lines changed

packages/legacy-events/EventPluginHub.js

-176
This file was deleted.

packages/legacy-events/EventPluginRegistry.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function publishEventForPlugin(
8989
): boolean {
9090
invariant(
9191
!eventNameDispatchConfigs.hasOwnProperty(eventName),
92-
'EventPluginHub: More than one plugin attempted to publish the same ' +
92+
'EventPluginRegistry: More than one plugin attempted to publish the same ' +
9393
'event name, `%s`.',
9494
eventName,
9595
);
@@ -133,7 +133,7 @@ function publishRegistrationName(
133133
): void {
134134
invariant(
135135
!registrationNameModules[registrationName],
136-
'EventPluginHub: More than one plugin attempted to publish the same ' +
136+
'EventPluginRegistry: More than one plugin attempted to publish the same ' +
137137
'registration name, `%s`.',
138138
registrationName,
139139
);
@@ -153,8 +153,6 @@ function publishRegistrationName(
153153

154154
/**
155155
* Registers plugins so that they can extract and dispatch events.
156-
*
157-
* @see {EventPluginHub}
158156
*/
159157

160158
/**
@@ -193,7 +191,6 @@ export const possibleRegistrationNames = __DEV__ ? {} : (null: any);
193191
*
194192
* @param {array} InjectedEventPluginOrder
195193
* @internal
196-
* @see {EventPluginHub.injection.injectEventPluginOrder}
197194
*/
198195
export function injectEventPluginOrder(
199196
injectedEventPluginOrder: EventPluginOrder,
@@ -209,14 +206,13 @@ export function injectEventPluginOrder(
209206
}
210207

211208
/**
212-
* Injects plugins to be used by `EventPluginHub`. The plugin names must be
209+
* Injects plugins to be used by plugin event system. The plugin names must be
213210
* in the ordering injected by `injectEventPluginOrder`.
214211
*
215212
* Plugins can be injected as part of page initialization or on-the-fly.
216213
*
217214
* @param {object} injectedNamesToPlugins Map from names to plugin modules.
218215
* @internal
219-
* @see {EventPluginHub.injection.injectEventPluginsByName}
220216
*/
221217
export function injectEventPluginsByName(
222218
injectedNamesToPlugins: NamesToPlugins,

packages/legacy-events/EventPropagators.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
traverseEnterLeave,
1212
} from 'shared/ReactTreeTraversal';
1313

14-
import {getListener} from './EventPluginHub';
14+
import getListener from 'legacy-events/getListener';
1515
import accumulateInto from './accumulateInto';
1616
import forEachAccumulated from './forEachAccumulated';
1717

packages/legacy-events/ResponderEventPlugin.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ to return true:wantsResponderID| |
282282
+ + */
283283

284284
/**
285-
* A note about event ordering in the `EventPluginHub`.
285+
* A note about event ordering in the `EventPluginRegistry`.
286286
*
287287
* Suppose plugins are injected in the following order:
288288
*
@@ -301,7 +301,7 @@ to return true:wantsResponderID| |
301301
* - When returned from `extractEvents`, deferred-dispatched events contain an
302302
* "accumulation" of deferred dispatches.
303303
* - These deferred dispatches are accumulated/collected before they are
304-
* returned, but processed at a later time by the `EventPluginHub` (hence the
304+
* returned, but processed at a later time by the `EventPluginRegistry` (hence the
305305
* name deferred).
306306
*
307307
* In the process of returning their deferred-dispatched events, event plugins
@@ -325,9 +325,9 @@ to return true:wantsResponderID| |
325325
* - `R`s on-demand events (if any) (dispatched by `R` on-demand)
326326
* - `S`s on-demand events (if any) (dispatched by `S` on-demand)
327327
* - `C`s on-demand events (if any) (dispatched by `C` on-demand)
328-
* - `R`s extracted events (if any) (dispatched by `EventPluginHub`)
329-
* - `S`s extracted events (if any) (dispatched by `EventPluginHub`)
330-
* - `C`s extracted events (if any) (dispatched by `EventPluginHub`)
328+
* - `R`s extracted events (if any) (dispatched by `EventPluginRegistry`)
329+
* - `S`s extracted events (if any) (dispatched by `EventPluginRegistry`)
330+
* - `C`s extracted events (if any) (dispatched by `EventPluginRegistry`)
331331
*
332332
* In the case of `ResponderEventPlugin`: If the `startShouldSetResponder`
333333
* on-demand dispatch returns `true` (and some other details are satisfied) the
@@ -336,9 +336,9 @@ to return true:wantsResponderID| |
336336
* will appear as follows:
337337
*
338338
* - `startShouldSetResponder` (`ResponderEventPlugin` dispatches on-demand)
339-
* - `touchStartCapture` (`EventPluginHub` dispatches as usual)
340-
* - `touchStart` (`EventPluginHub` dispatches as usual)
341-
* - `responderGrant/Reject` (`EventPluginHub` dispatches as usual)
339+
* - `touchStartCapture` (`EventPluginRegistry` dispatches as usual)
340+
* - `touchStart` (`EventPluginRegistry` dispatches as usual)
341+
* - `responderGrant/Reject` (`EventPluginRegistry` dispatches as usual)
342342
*/
343343

344344
function setResponderAndExtractTransfer(

packages/legacy-events/__tests__/EventPluginRegistry-test.internal.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe('EventPluginRegistry', () => {
211211
expect(function() {
212212
EventPluginRegistry.injectEventPluginOrder(['one', 'two']);
213213
}).toThrowError(
214-
'EventPluginHub: More than one plugin attempted to publish the same ' +
214+
'EventPluginRegistry: More than one plugin attempted to publish the same ' +
215215
'registration name, `onPhotoCapture`.',
216216
);
217217
});

packages/legacy-events/getListener.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
* @flow
7+
*/
8+
9+
import type {Fiber} from 'react-reconciler/src/ReactFiber';
10+
11+
import invariant from 'shared/invariant';
12+
13+
import {getFiberCurrentPropsFromNode} from './EventPluginUtils';
14+
15+
function isInteractive(tag) {
16+
return (
17+
tag === 'button' ||
18+
tag === 'input' ||
19+
tag === 'select' ||
20+
tag === 'textarea'
21+
);
22+
}
23+
24+
function shouldPreventMouseEvent(name, type, props) {
25+
switch (name) {
26+
case 'onClick':
27+
case 'onClickCapture':
28+
case 'onDoubleClick':
29+
case 'onDoubleClickCapture':
30+
case 'onMouseDown':
31+
case 'onMouseDownCapture':
32+
case 'onMouseMove':
33+
case 'onMouseMoveCapture':
34+
case 'onMouseUp':
35+
case 'onMouseUpCapture':
36+
case 'onMouseEnter':
37+
return !!(props.disabled && isInteractive(type));
38+
default:
39+
return false;
40+
}
41+
}
42+
43+
/**
44+
* @param {object} inst The instance, which is the source of events.
45+
* @param {string} registrationName Name of listener (e.g. `onClick`).
46+
* @return {?function} The stored callback.
47+
*/
48+
export default function getListener(inst: Fiber, registrationName: string) {
49+
let listener;
50+
51+
// TODO: shouldPreventMouseEvent is DOM-specific and definitely should not
52+
// live here; needs to be moved to a better place soon
53+
const stateNode = inst.stateNode;
54+
if (!stateNode) {
55+
// Work in progress (ex: onload events in incremental mode).
56+
return null;
57+
}
58+
const props = getFiberCurrentPropsFromNode(stateNode);
59+
if (!props) {
60+
// Work in progress.
61+
return null;
62+
}
63+
listener = props[registrationName];
64+
if (shouldPreventMouseEvent(registrationName, inst.type, props)) {
65+
return null;
66+
}
67+
invariant(
68+
!listener || typeof listener === 'function',
69+
'Expected `%s` listener to be a function, instead got a value of `%s` type.',
70+
registrationName,
71+
typeof listener,
72+
);
73+
return listener;
74+
}

packages/react-dom/src/__tests__/EventPluginHub-test.js packages/react-dom/src/__tests__/InvalidEventListeners-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
jest.mock('../events/isEventSupported');
1313

14-
describe('EventPluginHub', () => {
14+
describe('InvalidEventListeners', () => {
1515
let React;
1616
let ReactTestUtils;
1717

0 commit comments

Comments
 (0)