From 04effb68e738652d27211a9e3123ec49b86cdcac Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Sat, 17 Sep 2016 19:54:06 -0700 Subject: [PATCH] fix(schedulers): Fix Asap and AnimationFrame Schedulers to act like the Async Scheduler if delay > 0 #1952 --- spec/schedulers/AnimationFrameScheduler-spec.ts | 13 +++++++++++++ spec/schedulers/AsapScheduler-spec.ts | 13 +++++++++++++ src/scheduler/AnimationFrameAction.ts | 4 ++-- src/scheduler/AnimationFrameScheduler.ts | 4 ++-- src/scheduler/AsapAction.ts | 4 ++-- src/scheduler/AsapScheduler.ts | 4 ++-- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/spec/schedulers/AnimationFrameScheduler-spec.ts b/spec/schedulers/AnimationFrameScheduler-spec.ts index 1b0b75e9e29..dd27f4793ff 100644 --- a/spec/schedulers/AnimationFrameScheduler-spec.ts +++ b/spec/schedulers/AnimationFrameScheduler-spec.ts @@ -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; diff --git a/spec/schedulers/AsapScheduler-spec.ts b/spec/schedulers/AsapScheduler-spec.ts index 9cb1e338be6..63f50818a29 100644 --- a/spec/schedulers/AsapScheduler-spec.ts +++ b/spec/schedulers/AsapScheduler-spec.ts @@ -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; diff --git a/src/scheduler/AnimationFrameAction.ts b/src/scheduler/AnimationFrameAction.ts index 5fd19ef20c0..e6b0b661423 100644 --- a/src/scheduler/AnimationFrameAction.ts +++ b/src/scheduler/AnimationFrameAction.ts @@ -29,8 +29,8 @@ export class AnimationFrameAction extends AsyncAction { )); } 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 diff --git a/src/scheduler/AnimationFrameScheduler.ts b/src/scheduler/AnimationFrameScheduler.ts index 0d91b33d639..c550429f171 100644 --- a/src/scheduler/AnimationFrameScheduler.ts +++ b/src/scheduler/AnimationFrameScheduler.ts @@ -2,7 +2,7 @@ import { AsyncAction } from './AsyncAction'; import { AsyncScheduler } from './AsyncScheduler'; export class AnimationFrameScheduler extends AsyncScheduler { - public flush(): void { + public flush(action?: AsyncAction): void { this.active = true; this.scheduled = undefined; @@ -11,7 +11,7 @@ export class AnimationFrameScheduler extends AsyncScheduler { let error: any; let index: number = -1; let count: number = actions.length; - let action: AsyncAction = actions.shift(); + action = action || actions.shift(); do { if (error = action.execute(action.state, action.delay)) { diff --git a/src/scheduler/AsapAction.ts b/src/scheduler/AsapAction.ts index f63d615e1e2..eddf201808a 100644 --- a/src/scheduler/AsapAction.ts +++ b/src/scheduler/AsapAction.ts @@ -29,8 +29,8 @@ export class AsapAction extends AsyncAction { )); } 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 diff --git a/src/scheduler/AsapScheduler.ts b/src/scheduler/AsapScheduler.ts index 0a1eb740d5c..659aa5823cb 100644 --- a/src/scheduler/AsapScheduler.ts +++ b/src/scheduler/AsapScheduler.ts @@ -2,7 +2,7 @@ import { AsyncAction } from './AsyncAction'; import { AsyncScheduler } from './AsyncScheduler'; export class AsapScheduler extends AsyncScheduler { - public flush(): void { + public flush(action?: AsyncAction): void { this.active = true; this.scheduled = undefined; @@ -11,7 +11,7 @@ export class AsapScheduler extends AsyncScheduler { let error: any; let index: number = -1; let count: number = actions.length; - let action: AsyncAction = actions.shift(); + action = action || actions.shift(); do { if (error = action.execute(action.state, action.delay)) {