for-emit-of
Advanced tools
Comparing version 1.3.1 to 1.3.2
@@ -6,2 +6,3 @@ "use strict"; | ||
const debugging_1 = require("./debugging"); | ||
const instant_1 = require("./instant"); | ||
const defaults = { | ||
@@ -25,17 +26,9 @@ event: "data", | ||
} | ||
async function awaitAndResetTimeout(emitter, options, timeoutWrapper) { | ||
const result = await waitResponse(emitter, options); | ||
timeoutWrapper.updateDeadline(); | ||
return result; | ||
function getInBetweenTimeoutRace(options, emitter, context) { | ||
const timeoutWrapper = timeout_1.timeout(options.inBetweenTimeout, context); | ||
return () => [waitResponse(emitter, options), timeoutWrapper.awaiter]; | ||
} | ||
function getInBetweenTimeoutRace(options, emitter) { | ||
const timeoutWrapper = timeout_1.timeout(options.inBetweenTimeout); | ||
return () => [ | ||
awaitAndResetTimeout(emitter, options, timeoutWrapper), | ||
timeoutWrapper.awaiter, | ||
]; | ||
} | ||
function getFirstAwaiter(options, emitter) { | ||
function getFirstAwaiter(options, emitter, context) { | ||
if (options.firstEventTimeout) { | ||
const firstTimeout = timeout_1.timeout(options.firstEventTimeout); | ||
const firstTimeout = timeout_1.timeout(options.firstEventTimeout, context); | ||
return Promise.race([waitResponse(emitter, options), firstTimeout.awaiter]); | ||
@@ -45,3 +38,3 @@ } | ||
} | ||
function switchRace(options, emitter, getNextRace) { | ||
function switchRace(options, emitter, getNextRace, context) { | ||
let timeoutRace; | ||
@@ -51,3 +44,3 @@ return () => timeoutRace | ||
: [ | ||
getFirstAwaiter(options, emitter).then((result) => { | ||
getFirstAwaiter(options, emitter, context).then((result) => { | ||
if (result !== timeout_1.timedOut) { | ||
@@ -60,12 +53,12 @@ timeoutRace = getNextRace(); | ||
} | ||
function getTimeoutRace(options, emitter) { | ||
return switchRace(options, emitter, () => getInBetweenTimeoutRace(options, emitter)); | ||
function getTimeoutRace(options, emitter, context) { | ||
return switchRace(options, emitter, () => getInBetweenTimeoutRace(options, emitter, context), context); | ||
} | ||
function raceFactory(options, emitter) { | ||
function raceFactory(options, emitter, context) { | ||
if (options.inBetweenTimeout) { | ||
return getTimeoutRace(options, emitter); | ||
return getTimeoutRace(options, emitter, context); | ||
} | ||
const getWaitResponse = () => [waitResponse(emitter, options)]; | ||
return options.firstEventTimeout | ||
? switchRace(options, emitter, () => getWaitResponse) | ||
? switchRace(options, emitter, () => getWaitResponse, context) | ||
: getWaitResponse; | ||
@@ -95,3 +88,9 @@ } | ||
let active = true; | ||
const eventListener = (event) => events.push(event); | ||
const context = { | ||
lastResultAt: 0, | ||
}; | ||
const eventListener = (event) => { | ||
context.lastResultAt = instant_1.instant(); | ||
return events.push(event); | ||
}; | ||
const endListener = () => { | ||
@@ -111,3 +110,3 @@ active = false; | ||
options.end.forEach((event) => emitter.once(event, endListener)); | ||
const getRaceItems = raceFactory(options, emitter); | ||
const getRaceItems = raceFactory(options, emitter, context); | ||
async function* generator() { | ||
@@ -133,2 +132,3 @@ let shouldYield = true; | ||
} | ||
context.lastResultAt = instant_1.instant(); | ||
while (shouldYield && (events.length || active)) { | ||
@@ -135,0 +135,0 @@ if (error) { |
@@ -0,6 +1,6 @@ | ||
import { Context } from "./types"; | ||
export declare const timedOut: unique symbol; | ||
export interface TimeoutWrapper { | ||
awaiter: Promise<symbol>; | ||
updateDeadline(): void; | ||
} | ||
export declare function timeout(value: number): TimeoutWrapper; | ||
export declare function timeout(value: number, context: Context): TimeoutWrapper; |
@@ -5,18 +5,14 @@ "use strict"; | ||
const sleep_1 = require("./sleep"); | ||
function instant() { | ||
const [s, ns] = process.hrtime(); | ||
return s * 1e3 + ns / 1e6; | ||
} | ||
const instant_1 = require("./instant"); | ||
function hadTimedOut(deadline) { | ||
const now = instant(); | ||
const now = instant_1.instant(); | ||
return deadline < now; | ||
} | ||
function getDeadline(value) { | ||
return instant() + value; | ||
function getDeadline(value, context) { | ||
return context.lastResultAt + value; | ||
} | ||
exports.timedOut = Symbol("TimedOutSymbol"); | ||
function timeout(value) { | ||
let deadline = getDeadline(value); | ||
function timeout(value, context) { | ||
function getAwaiter() { | ||
return sleep_1.sleep(Math.max(deadline - instant(), 0)).then(() => hadTimedOut(deadline) ? exports.timedOut : getAwaiter()); | ||
return sleep_1.sleep(Math.max(getDeadline(value, context) - instant_1.instant(), 0)).then(() => hadTimedOut(getDeadline(value, context)) ? exports.timedOut : getAwaiter()); | ||
} | ||
@@ -26,7 +22,4 @@ const awaiter = getAwaiter(); | ||
awaiter, | ||
updateDeadline() { | ||
deadline = getDeadline(value); | ||
}, | ||
}; | ||
} | ||
exports.timeout = timeout; |
@@ -15,2 +15,5 @@ /// <reference types="node" /> | ||
} | ||
export interface Context { | ||
lastResultAt: number; | ||
} | ||
export declare type SuperEmitter = (EventEmitter | Readable | Writable) & { | ||
@@ -17,0 +20,0 @@ readableEnded?: boolean; |
{ | ||
"name": "for-emit-of", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "Turn Node.js Events into Async Iterables", | ||
@@ -50,2 +50,2 @@ "main": "./dist/index.js", | ||
} | ||
} | ||
} |
17627
15
302