Skip to content

Commit

Permalink
fix: allow callbacks to return a promise (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox authored Oct 19, 2023
1 parent 2ff9c6e commit 8dd8b75
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
case 'string': {
const [command, ...args] = cmd.split(' ');

return spawn.bind(undefined, command ?? cmd, args, {});
return spawn.bind(undefined, command ?? cmd, args, {}) as () => void;
}

case 'object': {
Expand All @@ -164,7 +164,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
cmd.command,
cmd.args ?? [],
cmd.options ?? {}
);
) as () => void;
}
}
}
Expand All @@ -191,7 +191,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {

fireOnTick() {
for (const callback of this._callbacks) {
callback.call(
void callback.call(
this.context,
this.onComplete as WithOnComplete<OC> extends true
? CronOnCompleteCallback
Expand Down Expand Up @@ -293,7 +293,7 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
if (this._timeout) clearTimeout(this._timeout);
this.running = false;
if (typeof this.onComplete === 'function') {
this.onComplete.call(this.context);
void this.onComplete.call(this.context);
}
}
}
4 changes: 2 additions & 2 deletions src/types/cron.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export type CronContext<C> = C extends null ? CronJob : NonNullable<C>;
export type CronCallback<C, WithOnCompleteBool extends boolean = false> = (
this: CronContext<C>,
onComplete: WithOnCompleteBool extends true ? CronOnCompleteCallback : never
) => void;
) => void | Promise<void>;

export type CronOnCompleteCallback = () => void;
export type CronOnCompleteCallback = () => void | Promise<void>;

export type CronSystemCommand =
| string
Expand Down
26 changes: 24 additions & 2 deletions tests/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ describe('cron', () => {
onComplete => {
const t = new Date();
expect(t.getSeconds()).toBe(d.getSeconds());
onComplete();
void onComplete();
},
() => {
callback();
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('cron', () => {
onTick: onComplete => {
const t = new Date();
expect(t.getSeconds()).toBe(d.getSeconds());
onComplete();
void onComplete();
},
onComplete: function () {
callback();
Expand Down Expand Up @@ -1193,4 +1193,26 @@ describe('cron', () => {
});
}).toThrow();
});

it('should support async callback', () => {
const clock = sinon.useFakeTimers();
const callback = jest.fn();
const job = new CronJob(
'* * * * * *',
async function () {
await new Promise<void>(resolve => {
setTimeout(() => {
callback();
resolve();
}, 500);
});
},
null,
true
);
clock.tick(1500);
job.stop();
clock.restore();
expect(callback).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 8dd8b75

Please sign in to comment.