Skip to content

Commit

Permalink
Add removeById (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Jul 14, 2021
1 parent 5368882 commit 7205f6e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/toadScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
51 changes: 51 additions & 0 deletions test/toadScheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7205f6e

Please sign in to comment.