lib/jpc/dispatcher.rb in jpc-1.2.0 vs lib/jpc/dispatcher.rb in jpc-1.2.1

- old
+ new

@@ -1,37 +1,41 @@ -class JPC::Dispatcher - include JPC::Helpers +module JPC + class Dispatcher + include JPC::Helpers - def subscribe(ws, channel) - channels[channel.to_sym] ||= [] - channels[channel.to_sym] << ws + def subscribe(ws, channel) + channels[channel.to_sym] ||= [] + channels[channel.to_sym].push(ws) unless channels[channel.to_sym].include?(ws) - { channel: channel, status: 'subscribed' } - end + { channel: channel, status: 'subscribed' } + end - def unsubscribe(ws, channel) - fail "Channel #{channel} not found" unless channels[channel.to_sym] + def unsubscribe(ws, channel) + raise "Channel #{channel} not found" unless channels[channel.to_sym] - channels[channel.to_sym] -= [ws] + channels[channel.to_sym].each_with_index do |object, index| + channels[channel.to_sym].delete_at(index) if object == ws + end - { channel: channel, status: 'unsubscribed' } - end + { channel: channel, status: 'unsubscribed' } + end - def cast(channel, payload) - message = make_message(channel: channel, payload: payload) + def cast(channel, payload) + message = make_message(channel: channel, payload: payload) - sent = 0 + sent = 0 - channels[channel.to_sym] && channels[channel.to_sym].each do |ws| - ws.send(message) - sent += 1 + channels[channel.to_sym] && channels[channel.to_sym].each do |ws| + ws.send(message) + sent += 1 + end + + { channel: channel, sent: sent } end - { channel: channel, sent: sent } - end + private - private - - def channels - @channels ||= {} + def channels + @channels ||= {} + end end end