From 7205f6e40c183b3ea80ccb4174d1fdd7c54f7020 Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Wed, 14 Jul 2021 19:48:06 +0300 Subject: [PATCH] Add removeById (#35) --- README.md | 1 + lib/toadScheduler.ts | 11 ++++++++ package.json | 14 +++++------ test/toadScheduler.spec.ts | 51 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 30bf91f..fcb2022 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ Note that in order to avoid memory leaks, it is recommended to use promise chain * `stop(): void` - stops all jobs, registered in the scheduler; * `getById(id: string): Job` - returns the job with a given id. * `stopById(id: string): void` - stops the job with a given id. +* `removeById(id: string): Job | undefined` - stops the job with a given id and removes it from the scheduler. If no such job exists, returns `undefined`, otherwise returns the job. * `startById(id: string): void` - starts, or restarts (if it's already running) the job with a given id. [npm-image]: https://img.shields.io/npm/v/toad-scheduler.svg diff --git a/lib/toadScheduler.ts b/lib/toadScheduler.ts index e36cc31..67b7877 100644 --- a/lib/toadScheduler.ts +++ b/lib/toadScheduler.ts @@ -44,6 +44,17 @@ export class ToadScheduler { return job } + removeById(id: string): Job | undefined { + const job = this.jobRegistry[id] + if (!job) { + return + } + job.stop() + delete this.jobRegistry[id] + + return job + } + stopById(id: string): void { const job = this.getById(id) job.stop() diff --git a/package.json b/package.json index 0626921..d055f1f 100644 --- a/package.json +++ b/package.json @@ -20,15 +20,15 @@ "prepublishOnly": "npm run build" }, "devDependencies": { - "@types/jest": "^26.0.23", - "@types/node": "^16.0.0", - "@typescript-eslint/eslint-plugin": "^4.27.0", - "@typescript-eslint/parser": "^4.27.0", - "eslint": "^7.29.0", + "@types/jest": "^26.0.24", + "@types/node": "^16.3.2", + "@typescript-eslint/eslint-plugin": "^4.28.3", + "@typescript-eslint/parser": "^4.28.3", + "eslint": "^7.30.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", - "jest": "^27.0.4", - "prettier": "^2.3.1", + "jest": "^27.0.6", + "prettier": "^2.3.2", "ts-jest": "^27.0.3", "typescript": "4.3.5" }, diff --git a/test/toadScheduler.spec.ts b/test/toadScheduler.spec.ts index f7442a0..9e9aee6 100644 --- a/test/toadScheduler.spec.ts +++ b/test/toadScheduler.spec.ts @@ -30,6 +30,57 @@ describe('ToadScheduler', () => { }) }) + describe('removeById', () => { + it('correctly removes job by id', () => { + let counter = 0 + let counter2 = 0 + const scheduler = new ToadScheduler() + const task = new Task('simple task', () => { + counter++ + }) + const task2 = new Task('simple task2', () => { + counter2++ + }) + const job = new SimpleIntervalJob( + { + milliseconds: 1, + }, + task, + 'job1' + ) + const job2 = new SimpleIntervalJob( + { + milliseconds: 10, + }, + task2, + 'job2' + ) + + scheduler.addSimpleIntervalJob(job) + scheduler.addSimpleIntervalJob(job2) + + expect(counter).toBe(0) + expect(counter2).toBe(0) + + const deletedJob = scheduler.removeById('job2') + expect(deletedJob?.id).toMatch('job2') + expect(() => { + scheduler.getById('job2') + }).toThrow(/not registered/) + const nonExistingJob = scheduler.removeById('job2') + expect(nonExistingJob).toBeUndefined() + + jest.advanceTimersByTime(2) + expect(counter).toBe(2) + expect(counter2).toBe(0) + jest.advanceTimersByTime(10) + expect(counter).toBe(12) + expect(counter2).toBe(0) + + scheduler.stop() + }) + }) + describe('stopById', () => { it('throws an error when non-existent id is stopped', () => { const scheduler = new ToadScheduler()