Remove usage of PossiblyWeakSet from createEventHandle (#19686)

This commit is contained in:
Dominic Gannaway 2020-08-24 16:10:23 +01:00 committed by GitHub
parent 2ada4bd0c2
commit 8c9fc4e90f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 8 deletions

View File

@ -9,7 +9,10 @@
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes'; import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
import type {ReactScopeInstance} from 'shared/ReactTypes'; import type {ReactScopeInstance} from 'shared/ReactTypes';
import type {ReactDOMEventHandleListener} from '../shared/ReactDOMTypes'; import type {
ReactDOMEventHandle,
ReactDOMEventHandleListener,
} from '../shared/ReactDOMTypes';
import type { import type {
Container, Container,
TextInstance, TextInstance,
@ -39,6 +42,7 @@ const internalPropsKey = '__reactProps$' + randomKey;
const internalContainerInstanceKey = '__reactContainer$' + randomKey; const internalContainerInstanceKey = '__reactContainer$' + randomKey;
const internalEventHandlersKey = '__reactEvents$' + randomKey; const internalEventHandlersKey = '__reactEvents$' + randomKey;
const internalEventHandlerListenersKey = '__reactListeners$' + randomKey; const internalEventHandlerListenersKey = '__reactListeners$' + randomKey;
const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
export type ElementListenerMap = Map< export type ElementListenerMap = Map<
DOMEventName | string, DOMEventName | string,
@ -232,3 +236,25 @@ export function getEventHandlerListeners(
): null | Set<ReactDOMEventHandleListener> { ): null | Set<ReactDOMEventHandleListener> {
return (scope: any)[internalEventHandlerListenersKey] || null; return (scope: any)[internalEventHandlerListenersKey] || null;
} }
export function addEventHandleToTarget(
target: EventTarget | ReactScopeInstance,
eventHandle: ReactDOMEventHandle,
): void {
let eventHandles = (target: any)[internalEventHandlesSetKey];
if (eventHandles === undefined) {
eventHandles = (target: any)[internalEventHandlesSetKey] = new Set();
}
eventHandles.add(eventHandle);
}
export function doesTargetHaveEventHandle(
target: EventTarget | ReactScopeInstance,
eventHandle: ReactDOMEventHandle,
): boolean {
const eventHandles = (target: any)[internalEventHandlesSetKey];
if (eventHandles === undefined) {
return false;
}
return eventHandles.has(eventHandle);
}

View File

@ -20,6 +20,8 @@ import {
getEventHandlerListeners, getEventHandlerListeners,
setEventHandlerListeners, setEventHandlerListeners,
getFiberFromScopeInstance, getFiberFromScopeInstance,
doesTargetHaveEventHandle,
addEventHandleToTarget,
} from './ReactDOMComponentTree'; } from './ReactDOMComponentTree';
import {ELEMENT_NODE, COMMENT_NODE} from '../shared/HTMLNodeType'; import {ELEMENT_NODE, COMMENT_NODE} from '../shared/HTMLNodeType';
import { import {
@ -42,8 +44,6 @@ type EventHandleOptions = {|
priority?: EventPriority, priority?: EventPriority,
|}; |};
const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set;
function getNearestRootOrPortalContainer(node: Fiber): null | Element { function getNearestRootOrPortalContainer(node: Fiber): null | Element {
while (node !== null) { while (node !== null) {
const tag = node.tag; const tag = node.tag;
@ -201,9 +201,7 @@ export function createEventHandle(
listenerPriority = getEventPriorityForListenerSystem(domEventName); listenerPriority = getEventPriorityForListenerSystem(domEventName);
} }
const registeredReactDOMEvents = new PossiblyWeakSet(); const eventHandle = (
return (
target: EventTarget | ReactScopeInstance, target: EventTarget | ReactScopeInstance,
callback: (SyntheticEvent<EventTarget>) => void, callback: (SyntheticEvent<EventTarget>) => void,
) => { ) => {
@ -212,8 +210,8 @@ export function createEventHandle(
'ReactDOM.createEventHandle: setter called with an invalid ' + 'ReactDOM.createEventHandle: setter called with an invalid ' +
'callback. The callback must be a function.', 'callback. The callback must be a function.',
); );
if (!registeredReactDOMEvents.has(target)) { if (!doesTargetHaveEventHandle(target, eventHandle)) {
registeredReactDOMEvents.add(target); addEventHandleToTarget(target, eventHandle);
registerReactDOMEvent( registerReactDOMEvent(
target, target,
domEventName, domEventName,
@ -241,6 +239,8 @@ export function createEventHandle(
); );
}; };
}; };
return eventHandle;
} }
return (null: any); return (null: any);
} }