event-iterator
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,4 +0,4 @@ | ||
import { EventIterator } from "./event-iterator"; | ||
export declare function subscribe(this: EventTarget, event: string, options?: AddEventListenerOptions): EventIterator<Event>; | ||
import { EventIterator, EventIteratorOptions } from "./event-iterator"; | ||
export declare function subscribe(this: EventTarget, event: string, options?: AddEventListenerOptions, evOptions?: EventIteratorOptions): EventIterator<Event>; | ||
export { EventIterator }; | ||
export default EventIterator; |
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.EventIterator = event_iterator_1.EventIterator; | ||
function subscribe(event, options) { | ||
function subscribe(event, options, evOptions) { | ||
return new event_iterator_1.EventIterator((push) => { | ||
@@ -11,5 +11,5 @@ this.addEventListener(event, push, options); | ||
this.removeEventListener(event, push, options); | ||
}); | ||
}, evOptions); | ||
} | ||
exports.subscribe = subscribe; | ||
exports.default = event_iterator_1.EventIterator; |
@@ -6,8 +6,12 @@ export declare type PushCallback<T> = (res: T) => void; | ||
export declare type RemoveHandler<T> = (push: PushCallback<T>, stop: StopCallback<T>, fail: FailCallback<T>) => void; | ||
export interface EventIteratorOptions { | ||
highWaterMark?: number; | ||
} | ||
export declare class EventIterator<T> implements AsyncIterable<T> { | ||
private listen; | ||
private remove?; | ||
constructor(listen: ListenHandler<T>, remove?: RemoveHandler<T>); | ||
private options; | ||
constructor(listen: ListenHandler<T>, remove?: RemoveHandler<T>, options?: EventIteratorOptions); | ||
[Symbol.asyncIterator](): AsyncIterator<T>; | ||
} | ||
export default EventIterator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
class EventIterator { | ||
constructor(listen, remove) { | ||
constructor(listen, remove, options = {}) { | ||
this.listen = listen; | ||
this.remove = remove; | ||
this.options = Object.assign({ highWaterMark: 100 }, options); | ||
Object.freeze(this); | ||
@@ -22,4 +23,5 @@ } | ||
queue.push(Promise.resolve(resolution)); | ||
if (queue.length > 100 && console) { | ||
console.warn("EventIterator queue filling up"); | ||
const { highWaterMark } = this.options; | ||
if (highWaterMark !== undefined && queue.length >= highWaterMark && console) { | ||
console.warn(`EventIterator queue reached ${queue.length} items`); | ||
} | ||
@@ -26,0 +28,0 @@ } |
/// <reference types="node" /> | ||
import { Readable } from "stream"; | ||
import { EventIterator } from "./event-iterator"; | ||
export declare function stream(this: Readable): EventIterator<Buffer>; | ||
import { EventIterator, EventIteratorOptions } from "./event-iterator"; | ||
export declare function stream(this: Readable, evOptions?: EventIteratorOptions): EventIterator<Buffer>; | ||
export { EventIterator }; | ||
export default EventIterator; |
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.EventIterator = event_iterator_1.EventIterator; | ||
function stream() { | ||
function stream(evOptions) { | ||
return new event_iterator_1.EventIterator((push, stop, fail) => { | ||
@@ -22,5 +22,5 @@ this.addListener("data", push); | ||
} | ||
}); | ||
}, evOptions); | ||
} | ||
exports.stream = stream; | ||
exports.default = event_iterator_1.EventIterator; |
{ | ||
"name": "event-iterator", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Convert event emitters and event targets to ES async iterators", | ||
@@ -35,4 +35,4 @@ "homepage": "https://github.com/rolftimmermans/event-iterator", | ||
"scripts": { | ||
"test": "mocha && rm -rf lib && tsc" | ||
"test": "mocha test/*-test.ts && rm -rf lib && tsc" | ||
} | ||
} |
@@ -115,4 +115,7 @@ # EventIterator: convert any JS event emitter to async iterators | ||
/* High water mark defaults to 100. Set to undefined to disable warnings. */ | ||
interface EventIteratorOptions = {highWatermark?: number } | ||
class EventIterator<T> { | ||
constructor(ListenHandler<T>, ?RemoveHandler<T>) | ||
constructor(ListenHandler<T>, ?RemoveHandler<T>, ?EventIteratorOptions) | ||
@@ -286,2 +289,3 @@ [Symbol.asyncIterator](): AsyncIterator<T> | ||
async iterator; the internal `EventIterator` queue will fill up indefinitely. | ||
A warning will be emitted when the queue reaches 100 items. | ||
@@ -294,8 +298,19 @@ One example is reading each line from a file with `stream()` and executing a | ||
The limit can be changed or disabled by settings `highWaterMark` in the options | ||
of the `EventIterator` constructor. | ||
A next version may support an optional API to pause/resume if the queue becomes | ||
too long. | ||
## Changes | ||
1.2.0: | ||
* Add options argument to constructor, allowing configuration of `highWaterMark` (@alanshaw). | ||
1.1.0: | ||
* First stable version. | ||
## Licensed under MIT license | ||
Copyright (c) 2017 Rolf Timmermans | ||
Copyright (c) 2017-2019 Rolf Timmermans | ||
@@ -302,0 +317,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
@@ -1,4 +0,4 @@ | ||
import {EventIterator} from "./event-iterator" | ||
import {EventIterator, EventIteratorOptions} from "./event-iterator" | ||
export function subscribe(this: EventTarget, event: string, options?: AddEventListenerOptions) { | ||
export function subscribe(this: EventTarget, event: string, options?: AddEventListenerOptions, evOptions?: EventIteratorOptions) { | ||
return new EventIterator<Event>( | ||
@@ -12,2 +12,4 @@ (push) => { | ||
}, | ||
evOptions, | ||
) | ||
@@ -14,0 +16,0 @@ } |
@@ -8,2 +8,6 @@ export type PushCallback<T> = (res: T) => void | ||
export interface EventIteratorOptions { | ||
highWaterMark?: number | ||
} | ||
type AsyncResolver<T> = { | ||
@@ -19,6 +23,8 @@ resolve: (res: IteratorResult<T>) => void | ||
private remove?: RemoveHandler<T> | ||
private options: EventIteratorOptions | ||
constructor(listen: ListenHandler<T>, remove?: RemoveHandler<T>) { | ||
constructor(listen: ListenHandler<T>, remove?: RemoveHandler<T>, options: EventIteratorOptions = {}) { | ||
this.listen = listen | ||
this.remove = remove | ||
this.options = {highWaterMark: 100, ...options} | ||
Object.freeze(this) | ||
@@ -40,4 +46,5 @@ } | ||
queue.push(Promise.resolve(resolution)) | ||
if (queue.length > 100 && console) { | ||
console.warn("EventIterator queue filling up") | ||
const {highWaterMark} = this.options | ||
if (highWaterMark !== undefined && queue.length >= highWaterMark && console) { | ||
console.warn(`EventIterator queue reached ${queue.length} items`) | ||
} | ||
@@ -44,0 +51,0 @@ } |
import {Readable} from "stream" | ||
import {EventIterator} from "./event-iterator" | ||
import {EventIterator, EventIteratorOptions} from "./event-iterator" | ||
export function stream(this: Readable) { | ||
export function stream(this: Readable, evOptions?: EventIteratorOptions) { | ||
return new EventIterator<Buffer>( | ||
@@ -24,2 +24,4 @@ (push, stop, fail) => { | ||
}, | ||
evOptions, | ||
) | ||
@@ -26,0 +28,0 @@ } |
@@ -154,2 +154,64 @@ import "./symbol-polyfill" | ||
}) | ||
describe("with high water mark", function() { | ||
it("should warn", async function() { | ||
const oldconsole = console | ||
const log = global.console = new MemoryConsole | ||
const it = new EventIterator(next => {next("val")}, undefined, {highWaterMark: 1}) | ||
await new Promise(setImmediate) | ||
const result = await it[Symbol.asyncIterator]().next() | ||
global.console = oldconsole | ||
assert.equal(log.stderr.toString(), "EventIterator queue reached 1 items\n") | ||
}) | ||
}) | ||
}) | ||
import {Console} from "console" | ||
import {Writable} from "stream" | ||
export class BufferStream extends Writable { | ||
private readonly buffers: Buffer[] = [] | ||
_write(chunk: Buffer | string, encoding: string, callback: (err?: Error) => void) { | ||
if (typeof chunk === "string") chunk = Buffer.from(chunk) | ||
this.buffers.push(chunk) | ||
callback() | ||
return true | ||
} | ||
clear() { | ||
this.buffers.length = 0 | ||
} | ||
inspect() { | ||
return Buffer.concat(this.buffers).toString() | ||
} | ||
toString() { | ||
return Buffer.concat(this.buffers).toString() | ||
} | ||
} | ||
export class MemoryConsole extends Console { | ||
stdout: BufferStream | ||
stderr: BufferStream | ||
constructor() { | ||
const stdout = new BufferStream | ||
const stderr = new BufferStream | ||
super(stdout, stderr) | ||
this.stdout = stdout | ||
this.stderr = stderr | ||
Object.freeze(this) | ||
} | ||
clear() { | ||
this.stdout.clear() | ||
this.stderr.clear() | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29964
561
331