UNPKG

reshuffle

Version:

Reshuffle is a fast, unopinionated, minimalist integration framework

57 lines 2.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; exports.__esModule = true; var reshuffle_base_connector_1 = require("reshuffle-base-connector"); var node_cron_1 = __importDefault(require("node-cron")); var nanoid_1 = require("nanoid"); var CronConnector = /** @class */ (function (_super) { __extends(CronConnector, _super); function CronConnector(app, id) { var _this = _super.call(this, app, null, id) || this; _this.tasksByEventId = {}; return _this; } CronConnector.prototype.on = function (options, handler, eventId) { var _this = this; if (!eventId) { eventId = "CRON/" + this.id + "/" + (eventId ? eventId : nanoid_1.nanoid()); } var event = new reshuffle_base_connector_1.EventConfiguration(eventId, this, options); this.eventConfigurations[event.id] = event; if (handler) { this.app.when(event, handler); } var task = node_cron_1["default"].schedule(event.options.expression, function () { return _this.app.handleEvent(event.id, event); }, { timezone: event.options.timezone, scheduled: false }); this.tasksByEventId[event.id] = task; this.started && this.onStart(); return event; }; CronConnector.prototype.onRemoveEvent = function (event) { var _a; (_a = this.tasksByEventId[event.id]) === null || _a === void 0 ? void 0 : _a.destroy(); }; CronConnector.prototype.onStart = function () { Object.values(this.tasksByEventId).forEach(function (task) { return task.start(); }); }; CronConnector.prototype.onStop = function () { Object.values(this.tasksByEventId).forEach(function (task) { return task.stop(); }); }; return CronConnector; }(reshuffle_base_connector_1.BaseConnector)); exports["default"] = CronConnector; //# sourceMappingURL=CronConnector.js.map