/usr/lib/ruby/vendor_ruby/em/channel.rb is in ruby-eventmachine 0.12.10-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | module EventMachine
# 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.
class Channel
# Create a new channel
def initialize
@subs = {}
@uid = 0
end
# Takes any arguments suitable for EM::Callback() and returns a subscriber
# id for use when unsubscribing.
def subscribe(*a, &b)
name = gen_id
EM.schedule { @subs[name] = EM::Callback(*a, &b) }
name
end
# Removes this subscriber from the list.
def unsubscribe(name)
EM.schedule { @subs.delete name }
end
# Add items to the channel, which are pushed out to all subscribers.
def push(*items)
items = items.dup
EM.schedule { @subs.values.each { |s| items.each { |i| s.call i } } }
end
alias << push
# Receive exactly one message from the channel.
def pop(*a, &b)
EM.schedule {
name = subscribe do |*args|
unsubscribe(name)
EM::Callback(*a, &b).call(*args)
end
}
end
private
def gen_id # :nodoc:
@uid += 1
end
end
end
|