Sha256: f42082e3f33dd59a24291835b3af00d959d7e039f7dd70224223074009692bd9

Contents?: true

Size: 1.25 KB

Versions: 54

Compression:

Stored size: 1.25 KB

Contents

module.exports = 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, cleanup)

      // listen to the event
      ee.on(event, fn)
      // push this listener to the list of cleanups
      cleanups.push({
        ee: ee,
        event: event,
        fn: fn,
      })
    }
  }

  return function (fn) {
    done = fn
  }

  function cleanup() {
    var x
    for (var i = 0; i < cleanups.length; i++) {
      x = cleanups[i]
      x.ee.removeListener(x.event, x.fn)
    }
    done.apply(null, arguments)
  }
}

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)
  }
}

Version data entries

54 entries across 20 versions & 3 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/serve-static/node_modules/send/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/morgan/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/morgan/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/serve-static/node_modules/send/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/morgan/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/serve-static/node_modules/send/node_modules/on-finished/node_modules/ee-first/index.js
gulp_assets-1.0.0.pre.5 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
gulp_assets-1.0.0.pre.4 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
gulp_assets-1.0.0.pre.3 template/node_modules/gulp-livereload/node_modules/tiny-lr/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/morgan/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/serve-static/node_modules/send/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/serve-static/node_modules/send/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/morgan/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.2.0 jasmine/node_modules/karma/node_modules/connect/node_modules/body-parser/node_modules/on-finished/node_modules/ee-first/index.js
hooch-0.2.0 jasmine/node_modules/karma/node_modules/connect/node_modules/morgan/node_modules/on-finished/node_modules/ee-first/index.js