Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions spec/schedulers/AsapScheduler-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ describe('Scheduler.asap', () => {
sandbox.restore();
});

it('should not pass setImmediate ids to clearInterval when rescheduling with a delay', (done) => {
const sandbox = sinon.createSandbox();
const setImmediateSpy = sandbox.spy(immediateProvider, 'setImmediate');
const clearIntervalSpy = sandbox.spy(intervalProvider, 'clearInterval');
const setIntervalSpy = sandbox.spy(intervalProvider, 'setInterval');

asap.schedule(function () {
try {
const immediateId = setImmediateSpy.getCall(0).returnValue;
this.schedule(undefined, 20);
expect(clearIntervalSpy).to.have.not.been.calledWith(immediateId);
expect(setIntervalSpy).to.have.been.calledOnce;
this.unsubscribe();
sandbox.restore();
done();
} catch (error) {
sandbox.restore();
done(error);
}
}, 0);
});

it('should schedule an action to happen later', (done) => {
let actionHappened = false;
asap.schedule(() => {
Expand Down
9 changes: 5 additions & 4 deletions src/internal/scheduler/AsapAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export class AsapAction<T> extends AsyncAction<T> {
}

protected recycleAsyncId(scheduler: AsapScheduler, id?: TimerHandle, delay: number = 0): TimerHandle | undefined {
// If delay exists and is greater than 0, or if the delay is null (the
// action wasn't rescheduled) but was originally scheduled as an async
// action, then recycle as an async action.
if (delay != null ? delay > 0 : this.delay > 0) {
// Only recycle as an interval action when this action was itself scheduled
// with setInterval. Immediate handles share the number space of timer IDs,
// so passing them to clearInterval can cancel unrelated intervals
// (for example timer(0, period, asapScheduler) rescheduling after the first tick).
if (this.delay > 0) {
return super.recycleAsyncId(scheduler, id, delay);
}
// If the scheduler queue has no remaining actions with the same async id,
Expand Down