smartdelay/dist/index.js
2017-10-09 11:33:59 +02:00

57 lines
3.7 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global");
const smartq = require("smartq");
/**
* delay something, works like setTimeout
* @param timeInMillisecond
* @param passOn
*/
exports.delayFor = (timeInMillisecond, passOn) => __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, timeInMillisecond);
});
return passOn;
});
/**
* delay for a random time
*/
exports.delayForRandom = (timeMinInMillisecond, timeMaxInMillisecond, passOn) => __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond);
});
return passOn;
});
class Timeout {
constructor(timeInMillisecondArg, passOn) {
this._cancelled = false;
this._deferred = smartq.defer();
this.promise = this._deferred.promise;
this._timeout = setTimeout(() => {
if (!this._cancelled) {
this._deferred.resolve(passOn);
}
}, timeInMillisecondArg);
}
makeUnrefed() {
this._timeout.unref();
}
cancel() {
this._cancelled = true;
this.makeUnrefed();
}
}
exports.Timeout = Timeout;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsMEJBQXVCO0FBQ3ZCLGlDQUFnQztBQUVoQzs7OztHQUlHO0F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