Sha256: 6d657a2378f207f4cb6f72ff557939d69a8e074038a47130676109af6f0e9d62

Contents?: true

Size: 1.47 KB

Versions: 25

Compression:

Stored size: 1.47 KB

Contents

class ChannelTasks
  @@listeners = {}
  @@channel_listeners = {}
  
  # The dispatcher passes its self in
  def initialize(channel, dispatcher=nil)
    @channel = channel
  end
  
  def add_listener(channel_name, scope={})
    # Track every channel that is listening
    @@listeners[channel_name] ||= []
    @@listeners[channel_name] << @channel
    
    # Also keep track of which channel names a channel is listening
    # on so it can be removed if a channel is closed.
    @@channel_listeners[@channel] ||= {}
    @@channel_listeners[@channel][channel_name] = true
  end
  
  def remove_listener(channel_name, scope={})
    if @@listeners[channel_name]
      @@listeners[channel_name].delete(@channel)
      if @@channel_listeners[@channel]
        @@channel_listeners[@channel].delete(channel_name)
      end
    end
  end
  
  # Called when a channel is closed, removes its listeners from
  # all channels.
  def close!
    channel_names = @@channel_listeners.delete(@channel)
    
    if channel_names
      channel_names.each_pair do |channel_name,val|
        remove_listener(channel_name)
      end
    end
  end
  
  def self.send_message_to_channel(channel_name, message, skip_channel=nil)
    listeners = @@listeners[channel_name]
    
    if listeners
      listeners.each do |listener|
        # We might need to skip a channel if the update came in on this
        # channel.
        next if listener == skip_channel
        
        listener.send_message(*message)
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
volt-0.5.18 app/volt/tasks/channel_tasks.rb
volt-0.5.17 app/volt/tasks/channel_tasks.rb
volt-0.5.16 app/volt/tasks/channel_tasks.rb
volt-0.5.15 app/volt/tasks/channel_tasks.rb
volt-0.5.14 app/volt/tasks/channel_tasks.rb
volt-0.5.13 app/volt/tasks/channel_tasks.rb
volt-0.5.12 app/volt/tasks/channel_tasks.rb
volt-0.5.11 app/volt/tasks/channel_tasks.rb
volt-0.5.10 app/volt/tasks/channel_tasks.rb
volt-0.5.9 app/volt/tasks/channel_tasks.rb
volt-0.5.8 app/volt/tasks/channel_tasks.rb
volt-0.5.7 app/volt/tasks/channel_tasks.rb
volt-0.5.6 app/volt/tasks/channel_tasks.rb
volt-0.5.4 app/volt/tasks/channel_tasks.rb
volt-0.5.3 app/volt/tasks/channel_tasks.rb
volt-0.5.2 app/volt/tasks/channel_tasks.rb
volt-0.5.1 app/volt/tasks/channel_tasks.rb
volt-0.5.0 app/volt/tasks/channel_tasks.rb
volt-0.4.18 app/volt/tasks/channel_tasks.rb
volt-0.4.17 app/volt/tasks/channel_tasks.rb