UNPKG

@juzi/wechaty-puppet-whatsapp

Version:
49 lines 1.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_schedule_1 = __importDefault(require("node-schedule")); const config_js_1 = require("../../config.js"); const PRE = 'ScheduleManager'; 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) { config_js_1.log.silly(PRE, 'addScheduledTask()'); const job = node_schedule_1.default.scheduleJob(rule, callback); this.jobPool.push(job); return job; } removeScheduledTask(job) { config_js_1.log.silly(PRE, 'removeScheduledTask()'); const jobIndex = this.jobPool.indexOf(job); if (jobIndex === -1) { config_js_1.log.warn(PRE, 'trying to cannel a job not in jobPool'); } else { this.jobPool.splice(jobIndex, 1); } return job.cancel(); } clearAllTasks() { config_js_1.log.silly(PRE, 'clearAllTasks()'); this.jobPool.map(job => job.cancel()); this.jobPool = []; } } exports.default = ScheduleManager; //# sourceMappingURL=schedule-manager.js.map