Class: EventMachine::Channel
- Inherits:
-
Object
- Object
- EventMachine::Channel
- Defined in:
- lib/em/channel.rb
Overview
Provides a simple thread-safe way to transfer data between (typically) long running tasks in defer and event loop thread.
Instance Method Summary (collapse)
-
- (Channel) initialize
constructor
A new instance of Channel.
-
- (Object) pop(*a, &b)
Fetches one message from the channel.
-
- (Object) push(*items)
(also: #<<)
Add items to the channel, which are pushed out to all subscribers.
-
- (Integer) subscribe(*a, &b)
Takes any arguments suitable for EM::Callback() and returns a subscriber id for use when unsubscribing.
-
- (Object) unsubscribe(name)
Removes subscriber from the list.
Constructor Details
- (Channel) initialize
A new instance of Channel
15 16 17 18 |
# File 'lib/em/channel.rb', line 15 def initialize @subs = {} @uid = 0 end |
Instance Method Details
- (Object) pop(*a, &b)
Fetches one message from the channel.
48 49 50 51 52 53 54 55 |
# File 'lib/em/channel.rb', line 48 def pop(*a, &b) EM.schedule { name = subscribe do |*args| unsubscribe(name) EM::Callback(*a, &b).call(*args) end } end |
- (Object) push(*items) Also known as: <<
Add items to the channel, which are pushed out to all subscribers.
41 42 43 44 |
# File 'lib/em/channel.rb', line 41 def push(*items) items = items.dup EM.schedule { items.each { |i| @subs.values.each { |s| s.call i } } } end |
- (Integer) subscribe(*a, &b)
Takes any arguments suitable for EM::Callback() and returns a subscriber id for use when unsubscribing.
25 26 27 28 29 30 |
# File 'lib/em/channel.rb', line 25 def subscribe(*a, &b) name = gen_id EM.schedule { @subs[name] = EM::Callback(*a, &b) } name end |
- (Object) unsubscribe(name)
Removes subscriber from the list.
36 37 38 |
# File 'lib/em/channel.rb', line 36 def unsubscribe(name) EM.schedule { @subs.delete name } end |