mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
e9f5ad2584
* Remove dead code branch This function is only called when initializing roots/containers (where we skip non-delegated events) and in the createEventHandle path for non-DOM nodes (where we never hit this path because targetElement is null). * Move related functions close to each other * Fork listenToNativeEvent for createEventHandle It doesn't need all of the logic that's needed for normal event path. And the normal codepath doesn't use the last two arguments. * Expand test coverage for non-delegated events This changes a test to fail if we removed the event handler Sets. Previously, we didn't cover that. * Add DEV-level check that top-level events and non-delegated events do not overlap This makes us confident that they're mutually exclusive and there is no duplication between them. * Add a test verifying selectionchange deduplication This is why we still need the Set bookkeeping. Adding a test for it. * Remove Set bookkeeping for root events Root events don't intersect with non-delegated bubbled events (so no need to deduplicate there). They also don't intersect with createEventHandle non-managed events (because those don't go on the DOM elements). So we can remove the bookeeping because we already have code ensuring the eager subscriptions only run once per element. I've moved the selectionchange special case outside, and added document-level deduplication for it alone. Technically this might change the behavior of createEventHandle with selectionchange on the document, but we're not using that, and I'm not sure that behavior makes sense anyway. * Flow
149 lines
4.7 KiB
JavaScript
149 lines
4.7 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import type {DOMEventName} from '../events/DOMEventNames';
|
|
import type {ReactScopeInstance} from 'shared/ReactTypes';
|
|
import type {
|
|
ReactDOMEventHandle,
|
|
ReactDOMEventHandleListener,
|
|
} from '../shared/ReactDOMTypes';
|
|
|
|
import {allNativeEvents} from '../events/EventRegistry';
|
|
import {
|
|
getEventHandlerListeners,
|
|
setEventHandlerListeners,
|
|
doesTargetHaveEventHandle,
|
|
addEventHandleToTarget,
|
|
} from './ReactDOMComponentTree';
|
|
import {ELEMENT_NODE} from '../shared/HTMLNodeType';
|
|
import {listenToNativeEventForNonManagedEventTarget} from '../events/DOMPluginEventSystem';
|
|
|
|
import {
|
|
enableScopeAPI,
|
|
enableCreateEventHandleAPI,
|
|
} from 'shared/ReactFeatureFlags';
|
|
import invariant from 'shared/invariant';
|
|
|
|
type EventHandleOptions = {|
|
|
capture?: boolean,
|
|
|};
|
|
|
|
function isValidEventTarget(target: EventTarget | ReactScopeInstance): boolean {
|
|
return typeof (target: Object).addEventListener === 'function';
|
|
}
|
|
|
|
function isReactScope(target: EventTarget | ReactScopeInstance): boolean {
|
|
return typeof (target: Object).getChildContextValues === 'function';
|
|
}
|
|
|
|
function createEventHandleListener(
|
|
type: DOMEventName,
|
|
isCapturePhaseListener: boolean,
|
|
callback: (SyntheticEvent<EventTarget>) => void,
|
|
): ReactDOMEventHandleListener {
|
|
return {
|
|
callback,
|
|
capture: isCapturePhaseListener,
|
|
type,
|
|
};
|
|
}
|
|
|
|
function registerReactDOMEvent(
|
|
target: EventTarget | ReactScopeInstance,
|
|
domEventName: DOMEventName,
|
|
isCapturePhaseListener: boolean,
|
|
): void {
|
|
if ((target: any).nodeType === ELEMENT_NODE) {
|
|
// Do nothing. We already attached all root listeners.
|
|
} else if (enableScopeAPI && isReactScope(target)) {
|
|
// Do nothing. We already attached all root listeners.
|
|
} else if (isValidEventTarget(target)) {
|
|
const eventTarget = ((target: any): EventTarget);
|
|
// These are valid event targets, but they are also
|
|
// non-managed React nodes.
|
|
listenToNativeEventForNonManagedEventTarget(
|
|
domEventName,
|
|
isCapturePhaseListener,
|
|
eventTarget,
|
|
);
|
|
} else {
|
|
invariant(
|
|
false,
|
|
'ReactDOM.createEventHandle: setter called on an invalid ' +
|
|
'target. Provide a valid EventTarget or an element managed by React.',
|
|
);
|
|
}
|
|
}
|
|
|
|
export function createEventHandle(
|
|
type: string,
|
|
options?: EventHandleOptions,
|
|
): ReactDOMEventHandle {
|
|
if (enableCreateEventHandleAPI) {
|
|
const domEventName = ((type: any): DOMEventName);
|
|
|
|
// We cannot support arbitrary native events with eager root listeners
|
|
// because the eager strategy relies on knowing the whole list ahead of time.
|
|
// If we wanted to support this, we'd have to add code to keep track
|
|
// (or search) for all portal and root containers, and lazily add listeners
|
|
// to them whenever we see a previously unknown event. This seems like a lot
|
|
// of complexity for something we don't even have a particular use case for.
|
|
// Unfortunately, the downside of this invariant is that *removing* a native
|
|
// event from the list of known events has now become a breaking change for
|
|
// any code relying on the createEventHandle API.
|
|
invariant(
|
|
allNativeEvents.has(domEventName),
|
|
'Cannot call unstable_createEventHandle with "%s", as it is not an event known to React.',
|
|
domEventName,
|
|
);
|
|
|
|
let isCapturePhaseListener = false;
|
|
if (options != null) {
|
|
const optionsCapture = options.capture;
|
|
if (typeof optionsCapture === 'boolean') {
|
|
isCapturePhaseListener = optionsCapture;
|
|
}
|
|
}
|
|
|
|
const eventHandle = (
|
|
target: EventTarget | ReactScopeInstance,
|
|
callback: (SyntheticEvent<EventTarget>) => void,
|
|
) => {
|
|
invariant(
|
|
typeof callback === 'function',
|
|
'ReactDOM.createEventHandle: setter called with an invalid ' +
|
|
'callback. The callback must be a function.',
|
|
);
|
|
if (!doesTargetHaveEventHandle(target, eventHandle)) {
|
|
addEventHandleToTarget(target, eventHandle);
|
|
registerReactDOMEvent(target, domEventName, isCapturePhaseListener);
|
|
}
|
|
const listener = createEventHandleListener(
|
|
domEventName,
|
|
isCapturePhaseListener,
|
|
callback,
|
|
);
|
|
let targetListeners = getEventHandlerListeners(target);
|
|
if (targetListeners === null) {
|
|
targetListeners = new Set();
|
|
setEventHandlerListeners(target, targetListeners);
|
|
}
|
|
targetListeners.add(listener);
|
|
return () => {
|
|
((targetListeners: any): Set<ReactDOMEventHandleListener>).delete(
|
|
listener,
|
|
);
|
|
};
|
|
};
|
|
|
|
return eventHandle;
|
|
}
|
|
return (null: any);
|
|
}
|