for-emit-of
Version:
Turn Node.js Events into Async Iterables
24 lines (23 loc) • 774 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeout = exports.timedOut = void 0;
const sleep_1 = require("./sleep");
const instant_1 = require("./instant");
function hadTimedOut(deadline) {
const now = instant_1.instant();
return deadline < now;
}
function getDeadline(value, context) {
return context.lastResultAt + value;
}
exports.timedOut = Symbol("TimedOutSymbol");
function timeout(value, context) {
function getAwaiter() {
return sleep_1.sleep(Math.max(getDeadline(value, context) - instant_1.instant(), 0)).then(() => hadTimedOut(getDeadline(value, context)) ? exports.timedOut : getAwaiter());
}
const awaiter = getAwaiter();
return {
awaiter,
};
}
exports.timeout = timeout;