Skip to content

Commit

Permalink
fix: onStart firing whenever runner is paused/unpaused
Browse files Browse the repository at this point in the history
  • Loading branch information
samrith-s committed Dec 9, 2023
1 parent 243ceea commit b146cab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/TaskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,8 @@ export class TaskRunner<T = any> {
}

#run() {
if (!this.#_destroyed && !this.#_paused) {
if (
!!this.#total &&
this.#completed < this.#total &&
this.#running < this.#_concurrency
) {
if (this.#completed !== this.#total) {
if (!this.#_paused && this.#running < this.#_concurrency) {
const difference = this.#_concurrency - this.#running;

const tasks = this.#_pending.removeRange(0, difference) as Tasks<T>;
Expand All @@ -145,17 +141,17 @@ export class TaskRunner<T = any> {
});

this.#_busy = true;
} else {
this.#_duration.end = Date.now();
this.#_duration.total = Math.ceil(
this.#_duration.end - this.#_duration.start
);
}
} else {
this.#_duration.end = Date.now();
this.#_duration.total = Math.ceil(
this.#_duration.end - this.#_duration.start
);

/* istanbul ignore next */
this.#runHook(RunnerEvents.END);
/* istanbul ignore next */
this.#runHook(RunnerEvents.END);

this.#_busy = false;
}
this.#_busy = false;
}
}

Expand Down Expand Up @@ -254,7 +250,9 @@ export class TaskRunner<T = any> {
return false;
}

if (this.#_paused) {
const previousPause = this.#_paused;

if (previousPause) {
this.#_paused = false;
}

Expand All @@ -265,7 +263,9 @@ export class TaskRunner<T = any> {
this.#_duration.start = Date.now();

/* istanbul ignore next */
this.#runHook(RunnerEvents.START);
if (!previousPause) {
this.#runHook(RunnerEvents.START);
}
this.#run();

return true;
Expand Down
27 changes: 27 additions & 0 deletions src/__tests__/TaskRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ describe("TaskRunner", () => {

runner.destroy();
});

it("should not fire `start` hook when going from paused to unpaused state", () => {
jest.useFakeTimers();

const onStart = jest.fn();
const onPause = jest.fn();
const runner = createRunner();

runner.addListener(CT.RunnerEvents.START, onStart);
runner.addListener(CT.RunnerEvents.PAUSE, onPause);

runner.start();

expect(onStart).toHaveBeenCalledTimes(1);

runner.pause();

jest.advanceTimersByTime(10000);

expect(onPause).toHaveBeenCalledTimes(1);

runner.start();

expect(onStart).toHaveBeenCalledTimes(1);

runner.destroy();
});
});

describe("off", () => {
Expand Down

0 comments on commit b146cab

Please sign in to comment.