Navigation Menu

Skip to content

Commit

Permalink
add ack for channel joins and .ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Aug 15, 2012
1 parent d3863ec commit 687f348
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 34 deletions.
10 changes: 9 additions & 1 deletion lib/Channel.coffee
Expand Up @@ -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...) =>
Expand Down Expand Up @@ -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'
Expand Down
29 changes: 17 additions & 12 deletions lib/Client.coffee
Expand Up @@ -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
Expand Down
42 changes: 22 additions & 20 deletions lib/Server.coffee
Expand Up @@ -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
2 changes: 1 addition & 1 deletion 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 <contact@wearefractal.com> (http://wearefractal.com/)",
Expand Down
30 changes: 30 additions & 0 deletions test/server.coffee
Expand Up @@ -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'
Expand Down

0 comments on commit 687f348

Please sign in to comment.