Remove usage of PossiblyWeakSet from createEventHandle (#19686)
Dominic Gannaway committed
Aug 24, 2020 at 16:10 UTC
8c9fc4e90f58fab25c2991ac60ddb6a6afe9271c
2 files changed
+34
-8
packages/react-dom/src/client/ReactDOMComponentTree.js
+27
-1
@@ -9,7 +9,10 @@
9
10
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
11
import type {ReactScopeInstance} from 'shared/ReactTypes';
12
-import type {ReactDOMEventHandleListener} from '../shared/ReactDOMTypes';
12
+import type {
13
+ ReactDOMEventHandle,
14
+ ReactDOMEventHandleListener,
15
+} from '../shared/ReactDOMTypes';
16
import type {
17
Container,
18
TextInstance,
@@ -39,6 +42,7 @@ const internalPropsKey = '__reactProps$' + randomKey;
42
const internalContainerInstanceKey = '__reactContainer$' + randomKey;
43
const internalEventHandlersKey = '__reactEvents$' + randomKey;
44
const internalEventHandlerListenersKey = '__reactListeners$' + randomKey;
45
+const internalEventHandlesSetKey = '__reactHandles$' + randomKey;
46
47
export type ElementListenerMap = Map<
48
DOMEventName | string,
@@ -232,3 +236,25 @@ export function getEventHandlerListeners(
236
): null | Set<ReactDOMEventHandleListener> {
237
return (scope: any)[internalEventHandlerListenersKey] || null;
238
}
239
+
240
+export function addEventHandleToTarget(
241
+ target: EventTarget | ReactScopeInstance,
242
+ eventHandle: ReactDOMEventHandle,
243
+): void {
244
+ let eventHandles = (target: any)[internalEventHandlesSetKey];
245
+ if (eventHandles === undefined) {
246
+ eventHandles = (target: any)[internalEventHandlesSetKey] = new Set();
247
+ }
248
+ eventHandles.add(eventHandle);
249
+}
250
+
251
+export function doesTargetHaveEventHandle(
252
+ target: EventTarget | ReactScopeInstance,
253
+ eventHandle: ReactDOMEventHandle,
254
+): boolean {
255
+ const eventHandles = (target: any)[internalEventHandlesSetKey];
256
+ if (eventHandles === undefined) {
257
+ return false;
258
+ }
259
+ return eventHandles.has(eventHandle);
260
+}
packages/react-dom/src/client/ReactDOMEventHandle.js
+7
-7
@@ -20,6 +20,8 @@ import {
20
getEventHandlerListeners,
21
setEventHandlerListeners,
22
getFiberFromScopeInstance,
23
+ doesTargetHaveEventHandle,
24
+ addEventHandleToTarget,
25
} from './ReactDOMComponentTree';
26
import {ELEMENT_NODE, COMMENT_NODE} from '../shared/HTMLNodeType';
27
import {
@@ -42,8 +44,6 @@ type EventHandleOptions = {|
44
priority?: EventPriority,
45
|};
46
45
-const PossiblyWeakSet = typeof WeakSet === 'function' ? WeakSet : Set;
46
-
47
function getNearestRootOrPortalContainer(node: Fiber): null | Element {
48
while (node !== null) {
49
const tag = node.tag;
@@ -201,9 +201,7 @@ export function createEventHandle(
201
listenerPriority = getEventPriorityForListenerSystem(domEventName);
202
}
203
204
- const registeredReactDOMEvents = new PossiblyWeakSet();
205
-
206
- return (
204
+ const eventHandle = (
205
target: EventTarget | ReactScopeInstance,
206
callback: (SyntheticEvent<EventTarget>) => void,
207
) => {
@@ -212,8 +210,8 @@ export function createEventHandle(
210
'ReactDOM.createEventHandle: setter called with an invalid ' +
211
'callback. The callback must be a function.',
212
);
215
- if (!registeredReactDOMEvents.has(target)) {
216
- registeredReactDOMEvents.add(target);
213
+ if (!doesTargetHaveEventHandle(target, eventHandle)) {
214
+ addEventHandleToTarget(target, eventHandle);
215
registerReactDOMEvent(
216
target,
217
domEventName,
@@ -241,6 +239,8 @@ export function createEventHandle(
239
);
240
};
241
};
242
+
243
+ return eventHandle;
244
}
245
return (null: any);
246
}