summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/node-gyp/node_modules/npmlog/node_modules/are-we-there-yet/test/lib/test-event.js
blob: 2aa7c05c5864fa2ab6cc0ed56f9efa48cea7f498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use strict'
var util = require('util')

module.exports = function (obj, event, next) {
  var timeout = setTimeout(gotTimeout, 10)
  obj.once(event, gotResult)

  function gotTimeout () {
    obj.removeListener(event, gotResult)
    next(new Error('Timeout listening for ' + event))
  }
  var result = []
  function gotResult () {
    result = Array.prototype.slice.call(arguments)
    clearTimeout(timeout)
    timeout = setTimeout(gotNoMoreResults, 10)
    obj.once(event, gotTooManyResults)
  }
  function gotNoMoreResults () {
    obj.removeListener(event, gotTooManyResults)
    var args = [null].concat(result)
    next.apply(null, args)
  }
  function gotTooManyResults () {
    var secondResult = Array.prototype.slice.call(arguments)
    clearTimeout(timeout)
    next(new Error('Got too many results, first ' + util.inspect(result) + ' and then ' + util.inspect(secondResult)))
  }
}