async-await 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +1 -2
- data/Gemfile +13 -4
- data/README.md +26 -3
- data/async-await.gemspec +1 -1
- data/examples/chickens.rb +37 -0
- data/lib/async/await.rb +20 -5
- data/lib/async/await/methods.rb +39 -0
- data/lib/async/await/version.rb +1 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 398571ced4108922b98d708046659830492d51ba7846f9ce5ae91166986fdfe7
|
4
|
+
data.tar.gz: '09bd9a8c418d7fe0414c278df43eb67dd801048d2d9fa6d32d5df50af78724bd'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9682a9abf5b336acd4b201a020ec4247701fb90ed505ffdad3cf315334719fe261cdaf2bb7df1d98b6a112c39aa30582058dc2c7a0ea5678f20eb66256d49b84
|
7
|
+
data.tar.gz: 55f7314d172c4ebe1eaea3afc142628cd402ddcd20e7b210bc733f38830d169c9e4da518327b1cffe60dbda109734519d56f9f6aee9d7316ac46fedac4f7e3d9
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -1,6 +1,15 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in async-await.gemspec
|
6
3
|
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem 'pry'
|
7
|
+
end
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'benchmark-ips'
|
11
|
+
gem 'ruby-prof', platforms: :mri
|
12
|
+
|
13
|
+
gem 'simplecov'
|
14
|
+
gem 'coveralls', require: false
|
15
|
+
end
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Async::Await
|
2
2
|
|
3
|
-
Implements the async/await pattern for [async].
|
3
|
+
Implements the async/await pattern for Ruby using [async].
|
4
4
|
|
5
5
|
[![Build Status](https://secure.travis-ci.org/socketry/async-await.svg)](http://travis-ci.org/socketry/async-await)
|
6
6
|
[![Code Climate](https://codeclimate.com/github/socketry/async-await.svg)](https://codeclimate.com/github/socketry/async-await)
|
@@ -31,9 +31,32 @@ In any asynchronous context (e.g. a reactor), simply use the `await` function li
|
|
31
31
|
```ruby
|
32
32
|
require 'async/await'
|
33
33
|
|
34
|
-
|
35
|
-
|
34
|
+
|
35
|
+
require_relative '../lib/async/await'
|
36
|
+
|
37
|
+
class Coop
|
38
|
+
include Async::Await
|
39
|
+
|
40
|
+
async def count_chickens(area_name)
|
41
|
+
3.times do |i|
|
42
|
+
sleep rand
|
43
|
+
|
44
|
+
puts "Found a chicken in the #{area_name}!"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
async def count_all_chickens
|
49
|
+
# These methods all run at the same time.
|
50
|
+
count_chickens("garden")
|
51
|
+
count_chickens("house")
|
52
|
+
|
53
|
+
# We wait for the result
|
54
|
+
count_chickens("tree").wait
|
55
|
+
end
|
36
56
|
end
|
57
|
+
|
58
|
+
coop = Coop.new
|
59
|
+
coop.count_all_chickens
|
37
60
|
```
|
38
61
|
|
39
62
|
## Contributing
|
data/async-await.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
|
17
17
|
spec.require_paths = ["lib"]
|
18
18
|
|
19
|
-
spec.add_dependency "async", "~> 1.
|
19
|
+
spec.add_dependency "async", "~> 1.3"
|
20
20
|
spec.add_development_dependency "async-rspec", "~> 1.1"
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.15"
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
require_relative '../lib/async/await'
|
3
|
+
|
4
|
+
class Coop
|
5
|
+
include Async::Await
|
6
|
+
|
7
|
+
async def count_chickens(area_name)
|
8
|
+
3.times do |i|
|
9
|
+
sleep rand
|
10
|
+
|
11
|
+
puts "Found a chicken in the #{area_name}!"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
async def find_chicken(areas)
|
16
|
+
puts "Searching for chicken..."
|
17
|
+
|
18
|
+
sleep rand * 5
|
19
|
+
|
20
|
+
return areas.sample
|
21
|
+
end
|
22
|
+
|
23
|
+
async def count_all_chickens
|
24
|
+
# These methods all run at the same time.
|
25
|
+
count_chickens("garden")
|
26
|
+
count_chickens("house")
|
27
|
+
count_chickens("tree")
|
28
|
+
|
29
|
+
# Wait for all previous async work to complete...
|
30
|
+
barrier!
|
31
|
+
|
32
|
+
puts "There was a chicken in the #{find_chicken(["garden", "house", "tree"]).wait}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
coop = Coop.new
|
37
|
+
coop.count_all_chickens
|
data/lib/async/await.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright,
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
2
|
#
|
3
3
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
4
|
# of this software and associated documentation files (the "Software"), to deal
|
@@ -18,11 +18,26 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
-
|
21
|
+
require_relative 'await/version'
|
22
|
+
require_relative 'await/methods'
|
22
23
|
|
23
24
|
module Async
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
module Await
|
26
|
+
def self.included(klass)
|
27
|
+
klass.include(Methods)
|
28
|
+
klass.extend(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def async(name)
|
32
|
+
original_method = instance_method(name)
|
33
|
+
|
34
|
+
remove_method(name)
|
35
|
+
|
36
|
+
define_method(name) do |*args|
|
37
|
+
Async::Reactor.run do |task|
|
38
|
+
original_method.bind(self).call(*args)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
27
42
|
end
|
28
43
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'async/reactor'
|
22
|
+
|
23
|
+
module Async
|
24
|
+
module Await
|
25
|
+
module Methods
|
26
|
+
def sleep(*args)
|
27
|
+
Async::Task.current.sleep(*args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def await(&block)
|
31
|
+
block.call.wait
|
32
|
+
end
|
33
|
+
|
34
|
+
def barrier!
|
35
|
+
Async::Task.current.children.each(&:wait)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/async/await/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-await
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: async-rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,7 +94,9 @@ files:
|
|
94
94
|
- README.md
|
95
95
|
- Rakefile
|
96
96
|
- async-await.gemspec
|
97
|
+
- examples/chickens.rb
|
97
98
|
- lib/async/await.rb
|
99
|
+
- lib/async/await/methods.rb
|
98
100
|
- lib/async/await/version.rb
|
99
101
|
homepage: https://github.com/socketry/async-await
|
100
102
|
licenses: []
|
@@ -115,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
117
|
version: '0'
|
116
118
|
requirements: []
|
117
119
|
rubyforge_project:
|
118
|
-
rubygems_version: 2.6
|
120
|
+
rubygems_version: 2.7.6
|
119
121
|
signing_key:
|
120
122
|
specification_version: 4
|
121
123
|
summary: Implements the async/await pattern on top of async :)
|