From f20bc892724e970257c6a17144c9e8ae7a4d4e59 Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Sat, 3 Jun 2017 14:24:32 -0700 Subject: [PATCH] fix(AsapScheduler): Fix issue where canceling an asap or animationFrame action early could throw --- spec/schedulers/AnimationFrameScheduler-spec.ts | 15 +++++++++++++++ spec/schedulers/AsapScheduler-spec.ts | 15 +++++++++++++++ src/scheduler/AsyncAction.ts | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/spec/schedulers/AnimationFrameScheduler-spec.ts b/spec/schedulers/AnimationFrameScheduler-spec.ts index 5cd5867a8e..2f4219d03c 100644 --- a/spec/schedulers/AnimationFrameScheduler-spec.ts +++ b/spec/schedulers/AnimationFrameScheduler-spec.ts @@ -25,6 +25,21 @@ describe('Scheduler.animationFrame', () => { sandbox.restore(); }); + it('should cancel animationFrame actions when delay > 0', () => { + let actionHappened = false; + const sandbox = sinon.sandbox.create(); + const fakeTimer = sandbox.useFakeTimers(); + animationFrame.schedule(() => { + actionHappened = true; + }, 50).unsubscribe(); + expect(actionHappened).to.be.false; + fakeTimer.tick(25); + expect(actionHappened).to.be.false; + fakeTimer.tick(25); + expect(actionHappened).to.be.false; + sandbox.restore(); + }); + it('should schedule an action to happen later', (done: MochaDone) => { let actionHappened = false; animationFrame.schedule(() => { diff --git a/spec/schedulers/AsapScheduler-spec.ts b/spec/schedulers/AsapScheduler-spec.ts index 9315918e12..66c6ff5df9 100644 --- a/spec/schedulers/AsapScheduler-spec.ts +++ b/spec/schedulers/AsapScheduler-spec.ts @@ -25,6 +25,21 @@ describe('Scheduler.asap', () => { sandbox.restore(); }); + it('should cancel asap actions when delay > 0', () => { + let actionHappened = false; + const sandbox = sinon.sandbox.create(); + const fakeTimer = sandbox.useFakeTimers(); + asap.schedule(() => { + actionHappened = true; + }, 50).unsubscribe(); + expect(actionHappened).to.be.false; + fakeTimer.tick(25); + expect(actionHappened).to.be.false; + fakeTimer.tick(25); + expect(actionHappened).to.be.false; + sandbox.restore(); + }); + it('should schedule an action to happen later', (done: MochaDone) => { let actionHappened = false; asap.schedule(() => { diff --git a/src/scheduler/AsyncAction.ts b/src/scheduler/AsyncAction.ts index 891dc991b1..31a95c73ef 100644 --- a/src/scheduler/AsyncAction.ts +++ b/src/scheduler/AsyncAction.ts @@ -137,7 +137,6 @@ export class AsyncAction extends Action { const index = actions.indexOf(this); this.work = null; - this.delay = null; this.state = null; this.pending = false; this.scheduler = null; @@ -149,5 +148,7 @@ export class AsyncAction extends Action { if (id != null) { this.id = this.recycleAsyncId(scheduler, id, null); } + + this.delay = null; } }