Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add support for more than one callback

  • Loading branch information...
commit 91cafec437ed717923cc7bdd8f9307ecb3cfea09 1 parent 0ab5294
@rkh rkh authored
Showing with 16 additions and 3 deletions.
  1. +3 −0  CHANGES
  2. +3 −3 lib/sinatra/base.rb
  3. +10 −0 test/streaming_test.rb
View
3  CHANGES
@@ -1,5 +1,8 @@
= 1.3.1 / Not Yet Released
+ * Support adding more than one callback to the stream object. (Konstantin
+ Haase)
+
= 1.3.0 / 2011-09-30
* Added `stream` helper method for easily creating streaming APIs, Server
View
6 lib/sinatra/base.rb
@@ -247,11 +247,11 @@ def self.schedule(*) yield end
def self.defer(*) yield end
def initialize(scheduler = self.class, keep_open = false, &back)
- @back, @scheduler, @callback, @keep_open = back.to_proc, scheduler, nil, keep_open
+ @back, @scheduler, @callbacks, @keep_open = back.to_proc, scheduler, [], keep_open
end
def close
- @scheduler.schedule { @callback.call if @callback }
+ @scheduler.schedule { @callbacks.each { |c| c.call }}
end
def each(&front)
@@ -272,7 +272,7 @@ def <<(data)
end
def callback(&block)
- @callback = block
+ @callbacks << block
end
alias errback callback
View
10 test/streaming_test.rb
@@ -56,6 +56,16 @@ class StreamingTest < Test::Unit::TestCase
assert_equal 0, final
end
+ it 'allows adding more than one callback' do
+ a = b = false
+ stream = Stream.new { }
+ stream.callback { a = true }
+ stream.callback { b = true }
+ stream.each { |str| }
+ assert a, 'should trigger first callback'
+ assert b, 'should trigger second callback'
+ end
+
class MockScheduler
def initialize(*) @schedule, @defer = [], [] end
def schedule(&block) @schedule << block end
Please sign in to comment.
Something went wrong with that request. Please try again.