Skip to content
Open
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 lib/toadScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class ToadScheduler {
this.jobRegistry = {}
}

/**
* @deprecated in favor of unified addJob method
*/
addIntervalJob(job: SimpleIntervalJob | LongIntervalJob) {
if (!this.engines.simpleIntervalEngine) {
this.engines.simpleIntervalEngine = new SimpleIntervalEngine()
Expand All @@ -28,10 +31,16 @@ export class ToadScheduler {
this.engines.simpleIntervalEngine.add(job)
}

/**
* @deprecated in favor of unified addJob method
*/
addLongIntervalJob(job: LongIntervalJob): void {
return this.addIntervalJob(job)
}

/**
* @deprecated in favor of unified addJob method
*/
addSimpleIntervalJob(job: SimpleIntervalJob): void {
return this.addIntervalJob(job)
}
Expand All @@ -45,6 +54,9 @@ export class ToadScheduler {
}
}

/**
* @deprecated in favor of unified addJob method
*/
addCronJob(job: CronJob): void {
if (!this.engines.cronJobEngine) {
this.engines.cronJobEngine = new CronJobEngine()
Expand All @@ -54,6 +66,16 @@ export class ToadScheduler {
this.engines.cronJobEngine.add(job)
}

addJob(job: Job): void {
if (job instanceof SimpleIntervalJob || job instanceof LongIntervalJob) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof check is notoriously unrealiable and will not work correctly when comparing instances created in a different realm (e. g. if you are using a job created inside a shared library in an app that depends both on a shared library and on toad-scheduler). I would recommend introducing some id field on the job, and using it for a proper type guard

this.addIntervalJob(job)
} else if (job instanceof CronJob) {
this.addCronJob(job)
} else {
throw new Error('Unsupported job type.')
}
}

stop(): void {
for (const engine of Object.values(this.engines)) {
engine?.stop()
Expand Down