async-await 0.2.0 → 0.6.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 452fef45e2da9f4b5f6a48102304e5a06ad501ca084fedba246cff271687828e
4
- data.tar.gz: 3d909df655b718a64d0a9b743e0ffe6c2dad06f9481ce22f60ec464ced45eec7
3
+ metadata.gz: 60b68ca501e24b540ab2868d13331718fedce992928ed6ac3e29ae00caa3ef82
4
+ data.tar.gz: 1e56657845f348d4c5657974a35c417176ddf5e63c595a8084e3fe0f7ff2f291
5
5
  SHA512:
6
- metadata.gz: afe87a31cf9cbc63727362839868a7575532f824bae986643bce644e0a04116f7fe44a09049f1e373d537e768105b837575bd011ec1b5dd2983a04453977df37
7
- data.tar.gz: 2a67e3c5d77f9290a9372107ed03d55d074f0a66be4af217255534ae59085100e3ecd88485f07f5a45bd4e9109f0160a7391bd56e5cdaebb47dac7ee04bcd34d
6
+ metadata.gz: 502521efb2e174c861cc324fbdb8c141e37c16ddd2c91664fd54e3b90d9e4a3bc103ef12a01b41deb4fed4d3997a2bce4820b14d9d10f3ea38f50b4b0b505810
7
+ data.tar.gz: 231a2b125fa5f63260ea6cfb83f06c7beab995e35c868b7d87932b4105d43b31491287081d431ecdee9ef9a906d4b051dcf3c79a49d90826511fef2d22561b15
@@ -0,0 +1,51 @@
1
+ # Copyright, 2019, 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
+ module Async
22
+ module Await
23
+ module Enumerable
24
+ def async_map(parent: Task.current, &block)
25
+ self.map do |*arguments|
26
+ parent.async do
27
+ yield(*arguments)
28
+ end
29
+ end.map(&:wait)
30
+ end
31
+
32
+ def async_each(parent: Task.current, &block)
33
+ self.each do |*arguments|
34
+ parent.async do
35
+ yield(*arguments)
36
+ end
37
+ end
38
+
39
+ return self
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ # ::Enumerable.include(Async::Await::Enumerable)
46
+ # https://bugs.ruby-lang.org/issues/9573
47
+ module Enumerable
48
+ Async::Await::Enumerable.instance_methods.each do |name|
49
+ self.define_method(name, Async::Await::Enumerable.instance_method(name))
50
+ end
51
+ end
@@ -29,7 +29,7 @@ module Async
29
29
  Async::Task.current
30
30
  end
31
31
 
32
- def_delegators :task, :timeout, :sleep, :async
32
+ def_delegators :task, :with_timeout, :sleep, :async
33
33
 
34
34
  def await(&block)
35
35
  block.call.wait
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Async
22
22
  module Await
23
- VERSION = "0.2.0"
23
+ VERSION = "0.6.0"
24
24
  end
25
25
  end
data/lib/async/await.rb CHANGED
@@ -21,6 +21,8 @@
21
21
  require_relative 'await/version'
22
22
  require_relative 'await/methods'
23
23
 
24
+ require 'ruby2_keywords'
25
+
24
26
  module Async
25
27
  module Await
26
28
  def self.included(klass)
@@ -28,16 +30,36 @@ module Async
28
30
  klass.extend(self)
29
31
  end
30
32
 
33
+ def sync(name)
34
+ original_method = instance_method(name)
35
+
36
+ remove_method(name)
37
+
38
+ define_method(name) do |*arguments, &block|
39
+ if task = Task.current?
40
+ original_method.bind(self).call(*arguments, &block)
41
+ else
42
+ Async::Reactor.run do
43
+ original_method.bind(self).call(*arguments, &block)
44
+ end.wait
45
+ end
46
+ end
47
+
48
+ ruby2_keywords(name)
49
+ end
50
+
31
51
  def async(name)
32
52
  original_method = instance_method(name)
33
53
 
34
54
  remove_method(name)
35
55
 
36
- define_method(name) do |*args|
56
+ define_method(name) do |*arguments, &block|
37
57
  Async::Reactor.run do |task|
38
- original_method.bind(self).call(*args)
58
+ original_method.bind(self).call(*arguments, &block)
39
59
  end
40
60
  end
61
+
62
+ ruby2_keywords(name)
41
63
  end
42
64
  end
