for-emit-of
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -7,3 +7,3 @@ /// <reference types="node" /> | ||
*/ | ||
interface Options<T = any> { | ||
export interface Options<T = any> { | ||
/** | ||
@@ -40,2 +40,14 @@ * The event that generates the AsyncIterable items | ||
limit?: number; | ||
/** | ||
* The max interval, in milliseconds, of idleness for the iterable generated. For the iterable | ||
* to kept node process running, it need to have at least one task not based on events created, | ||
* this property defines the keepAlive time for such task. If timeout is used, this property is | ||
* ignored. If 0 is informed, the keepAlive is disabled. Default: 0 | ||
*/ | ||
keepAlive?: number; | ||
/** | ||
* if some debug code lines will be printed. Useful to understand how for-emit-of are performing. | ||
* Default: false | ||
*/ | ||
debug?: boolean; | ||
} | ||
@@ -42,0 +54,0 @@ declare type SuperEmitter = (EventEmitter | Readable | Writable) & { |
@@ -14,6 +14,10 @@ "use strict"; | ||
var _debugging = require("./debugging"); | ||
const defaults = { | ||
event: "data", | ||
error: "error", | ||
end: ["close", "end"] | ||
end: ["close", "end"], | ||
keepAlive: 0, | ||
debug: false | ||
}; | ||
@@ -136,3 +140,18 @@ /** | ||
let countEvents = 0; | ||
let countKeepAlive = 0; | ||
const start = process.hrtime(); | ||
if (options.keepAlive && (!options.firstEventTimeout || !options.inBetweenTimeout)) { | ||
const keepAlive = () => { | ||
if (active && !error && (countEvents === 0 || !options.inBetweenTimeout)) { | ||
countKeepAlive = (0, _debugging.debugKeepAlive)(options, countKeepAlive, start); | ||
setTimeout(keepAlive, options.keepAlive); | ||
} else { | ||
(0, _debugging.debugKeepAliveEnding)(options, countKeepAlive, start); | ||
} | ||
}; | ||
setTimeout(keepAlive, options.keepAlive); | ||
} | ||
while (shouldYield && (events.length || active)) { | ||
@@ -144,2 +163,3 @@ if (error) { | ||
while (shouldYield && events.length > 0) { | ||
(0, _debugging.debugYielding)(options, events); | ||
/* We do not want to block the process! | ||
@@ -149,2 +169,3 @@ This call allows other processes | ||
*/ | ||
await (0, _sleep.sleep)(0); | ||
@@ -157,2 +178,3 @@ const [event, ...rest] = events; | ||
if (options.limit && countEvents >= options.limit) { | ||
(0, _debugging.debugYieldLimit)(options); | ||
shouldYield = false; | ||
@@ -163,6 +185,9 @@ } | ||
if (active && !error) { | ||
(0, _debugging.debugRaceStart)(options); | ||
const winner = await Promise.race(getRaceItems()); | ||
(0, _debugging.debugRaceEnd)(options, winner); | ||
if (winner === _timeout.timedOut) { | ||
removeListeners(); | ||
active = false; | ||
throw Error("Event timed out"); | ||
@@ -173,2 +198,3 @@ } | ||
active = false; | ||
removeListeners(); | ||
@@ -175,0 +201,0 @@ } |
{ | ||
"name": "for-emit-of", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Turn Node.js Events into Async Iterables", | ||
@@ -60,2 +60,2 @@ "main": "./dist/index.js", | ||
} | ||
} | ||
} |
@@ -18,2 +18,4 @@ # for-emit-of | ||
- [Limit](#limit) | ||
- [Debug](#debug) | ||
- [Keep Alive](#keep-alive) | ||
@@ -161,2 +163,31 @@ # Example | ||
console.log(msgCount); // 10 | ||
``` | ||
# Debug | ||
```ts | ||
import forEmitOf from 'for-emit-of'; | ||
import { EventEmitter } from "events"; | ||
const emitter = new EventEmitter(); | ||
const iterator = forEmitOf(emitter, { | ||
limit: 10, | ||
debug: true // logs | ||
}); | ||
``` | ||
# Keep Alive | ||
```ts | ||
import forEmitOf from 'for-emit-of'; | ||
import { EventEmitter } from "events"; | ||
const neverEmit = new EventEmitter(); | ||
const iterator = forEmitOf(neverEmit, { | ||
keepAlive: 1000 | ||
}); | ||
for await (const data of iterator){ | ||
// waiting ⌛ | ||
} | ||
``` |
17773
11
317
191