smartdelay/dist/index.js

46 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-01-16 13:54:08 +00:00
"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());
});
};
2017-06-04 22:32:01 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2017-01-16 13:54:08 +00:00
require("typings-global");
2017-06-04 22:32:01 +00:00
const smartq = require("smartq");
/**
* delay something, works like setTimeout
* @param timeInMillisecond
* @param passOn
*/
2017-01-16 13:54:08 +00:00
exports.delayFor = (timeInMillisecond, passOn) => __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, timeInMillisecond);
});
return passOn;
});
2017-06-04 22:32:01 +00:00
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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsMEJBQXVCO0FBQ3ZCLGlDQUFnQztBQUVoQzs7OztHQUlHO0FBQ1EsUUFBQSxRQUFRLEdBQUcsQ0FBVSxpQkFBeUIsRUFBRSxNQUFVO0lBQ25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