UNPKG

tsbase

Version:

Base class libraries for TypeScript

127 lines 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConditionalTimer = void 0; var tslib_1 = require("tslib"); var Timer_1 = require("./Timer"); /** * A timer that performs an action over time based on the evaluation of a condition */ var ConditionalTimer = /** @class */ (function () { function ConditionalTimer(timer) { this.timer = timer || new Timer_1.Timer(100); } /** * Short hand for creating a new instance of ConditionalTimer * @param interval */ ConditionalTimer.Instance = function (interval) { return new ConditionalTimer(new Timer_1.Timer(interval)); }; /** * Performs the given action one time after the condition evaluates to true * @param action * @param condition */ ConditionalTimer.prototype.DoWhen = function (action, condition) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.resetTimer(); return [4 /*yield*/, new Promise(function (resolve) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.timer.Elapsed.push(function () { if (condition()) { action(); _this.resetTimer(); resolve(); } }); return [4 /*yield*/, this.timer.Start()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; /** * Performs the given action repeatedly until the condition evaluates to true * @param action * @param condition */ ConditionalTimer.prototype.DoUntil = function (action, condition) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2 /*return*/, this.getRepeatingConditionalTimer(action, condition)]; }); }); }; /** * Performs the given action repeatedly as long as the condition evaluates to true * @param action * @param condition */ ConditionalTimer.prototype.DoWhile = function (action, condition) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2 /*return*/, this.getRepeatingConditionalTimer(action, function () { return !condition(); })]; }); }); }; /** * Stops the underlying timer, preventing further execution */ ConditionalTimer.prototype.Stop = function () { this.timer.Stop(); }; ConditionalTimer.prototype.getRepeatingConditionalTimer = function (action, condition) { return tslib_1.__awaiter(this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.resetTimer(); return [4 /*yield*/, new Promise(function (resolve) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var _this = this; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: this.timer.Elapsed.push(function () { action(); if (condition()) { _this.resetTimer(); resolve(); } }); return [4 /*yield*/, this.timer.Start()]; case 1: _a.sent(); return [2 /*return*/]; } }); }); })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; ConditionalTimer.prototype.resetTimer = function () { if (this.timer.Enabled) { this.Stop(); this.timer.AutoReset = true; this.timer.Elapsed = []; } }; return ConditionalTimer; }()); exports.ConditionalTimer = ConditionalTimer; //# sourceMappingURL=ConditionalTimer.js.map