UNPKG

reshuffle

Version:

Reshuffle is a fast, unopinionated, minimalist integration framework

67 lines 3.08 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 __()); }; })(); exports.__esModule = true; var reshuffle_base_connector_1 = require("reshuffle-base-connector"); var DEFAULT_EVENT_OPTIONS = { interval: 5000 }; var CronConnector = /** @class */ (function (_super) { __extends(CronConnector, _super); function CronConnector(id) { var _this = _super.call(this, undefined, id) || this; _this.intervalsByEventId = {}; return _this; } CronConnector.prototype.on = function (options, eventId) { var _this = this; if (options === void 0) { options = DEFAULT_EVENT_OPTIONS; } if (!eventId) { eventId = "CRON/" + options.interval + "/" + this.id; } var event = new reshuffle_base_connector_1.EventConfiguration(eventId, this, options); this.eventConfigurations[event.id] = event; // lazy run if already running if (this.started && this.app) { var intervalId = this.app.setInterval(function () { var _a; (_a = _this.app) === null || _a === void 0 ? void 0 : _a.handleEvent(event.id, event); }, event.options.interval); this.intervalsByEventId[event.id] = intervalId; } return event; }; CronConnector.prototype.onRemoveEvent = function (event) { var _a; (_a = this.app) === null || _a === void 0 ? void 0 : _a.clearInterval(this.intervalsByEventId[event.id]); }; CronConnector.prototype.onStart = function (app) { var _this = this; Object.values(this.eventConfigurations).forEach(function (eventConfiguration) { var _a; var intervalId = (_a = _this.app) === null || _a === void 0 ? void 0 : _a.setInterval(function () { var _a; (_a = _this.app) === null || _a === void 0 ? void 0 : _a.handleEvent(eventConfiguration.id, {}); }, eventConfiguration.options.interval); if (intervalId) { _this.intervalsByEventId[eventConfiguration.id] = intervalId; } }); }; CronConnector.prototype.onStop = function () { var _this = this; Object.values(this.intervalsByEventId).forEach(function (intervalId) { var _a; return (_a = _this.app) === null || _a === void 0 ? void 0 : _a.clearInterval(intervalId); }); }; return CronConnector; }(reshuffle_base_connector_1.BaseConnector)); exports["default"] = CronConnector; //# sourceMappingURL=CronConnector.js.map