@juzi/wechaty-puppet-whatsapp
Version:
Wechaty Puppet for WhatsApp
43 lines • 1.34 kB
JavaScript
import schedule from 'node-schedule';
import { log } from '../../config.js';
const PRE = 'ScheduleManager';
export default class ScheduleManager {
static _instance;
jobPool = [];
constructor() { }
static get Instance() {
if (!this._instance) {
this._instance = new ScheduleManager();
}
return this._instance;
}
/**
* Create a schedule job.
*
* @param rule scheduling info, ref: https://github.com/node-schedule/node-schedule#cron-style-scheduling
* @param callback callback to be executed on each invocation
*/
addScheduledTask(rule, callback) {
log.silly(PRE, 'addScheduledTask()');
const job = schedule.scheduleJob(rule, callback);
this.jobPool.push(job);
return job;
}
removeScheduledTask(job) {
log.silly(PRE, 'removeScheduledTask()');
const jobIndex = this.jobPool.indexOf(job);
if (jobIndex === -1) {
log.warn(PRE, 'trying to cannel a job not in jobPool');
}
else {
this.jobPool.splice(jobIndex, 1);
}
return job.cancel();
}
clearAllTasks() {
log.silly(PRE, 'clearAllTasks()');
this.jobPool.map(job => job.cancel());
this.jobPool = [];
}
}
//# sourceMappingURL=schedule-manager.js.map