Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mock Scheduler] Mimic browser's advanceTime #17967

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ describe('ChangeEventPlugin', () => {

// 3s should be enough to expire the updates
Scheduler.unstable_advanceTime(3000);
expect(Scheduler).toFlushExpired([]);
expect(container.textContent).toEqual('hovered');
});
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ describe('ReactIncrementalUpdates', () => {

// All the updates should render and commit in a single batch.
Scheduler.unstable_advanceTime(10000);
expect(Scheduler).toHaveYielded(['Render: goodbye']);
expect(Scheduler).toFlushExpired(['Render: goodbye']);
// Passive effect
expect(Scheduler).toFlushAndYield(['Commit: goodbye']);
});
Expand Down Expand Up @@ -645,7 +645,7 @@ describe('ReactIncrementalUpdates', () => {

// All the updates should render and commit in a single batch.
Scheduler.unstable_advanceTime(10000);
expect(Scheduler).toHaveYielded([
expect(Scheduler).toFlushExpired([
'Render: goodbye',
'Commit: goodbye',
'Render: goodbye',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
</Suspense>,
);
Scheduler.unstable_advanceTime(10000);
expect(Scheduler).toHaveYielded([
expect(Scheduler).toFlushExpired([
'Suspend! [A]',
'Suspend! [B]',
'Loading...',
Expand Down Expand Up @@ -987,7 +987,7 @@ describe('ReactSuspenseWithNoopRenderer', () => {
Scheduler.unstable_advanceTime(10000);
jest.advanceTimersByTime(10000);

expect(Scheduler).toHaveYielded([
expect(Scheduler).toFlushExpired([
'Suspend! [goodbye]',
'Loading...',
'Commit: goodbye',
Expand Down
10 changes: 5 additions & 5 deletions packages/scheduler/src/__tests__/Scheduler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ describe('Scheduler', () => {

// Advance by just a bit more to expire the user blocking callbacks
Scheduler.unstable_advanceTime(1);
expect(Scheduler).toHaveYielded([
expect(Scheduler).toFlushExpired([
'B (did timeout: true)',
'C (did timeout: true)',
]);

// Expire A
Scheduler.unstable_advanceTime(4600);
expect(Scheduler).toHaveYielded(['A (did timeout: true)']);
expect(Scheduler).toFlushExpired(['A (did timeout: true)']);

// Flush the rest without expiring
expect(Scheduler).toFlushAndYield([
Expand All @@ -140,7 +140,7 @@ describe('Scheduler', () => {
expect(Scheduler).toHaveYielded([]);

Scheduler.unstable_advanceTime(1);
expect(Scheduler).toHaveYielded(['A']);
expect(Scheduler).toFlushExpired(['A']);
});

it('continues working on same task after yielding', () => {
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('Scheduler', () => {

// Advance time by just a bit more. This should expire all the remaining work.
Scheduler.unstable_advanceTime(1);
expect(Scheduler).toHaveYielded(['C', 'D']);
expect(Scheduler).toFlushExpired(['C', 'D']);
});

it('continuations are interrupted by higher priority work', () => {
Expand Down Expand Up @@ -705,7 +705,7 @@ describe('Scheduler', () => {

// Now it expires
Scheduler.unstable_advanceTime(1);
expect(Scheduler).toHaveYielded(['A']);
expect(Scheduler).toFlushExpired(['A']);
});

it('cancels a delayed task', () => {
Expand Down
11 changes: 4 additions & 7 deletions packages/scheduler/src/forks/SchedulerHostConfig.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,10 @@ export function unstable_yieldValue(value: mixed): void {

export function unstable_advanceTime(ms: number) {
currentTime += ms;
if (!isFlushing) {
if (scheduledTimeout !== null && timeoutTime <= currentTime) {
scheduledTimeout(currentTime);
timeoutTime = -1;
scheduledTimeout = null;
}
unstable_flushExpired();
if (scheduledTimeout !== null && timeoutTime <= currentTime) {
scheduledTimeout(currentTime);
timeoutTime = -1;
scheduledTimeout = null;
}
}

Expand Down