Skip to content

Commit

Permalink
Add EM::Channel#pop to receive one message from a channel
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed May 12, 2009
1 parent eb70e07 commit 391b730
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/em/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def push(*items)
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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def test_channel_unsubscribe
assert_not_equal 1, s
end

def test_channel_pop
s = 0
EM.run do
c = EM::Channel.new
c.pop{ |v| s = v }
c << 1
c << 2
EM.next_tick { EM.stop }
end
assert_equal 1, s
end

def test_channel_reactor_thread_push
out = []
c = EM::Channel.new
Expand Down

0 comments on commit 391b730

Please sign in to comment.