index.js
function attachFinishedListener (msg, callback) {
  var eeMsg
  var eeSocket
  var finished = false

  function onFinish (error) {
    eeMsg.cancel()
    eeSocket.cancel()

    finished = true
    callback(error)
  }

  // finished on first message event
  eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish)

  function onSocket (socket) {
    // remove listener
    msg.removeListener('socket', onSocket)

    if (finished) return
    if (eeMsg !== eeSocket) return

    // finished on first socket event
    eeSocket = first([[socket, 'error', 'close']], onFinish)
  }

  if (msg.socket) {
    // socket already assigned
    onSocket(msg.socket)
    return
  }

  // wait for socket to be assigned
  msg.on('socket', onSocket)

  if (msg.socket === undefined) {
    // istanbul ignore next: node.js 0.8 patch
    patchAssignSocket(msg, onSocket)
  }
}
function attachFinishedListener (msg, callback) {
  var eeMsg
  var eeSocket
  var finished = false

  function onFinish (error) {
    eeMsg.cancel()
    eeSocket.cancel()

    finished = true
    callback(error)
  }

  // finished on first message event
  eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish)

  function onSocket (socket) {
    // remove listener
    msg.removeListener('socket', onSocket)

    if (finished) return
    if (eeMsg !== eeSocket) return

    // finished on first socket event
    eeSocket = first([[socket, 'error', 'close']], onFinish)
  }

  if (msg.socket) {
    // socket already assigned
    onSocket(msg.socket)
    return
  }

  // wait for socket to be assigned
  msg.on('socket', onSocket)

  if (msg.socket === undefined) {
    // istanbul ignore next: node.js 0.8 patch
    patchAssignSocket(msg, onSocket)
  }
}
function attachFinishedListener ( msg , callback ) { var eeMsg var eeSocket var finished = false function onFinish ( error ) { eeMsg . cancel ( ) eeSocket . cancel ( ) finished = true callback ( error ) } // finished on first message event eeMsg = eeSocket = first ( [ [ msg , 'end' , 'finish' ] ] , onFinish ) function onSocket ( socket ) { // remove listener msg . removeListener ( 'socket' , onSocket ) if ( finished ) return if ( eeMsg !== eeSocket ) return // finished on first socket event eeSocket = first ( [ [ socket , 'error' , 'close' ] ] , onFinish ) } if ( msg . socket ) { // socket already assigned onSocket ( msg . socket ) return } // wait for socket to be assigned msg . on ( 'socket' , onSocket ) if ( msg . socket === undefined ) { // istanbul ignore next: node.js 0.8 patch patchAssignSocket ( msg , onSocket ) } }
Attach a finished listener to the message.

@param {object} msg
@param {function} callback
@private
Attach a finished listener to the message .