Skip to content

Commit 01d2f72

Browse files
committed
fix: animationFrameScheduler prematurely executeing actions without delay
Fixes: #6891
1 parent 4319c90 commit 01d2f72

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

spec/schedulers/AnimationFrameScheduler-spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,26 @@ describe('Scheduler.animationFrame', () => {
259259
}
260260
animationFrameScheduler.schedule(work);
261261
});
262+
263+
it('should execute actions with delay separate from all other actions', () => {
264+
const sandbox = sinon.createSandbox();
265+
const timers = sandbox.useFakeTimers();
266+
let rafCallback!: () => void;
267+
const stub = sinon.stub(animationFrameProvider, 'requestAnimationFrame').callsFake((cb) => rafCallback = cb);
268+
269+
let asyncExecuted = false;
270+
let animationFrameExecuted = false
271+
animationFrameScheduler.schedule(() => asyncExecuted = true, 1);
272+
animationFrameScheduler.schedule(() => animationFrameExecuted = true);
273+
274+
timers.tick(1);
275+
expect(asyncExecuted).to.equal(true);
276+
expect(animationFrameExecuted).to.equal(false);
277+
278+
rafCallback();
279+
expect(animationFrameExecuted).to.equal(true);
280+
281+
stub.restore();
282+
sandbox.restore();
283+
});
262284
});

src/internal/scheduler/AnimationFrameScheduler.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import { AsyncScheduler } from './AsyncScheduler';
33

44
export class AnimationFrameScheduler extends AsyncScheduler {
55
public flush(action?: AsyncAction<any>): void {
6+
let error: any;
7+
8+
if (action) {
9+
// This code path handles AsyncActions scheduled with delay.
10+
// These are not executed from _scheduled nor part of the actions queue.
11+
this._active = true;
12+
error = action.execute(action.state, action.delay);
13+
this._active = false;
14+
if (error) {
15+
throw error;
16+
}
17+
return;
18+
}
19+
620
this._active = true;
721
// The async id that effects a call to flush is stored in _scheduled.
822
// Before executing an action, it's necessary to check the action's async
@@ -17,8 +31,7 @@ export class AnimationFrameScheduler extends AsyncScheduler {
1731
this._scheduled = undefined;
1832

1933
const { actions } = this;
20-
let error: any;
21-
action = action || actions.shift()!;
34+
action = actions.shift()!;
2235

2336
do {
2437
if ((error = action.execute(action.state, action.delay))) {

0 commit comments

Comments
 (0)