UNPKG

uc-base-libraries

Version:
169 lines 6.02 kB
System.register(["./Lock"], function (exports_1, context_1) { "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var Locking, Task, RecurringTask; var __moduleName = context_1 && context_1.id; function runAfterWait(waitTimeMilliSeconds) { const t = new Task((cback) => { cback(); }); let timer = null; const throttle = () => { clearTimeout(timer); timer = window.setTimeout(() => { t.start(); }, waitTimeMilliSeconds || 500); }; t.trigger = () => { throttle(); }; t.call = () => { clearTimeout(timer); t.start(); }; return t; } exports_1("runAfterWait", runAfterWait); function debounced() { const t = new Task((cback) => { cback(); }); t.trigger = () => { t.start(); }; t.call = () => { t.start(); }; return t; } exports_1("debounced", debounced); function delay(msec) { return new Promise((resolve) => { setTimeout(resolve, msec); }); } exports_1("delay", delay); function whenReady() { return new Promise((resolve) => { if ($) { $(() => { resolve(); }); } else { resolve(); } }); } exports_1("whenReady", whenReady); function whenTrue(trueFunc, maxLockTime = 60 * 60 * 1000) { return Locking.WhenTrueAsync(trueFunc, maxLockTime); } exports_1("whenTrue", whenTrue); function WhenAll(list, progressCB) { return __awaiter(this, void 0, void 0, function* () { const tot = list.length; let fin = 0; list.forEach((p) => { p.then(() => { fin++; if (progressCB) { progressCB(fin, tot); } }); }); return yield Promise.all(list); }); } exports_1("WhenAll", WhenAll); return { setters: [ function (Locking_1) { Locking = Locking_1; } ], execute: function () { Task = class Task { constructor(func) { this.func = func; this.promise = null; this.then = (onFulfilled) => { return this.promise.then(onFulfilled); }; this.start = () => { this.func((val) => { this.resolveFunc(val); }); }; this.promise = new Promise((resolve) => { this.resolveFunc = resolve; }); if (!this.func) { this.func = (rFunc) => { return rFunc(); }; } else if (func.length === 0) { const bfunc = this.func; this.func = (rFunc) => { bfunc(); rFunc(); }; } } }; exports_1("Task", Task); RecurringTask = class RecurringTask { timedCall() { return __awaiter(this, void 0, void 0, function* () { if (this.callback) { if (!this.refreshLock.isLocked) { yield this.refreshLock.LockAreaAsync(() => __awaiter(this, void 0, void 0, function* () { yield this.callback(); })); } } if (this.isRunning) { setTimeout(() => { this.timedCall(); }, this.timeout); } }); } constructor(callback, timeout, maxLockTime = 30000) { this.callback = callback; this.timeout = timeout; this.maxLockTime = maxLockTime; this._isRunning = false; this.refreshLock = null; this.setTimeOut = (time) => { this.timeout = time; }; this.start = () => { if (!this.isRunning) { this._isRunning = true; this.timedCall(); } }; this.stop = () => { this._isRunning = false; }; this.refreshLock = new Locking.MutexLock(this.maxLockTime); } get isRunning() { return this._isRunning; } }; exports_1("RecurringTask", RecurringTask); } }; }); //# sourceMappingURL=Tasks.js.map