Skip to content

Commit

Permalink
fix: don't execute rescheduled animation frame and asap actions in fl…
Browse files Browse the repository at this point in the history
…ush (#5399)

* test: add failing animationFrameScheduler test

* test: add failing asapScheduler test

* fix: don't execute rescheduled animation frame actions

Closes #4972 and #5397

* fix: don't execute rescheduled asap actions
  • Loading branch information
cartant authored Apr 22, 2020
1 parent b8c2a52 commit 33c9c8c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
28 changes: 28 additions & 0 deletions spec/schedulers/AnimationFrameScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,32 @@ describe('Scheduler.animationFrame', () => {
firstSubscription.unsubscribe();
}
});

it('should not execute rescheduled actions when flushing', (done: MochaDone) => {
let flushCount = 0;
let scheduledIndices: number[] = [];

let originalFlush = animationFrame.flush;
animationFrame.flush = (...args) => {
++flushCount;
originalFlush.apply(animationFrame, args);
if (flushCount === 2) {
animationFrame.flush = originalFlush;
try {
expect(scheduledIndices).to.deep.equal([0, 1]);
done();
} catch (error) {
done(error);
}
}
};

animationFrame.schedule(function (index) {
if (flushCount < 2) {
this.schedule(index! + 1);
scheduledIndices.push(index! + 1);
}
}, 0, 0);
scheduledIndices.push(0);
});
});
28 changes: 28 additions & 0 deletions spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,32 @@ describe('Scheduler.asap', () => {
firstSubscription.unsubscribe();
}
});

it('should not execute rescheduled actions when flushing', (done: MochaDone) => {
let flushCount = 0;
let scheduledIndices: number[] = [];

let originalFlush = asap.flush;
asap.flush = (...args) => {
++flushCount;
originalFlush.apply(asap, args);
if (flushCount === 2) {
asap.flush = originalFlush;
try {
expect(scheduledIndices).to.deep.equal([0, 1]);
done();
} catch (error) {
done(error);
}
}
};

asap.schedule(function (index) {
if (flushCount < 2) {
this.schedule(index! + 1);
scheduledIndices.push(index! + 1);
}
}, 0, 0);
scheduledIndices.push(0);
});
});
2 changes: 1 addition & 1 deletion src/internal/scheduler/AnimationFrameScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export class AnimationFrameScheduler extends AsyncScheduler {
const {actions} = this;
let error: any;
let index: number = -1;
let count: number = actions.length;
action = action || actions.shift()!;
let count: number = actions.length;

do {
if (error = action.execute(action.state, action.delay)) {
Expand Down
2 changes: 1 addition & 1 deletion src/internal/scheduler/AsapScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export class AsapScheduler extends AsyncScheduler {
const {actions} = this;
let error: any;
let index: number = -1;
let count: number = actions.length;
action = action || actions.shift()!;
let count: number = actions.length;

do {
if (error = action.execute(action.state, action.delay)) {
Expand Down

0 comments on commit 33c9c8c

Please sign in to comment.