Skip to content

Commit

Permalink
fix(schedulers): Fix Asap and AnimationFrame Schedulers to act like t…
Browse files Browse the repository at this point in the history
…he Async Scheduler if delay > 0

#1952
  • Loading branch information
trxcllnt committed Sep 18, 2016
1 parent e91d113 commit 04effb6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
13 changes: 13 additions & 0 deletions spec/schedulers/AnimationFrameScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe('Scheduler.animationFrame', () => {
}
});

it('should act like the async scheduler if delay > 0', (done: MochaDone) => {
let startTime = Date.now();
let actionHappened = false;
animationFrame.schedule(() => {
actionHappened = true;
expect(Date.now() - startTime >= 25);
done();
}, 50);
if (actionHappened) {
done(new Error('Scheduled action happened synchronously'));
}
});

it('should execute recursively scheduled actions in separate asynchronous contexts', (done: MochaDone) => {
let syncExec1 = true;
let syncExec2 = true;
Expand Down
13 changes: 13 additions & 0 deletions spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe('Scheduler.asap', () => {
}
});

it('should act like the async scheduler if delay > 0', (done: MochaDone) => {
let startTime = Date.now();
let actionHappened = false;
asap.schedule(() => {
actionHappened = true;
expect(Date.now() - startTime >= 25);
done();
}, 50);
if (actionHappened) {
done(new Error('Scheduled action happened synchronously'));
}
});

it('should execute recursively scheduled actions in separate asynchronous contexts', (done: MochaDone) => {
let syncExec1 = true;
let syncExec2 = true;
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler/AnimationFrameAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class AnimationFrameAction<T> extends AsyncAction<T> {
));
}
protected recycleAsyncId(scheduler: AnimationFrameScheduler, id?: any, delay: number = 0): any {
// If delay exists and is greater than 0, recycle as an async action.
if (delay !== null && delay > 0) {
// If delay is null or is greater than 0, recycle as an async action.
if (delay > 0 || (delay === null && this.delay > 0)) {
return super.recycleAsyncId(scheduler, id, delay);
}
// If the scheduler queue is empty, cancel the requested animation frame and
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler/AnimationFrameScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AsyncAction } from './AsyncAction';
import { AsyncScheduler } from './AsyncScheduler';

export class AnimationFrameScheduler extends AsyncScheduler {
public flush(): void {
public flush(action?: AsyncAction<any>): void {

this.active = true;
this.scheduled = undefined;
Expand All @@ -11,7 +11,7 @@ export class AnimationFrameScheduler extends AsyncScheduler {
let error: any;
let index: number = -1;
let count: number = actions.length;
let action: AsyncAction<any> = actions.shift();
action = action || actions.shift();

do {
if (error = action.execute(action.state, action.delay)) {
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler/AsapAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class AsapAction<T> extends AsyncAction<T> {
));
}
protected recycleAsyncId(scheduler: AsapScheduler, id?: any, delay: number = 0): any {
// If delay exists and is greater than 0, recycle as an async action.
if (delay !== null && delay > 0) {
// If delay is null or is greater than 0, recycle as an async action.
if (delay > 0 || (delay === null && this.delay > 0)) {
return super.recycleAsyncId(scheduler, id, delay);
}
// If the scheduler queue is empty, cancel the requested microtask and
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler/AsapScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AsyncAction } from './AsyncAction';
import { AsyncScheduler } from './AsyncScheduler';

export class AsapScheduler extends AsyncScheduler {
public flush(): void {
public flush(action?: AsyncAction<any>): void {

this.active = true;
this.scheduled = undefined;
Expand All @@ -11,7 +11,7 @@ export class AsapScheduler extends AsyncScheduler {
let error: any;
let index: number = -1;
let count: number = actions.length;
let action: AsyncAction<any> = actions.shift();
action = action || actions.shift();

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

0 comments on commit 04effb6

Please sign in to comment.