Skip to content

Commit

Permalink
Re-add discrete flushing timeStamp heuristic
Browse files Browse the repository at this point in the history
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
  • Loading branch information
trueadm committed Aug 5, 2020
1 parent 5cff775 commit a743f27
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
13 changes: 12 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMFiber-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,17 @@ describe('ReactDOMFiber', () => {
const handlerA = () => ops.push('A');
const handlerB = () => ops.push('B');

function click() {
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
Object.defineProperty(event, 'timeStamp', {
value: 0,
});
node.dispatchEvent(event);
}

class Example extends React.Component {
state = {flip: false, count: 0};
flip() {
Expand Down Expand Up @@ -1064,7 +1075,7 @@ describe('ReactDOMFiber', () => {
const node = container.firstChild;
expect(node.tagName).toEqual('DIV');

node.click();
click();

expect(ops).toEqual(['A']);
ops = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function dispatchDiscreteEvent(
// flushed for this event and we don't need to do it again.
(eventSystemFlags & IS_LEGACY_FB_SUPPORT_MODE) === 0
) {
flushDiscreteUpdatesIfNeeded();
flushDiscreteUpdatesIfNeeded(nativeEvent.timeStamp);
}
discreteUpdates(
dispatchEvent,
Expand Down
21 changes: 19 additions & 2 deletions packages/react-dom/src/events/ReactDOMUpdateBatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,25 @@ export function discreteUpdates(fn, a, b, c, d) {
}
}

export function flushDiscreteUpdatesIfNeeded() {
if (!isInsideEventHandler) {
let lastFlushedEventTimeStamp = 0;
export function flushDiscreteUpdatesIfNeeded(timeStamp: number) {
// event.timeStamp isn't overly reliable due to inconsistencies in
// how different browsers have historically provided the time stamp.
// Some browsers provide high-resolution time stamps for all events,
// some provide low-resolution time stamps for all events. FF < 52
// even mixes both time stamps together. Some browsers even report
// negative time stamps or time stamps that are 0 (iOS9) in some cases.
// Given we are only comparing two time stamps with equality (!==),
// we are safe from the resolution differences. If the time stamp is 0
// we bail-out of preventing the flush, which can affect semantics,
// such as if an earlier flush removes or adds event listeners that
// are fired in the subsequent flush. However, this is the same
// behaviour as we had before this change, so the risks are low.
if (
!isInsideEventHandler &&
(timeStamp === 0 || lastFlushedEventTimeStamp !== timeStamp)
) {
lastFlushedEventTimeStamp = timeStamp;
flushDiscreteUpdatesImpl();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,14 @@ describe('SimpleEventPlugin', function() {
expect(Scheduler).toFlushAndYield(['render button: enabled']);

function click() {
button.dispatchEvent(
new MouseEvent('click', {bubbles: true, cancelable: true}),
);
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
Object.defineProperty(event, 'timeStamp', {
value: 0,
});
button.dispatchEvent(event);
}

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

function click() {
button.dispatchEvent(
new MouseEvent('click', {bubbles: true, cancelable: true}),
);
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
Object.defineProperty(event, 'timeStamp', {
value: 0,
});
button.dispatchEvent(event);
}

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

function click() {
button.dispatchEvent(
new MouseEvent('click', {bubbles: true, cancelable: true}),
);
const event = new MouseEvent('click', {
bubbles: true,
cancelable: true,
});
Object.defineProperty(event, 'timeStamp', {
value: 0,
});
button.dispatchEvent(event);
}

// Click the button a single time
Expand Down

0 comments on commit a743f27

Please sign in to comment.