pax_global_header00006660000000000000000000000064125306721140014513gustar00rootroot0000000000000052 comment=512e0ce4cc3643f603708f965a97b61b1a9c0441 ee-first-1.1.1/000077500000000000000000000000001253067211400132315ustar00rootroot00000000000000ee-first-1.1.1/.gitignore000066400000000000000000000000411253067211400152140ustar00rootroot00000000000000.DS_Store* node_modules coverage ee-first-1.1.1/.travis.yml000066400000000000000000000003351253067211400153430ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" - "0.12" - "1.0" - "1.8" - "2.0" - "2.1" sudo: false script: "npm run test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" ee-first-1.1.1/LICENSE000066400000000000000000000021131253067211400142330ustar00rootroot00000000000000 The MIT License (MIT) Copyright (c) 2014 Jonathan Ong me@jongleberry.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ee-first-1.1.1/README.md000066400000000000000000000050711253067211400145130ustar00rootroot00000000000000# EE First [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] [![Gittip][gittip-image]][gittip-url] Get the first event in a set of event emitters and event pairs, then clean up after itself. ## Install ```sh $ npm install ee-first ``` ## API ```js var first = require('ee-first') ``` ### first(arr, listener) Invoke `listener` on the first event from the list specified in `arr`. `arr` is an array of arrays, with each array in the format `[ee, ...event]`. `listener` will be called only once, the first time any of the given events are emitted. If `error` is one of the listened events, then if that fires first, the `listener` will be given the `err` argument. The `listener` is invoked as `listener(err, ee, event, args)`, where `err` is the first argument emitted from an `error` event, if applicable; `ee` is the event emitter that fired; `event` is the string event name that fired; and `args` is an array of the arguments that were emitted on the event. ```js var ee1 = new EventEmitter() var ee2 = new EventEmitter() first([ [ee1, 'close', 'end', 'error'], [ee2, 'error'] ], function (err, ee, event, args) { // listener invoked }) ``` #### .cancel() The group of listeners can be cancelled before being invoked and have all the event listeners removed from the underlying event emitters. ```js var thunk = first([ [ee1, 'close', 'end', 'error'], [ee2, 'error'] ], function (err, ee, event, args) { // listener invoked }) // cancel and clean up thunk.cancel() ``` [npm-image]: https://img.shields.io/npm/v/ee-first.svg?style=flat-square [npm-url]: https://npmjs.org/package/ee-first [github-tag]: http://img.shields.io/github/tag/jonathanong/ee-first.svg?style=flat-square [github-url]: https://github.com/jonathanong/ee-first/tags [travis-image]: https://img.shields.io/travis/jonathanong/ee-first.svg?style=flat-square [travis-url]: https://travis-ci.org/jonathanong/ee-first [coveralls-image]: https://img.shields.io/coveralls/jonathanong/ee-first.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/jonathanong/ee-first?branch=master [license-image]: http://img.shields.io/npm/l/ee-first.svg?style=flat-square [license-url]: LICENSE.md [downloads-image]: http://img.shields.io/npm/dm/ee-first.svg?style=flat-square [downloads-url]: https://npmjs.org/package/ee-first [gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square [gittip-url]: https://www.gittip.com/jonathanong/ ee-first-1.1.1/index.js000066400000000000000000000032241253067211400146770ustar00rootroot00000000000000/*! * ee-first * Copyright(c) 2014 Jonathan Ong * MIT Licensed */ 'use strict' /** * Module exports. * @public */ module.exports = first /** * Get the first event in a set of event emitters and event pairs. * * @param {array} stuff * @param {function} done * @public */ function first(stuff, done) { if (!Array.isArray(stuff)) throw new TypeError('arg must be an array of [ee, events...] arrays') var cleanups = [] for (var i = 0; i < stuff.length; i++) { var arr = stuff[i] if (!Array.isArray(arr) || arr.length < 2) throw new TypeError('each array member must be [ee, events...]') var ee = arr[0] for (var j = 1; j < arr.length; j++) { var event = arr[j] var fn = listener(event, callback) // listen to the event ee.on(event, fn) // push this listener to the list of cleanups cleanups.push({ ee: ee, event: event, fn: fn, }) } } function callback() { cleanup() done.apply(null, arguments) } function cleanup() { var x for (var i = 0; i < cleanups.length; i++) { x = cleanups[i] x.ee.removeListener(x.event, x.fn) } } function thunk(fn) { done = fn } thunk.cancel = cleanup return thunk } /** * Create the event listener. * @private */ function listener(event, done) { return function onevent(arg1) { var args = new Array(arguments.length) var ee = this var err = event === 'error' ? arg1 : null // copy args to prevent arguments escaping scope for (var i = 0; i < args.length; i++) { args[i] = arguments[i] } done(err, ee, event, args) } } ee-first-1.1.1/package.json000066400000000000000000000015331253067211400155210ustar00rootroot00000000000000{ "name": "ee-first", "description": "return the first event in a set of ee/event pairs", "version": "1.1.1", "author": { "name": "Jonathan Ong", "email": "me@jongleberry.com", "url": "http://jongleberry.com", "twitter": "https://twitter.com/jongleberry" }, "contributors": [ "Douglas Christopher Wilson " ], "license": "MIT", "repository": "jonathanong/ee-first", "devDependencies": { "istanbul": "0.3.9", "mocha": "2.2.5" }, "files": [ "index.js", "LICENSE" ], "scripts": { "test": "mocha --reporter spec --bail --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" } } ee-first-1.1.1/test/000077500000000000000000000000001253067211400142105ustar00rootroot00000000000000ee-first-1.1.1/test/test.js000066400000000000000000000055501253067211400155320ustar00rootroot00000000000000 var EventEmitter = require('events').EventEmitter var assert = require('assert') var first = require('..') describe('first', function () { var ee1 = new EventEmitter() var ee2 = new EventEmitter() var ee3 = new EventEmitter() it('should require array argument', function () { assert.throws(first.bind()) assert.throws(first.bind(null, 'string')) assert.throws(first.bind(null, 42)) assert.throws(first.bind(null, {})) }) it('should require array of arrays argument', function () { assert.throws(first.bind(null, [0])) assert.throws(first.bind(null, ['string'])) assert.throws(first.bind(null, [[ee1], 'string'])) }) it('should emit the first event', function (done) { first([ [ee1, 'a', 'b', 'c'], [ee2, 'a', 'b', 'c'], [ee3, 'a', 'b', 'c'], ], function (err, ee, event, args) { assert.ifError(err) assert.equal(ee, ee2) assert.equal(event, 'b') assert.deepEqual(args, [1, 2, 3]) done() }) ee2.emit('b', 1, 2, 3) }) it('it should return an error if event === error', function (done) { first([ [ee1, 'error', 'b', 'c'], [ee2, 'error', 'b', 'c'], [ee3, 'error', 'b', 'c'], ], function (err, ee, event, args) { assert.equal(err.message, 'boom') assert.equal(ee, ee3) assert.equal(event, 'error') done() }) ee3.emit('error', new Error('boom')) }) it('should cleanup after itself', function (done) { first([ [ee1, 'a', 'b', 'c'], [ee2, 'a', 'b', 'c'], [ee3, 'a', 'b', 'c'], ], function (err, ee, event, args) { assert.ifError(err) ;[ee1, ee2, ee3].forEach(function (ee) { ['a', 'b', 'c'].forEach(function (event) { assert(!ee.listeners(event).length) }) }) done() }) ee1.emit('a') }) it('should return a thunk', function (done) { var thunk = first([ [ee1, 'a', 'b', 'c'], [ee2, 'a', 'b', 'c'], [ee3, 'a', 'b', 'c'], ]) thunk(function (err, ee, event, args) { assert.ifError(err) assert.equal(ee, ee2) assert.equal(event, 'b') assert.deepEqual(args, [1, 2, 3]) done() }) ee2.emit('b', 1, 2, 3) }) it('should not emit after thunk.cancel()', function (done) { var thunk = first([ [ee1, 'a', 'b', 'c'], [ee2, 'a', 'b', 'c'], [ee3, 'a', 'b', 'c'], ]) thunk(function () { assert.ok(false) }) thunk.cancel() ee2.emit('b', 1, 2, 3) setTimeout(done, 10) }) it('should cleanup after thunk.cancel()', function (done) { var thunk = first([ [ee1, 'a', 'b', 'c'], [ee2, 'a', 'b', 'c'], [ee3, 'a', 'b', 'c'], ]) thunk.cancel() ;[ee1, ee2, ee3].forEach(function (ee) { ['a', 'b', 'c'].forEach(function (event) { assert(!ee.listeners(event).length) }) }) done() }) })