Skip to content

Commit a743f27

Browse files
committed
Re-add discrete flushing timeStamp heuristic
Re-add discrete flushing timeStamp heuristic Re-add discrete flushing timeStamp heuristic Re-add discrete flushing timeStamp heuristic Prettier Add alternate approach to deal with tests Add alternate approach to deal with tests Mock Date.now() instead Replace timeStamp Prettier Fix test
1 parent 5cff775 commit a743f27

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

packages/react-dom/src/__tests__/ReactDOMFiber-test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,17 @@ describe('ReactDOMFiber', () => {
10311031
const handlerA = () => ops.push('A');
10321032
const handlerB = () => ops.push('B');
10331033

1034+
function click() {
1035+
const event = new MouseEvent('click', {
1036+
bubbles: true,
1037+
cancelable: true,
1038+
});
1039+
Object.defineProperty(event, 'timeStamp', {
1040+
value: 0,
1041+
});
1042+
node.dispatchEvent(event);
1043+
}
1044+
10341045
class Example extends React.Component {
10351046
state = {flip: false, count: 0};
10361047
flip() {
@@ -1064,7 +1075,7 @@ describe('ReactDOMFiber', () => {
10641075
const node = container.firstChild;
10651076
expect(node.tagName).toEqual('DIV');
10661077

1067-
node.click();
1078+
click();
10681079

10691080
expect(ops).toEqual(['A']);
10701081
ops = [];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ function dispatchDiscreteEvent(
130130
// flushed for this event and we don't need to do it again.
131131
(eventSystemFlags & IS_LEGACY_FB_SUPPORT_MODE) === 0
132132
) {
133-
flushDiscreteUpdatesIfNeeded();
133+
flushDiscreteUpdatesIfNeeded(nativeEvent.timeStamp);
134134
}
135135
discreteUpdates(
136136
dispatchEvent,

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,25 @@ export function discreteUpdates(fn, a, b, c, d) {
8787
}
8888
}
8989

90-
export function flushDiscreteUpdatesIfNeeded() {
91-
if (!isInsideEventHandler) {
90+
let lastFlushedEventTimeStamp = 0;
91+
export function flushDiscreteUpdatesIfNeeded(timeStamp: number) {
92+
// event.timeStamp isn't overly reliable due to inconsistencies in
93+
// how different browsers have historically provided the time stamp.
94+
// Some browsers provide high-resolution time stamps for all events,
95+
// some provide low-resolution time stamps for all events. FF < 52
96+
// even mixes both time stamps together. Some browsers even report
97+
// negative time stamps or time stamps that are 0 (iOS9) in some cases.
98+
// Given we are only comparing two time stamps with equality (!==),
99+
// we are safe from the resolution differences. If the time stamp is 0
100+
// we bail-out of preventing the flush, which can affect semantics,
101+
// such as if an earlier flush removes or adds event listeners that
102+
// are fired in the subsequent flush. However, this is the same
103+
// behaviour as we had before this change, so the risks are low.
104+
if (
105+
!isInsideEventHandler &&
106+
(timeStamp === 0 || lastFlushedEventTimeStamp !== timeStamp)
107+
) {
108+
lastFlushedEventTimeStamp = timeStamp;
92109
flushDiscreteUpdatesImpl();
93110
}
94111
}

packages/react-dom/src/events/plugins/__tests__/ModernSimpleEventPlugin-test.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,14 @@ describe('SimpleEventPlugin', function() {
275275
expect(Scheduler).toFlushAndYield(['render button: enabled']);
276276

277277
function click() {
278-
button.dispatchEvent(
279-
new MouseEvent('click', {bubbles: true, cancelable: true}),
280-
);
278+
const event = new MouseEvent('click', {
279+
bubbles: true,
280+
cancelable: true,
281+
});
282+
Object.defineProperty(event, 'timeStamp', {
283+
value: 0,
284+
});
285+
button.dispatchEvent(event);
281286
}
282287

283288
// Click the button to trigger the side-effect
@@ -340,9 +345,14 @@ describe('SimpleEventPlugin', function() {
340345
expect(button.textContent).toEqual('Count: 0');
341346

342347
function click() {
343-
button.dispatchEvent(
344-
new MouseEvent('click', {bubbles: true, cancelable: true}),
345-
);
348+
const event = new MouseEvent('click', {
349+
bubbles: true,
350+
cancelable: true,
351+
});
352+
Object.defineProperty(event, 'timeStamp', {
353+
value: 0,
354+
});
355+
button.dispatchEvent(event);
346356
}
347357

348358
// Click the button a single time
@@ -421,9 +431,14 @@ describe('SimpleEventPlugin', function() {
421431
expect(button.textContent).toEqual('High-pri count: 0, Low-pri count: 0');
422432

423433
function click() {
424-
button.dispatchEvent(
425-
new MouseEvent('click', {bubbles: true, cancelable: true}),
426-
);
434+
const event = new MouseEvent('click', {
435+
bubbles: true,
436+
cancelable: true,
437+
});
438+
Object.defineProperty(event, 'timeStamp', {
439+
value: 0,
440+
});
441+
button.dispatchEvent(event);
427442
}
428443

429444
// Click the button a single time

0 commit comments

Comments
 (0)