@async-generators/from-emitter
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -5,2 +5,9 @@ export declare type EventEmitter = { | ||
}; | ||
export default function <T>(emitter: EventEmitter, onNext?: string, onError?: string, onDone?: string, selectNext?: (...any) => T, selectError?: (...any) => any): AsyncIterable<T>; | ||
export default function <T>(emitter: EventEmitter, opts?: { | ||
onNext?: string; | ||
onError?: string; | ||
onDone?: string; | ||
selectNext?: (...any) => T; | ||
selectError?: (...any) => any; | ||
dispose?: () => void; | ||
}): AsyncIterable<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
if (Symbol["asyncIterator"] === undefined) | ||
(Symbol["asyncIterator"]) = Symbol.for("asyncIterator"); | ||
const subject_1 = require("@async-generators/subject"); | ||
function default_1(emitter, onNext = "next", onError = "error", onDone = "done", selectNext = (x) => x, selectError = (x) => x) { | ||
function default_1(emitter, opts) { | ||
let _opts = Object.assign({ | ||
onNext: "next", | ||
onError: "error", | ||
onDone: "done", | ||
selectNext: (x) => x, | ||
selectError: (x) => x, | ||
dispose: () => { } | ||
}, opts); | ||
let subject = subject_1.default(); | ||
let disposed = false; | ||
const nextListener = (...args) => subject.next(selectNext(...args)); | ||
const nextListener = (...args) => subject.next(_opts.selectNext(...args)); | ||
const errorListener = (...args) => { | ||
if (disposed) | ||
return; | ||
subject.error(selectError(...args)); | ||
subject.error(_opts.selectError(...args)); | ||
dispose(); | ||
@@ -21,11 +31,12 @@ }; | ||
const dispose = () => { | ||
emitter.removeListener(onNext, nextListener); | ||
emitter.removeListener(onError, errorListener); | ||
emitter.removeListener(onDone, doneListener); | ||
emitter.removeListener(_opts.onNext, nextListener); | ||
emitter.removeListener(_opts.onError, errorListener); | ||
emitter.removeListener(_opts.onDone, doneListener); | ||
_opts.dispose(); | ||
disposed = true; | ||
; | ||
}; | ||
emitter.addListener(onNext, nextListener); | ||
emitter.addListener(onError, errorListener); | ||
emitter.addListener(onDone, doneListener); | ||
emitter.addListener(_opts.onNext, nextListener); | ||
emitter.addListener(_opts.onError, errorListener); | ||
emitter.addListener(_opts.onDone, doneListener); | ||
subject.on("disposed", () => dispose()); | ||
@@ -32,0 +43,0 @@ return subject; |
{ | ||
"name": "@async-generators/from-emitter", | ||
"description": "convert an event-emitter to an async-iterable ", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -31,3 +31,3 @@ "name": "Meirion Hughes", | ||
"devDependencies": { | ||
"@async-generators/equal": "^0.2.1", | ||
"@async-generators/equal": "^0.6.1", | ||
"@types/chai": "^4.0.4", | ||
@@ -46,6 +46,5 @@ "@types/mocha": "^2.2.43", | ||
"scripts": { | ||
"test": "nyc node_modules/mocha/bin/mocha --harmony-async-iteration --require source-map-support/register --compilers ts:ts-node/register test/*.ts", | ||
"build": "npm run build:commonjs && npm run build:es2015", | ||
"test": "nyc node_modules/mocha/bin/mocha --require source-map-support/register --compilers ts:ts-node/register test/*.ts", | ||
"build": "npm run build:commonjs", | ||
"build:commonjs": "tsc --project tsconfig.build.json --rootDir src/ --outDir ./dist/commonjs", | ||
"build:es2015": "tsc --project tsconfig.build.json --rootDir src/ --outDir ./dist/es2015 --module es2015", | ||
"cover": "codecov" | ||
@@ -76,5 +75,5 @@ }, | ||
"dependencies": { | ||
"@async-generators/subject": "^0.2.1", | ||
"@async-generators/terminator": "^0.2.0" | ||
"@async-generators/subject": "^0.3.1", | ||
"@async-generators/terminator": "^0.3.1" | ||
} | ||
} |
# from-emitter | ||
[![logo](https://avatars1.githubusercontent.com/u/31987273?v=4&s=100)][async-url] | ||
[![logo](https://avatars1.githubusercontent.com/u/31987273?v=4&s=110)][async-url] | ||
@@ -10,6 +10,2 @@ inform an iterable when it is prematurely terminated by the consumer. | ||
## Usage | ||
_package requires a system that supports async-iteration, either natively or via down-compiling_ | ||
### Install | ||
@@ -20,10 +16,6 @@ ``` | ||
This package's `main` entry points to a `commonjs` distribution. | ||
## Exports | ||
Additionally, the `module` entry points to a `es2015` distribution, which can be used by build systems, such as webpack, to directly use es2015 modules. | ||
### (default) from-emitter(emitter, onNext, onError, onDone [,selectNext][,selectError]) | ||
## Api | ||
### from-emitter(emitter, onNext, onError, onDone [,selectNext][,selectError]) | ||
<code>from-emitter()</code> subscribes to `onNext`, `onError` and `onDone` and returns a (one-time) iterable-sequence of captured events. When the event listeners are called, the arguments are passed to `selectNext(...args)` and `selectError(...args)` to pick a value (defaults to the first argument). If the sequence completes (`onDone`), or the consumer terminates early, the event listeners are detached from the emitter and the iterable becomes disposed and cannot be iterated again. | ||
@@ -66,3 +58,4 @@ | ||
``` | ||
node --harmony-async-iteration example.js | ||
node example.js // or... | ||
node --harmony-async-iteration example.js | ||
``` | ||
@@ -69,0 +62,0 @@ |
@@ -0,1 +1,3 @@ | ||
if(Symbol["asyncIterator"] === undefined) ((<any>Symbol)["asyncIterator"]) = Symbol.for("asyncIterator"); | ||
import Subject from '@async-generators/subject'; | ||
@@ -10,15 +12,28 @@ | ||
emitter: EventEmitter, | ||
onNext: string = "next", | ||
onError: string = "error", | ||
onDone: string = "done", | ||
selectNext: (...any) => T = (x) => x as T, | ||
selectError: (...any) => any = (x) => x | ||
opts?: { | ||
onNext?: string | ||
onError?: string, | ||
onDone?: string, | ||
selectNext?: (...any) => T, | ||
selectError?: (...any) => any, | ||
dispose?: () => void, | ||
} | ||
): AsyncIterable<T> { | ||
let _opts = Object.assign({ | ||
onNext: "next", | ||
onError: "error", | ||
onDone: "done", | ||
selectNext: (x) => x as T, | ||
selectError: (x) => x, | ||
dispose: () => { } | ||
}, opts); | ||
let subject = Subject<T>(); | ||
let disposed = false; | ||
const nextListener = (...args) => subject.next(selectNext(...args)); | ||
const nextListener = (...args) => subject.next(_opts.selectNext(...args)); | ||
const errorListener = (...args) => { | ||
if (disposed) return; | ||
subject.error(selectError(...args)); | ||
subject.error(_opts.selectError(...args)); | ||
dispose(); | ||
@@ -33,11 +48,12 @@ } | ||
const dispose = () => { | ||
emitter.removeListener(onNext, nextListener); | ||
emitter.removeListener(onError, errorListener); | ||
emitter.removeListener(onDone, doneListener); | ||
emitter.removeListener(_opts.onNext, nextListener); | ||
emitter.removeListener(_opts.onError, errorListener); | ||
emitter.removeListener(_opts.onDone, doneListener); | ||
_opts.dispose(); | ||
disposed = true;; | ||
} | ||
emitter.addListener(onNext, nextListener); | ||
emitter.addListener(onError, errorListener); | ||
emitter.addListener(onDone, doneListener); | ||
emitter.addListener(_opts.onNext, nextListener); | ||
emitter.addListener(_opts.onError, errorListener); | ||
emitter.addListener(_opts.onDone, doneListener); | ||
@@ -44,0 +60,0 @@ subject.on("disposed", () => dispose()); |
Sorry, the diff of this file is not supported yet
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
10890
7
106
89
+ Added@async-generators/subject@0.3.1(transitive)
+ Added@async-generators/terminator@0.3.1(transitive)
- Removed@async-generators/subject@0.2.1(transitive)
- Removed@async-generators/terminator@0.2.0(transitive)