Skip to content
Merged
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 src/BusyStatusTracker.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { expect } from 'chai';
import { Deferred } from './deferred';
import { BusyStatus, BusyStatusTracker } from './BusyStatusTracker';
import { createSandbox } from 'sinon';
const sinon = createSandbox();

describe('BusyStatusTracker', () => {
let tracker: BusyStatusTracker;

let latestStatus: BusyStatus;

beforeEach(() => {
sinon.restore();
latestStatus = BusyStatus.idle;
tracker = new BusyStatusTracker();
tracker.on('change', (value) => {
Expand All @@ -16,6 +19,7 @@ describe('BusyStatusTracker', () => {
});

afterEach(() => {
sinon.restore();
tracker?.destroy();
});

Expand Down Expand Up @@ -209,6 +213,24 @@ describe('BusyStatusTracker', () => {
expect(count).to.eql(10);
});

it('emits active-runs-change with the correct list of remaining active runs', () => {
const spy = sinon.spy();
tracker.on('active-runs-change', spy);
tracker.run(() => {
expect(tracker.status).to.eql(BusyStatus.busy);
}, 'test');
//small timeout to allow all the events to show up
expect(spy.callCount).to.eql(2);
expect(
spy.getCall(0).args[0].activeRuns.map(x => ({ label: x.label }))
).to.eql([
{ label: 'test' }
]);
expect(
spy.getCall(1).args[0].activeRuns
).to.eql([]);
});

it('removes the entry for the scope when the last run is cleared', async () => {
expect(tracker['activeRuns']).to.be.empty;

Expand Down
5 changes: 3 additions & 2 deletions src/BusyStatusTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ export class BusyStatusTracker<T = any> {
if (isFinalized === false) {
isFinalized = true;

this.emit('active-runs-change', { activeRuns: [...this.activeRuns] });

let idx = this.activeRuns.indexOf(runInfo);
if (idx > -1) {
this.activeRuns.splice(idx, 1);
}

this.emit('active-runs-change', { activeRuns: [...this.activeRuns] });

if (this.activeRuns.length <= 0) {
this.emit('change', BusyStatus.idle);
}
Expand Down