43
65
  end
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-await
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-19 00:00:00.000000000 Z
11
+ date: 2021-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.3'
19
+ version: '0'
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.3'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: async-rspec
28
+ name: ruby2_keywords
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.1'
34
- type: :development
33
+ version: '0'
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.1'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bundler
42
+ name: async-rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.15'
47
+ version: '1.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.15'
54
+ version: '1.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rake
56
+ name: covered
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,31 +80,21 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.0'
83
- description:
83
+ description:
84
84
  email:
85
- - samuel.williams@oriontransfer.co.nz
86
85
  executables: []
87
86
  extensions: []
88
87
  extra_rdoc_files: []
89
88
  files:
90
- - ".editorconfig"
91
- - ".gitignore"
92
- - ".rspec"
93
- - ".travis.yml"
94
- - Gemfile
95
- - README.md
96
- - Rakefile
97
- - async-await.gemspec
98
- - examples/chickens.rb
99
- - examples/echo.rb
100
- - examples/sleep_sort.rb
101
89
  - lib/async/await.rb
90
+ - lib/async/await/enumerable.rb
102
91
  - lib/async/await/methods.rb
103
92
  - lib/async/await/version.rb
104
93
  homepage: https://github.com/socketry/async-await
105
- licenses: []
94
+ licenses:
95
+ - MIT
106
96
  metadata: {}
107
- post_install_message:
97
+ post_install_message:
108
98
  rdoc_options: []
109
99
  require_paths:
110
100
  - lib
@@ -119,9 +109,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
109
  - !ruby/object:Gem::Version
120
110
  version: '0'
121
111
  requirements: []
122
- rubyforge_project:
123
- rubygems_version: 2.7.6
124
- signing_key:
112
+ rubygems_version: 3.3.0
113
+ signing_key:
125
114
  specification_version: 4
126
115
  summary: Implements the async/await pattern on top of async :)
127
116
  test_files: []
