for-emit-of
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -8,1 +8,2 @@ import { Options } from "./types"; | ||
export declare function debugKeepAliveEnding(options: Options, countKeepAlive: number, start: [number, number]): void; | ||
export declare function debugIteratorReturn(options: Options): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.debugKeepAliveEnding = exports.debugKeepAlive = exports.debugYielding = exports.debugYieldLimit = exports.debugRaceStart = exports.debugRaceEnd = void 0; | ||
exports.debugIteratorReturn = exports.debugKeepAliveEnding = exports.debugKeepAlive = exports.debugYielding = exports.debugYieldLimit = exports.debugRaceStart = exports.debugRaceEnd = void 0; | ||
function debugRaceEnd(options, winner) { | ||
@@ -46,1 +46,7 @@ if (options.debug) { | ||
exports.debugKeepAliveEnding = debugKeepAliveEnding; | ||
function debugIteratorReturn(options) { | ||
if (options.debug) { | ||
console.log("Iterator return called and process finalized"); | ||
} | ||
} | ||
exports.debugIteratorReturn = debugIteratorReturn; |
@@ -97,2 +97,3 @@ "use strict"; | ||
const removeListeners = () => { | ||
events = []; | ||
emitter.removeListener(options.event, eventListener); | ||
@@ -106,3 +107,4 @@ emitter.removeListener(options.error, errorListener); | ||
const getRaceItems = raceFactory(options, emitter, context); | ||
async function* generator() { | ||
function generator() { | ||
let completed = false; | ||
let shouldYield = true; | ||
@@ -112,2 +114,11 @@ let countEvents = 0; | ||
const start = process.hrtime(); | ||
async function runReturn(value) { | ||
if (!completed) { | ||
shouldYield = false; | ||
completed = true; | ||
removeListeners(); | ||
debugging_1.debugIteratorReturn(options); | ||
} | ||
return { done: true, value }; | ||
} | ||
if (options.keepAlive && | ||
@@ -129,31 +140,40 @@ (!options.firstEventTimeout || !options.inBetweenTimeout)) { | ||
context.lastResultAt = instant_1.instant(); | ||
while (shouldYield && (events.length || active)) { | ||
if (error) { | ||
throw error; | ||
} | ||
while (shouldYield && events.length > 0) { | ||
debugging_1.debugYielding(options, events); | ||
await sleep_1.sleep(0); | ||
const [event, ...rest] = events; | ||
events = rest; | ||
yield options.transform ? options.transform(event) : event; | ||
countEvents++; | ||
if (options.limit && countEvents >= options.limit) { | ||
debugging_1.debugYieldLimit(options); | ||
shouldYield = false; | ||
} | ||
} | ||
if (active && !error) { | ||
debugging_1.debugRaceStart(options); | ||
const winner = await Promise.race(getRaceItems()); | ||
debugging_1.debugRaceEnd(options, winner); | ||
if (winner === timeout_1.timedOut) { | ||
removeListeners(); | ||
active = false; | ||
throw Error("Event timed out"); | ||
} | ||
} | ||
} | ||
active = false; | ||
removeListeners(); | ||
return { | ||
[Symbol.asyncIterator]() { | ||
return { | ||
async next() { | ||
if (error) { | ||
throw error; | ||
} | ||
if (shouldYield && !events.length && active) { | ||
debugging_1.debugRaceStart(options); | ||
const winner = await Promise.race(getRaceItems()); | ||
debugging_1.debugRaceEnd(options, winner); | ||
if (winner === timeout_1.timedOut) { | ||
removeListeners(); | ||
active = false; | ||
throw Error("Event timed out"); | ||
} | ||
} | ||
if (!shouldYield || (events.length === 0 && !active)) { | ||
return runReturn(); | ||
} | ||
debugging_1.debugYielding(options, events); | ||
await sleep_1.sleep(0); | ||
const [event, ...rest] = events; | ||
events = rest; | ||
countEvents++; | ||
if (options.limit && countEvents >= options.limit) { | ||
debugging_1.debugYieldLimit(options); | ||
shouldYield = false; | ||
} | ||
return { | ||
done: false, | ||
value: options.transform ? options.transform(event) : event, | ||
}; | ||
}, | ||
return: runReturn, | ||
}; | ||
}, | ||
}; | ||
} | ||
@@ -160,0 +180,0 @@ return generator(); |
@@ -23,1 +23,2 @@ /// <reference types="node" /> | ||
export declare type TimeoutRaceFactory = () => Array<Promise<void | symbol>>; | ||
export declare const Abort: unique symbol; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Abort = void 0; | ||
exports.Abort = Symbol("AbortIterable"); |
{ | ||
"name": "for-emit-of", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Turn Node.js Events into Async Iterables", | ||
@@ -36,2 +36,4 @@ "main": "./dist/index.js", | ||
"@types/node": "^13.13.15", | ||
"@types/sinon": "^9.0.4", | ||
"@types/sinon-chai": "^3.2.4", | ||
"@typescript-eslint/eslint-plugin": "^2.34.0", | ||
@@ -48,5 +50,8 @@ "@typescript-eslint/parser": "^2.34.0", | ||
"prettier": "^2.0.5", | ||
"sinon": "^9.0.2", | ||
"sinon-chai": "^3.5.0", | ||
"ts-node": "^8.10.2", | ||
"typescript": "^3.9.7" | ||
} | ||
}, | ||
"dependencies": {} | ||
} |
19065
332
20