Skip to content

Commit be603f5

Browse files
authored
[react-events] Remove lastNativeEvent in favor of SystemFlags (#17585)
1 parent b15bf36 commit be603f5

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

packages/legacy-events/EventSystemFlags.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export const IS_PASSIVE = 1 << 2;
1515
export const IS_ACTIVE = 1 << 3;
1616
export const PASSIVE_NOT_SUPPORTED = 1 << 4;
1717
export const IS_REPLAYED = 1 << 5;
18+
export const IS_FIRST_ANCESTOR = 1 << 6;

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TOP_POINTER_OUT,
1414
TOP_POINTER_OVER,
1515
} from './DOMTopLevelEventTypes';
16-
import {IS_REPLAYED} from 'legacy-events/EventSystemFlags';
16+
import {IS_REPLAYED, IS_FIRST_ANCESTOR} from 'legacy-events/EventSystemFlags';
1717
import SyntheticMouseEvent from './SyntheticMouseEvent';
1818
import SyntheticPointerEvent from './SyntheticPointerEvent';
1919
import {
@@ -42,12 +42,6 @@ const eventTypes = {
4242
},
4343
};
4444

45-
// We track the lastNativeEvent to ensure that when we encounter
46-
// cases where we process the same nativeEvent multiple times,
47-
// which can happen when have multiple ancestors, that we don't
48-
// duplicate enter
49-
let lastNativeEvent;
50-
5145
const EnterLeaveEventPlugin = {
5246
eventTypes: eventTypes,
5347

@@ -169,11 +163,12 @@ const EnterLeaveEventPlugin = {
169163

170164
accumulateEnterLeaveDispatches(leave, enter, from, to);
171165

172-
if (nativeEvent === lastNativeEvent) {
173-
lastNativeEvent = null;
166+
// If we are not processing the first ancestor, then we
167+
// should not process the same nativeEvent again, as we
168+
// will have already processed it in the first ancestor.
169+
if ((eventSystemFlags & IS_FIRST_ANCESTOR) === 0) {
174170
return [leave];
175171
}
176-
lastNativeEvent = nativeEvent;
177172

178173
return [leave, enter];
179174
},

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
IS_PASSIVE,
5050
IS_ACTIVE,
5151
PASSIVE_NOT_SUPPORTED,
52+
IS_FIRST_ANCESTOR,
5253
} from 'legacy-events/EventSystemFlags';
5354

5455
import {
@@ -175,13 +176,19 @@ function handleTopLevel(bookKeeping: BookKeepingInstance) {
175176
const eventTarget = getEventTarget(bookKeeping.nativeEvent);
176177
const topLevelType = ((bookKeeping.topLevelType: any): DOMTopLevelEventType);
177178
const nativeEvent = ((bookKeeping.nativeEvent: any): AnyNativeEvent);
179+
let eventSystemFlags = bookKeeping.eventSystemFlags;
180+
181+
// If this is the first ancestor, we mark it on the system flags
182+
if (i === 0) {
183+
eventSystemFlags |= IS_FIRST_ANCESTOR;
184+
}
178185

179186
runExtractedPluginEventsInBatch(
180187
topLevelType,
181188
targetInst,
182189
nativeEvent,
183190
eventTarget,
184-
bookKeeping.eventSystemFlags,
191+
eventSystemFlags,
185192
);
186193
}
187194
}

0 commit comments

Comments
 (0)