thin_async 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6621dc207fddf2a5be133698ac007650bb408cac
4
+ data.tar.gz: 82d1f517ecee486f69249c8d6bdf3efdcbbb91ee
5
+ SHA512:
6
+ metadata.gz: 6142c8a43ac121ff3d367a6f332226b9e6ae3dd170198b6cb36d7645f6f60bc5f04ac72702e1ff4346398abd684ba7a1da6402c90ef770a28ac8de1fc6290786
7
+ data.tar.gz: 4de77c64062848014713de59beae69c23bd36d81be3ff3166b2d8e27c006315b099ca45200c7703b0f7068c9752f046f0e904bcb78636374150abc179ad57e2d
data/README.md CHANGED
@@ -11,20 +11,19 @@ will prevent the EventMachine event loop from running and block all other reques
11
11
  ## Usage
12
12
  Inside your Rack app #call(env):
13
13
 
14
- response = Thin::AsyncResponse.new(env)
15
- response.status = 201
16
- response.headers["X-Muffin-Mode"] = "ACTIVATED!"
17
-
18
- response << "this is ... "
19
-
20
- EM.add_timer(1) do
21
- # This will be sent to the client 1 sec later without blocking other requests.
22
- response << "async!"
23
- response.done
14
+ Thin::AsyncResponse.perform(env) do |response|
15
+ response.status = 201
16
+ response.headers["X-Muffin-Mode"] = "ACTIVATED!"
17
+
18
+ response << "this is ... "
19
+
20
+ EM.add_timer(1) do
21
+ # This will be sent to the client 1 sec later without blocking other requests.
22
+ response << "async!"
23
+ response.done
24
+ end
24
25
  end
25
-
26
- response.finish
27
26
 
28
27
  See example/ dir for more.
29
28
 
30
- (c) macournoyer
29
+ (c) macournoyer
@@ -17,7 +17,7 @@ module Thin
17
17
  @body_callback = blk
18
18
  schedule_dequeue
19
19
  end
20
-
20
+
21
21
  private
22
22
  def schedule_dequeue
23
23
  return unless @body_callback
@@ -31,51 +31,64 @@ module Thin
31
31
  end
32
32
  end
33
33
  end
34
-
34
+
35
35
  # Response whos body is sent asynchronously.
36
36
  class AsyncResponse
37
37
  include Rack::Response::Helpers
38
-
38
+
39
39
  Marker = [-1, {}, []].freeze
40
-
40
+
41
41
  attr_reader :headers, :callback
42
42
  attr_accessor :status
43
-
43
+
44
+ # Creates a instance and yields it to the block given
45
+ # returns the async marker
46
+ def self.perform(*args, &block)
47
+ new(*args, &block).finish
48
+ end
49
+
44
50
  def initialize(env, status=200, headers={})
45
51
  @callback = env['async.callback']
46
52
  @body = DeferrableBody.new
47
53
  @status = status
48
54
  @headers = headers
49
55
  @headers_sent = false
50
-
56
+ @done = false
57
+
51
58
  if block_given?
52
59
  yield self
53
- finish
54
60
  end
55
61
  end
56
-
57
- def send_headers(response=nil)
62
+
63
+ def send_headers
58
64
  return if @headers_sent
59
- @callback.call response || [@status, @headers, @body]
65
+ @callback.call [@status, @headers, @body]
60
66
  @headers_sent = true
61
67
  end
62
-
68
+
63
69
  def write(body)
64
70
  send_headers
65
71
  @body.call(body.respond_to?(:each) ? body : [body])
66
72
  end
67
73
  alias :<< :write
68
-
74
+
69
75
  # Tell Thin the response is complete and the connection can be closed.
70
- def done(response=nil)
71
- send_headers(response)
76
+ def done
77
+ return if done?
78
+ send_headers
72
79
  EM.next_tick { @body.succeed }
80
+ @done = true
73
81
  end
74
-
82
+
83
+ # Tells if the response has already been completed
84
+ def done?
85
+ @done
86
+ end
87
+
75
88
  # Tell Thin the response is gonna be sent asynchronously.
76
89
  # The status code of -1 is the magic trick here.
77
90
  def finish
78
91
  Marker
79
92
  end
80
93
  end
81
- end
94
+ end
Binary file
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "thin_async"
3
- s.version = "0.1.1"
3
+ s.version = "0.2.0"
4
4
  s.summary = "A nice wrapper to send response body asynchronously with Thin"
5
5
 
6
6
  s.author = "Marc-Andre Cournoyer"
metadata CHANGED
@@ -1,68 +1,62 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: thin_async
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Marc-Andre Cournoyer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2010-03-09 00:00:00 -05:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
16
14
  name: thin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.1
17
20
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
21
24
  - - ">="
22
- - !ruby/object:Gem::Version
25
+ - !ruby/object:Gem::Version
23
26
  version: 1.2.1
24
- version:
25
27
  description:
26
28
  email: macournoyer@gmail.com
27
29
  executables: []
28
-
29
30
  extensions: []
30
-
31
31
  extra_rdoc_files: []
32
-
33
- files:
32
+ files:
33
+ - README.md
34
34
  - example/headers.ru
35
35
  - example/simple.ru
36
36
  - lib/thin/async.rb
37
- - README.md
37
+ - thin_async-0.1.1.gem
38
38
  - thin_async.gemspec
39
- has_rdoc: true
40
39
  homepage: http://github.com/macournoyer/thin_async
41
40
  licenses: []
42
-
41
+ metadata: {}
43
42
  post_install_message:
44
43
  rdoc_options: []
45
-
46
- require_paths:
44
+ require_paths:
47
45
  - lib
48
- required_ruby_version: !ruby/object:Gem::Requirement
49
- requirements:
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
50
48
  - - ">="
51
- - !ruby/object:Gem::Version
52
- version: "0"
53
- version:
54
- required_rubygems_version: !ruby/object:Gem::Requirement
55
- requirements:
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
56
53
  - - ">="
57
- - !ruby/object:Gem::Version
58
- version: "0"
59
- version:
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
60
56
  requirements: []
61
-
62
57
  rubyforge_project:
63
- rubygems_version: 1.3.5
58
+ rubygems_version: 2.2.2
64
59
  signing_key:
65
- specification_version: 3
60
+ specification_version: 4
66
61
  summary: A nice wrapper to send response body asynchronously with Thin
67
62
  test_files: []
68
-