You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.6 KiB

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