import Promise from 'bluebird'
import config from '../config/config'
import events from './events'

function connectSocket (token) {
  return new Promise((resolve, reject) => {
    window._socket = window.io(config.backend, { query: `token=${token}` })

    window._socket.once(events.LOGGED_IN, () => {
      resolve()
    })

    window._socket.once(events.NAME_TAKEN, () => {
      window._socket.disconnect()
      reject({ message: 'That username is taken!' })
    })

    window._socket.once(events.CONNECT_ERROR, () => {
      window._socket.disconnect()
      reject({ message: 'Something went wrong.' })
    })
  })
}

export default connectSocket

@@ -312,16 +312,108 @@
 esolve()
+%0A%0A      window._socket.off(events.NAME_TAKEN)%0A      window._socket.off(events.CONNECT_ERROR)
 %0A    %7D)%0A
@@ -515,37 +515,32 @@
 ect(
-%7B message: 
+new Error(
 'That 
-user
 name is 
 take
@@ -539,17 +539,23 @@
  is 
+already 
 taken
-!' %7D
+')
 )%0A  
@@ -665,44 +665,47 @@
 ect(
-%7B message: 'Something went wrong.' %7D
+new Error('Error connecting to server')
 )%0A  

Remove error listeners in connect promise once succesfully connected