Skip to content

Commit

Permalink
v3.0.0 connect is now event driven and does not take a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilson / iamruinous committed Mar 11, 2017
1 parent c84f2a8 commit d8062e0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meshblu-firehose-socket.io",
"version": "2.2.0",
"version": "3.0.0",
"description": "Meshblu Firehose Client for socket.io",
"main": "index.js",
"browser": {
Expand Down
33 changes: 23 additions & 10 deletions src/firehose-socket-io.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ class MeshbluFirehoseSocketIO extends EventEmitter2

@meshbluConfig = {uuid, token, resolveSrv, protocol, hostname, port, service, domain, secure}

connect: (callback) =>
callback = _.once callback
connect: =>
@emit 'connecting'

@_resolveBaseUrl (error, baseUrl) =>
return callback error if error?
if error?
@emit 'resolve-base-url:error', error
return @_reconnect()

options =
path: "/socket.io/v1/#{@meshbluConfig.uuid}"
Expand All @@ -72,23 +74,34 @@ class MeshbluFirehoseSocketIO extends EventEmitter2
transports: @transports

@socket = SocketIOClient baseUrl, options

@socket.once 'identify', => @emit 'error', new Error(WRONG_SERVER_ERROR)

@socket.once 'connect', =>
@backoff.reset()
callback()
@socket.once 'connect_error', =>
return callback error unless @srvFailover?
@srvFailover.markBadUrl baseUrl, ttl: 60000
_.delay @connect, @backoff.duration(), callback
@emit 'connect'

@socket.once 'disconnect', =>
@_reconnect()

@socket.once 'connect_error', (error) =>
@emit 'connect_error', error if error? && !@srvFailover?
@srvFailover.markBadUrl baseUrl, ttl: 60000 if @srvFailover
@_reconnect()

@bindEvents()

_reconnect: (callback=->) =>
@emit 'reconnecting'
_.delay @connect, @backoff.duration(), callback

bindEvents: =>
@socket.on 'message', @_onMessage
_.each MeshbluFirehoseSocketIO.EVENTS, (event) =>
@socket.on event, =>
@emit event, arguments...

@socket.on 'error', =>
@socket.on 'error', (error) =>
@emit 'socket-io:error', arguments...

@socket.on 'close', =>
Expand All @@ -98,8 +111,8 @@ class MeshbluFirehoseSocketIO extends EventEmitter2
@emit 'socket-io:disconnect', arguments...

close: (callback) =>
@socket.once 'disconnect', callback
@socket.disconnect()
callback()

_assertNoSrv: ({service, domain, secure}) =>
throw new Error('domain parameter is only valid when the parameter resolveSrv is true') if domain?
Expand Down
15 changes: 10 additions & 5 deletions test/firehose-socket-io-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe 'MeshbluFirehoseSocketIO', ->
{@pathname, @query} = URL.parse @socket.client.request.url, true
@uuid = @socket.client.request.headers['x-meshblu-uuid']
@token = @socket.client.request.headers['x-meshblu-token']
@sut.connect done
@sut.once 'connect', done
@sut.connect()

it 'should connect', ->
expect(@socket).to.exist
Expand All @@ -43,7 +44,8 @@ describe 'MeshbluFirehoseSocketIO', ->
describe '-> onMessage', ->
beforeEach (done) ->
@server.on 'connection', (@socket) =>
@sut.connect done
@sut.once 'connect', done
@sut.connect()

beforeEach (done) ->
message =
Expand All @@ -60,7 +62,8 @@ describe 'MeshbluFirehoseSocketIO', ->
describe '-> onTypeFrom', ->
beforeEach (done) ->
@server.on 'connection', (@socket) =>
@sut.connect done
@sut.once 'connect', done
@sut.connect()

beforeEach (done) ->
message =
Expand All @@ -77,7 +80,8 @@ describe 'MeshbluFirehoseSocketIO', ->
describe '-> onType*', ->
beforeEach (done) ->
@server.on 'connection', (@socket) =>
@sut.connect done
@sut.once 'connect', done
@sut.connect()

beforeEach (done) ->
message =
Expand All @@ -94,7 +98,8 @@ describe 'MeshbluFirehoseSocketIO', ->
describe '-> onSubType**', ->
beforeEach (done) ->
@server.on 'connection', (@socket) =>
@sut.connect done
@sut.once 'connect', done
@sut.connect()

beforeEach (done) ->
message =
Expand Down
3 changes: 2 additions & 1 deletion test/resolve-srv-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ describe 'Meshblu', ->
weight: 100
}]
@sut.on 'error', done
@sut.connect done
@sut.once 'connect', done
@sut.connect()

it 'should get here', ->
# getting here is enough
3 changes: 1 addition & 2 deletions webpack.config.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
module: {
loaders: [
{
test: /\.coffee$/, loader: 'coffee', include: /src/
test: /\.coffee$/, loader: 'coffee-loader', include: /src/
}
]
},
Expand All @@ -24,7 +24,6 @@ module.exports = {
plugins: [
new webpack.IgnorePlugin(/^(buffertools)$/), // unwanted "deeper" dependency
new webpack.NoErrorsPlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
Expand Down

0 comments on commit d8062e0

Please sign in to comment.