UNPKG

timerjobs

Version:

TimerJobs is a simple way to create recurring tasks that can react to events.

355 lines 9.56 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimerJobs = void 0; const helpers_1 = require("./helpers"); const options_1 = require("./options"); const emitter_1 = require("./emitter"); __exportStar(require("./emit-level"), exports); __exportStar(require("./interfaces"), exports); class TimerJobs { constructor(callback, options = {}) { this.busy = false; this.errors = []; this.executions = 0; this.hasStarted = false; this.timer = null; this.startWait = 0; this._interval = 0; if (helpers_1.isObject(callback)) { options = callback; callback = null; } TimerJobs.timers.push(this); this.options = new options_1.Options(options); this._emitter = new emitter_1.Emitter(this, this.options); const { countdown, autoStart } = this.options; this.countdown = countdown; this.callback = callback; if (autoStart) { this.start(); } } static set emitter(value) { emitter_1.Emitter.emitter = value; } static get emitter() { return emitter_1.Emitter.emitter; } start() { if (!this.timer) { if (helpers_1.not(helpers_1.isFunction(this.callback))) { throw new Error('TimerJobs Error: a callback must be provided'); } const opts = this.options; this.startWait = Date.now(); this.hasStarted = true; if (this.countdown < 1) { this.countdown = opts.countdown; } this.timer = setInterval(this.go.bind(this), opts.interval); this.emit('jobStart', null, this); if (opts.immediate) { this.go(); } } return this; } emit(event, error, ...args) { this._emitter.emit(event, { error, args, }); } get isStopped() { return this.timer === null; } get isStarted() { return Boolean(this.timer); } stop() { if (this.timer) { clearInterval(this.timer); this.emit('jobStop', null, this); this.timer = null; this.startWait = 0; } return this; } restart(interval) { if (helpers_1.isInteger(interval) && interval > 1) { this.options.interval = interval; } if (this.hasStarted) { this.stop(); this.start(); } return this; } get waitTime() { if (!this.startWait) { return this.startWait; } return this.startWait + this.options.interval - Date.now(); } get countdown() { return this._countdown; } set countdown(value) { this._countdown = this.options.countdown = value && value > 1 ? Math.floor(value) : 1; } set emitLevel(value) { this.options.emitLevel = value; } get emitLevel() { return this.options.emitLevel; } set infinite(value) { this.options.infinite = value; } get infinite() { return this.options.infinite; } set interval(value) { this.options.interval = value; } get interval() { return this.options.interval; } get emitter() { return this._emitter.emitter; } static findTimers(predicate) { return this.timers.filter(predicate); } static removeTimers(predicate) { const timersToRemove = this.timers.filter(predicate); this.timers = this.timers.filter((timer) => helpers_1.not(timersToRemove.includes(timer))); return timersToRemove; } static removeTimer(timer) { this.timers = this.timers.filter((t) => t !== timer); } dispose() { this.stop(); TimerJobs.removeTimer(this); } go() { if (!this.busy || !this.options.blocking) { this.busy = true; this.emit('jobBegin', null, this); this.executions++; this.startWait = Date.now(); const args = this.callback.length ? [this.done.bind(this)] : []; try { this.callback.call(this.options.context, ...args); } catch (error) { this.done(error); } } } done(err, ...args) { const { ignoreErrors, infinite } = this.options; this.emit('jobEnd', null, this, ...args); if (err) { this.errors.push(err); this.emit('jobError', err, this, this.errors); if (helpers_1.not(ignoreErrors)) { this.stop(); } } if (helpers_1.not(infinite)) { if (--this._countdown < 1) { this.emit('jobComplete', null, this); this.stop(); } } this.busy = false; } after(interval, resetInterval = true) { if (resetInterval) { this.options.interval = 0; } this._interval = interval; return this; } and(interval) { return this.after(interval, false); } every(interval, resetInterval = true) { return this.after(interval, resetInterval); } level(level) { this.emitLevel = level; return this; } times(countdown) { this.countdown = countdown; return this; } namespace(namespace) { this.options.namespace = namespace; return this; } namespacing(namespace) { return this.namespace(namespace); } reference(reference) { this.options.reference = reference; return this; } referencing(reference) { return this.reference(reference); } forever(countdown) { this.infinite = this._not ? (this._not = false) : true; countdown && this.times(countdown); return this; } using(emitter) { this._emitter.emitter = emitter; return this; } do(callback) { this.callback = callback; return this; } execute(callback) { return this.do(callback); } on(event, callback) { const _this = this; _this.options[`${this._event}On`] = event; _this.options[`${this._event}Callback`] = callback; _this._emitter[`${this._event}Setup`](); return this; } get blocking() { this.options.blocking = this._not ? (this._not = false) : true; return this; } get blocks() { return this.blocking; } get immediate() { this.options.immediate = this._not ? (this._not = false) : true; return this; } get immediately() { return this.immediate; } get not() { this._not = true; return this; } get ignore() { this.options.ignoreErrors = this._not ? (this._not = false) : true; return this; } get ignoring() { return this.ignore; } get automatically() { const auto = this._not ? (this._not = false) : true; this.options.autoStart = auto; if (auto) { setTimeout(() => this.start(), 0); } return this; } get automatic() { return this.automatically; } get once() { this.infinite = false; this.countdown = 1; return this; } get twice() { this.once; this.countdown++; return this; } get thrice() { this.twice; this.countdown++; return this; } get repeat() { this.infinite = this._not ? (this._not = false) : true; return this; } get repeating() { return this.repeat; } get week() { this._interval = 1; return this.weeks; } get weeks() { this._interval *= 7; return this.days; } get day() { this._interval = 1; return this.days; } get days() { this._interval *= 24; return this.hours; } get hour() { this._interval = 1; return this.hours; } get hours() { this._interval *= 60; return this.minutes; } get minute() { this._interval = 1; return this.minutes; } get minutes() { this._interval *= 60; return this.seconds; } get second() { this.interval += 1000; return this; } get seconds() { this._interval *= 1000; return this.milliseconds; } get milliseconds() { this.interval += this._interval; return this; } get starting() { this._event = 'start'; return this; } get stopping() { this._event = 'stop'; return this; } get restarting() { this._event = 'restart'; return this; } } exports.TimerJobs = TimerJobs; TimerJobs.timers = []; //# sourceMappingURL=index.js.map