1 |
3a515b92
|
cagy
|
# EE First
|
2 |
|
|
|
3 |
|
|
[![NPM version][npm-image]][npm-url]
|
4 |
|
|
[![Build status][travis-image]][travis-url]
|
5 |
|
|
[![Test coverage][coveralls-image]][coveralls-url]
|
6 |
|
|
[![License][license-image]][license-url]
|
7 |
|
|
[![Downloads][downloads-image]][downloads-url]
|
8 |
|
|
[![Gittip][gittip-image]][gittip-url]
|
9 |
|
|
|
10 |
|
|
Get the first event in a set of event emitters and event pairs,
|
11 |
|
|
then clean up after itself.
|
12 |
|
|
|
13 |
|
|
## Install
|
14 |
|
|
|
15 |
|
|
```sh
|
16 |
|
|
$ npm install ee-first
|
17 |
|
|
```
|
18 |
|
|
|
19 |
|
|
## API
|
20 |
|
|
|
21 |
|
|
```js
|
22 |
|
|
var first = require('ee-first')
|
23 |
|
|
```
|
24 |
|
|
|
25 |
|
|
### first(arr, listener)
|
26 |
|
|
|
27 |
|
|
Invoke `listener` on the first event from the list specified in `arr`. `arr` is
|
28 |
|
|
an array of arrays, with each array in the format `[ee, ...event]`. `listener`
|
29 |
|
|
will be called only once, the first time any of the given events are emitted. If
|
30 |
|
|
`error` is one of the listened events, then if that fires first, the `listener`
|
31 |
|
|
will be given the `err` argument.
|
32 |
|
|
|
33 |
|
|
The `listener` is invoked as `listener(err, ee, event, args)`, where `err` is the
|
34 |
|
|
first argument emitted from an `error` event, if applicable; `ee` is the event
|
35 |
|
|
emitter that fired; `event` is the string event name that fired; and `args` is an
|
36 |
|
|
array of the arguments that were emitted on the event.
|
37 |
|
|
|
38 |
|
|
```js
|
39 |
|
|
var ee1 = new EventEmitter()
|
40 |
|
|
var ee2 = new EventEmitter()
|
41 |
|
|
|
42 |
|
|
first([
|
43 |
|
|
[ee1, 'close', 'end', 'error'],
|
44 |
|
|
[ee2, 'error']
|
45 |
|
|
], function (err, ee, event, args) {
|
46 |
|
|
// listener invoked
|
47 |
|
|
})
|
48 |
|
|
```
|
49 |
|
|
|
50 |
|
|
#### .cancel()
|
51 |
|
|
|
52 |
|
|
The group of listeners can be cancelled before being invoked and have all the event
|
53 |
|
|
listeners removed from the underlying event emitters.
|
54 |
|
|
|
55 |
|
|
```js
|
56 |
|
|
var thunk = first([
|
57 |
|
|
[ee1, 'close', 'end', 'error'],
|
58 |
|
|
[ee2, 'error']
|
59 |
|
|
], function (err, ee, event, args) {
|
60 |
|
|
// listener invoked
|
61 |
|
|
})
|
62 |
|
|
|
63 |
|
|
// cancel and clean up
|
64 |
|
|
thunk.cancel()
|
65 |
|
|
```
|
66 |
|
|
|
67 |
|
|
[npm-image]: https://img.shields.io/npm/v/ee-first.svg?style=flat-square
|
68 |
|
|
[npm-url]: https://npmjs.org/package/ee-first
|
69 |
|
|
[github-tag]: http://img.shields.io/github/tag/jonathanong/ee-first.svg?style=flat-square
|
70 |
|
|
[github-url]: https://github.com/jonathanong/ee-first/tags
|
71 |
|
|
[travis-image]: https://img.shields.io/travis/jonathanong/ee-first.svg?style=flat-square
|
72 |
|
|
[travis-url]: https://travis-ci.org/jonathanong/ee-first
|
73 |
|
|
[coveralls-image]: https://img.shields.io/coveralls/jonathanong/ee-first.svg?style=flat-square
|
74 |
|
|
[coveralls-url]: https://coveralls.io/r/jonathanong/ee-first?branch=master
|
75 |
|
|
[license-image]: http://img.shields.io/npm/l/ee-first.svg?style=flat-square
|
76 |
|
|
[license-url]: LICENSE.md
|
77 |
|
|
[downloads-image]: http://img.shields.io/npm/dm/ee-first.svg?style=flat-square
|
78 |
|
|
[downloads-url]: https://npmjs.org/package/ee-first
|
79 |
|
|
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square
|
80 |
|
|
[gittip-url]: https://www.gittip.com/jonathanong/
|