Class: Rack::EMStream

Inherits:
Object
  • Object
show all
Includes:
EventMachine::Deferrable
Defined in:
lib/rack-emstream.rb,
lib/rack-emstream/version.rb

Constant Summary collapse

VERSION =
"0.1.4"

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ EMStream

Returns a new instance of EMStream.



7
8
9
# File 'lib/rack-emstream.rb', line 7

def initialize(app, &block)
  @app, @block = app, block
end

Instance Method Details

#_call(env) ⇒ Object



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
# File 'lib/rack-emstream.rb', line 19

def _call(env)
  result = @app.call(env)

  result[2].close if result[2].respond_to?(:close)

  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
          }
        }
      rescue => e
        @callback.call(@block.call(e, env)) if @block
      end

      EM.next_tick { succeed }
    }

    throw :async
  else
    result
  end
end

#call(env) ⇒ Object



15
16
17
# File 'lib/rack-emstream.rb', line 15

def call(env)
  dup._call(env)
end

#each(&b) ⇒ Object



11
12
13
# File 'lib/rack-emstream.rb', line 11

def each(&b)
  @callback = b
end