core-async 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d07ba2a96ea37afd1f5abe0bb7a24182ef1b94e055b39cd2b4ad6cf08c420a5a
4
- data.tar.gz: 893d9b5b72309cea17f9ca5cba0c840f9bfbacfb8d56e1db2b8df34c9cfd58ff
3
+ metadata.gz: 2f2c40406be47b9385d6252ddfc534383b817249c44aeda7e3410ea12d15afde
4
+ data.tar.gz: 48ee77b1bec473726d7deb3f35481c487e946c8fd00668c039d7556164bd70fc
5
5
  SHA512:
6
- metadata.gz: 04e6ebb9c4684fa2501d9b069fdcc4368d8c9cbdfa9294e5a379b423a9a1894ab8d05854a7bd8137cf2da061fdecaac859d7cc0466e631a704244c02a514763b
7
- data.tar.gz: ed03928b876cd32a5df6f37f787400c2e4ef2db581b5e5de3c8c111734e5f53518675c35303ceb2a45e26af8f7b65cba4554ebfdd5d724e90d65ea15fdb0ef1f
6
+ metadata.gz: 21f8e4bf33f14497c2080450f6ba4db0abde814ffbb86a3b0ed64f187edfd4c042830fcccd4c9044b5ff67d31bdf25904b2430bf715c02ea0334c7152cbe597d
7
+ data.tar.gz: b41c4e064774fc736814255aa5f92dfd160967c0370c7a5b76784418793a2550e0403beb6524e37b488ea76e9db25c7f1810b8919f699b9182a3268feaa44431
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## [v0.1.0](https://github.com/metabahn/corerb/releases/tag/2021-02-11)
2
+
3
+ *released on 2021-02-11*
4
+
5
+ * `chg` [#8](https://github.com/metabahn/corerb/pull/8) Wait for all async work to complete ([bryanp](https://github.com/bryanp))
6
+ * `add` [#7](https://github.com/metabahn/corerb/pull/7) Add Core::Async::Reactor ([bryanp](https://github.com/bryanp))
7
+ * `chg` [#6](https://github.com/metabahn/corerb/pull/6) Don't yield tasks from async and await ([bryanp](https://github.com/bryanp))
8
+
1
9
  ## [v0.0.0](https://github.com/metabahn/corerb/releases/tag/2020-12-17)
2
10
 
3
11
  *released on 2020-12-17*
data/lib/core/async.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Core
4
4
  module Async
5
+ require_relative "async/reactor"
5
6
  require_relative "async/version"
6
7
  end
7
8
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../is/async"
4
+
5
+ module Core
6
+ module Async
7
+ # [public] The top-level async context. Runs until all scheduled work is complete.
8
+ #
9
+ class Reactor
10
+ include Is::Async
11
+
12
+ class << self
13
+ # [public] Create a new reactor and immediately run it.
14
+ #
15
+ def run(&block)
16
+ instance = new
17
+ instance.run(&block)
18
+ end
19
+ end
20
+
21
+ # [public] Run the reactor, yielding within the async context.
22
+ #
23
+ def run
24
+ if (task = ::Async::Task.current?)
25
+ reference = task.async { |child|
26
+ @runnable = child
27
+
28
+ yield self
29
+ }
30
+
31
+ wait_all(reference)
32
+ else
33
+ @runnable = ::Async::Reactor.new
34
+
35
+ @runnable.run {
36
+ yield self
37
+ }.wait
38
+ end
39
+ end
40
+
41
+ # [public] Stop the reactor.
42
+ #
43
+ def stop
44
+ @runnable&.stop
45
+ end
46
+ end
47
+ end
48
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Core
4
4
  module Async
5
- VERSION = "0.0.0"
5
+ VERSION = "0.1.0"
6
6
 
7
7
  def self.version
8
8
  VERSION
data/lib/is/async.rb CHANGED
@@ -12,8 +12,8 @@ module Is
12
12
  # [public] Call asynchronous behavior in a proper async context.
13
13
  #
14
14
  def async
15
- ::Async::Reactor.run do |task|
16
- yield task
15
+ ::Async::Reactor.run do
16
+ yield
17
17
  end
18
18
  end
19
19
 
@@ -21,10 +21,14 @@ module Is
21
21
  #
22
22
  def await
23
23
  if (task = ::Async::Task.current?)
24
- yield task
24
+ reference = task.async {
25
+ yield
26
+ }
27
+
28
+ wait_all(reference)
25
29
  else
26
- ::Async::Reactor.run { |task|
27
- yield task
30
+ ::Async::Reactor.run {
31
+ yield
28
32
  }.wait
29
33
  end
30
34
  end
@@ -32,7 +36,7 @@ module Is
32
36
  # [public] Sleeps for `seconds` in a proper async context.
33
37
  #
34
38
  def sleep(seconds)
35
- await do |task|
39
+ internal_await do |task|
36
40
  task.sleep(seconds)
37
41
  end
38
42
  end
@@ -42,13 +46,13 @@ module Is
42
46
  # Raises `Core::Async::Timeout` if execution exceeds `seconds`.
43
47
  #
44
48
  def timeout(seconds, &block)
45
- await do |task|
49
+ internal_await do |task|
46
50
  if seconds && seconds > 0
47
51
  task.with_timeout(seconds, Core::Async::Timeout) do
48
- yield task
52
+ yield
49
53
  end
50
54
  else
51
- yield task
55
+ yield
52
56
  end
53
57
  end
54
58
  end
@@ -60,5 +64,33 @@ module Is
60
64
  task.yield
61
65
  end
62
66
  end
67
+
68
+ private def internal_async
69
+ ::Async::Reactor.run do |task|
70
+ yield task
71
+ end
72
+ end
73
+
74
+ private def internal_await
75
+ if (task = ::Async::Task.current?)
76
+ reference = task.async {
77
+ yield task
78
+ }
79
+
80
+ wait_all(reference)
81
+ else
82
+ ::Async::Reactor.run { |task|
83
+ yield task
84
+ }.wait
85
+ end
86
+ end
87
+
88
+ private def wait_all(task)
89
+ task.children&.each do |child|
90
+ wait_all(child)
91
+ end
92
+
93
+ task.wait
94
+ end
63
95
  end
64
96
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: core-async
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Powell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-17 00:00:00.000000000 Z
11
+ date: 2021-02-12 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.27'
19
+ version: '1.28'
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.27'
26
+ version: '1.28'
27
27
  description: Makes Ruby objects async-aware.
28
28
  email: bryan@metabahn.com
29
29
  executables: []
@@ -33,6 +33,7 @@ files:
33
33
  - CHANGELOG.md
34
34
  - LICENSE
35
35
  - lib/core/async.rb
36
+ - lib/core/async/reactor.rb
36
37
  - lib/core/async/timeout.rb
37
38
  - lib/core/async/version.rb
38
39
  - lib/is/async.rb
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
56
  - !ruby/object:Gem::Version
56
57
  version: '0'
57
58
  requirements: []
58
- rubygems_version: 3.1.4
59
+ rubygems_version: 3.2.4
59
60
  signing_key:
60
61
  specification_version: 4
61
62
  summary: Makes Ruby objects async-aware.