diff --git a/lib/Channel.coffee b/lib/Channel.coffee index 9a13b5c..b0d002d 100644 --- a/lib/Channel.coffee +++ b/lib/Channel.coffee @@ -5,10 +5,12 @@ class Channel @events = {} @stack = [] if @socket + @joined = false @socket.write type: 'join' channel: @name else + @joined = true @listeners = [] realEmit: (event, args...) => @@ -56,7 +58,13 @@ class Channel delete @events[event] return @ - use: (fn) -> @stack.push(fn); @ + ready: (fn) => + if @joined + fn @ + else + @on 'join', => fn @ + + use: (fn) => @stack.push(fn); @ runStack: (event, args, cb) => return cb args if @stack.length is 0 return cb args if event is 'newListener' diff --git a/lib/Client.coffee b/lib/Client.coffee index 9716940..42a8506 100644 --- a/lib/Client.coffee +++ b/lib/Client.coffee @@ -29,24 +29,29 @@ client = (opt) -> validate: (socket, msg, done) -> return done false unless typeof msg is 'object' return done false unless typeof msg.type is 'string' - if msg.type is 'emit' - return done false unless typeof msg.channel is 'string' - return done false unless typeof @channels[msg.channel]? - return done false unless typeof msg.event is 'string' - return done false unless Array.isArray msg.args - else - return done false + switch msg.type + when 'emit' + return done false unless typeof msg.channel is 'string' + return done false unless typeof @channels[msg.channel]? + return done false unless typeof msg.event is 'string' + return done false unless Array.isArray msg.args + when 'joined' + return done false unless typeof msg.channel is 'string' + return done false unless typeof @channels[msg.channel]? + else + return done false return done true error: (socket, err) -> throw err close: (socket, reason) -> @emit 'close', reason message: (socket, msg) -> - try - chan = @channels[msg.channel] - if msg.type is 'emit' + chan = @channels[msg.channel] + switch msg.type + when 'emit' chan.realEmit msg.event, msg.args... - catch err - @error socket, err + when 'joined' + chan.joined = true + chan.realEmit 'join' out.options[k]=v for k,v of opt return out diff --git a/lib/Server.coffee b/lib/Server.coffee index 9e789f8..11053f4 100644 --- a/lib/Server.coffee +++ b/lib/Server.coffee @@ -27,37 +27,39 @@ module.exports = (opt) -> validate: (socket, msg, done) -> return done false unless typeof msg is 'object' return done false unless typeof msg.type is 'string' - if msg.type is 'emit' - return done false unless typeof msg.channel is 'string' - return done false unless typeof @channels[msg.channel]? - return done false unless typeof msg.event is 'string' - return done false unless Array.isArray msg.args - else if msg.type is 'join' - return done false unless typeof msg.channel is 'string' - return done false unless typeof @channels[msg.channel]? - else if msg.type is 'unjoin' - return done false unless typeof msg.channel is 'string' - return done false unless typeof @channels[msg.channel]? - else - return done false + switch msg.type + when 'emit' + return done false unless typeof msg.channel is 'string' + return done false unless typeof @channels[msg.channel]? + return done false unless typeof msg.event is 'string' + return done false unless Array.isArray msg.args + when 'join' + return done false unless typeof msg.channel is 'string' + return done false unless typeof @channels[msg.channel]? + when 'unjoin' + return done false unless typeof msg.channel is 'string' + return done false unless typeof @channels[msg.channel]? + else + return done false return done true invalid: (socket, msg) -> socket.close() message: (socket, msg) -> - try - chan = @channels[msg.channel] - if msg.type is 'emit' + chan = @channels[msg.channel] + switch msg.type + when'emit' chan.realEmit msg.event, msg.args... - else if msg.type is 'join' + when 'join' # TODO: Pass an eventemitter instead of socket chan.listeners.push socket chan.realEmit 'join', socket - else if msg.type is 'unjoin' + socket.write + type: 'joined' + channel: msg.channel + when 'unjoin' # TODO: remove socket from chan.listeners chan.realEmit 'unjoin', socket - catch err - @error socket, err out.options[k]=v for k,v of opt return out \ No newline at end of file diff --git a/package.json b/package.json index c84815c..ad657a5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name":"pulsar", "description":"Events/PubSub via WebSockets", - "version":"0.0.6", + "version":"0.0.7", "homepage":"http://github.com/wearefractal/pulsar", "repository":"git://github.com/wearefractal/pulsar.git", "author":"Fractal (http://wearefractal.com/)", diff --git a/test/server.coffee b/test/server.coffee index f933df4..0b4230e 100644 --- a/test/server.coffee +++ b/test/server.coffee @@ -23,6 +23,36 @@ describe 'Pulsar', -> should.exist channel done() + it 'should emit join on server', (done) -> + serv = getServer() + channel = serv.channel 'test' + should.exist channel + + client = getClient serv + cchan = client.channel 'test' + + channel.on 'join', (socket) -> + done() + + it 'should emit join on client', (done) -> + serv = getServer() + channel = serv.channel 'test' + should.exist channel + + client = getClient serv + cchan = client.channel 'test' + + cchan.on 'join', -> done() + + it 'should call ready on client', (done) -> + serv = getServer() + channel = serv.channel 'test' + should.exist channel + + client = getClient serv + cchan = client.channel 'test' + cchan.ready -> done() + it 'should call', (done) -> serv = getServer() channel = serv.channel 'test'