diff --git a/README.md b/README.md index 132ae61..f6bf4e9 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ class FileStreamer end end -# then respond with a FileStreamer +# then respond with a `FileStreamer` def call(env) # ... do stuff ... @@ -44,3 +44,5 @@ use Rack::EMStream do |exception, environment| end ``` +I'm still pretty n00b to async stuff, so if you have suggestions, let me know! + diff --git a/lib/rack-emstream.rb b/lib/rack-emstream.rb index 50111cd..691e715 100644 --- a/lib/rack-emstream.rb +++ b/lib/rack-emstream.rb @@ -21,27 +21,31 @@ module Rack result[2].close if result[2].respond_to?(:close) - EM.next_tick { - env['async.callback'].call [ result[0], result[1], self ] + if env['async.callback'] + EM.next_tick { + env['async.callback'].call [ result[0], result[1], self ] - begin - result[2].each { |data| - EM.next_tick { - begin - @callback.call(data) - rescue => e - @callback.call(@block.call(e, env)) if @block - end + begin + result[2].each { |data| + EM.next_tick { + begin + @callback.call(data) + rescue => e + @callback.call(@block.call(e, env)) if @block + end + } } - } - rescue => e - @callback.call(@block.call(e, env)) if @block - end + rescue => e + @callback.call(@block.call(e, env)) if @block + end - EM.next_tick { succeed } - } + EM.next_tick { succeed } + } - throw :async + throw :async + else + result + end end end end diff --git a/lib/rack-emstream/version.rb b/lib/rack-emstream/version.rb index 30b5ed9..30e0a15 100644 --- a/lib/rack-emstream/version.rb +++ b/lib/rack-emstream/version.rb @@ -1,5 +1,5 @@ module Rack class EMStream - VERSION = "0.1.3" + VERSION = "0.1.4" end end