async_sinatra 0.1.3 → 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.rdoc +1 -1
- data/examples/basic.ru +1 -2
- data/lib/sinatra/async.rb +40 -42
- metadata +1 -1
data/README.rdoc
CHANGED
data/examples/basic.ru
CHANGED
data/lib/sinatra/async.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
require 'sinatra'
|
1
|
+
require 'sinatra/base'
|
2
2
|
|
3
3
|
module Sinatra #:nodoc:
|
4
4
|
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# Normally Sinatra::Base expects that the completion of a request is
|
8
|
-
# determined by the block exiting, and returning a value for the body.
|
5
|
+
# Normally Sinatra expects that the completion of a request is # determined
|
6
|
+
# by the block exiting, and returning a value for the body.
|
9
7
|
#
|
10
8
|
# In an async environment, we want to tell the webserver that we're not going
|
11
9
|
# to provide a response now, but some time in the future.
|
@@ -25,7 +23,7 @@ module Sinatra #:nodoc:
|
|
25
23
|
# require 'sinatra/async'
|
26
24
|
#
|
27
25
|
# class AsyncTest < Sinatra::Base
|
28
|
-
#
|
26
|
+
# register Sinatra::Async
|
29
27
|
#
|
30
28
|
# aget '/' do
|
31
29
|
# body "hello async"
|
@@ -37,50 +35,50 @@ module Sinatra #:nodoc:
|
|
37
35
|
#
|
38
36
|
# end
|
39
37
|
module Async
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
aroute('GET', path, opts, &block)
|
38
|
+
# Similar to Sinatra::Base#get, but the block will be scheduled to run
|
39
|
+
# during the next tick of the EventMachine reactor. In the meantime,
|
40
|
+
# Thin will hold onto the client connection, awaiting a call to
|
41
|
+
# Async#body with the response.
|
42
|
+
def aget(path, opts={}, &block)
|
43
|
+
conditions = @conditions.dup
|
44
|
+
aroute('GET', path, opts, &block)
|
48
45
|
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
@conditions = conditions
|
47
|
+
aroute('HEAD', path, opts, &block)
|
48
|
+
end
|
52
49
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
50
|
+
# See #aget.
|
51
|
+
def aput(path, opts={}, &bk); aroute 'PUT', path, opts, &bk; end
|
52
|
+
# See #aget.
|
53
|
+
def apost(path, opts={}, &bk); aroute 'POST', path, opts, &bk; end
|
54
|
+
# See #aget.
|
55
|
+
def adelete(path, opts={}, &bk); aroute 'DELETE', path, opts, &bk; end
|
56
|
+
# See #aget.
|
57
|
+
def ahead(path, opts={}, &bk); aroute 'HEAD', path, opts, &bk; end
|
61
58
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
59
|
+
private
|
60
|
+
def aroute(*args, &block) #:nodoc:
|
61
|
+
self.send :route, *args do |*bargs|
|
62
|
+
mc = class << self; self; end
|
63
|
+
mc.send :define_method, :__async_callback, &block
|
64
|
+
EM.next_tick { send(:__async_callback, *bargs) }
|
65
|
+
throw :async
|
70
66
|
end
|
71
67
|
end
|
72
68
|
|
73
|
-
|
74
|
-
|
69
|
+
module Helpers
|
70
|
+
# Send the given body or block as the final response to the asynchronous
|
71
|
+
# request.
|
72
|
+
def body(*args, &blk)
|
73
|
+
super
|
74
|
+
request.env['async.callback'][
|
75
|
+
[response.status, response.headers, response.body]
|
76
|
+
] if respond_to?(:__async_callback)
|
77
|
+
end
|
75
78
|
end
|
76
79
|
|
77
|
-
|
78
|
-
|
79
|
-
def body(*args, &blk)
|
80
|
-
super
|
81
|
-
request.env['async.callback'][
|
82
|
-
[response.status, response.headers, response.body]
|
83
|
-
] if respond_to?(:__async_callback)
|
80
|
+
def self.registered(app) #:nodoc:
|
81
|
+
app.helpers Helpers
|
84
82
|
end
|
85
83
|
end
|
86
84
|
end
|