for-emit-of
Advanced tools
Comparing version 1.3.3 to 1.4.0
@@ -0,1 +1,2 @@ | ||
import { Queue } from "./queue"; | ||
import { Options } from "./types"; | ||
@@ -5,5 +6,5 @@ export declare function debugRaceEnd(options: Options, winner: symbol | void): void; | ||
export declare function debugYieldLimit(options: Options): void; | ||
export declare function debugYielding(options: Options, events: any[]): void; | ||
export declare function debugYielding(options: Options, events: Queue): void; | ||
export declare function debugKeepAlive(options: Options, countKeepAlive: number, start: [number, number]): number; | ||
export declare function debugKeepAliveEnding(options: Options, countKeepAlive: number, start: [number, number]): void; | ||
export declare function debugIteratorReturn(options: Options): void; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const instant_1 = require("./instant"); | ||
const queue_1 = require("./queue"); | ||
const defaults = { | ||
@@ -14,7 +15,10 @@ event: "data", | ||
debug: false, | ||
noSleep: false, | ||
}; | ||
function waitResponse(emitter, options) { | ||
function waitResponse(emitter, options, eventListener) { | ||
emitter.off(options.event, eventListener); | ||
return new Promise((resolve, reject) => { | ||
emitter.once(options.event, () => { | ||
resolve(); | ||
emitter.once(options.event, (value) => { | ||
emitter.on(options.event, eventListener); | ||
resolve({ value }); | ||
emitter.removeListener(options.error, reject); | ||
@@ -27,14 +31,20 @@ options.end.forEach((event) => emitter.removeListener(event, resolve)); | ||
} | ||
function getInBetweenTimeoutRace(options, emitter, context) { | ||
function getInBetweenTimeoutRace(options, emitter, context, eventListener) { | ||
const timeoutWrapper = timeout_1.timeout(options.inBetweenTimeout, context); | ||
return () => [waitResponse(emitter, options), timeoutWrapper.awaiter]; | ||
return () => [ | ||
waitResponse(emitter, options, eventListener), | ||
timeoutWrapper.awaiter, | ||
]; | ||
} | ||
function getFirstAwaiter(options, emitter, context) { | ||
function getFirstAwaiter(options, emitter, context, eventListener) { | ||
if (options.firstEventTimeout) { | ||
const firstTimeout = timeout_1.timeout(options.firstEventTimeout, context); | ||
return Promise.race([waitResponse(emitter, options), firstTimeout.awaiter]); | ||
return Promise.race([ | ||
waitResponse(emitter, options, eventListener), | ||
firstTimeout.awaiter, | ||
]); | ||
} | ||
return waitResponse(emitter, options); | ||
return waitResponse(emitter, options, eventListener); | ||
} | ||
function switchRace(options, emitter, getNextRace, context) { | ||
function switchRace(options, emitter, getNextRace, context, eventListener) { | ||
let timeoutRace; | ||
@@ -44,3 +54,3 @@ return () => timeoutRace | ||
: [ | ||
getFirstAwaiter(options, emitter, context).then((result) => { | ||
getFirstAwaiter(options, emitter, context, eventListener).then((result) => { | ||
if (result !== timeout_1.timedOut) { | ||
@@ -53,12 +63,14 @@ timeoutRace = getNextRace(); | ||
} | ||
function getTimeoutRace(options, emitter, context) { | ||
return switchRace(options, emitter, () => getInBetweenTimeoutRace(options, emitter, context), context); | ||
function getTimeoutRace(options, emitter, context, eventListener) { | ||
return switchRace(options, emitter, () => getInBetweenTimeoutRace(options, emitter, context, eventListener), context, eventListener); | ||
} | ||
function raceFactory(options, emitter, context) { | ||
function raceFactory(options, emitter, context, eventListener) { | ||
if (options.inBetweenTimeout) { | ||
return getTimeoutRace(options, emitter, context); | ||
return getTimeoutRace(options, emitter, context, eventListener); | ||
} | ||
const getWaitResponse = () => [waitResponse(emitter, options)]; | ||
const getWaitResponse = () => [ | ||
waitResponse(emitter, options, eventListener), | ||
]; | ||
return options.firstEventTimeout | ||
? switchRace(options, emitter, () => getWaitResponse, context) | ||
? switchRace(options, emitter, () => getWaitResponse, context, eventListener) | ||
: getWaitResponse; | ||
@@ -85,3 +97,3 @@ } | ||
} | ||
let events = []; | ||
let events = queue_1.getQueue(); | ||
let error; | ||
@@ -103,3 +115,3 @@ let active = true; | ||
const removeListeners = () => { | ||
events = []; | ||
events = queue_1.getQueue(); | ||
emitter.removeListener(options.event, eventListener); | ||
@@ -112,3 +124,3 @@ emitter.removeListener(options.error, errorListener); | ||
options.end.forEach((event) => emitter.once(event, endListener)); | ||
const getRaceItems = raceFactory(options, emitter, context); | ||
const getRaceItems = raceFactory(options, emitter, context, eventListener); | ||
function generator() { | ||
@@ -161,2 +173,10 @@ let completed = false; | ||
} | ||
if (winner) { | ||
return { | ||
done: false, | ||
value: options.transform | ||
? options.transform(winner.value) | ||
: winner.value, | ||
}; | ||
} | ||
} | ||
@@ -167,5 +187,6 @@ if (!shouldYield || (events.length === 0 && !active)) { | ||
debugging_1.debugYielding(options, events); | ||
await sleep_1.sleep(0); | ||
const [event, ...rest] = events; | ||
events = rest; | ||
if (!options.noSleep) { | ||
await sleep_1.breath(); | ||
} | ||
const event = events.shift(); | ||
countEvents++; | ||
@@ -172,0 +193,0 @@ if (options.limit && countEvents >= options.limit) { |
/// <reference types="node" /> | ||
export declare const sleep: typeof setTimeout.__promisify__; | ||
export declare const breath: typeof setImmediate.__promisify__; |
@@ -22,4 +22,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sleep = void 0; | ||
exports.breath = exports.sleep = void 0; | ||
const util = __importStar(require("util")); | ||
exports.sleep = util.promisify(setTimeout); | ||
exports.breath = util.promisify(setImmediate); |
@@ -14,2 +14,3 @@ /// <reference types="node" /> | ||
debug?: boolean; | ||
noSleep?: boolean; | ||
} | ||
@@ -16,0 +17,0 @@ export interface Context { |
{ | ||
"name": "for-emit-of", | ||
"version": "1.3.3", | ||
"version": "1.4.0", | ||
"description": "Turn Node.js Events into Async Iterables", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
21113
17
395