Parent

EventMachine::Channel

Provides a simple interface to push items to a number of subscribers. The channel will schedule all operations on the main reactor thread for thread safe reactor operations.

This provides a convenient way for connections to consume messages from long running code in defer, without threading issues.

 channel = EM::Channel.new
 sid = channel.subscribe{ |msg| p [:got, msg] }
 channel.push('hello world')
 channel.unsubscribe(sid)

See examples/ex_channel.rb for a detailed example.

Public Class Methods

new() click to toggle source

Create a new channel

    # File lib/em/channel.rb, line 17
17:     def initialize
18:       @subs = {}
19:       @uid = 0
20:     end

Public Instance Methods

<<(*items) click to toggle source
Alias for: push
pop(*a, &b) click to toggle source

Receive exactly one message from the channel.

    # File lib/em/channel.rb, line 43
43:     def pop(*a, &b)
44:       EM.schedule {
45:         name = subscribe do |*args|
46:           unsubscribe(name)
47:           EM::Callback(*a, &b).call(*args)
48:         end
49:       }
50:     end
push(*items) click to toggle source

Add items to the channel, which are pushed out to all subscribers.

    # File lib/em/channel.rb, line 36
36:     def push(*items)
37:       items = items.dup
38:       EM.schedule { @subs.values.each { |s| items.each { |i| s.call i } } }
39:     end
Also aliased as: <<
subscribe(*a, &b) click to toggle source

Takes any arguments suitable for EM::Callback() and returns a subscriber id for use when unsubscribing.

    # File lib/em/channel.rb, line 24
24:     def subscribe(*a, &b)
25:       name = gen_id
26:       EM.schedule { @subs[name] = EM::Callback(*a, &b) }
27:       name
28:     end
unsubscribe(name) click to toggle source

Removes this subscriber from the list.

    # File lib/em/channel.rb, line 31
31:     def unsubscribe(name)
32:       EM.schedule { @subs.delete name }
33:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.