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

fix: animationFrameScheduler and asapScheduler no longer executing actions #6889

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion spec/schedulers/AnimationFrameScheduler-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { animationFrameScheduler, Subscription, merge } from 'rxjs';
import {animationFrameScheduler, Subscription, merge, SchedulerAction} from 'rxjs';
import { delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
Expand Down Expand Up @@ -238,4 +238,27 @@ describe('Scheduler.animationFrame', () => {
done();
});
});

it('should handle actions scheduled during flush before current action is rescheduled', (done) => {
const sandbox = sinon.createSandbox();

const result: string[] = [];
let reschedule = true;
function work(this: SchedulerAction<unknown>) {
result.push('work');
if (reschedule) {
animationFrameScheduler.schedule(() => result.push('task 1'));
animationFrameScheduler.schedule(() => result.push('task 2'));
this.schedule();
expect(result).to.deep.equal(['work']);
reschedule = false;
} else {
expect(result).to.deep.equal(['work', 'task 1', 'task 2', 'work']);
sandbox.restore();
done();
}
}
animationFrameScheduler.schedule(work);
expect(result).to.deep.equal([]);
});
});
25 changes: 24 additions & 1 deletion spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import { asapScheduler, Subscription, SchedulerAction, merge } from 'rxjs';
import {asapScheduler, Subscription, SchedulerAction, merge, animationFrameScheduler} from 'rxjs';
import { delay } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
Expand Down Expand Up @@ -288,4 +288,27 @@ describe('Scheduler.asap', () => {
done();
});
});

it('should handle actions scheduled during flush before current action is rescheduled', (done) => {
const sandbox = sinon.createSandbox();

const result: string[] = [];
let reschedule = true;
function work(this: SchedulerAction<unknown>) {
result.push('work');
if (reschedule) {
asapScheduler.schedule(() => result.push('task 1'));
asapScheduler.schedule(() => result.push('task 2'));
Copy link
Member

@benlesh benlesh Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add an assertion right here to make sure that the above tasks didn't execute synchronously in this spot? Basically, just check to make sure results haven't changed yet. It's not so much that I think they did, it's that I want to make sure they're "scheduled" and don't just happen to mimic the proper outcome by virtue of when we checked.

Please do the same for the other test? Thanks, @ajafff!

Otherwise, this looks great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlesh could you please review it again? Can't upgrade above 7.4.0 due to this problem.

this.schedule();
expect(result).to.deep.equal(['work']);
reschedule = false;
} else {
expect(result).to.deep.equal(['work', 'task 1', 'task 2', 'work']);
sandbox.restore();
done();
}
}
asapScheduler.schedule(work);
expect(result).to.deep.equal([]);
});
});
4 changes: 2 additions & 2 deletions src/internal/scheduler/AnimationFrameAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class AnimationFrameAction<T> extends AsyncAction<T> {
if ((delay != null && delay > 0) || (delay == null && this.delay > 0)) {
return super.recycleAsyncId(scheduler, id, delay);
}
// If the scheduler queue has no remaining actions with the same async id,
// If the scheduler queue has no remaining actions for the next schedule,
// cancel the requested animation frame and set the scheduled flag to
// undefined so the next AnimationFrameAction will request its own.
if (!scheduler.actions.some((action) => action.id === id)) {
if (scheduler._scheduled === id && !scheduler.actions.some((action) => action.id === id)) {
animationFrameProvider.cancelAnimationFrame(id);
scheduler._scheduled = undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions src/internal/scheduler/AsapAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export class AsapAction<T> extends AsyncAction<T> {
if ((delay != null && delay > 0) || (delay == null && this.delay > 0)) {
return super.recycleAsyncId(scheduler, id, delay);
}
// If the scheduler queue has no remaining actions with the same async id,
// If the scheduler queue has no remaining actions for the next schedule,
// cancel the requested microtask and set the scheduled flag to undefined
// so the next AsapAction will request its own.
if (!scheduler.actions.some((action) => action.id === id)) {
if (scheduler._scheduled === id && !scheduler.actions.some((action) => action.id === id)) {
immediateProvider.clearImmediate(id);
scheduler._scheduled = undefined;
}
Expand Down