data/.editorconfig DELETED
@@ -1,5 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- indent_style = tab
5
- indent_size = 4
data/.gitignore DELETED
@@ -1,12 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
-
11
- # rspec failure tracking
12
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --warnings
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,17 +0,0 @@
1
- language: ruby
2
- sudo: false
3
- dist: trusty
4
- cache: bundler
5
- rvm:
6
- - 2.2
7
- - 2.3
8
- - 2.4
9
- - 2.5
10
- - jruby-head
11
- - ruby-head
12
- - rbx-3
13
- matrix:
14
- allow_failures:
15
- - rvm: ruby-head
16
- - rvm: jruby-head
17
- - rvm: rbx-3
data/Gemfile DELETED
@@ -1,16 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- group :development do
6
- gem 'pry'
7
- gem 'async-io', '~> 1.4'
8
- end
9
-
10
- group :test do
11
- gem 'benchmark-ips'
12
- gem 'ruby-prof', platforms: :mri
13
-
14
- gem 'simplecov'
15
- gem 'coveralls', require: false
16
- end
data/README.md DELETED
@@ -1,95 +0,0 @@
1
- # Async::Await
2
-
3
- Implements the async/await pattern for Ruby using [async].
4
-
5
- [![Build Status](https://secure.travis-ci.org/socketry/async-await.svg)](http://travis-ci.org/socketry/async-await)
6
- [![Code Climate](https://codeclimate.com/github/socketry/async-await.svg)](https://codeclimate.com/github/socketry/async-await)
7
- [![Coverage Status](https://coveralls.io/repos/socketry/async-await/badge.svg)](https://coveralls.io/r/socketry/async-await)
8
-
9
- [async]: https://github.com/socketry/async
10
-
11
- ## Installation
12
-
13
- Add this line to your application's Gemfile:
14
-
15
- ```ruby
16
- gem 'async-await'
17
- ```
18
-
19
- And then execute:
20
-
21
- $ bundle
22
-
23
- Or install it yourself as:
24
-
25
- $ gem install async-await
26
-
27
- ## Usage
28
-
29
- In any asynchronous context (e.g. a reactor), simply use the `await` function like so:
30
-
31
- ```ruby
32
- require 'async/await'
33
-
34
- class Coop
35
- include Async::Await
36
-
37
- async def count_chickens(area_name)
38
- 3.times do |i|
39
- sleep rand
40
-
41
- puts "Found a chicken in the #{area_name}!"
42
- end
43
- end
44
-
45
- async def count_all_chickens
46
- # These methods all run at the same time.
47
- count_chickens("garden")
48
- count_chickens("house")
49
-
50
- # We wait for the result
51
- count_chickens("tree").wait
52
- end
53
- end
54
-
55
- coop = Coop.new
56
- coop.count_all_chickens
57
- ```
58
-
59
- ## Contributing
60
-
61
- 1. Fork it
62
- 2. Create your feature branch (`git checkout -b my-new-feature`)
63
- 3. Commit your changes (`git commit -am 'Add some feature'`)
64
- 4. Push to the branch (`git push origin my-new-feature`)
65
- 5. Create new Pull Request
66
-
67
- ## See Also
68
-
69
- - [async-io](https://github.com/socketry/async-io) — Asynchronous networking and sockets.
70
- - [async-dns](https://github.com/socketry/async-dns) — Asynchronous DNS resolver and server.
71
- - [async-rspec](https://github.com/socketry/async-rspec) — Shared contexts for running async specs.
72
-
73
- ## License
74
-
75
- Released under the MIT license.
76
-
77
- Copyright, 2017, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
78
-
79
- Permission is hereby granted, free of charge, to any person obtaining a copy
80
- of this software and associated documentation files (the "Software"), to deal
81
- in the Software without restriction, including without limitation the rights
82
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
83
- copies of the Software, and to permit persons to whom the Software is
84
- furnished to do so, subject to the following conditions:
85
-
86
- The above copyright notice and this permission notice shall be included in
87
- all copies or substantial portions of the Software.
88
-
89
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
95
- THE SOFTWARE.
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
data/async-await.gemspec DELETED
@@ -1,25 +0,0 @@
1
- # coding: utf-8
2
- require_relative "lib/async/await/version"
3
-
4
- Gem::Specification.new do |spec|
5
- spec.name = "async-await"
6
- spec.version = Async::Await::VERSION
7
- spec.authors = ["Samuel Williams"]
8
- spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
-
10
- spec.summary = "Implements the async/await pattern on top of async :)"
11
- spec.homepage = "https://github.com/socketry/async-await"
12
-
13
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
- f.match(%r{^(test|spec|features)/})
15
- end
16
-
17
- spec.require_paths = ["lib"]
18
-
19
- spec.add_dependency "async", "~> 1.3"
20
- spec.add_development_dependency "async-rspec", "~> 1.1"
21
-
22
- spec.add_development_dependency "bundler", "~> 1.15"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.0"
25
- end
data/examples/chickens.rb DELETED
@@ -1,37 +0,0 @@
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/examples/echo.rb DELETED
@@ -1,55 +0,0 @@
1
-
2
- require_relative '../lib/async/await'
3
-
4
- require 'async/io'
5
- require 'async/io/tcp_socket'
6
-
7
- require 'pry'
8
-
9
- class Echo
10
- include Async::Await
11
- include Async::IO
12
-
13
- async def handle(peer, address)
14
- data = peer.gets
15
- peer.puts("#{data} #{Time.now}")
16
- ensure
17
- peer.close
18
- end
19
-
20
- async def server
21
- puts "Binding server..."
22
- server = TCPServer.new("127.0.0.1", 9009)
23
-
24
- handle(*server.accept)
25
- ensure
26
- server.close rescue nil
27
- end
28
-
29
- async def client
30
- puts "Client connecting..."
31
- client = TCPSocket.new("127.0.0.1", 9009)
32
-
33
- client.puts("Hello World!")
34
- response = client.gets
35
-
36
- puts "Server said: #{response}"
37
- ensure
38
- client.close rescue nil
39
- end
40
-
41
- async def run
42
- puts "Creating server..."
43
- server
44
-
45
- puts "Creating client..."
46
- client
47
-
48
- puts "Run returning..."
49
- end
50
- end
51
-
52
- puts "Starting echo..."
53
- echo = Echo.new
54
- echo.run
55
- puts "Echo finished :)"
@@ -1,29 +0,0 @@
1
-
2
- require_relative '../lib/async/await'
3
-
4
- class << self
5
- include Async::Await
6
-
7
- async def sort_one(item, into)
8
- sleep(item.to_f)
9
- into << item
10
-
11
- puts "I've sorted #{item} for you."
12
- end
13
-
14
- async def sort(items)
15
- result = []
16
-
17
- items.each do |item|
18
- sort_one(item, result)
19
- end
20
-
21
- # Wait until all previous async method calls have finished executing.
22
- barrier!
23
-
24
- return result
25
- end
26
- end
27
-
28
- puts "Hold on, sorting..."
29
- puts sort([5, 2, 3, 4, 9, 2, 5, 7, 8]).result.inspect