rack-emstream 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -18,7 +18,7 @@ class FileStreamer
18
18
  end
19
19
  end
20
20
 
21
- # then respond with a FileStreamer
21
+ # then respond with a `FileStreamer`
22
22
 
23
23
  def call(env)
24
24
  # ... do stuff ...
@@ -27,16 +27,22 @@ def call(env)
27
27
  end
28
28
  ```
29
29
 
30
- Nothing to configure, just drop it in to your Rack middleware stack and
31
- use Thin as your server:
30
+ Only one thing to configure, the error handler if something explodes during the deferred
31
+ callback (since you no longer have your Rack handlers at that point):
32
32
 
33
33
  ``` ruby
34
34
  # for Rails:
35
35
 
36
- config.middleware.insert_before(::Rack::Lock, ::Rack::EMStream)
36
+ config.middleware.insert_before(::Rack::Lock, ::Rack::EMStream) do |exception, environment|
37
+ # do something when there's a deferred error
38
+ end
37
39
 
38
40
  # for Rack::Builder and derivatives:
39
41
 
40
- use Rack::EMStream
42
+ use Rack::EMStream do |exception, environment|
43
+ # do something when there's a deferred error
44
+ end
41
45
  ```
42
46
 
47
+ I'm still pretty n00b to async stuff, so if you have suggestions, let me know!
48
+
@@ -4,8 +4,8 @@ module Rack
4
4
  class EMStream
5
5
  include EventMachine::Deferrable
6
6
 
7
- def initialize(app)
8
- @app = app
7
+ def initialize(app, &block)
8
+ @app, @block = app, block
9
9
  end
10
10
 
11
11
  def each(&b)
@@ -21,14 +21,31 @@ module Rack
21
21
 
22
22
  result[2].close if result[2].respond_to?(:close)
23
23
 
24
- EM.next_tick {
25
- env['async.callback'].call [ result[0], result[1], self ]
26
-
27
- result[2].each { |data| EM.next_tick { @callback.call(data) } }
28
- EM.next_tick { succeed }
29
- }
30
-
31
- throw :async
24
+ if env['async.callback']
25
+ EM.next_tick {
26
+ env['async.callback'].call [ result[0], result[1], self ]
27
+
28
+ begin
29
+ result[2].each { |data|
30
+ EM.next_tick {
31
+ begin
32
+ @callback.call(data)
33
+ rescue => e
34
+ @callback.call(@block.call(e, env)) if @block
35
+ end
36
+ }
37
+ }
38
+ rescue => e
39
+ @callback.call(@block.call(e, env)) if @block
40
+ end
41
+
42
+ EM.next_tick { succeed }
43
+ }
44
+
45
+ throw :async
46
+ else
47
+ result
48
+ end
32
49
  end
33
50
  end
34
51
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class EMStream
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-emstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-29 00:00:00.000000000 Z
12
+ date: 2012-09-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -69,3 +69,4 @@ signing_key:
69
69
  specification_version: 3
70
70
  summary: Super-simple Rack streaming with Thin
71
71
  test_files: []
72
+ has_rdoc: