Module: Is::Async
- Included in:
- Core::Async::Collection, Core::Async::Collection, Core::Async::Enumerator, Core::Async::Reactor
- Defined in:
- lib/is/async.rb
Overview
- public
-
Makes Ruby objects async-aware.
Instance Method Summary collapse
-
#async ⇒ Object
- public
-
Call behavior asychronously, returning a future.
-
#await ⇒ Object
- public
-
Call behavior synchronously but within an async context, waiting on the result.
-
#aware ⇒ Object
- public
-
Call behavior within an async context without additional nesting.
-
#defer ⇒ Object
- public
-
Yields control to allow other fibers to execute.
-
#resolve(value) ⇒ Object
- public
-
Resolves a potential future to a final result.
-
#sleep(seconds) ⇒ Object
- public
-
Sleeps for
secondsin a proper async context.
-
#timeout(seconds, &block) ⇒ Object
- public
-
Call asynchonous behavior in a proper async context, wrapped in a timeout.
Instance Method Details
#async ⇒ Object
- public
-
Call behavior asychronously, returning a future.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/is/async.rb', line 14 def async if block_given? task = ::Async::Reactor.run { |current| begin yield ensure current.yield end } Core::Async::Future.new(task) else Core::Async::Wrapper.new(self) end end |
#await ⇒ Object
- public
-
Call behavior synchronously but within an async context, waiting on the result.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/is/async.rb', line 32 def await if (task = ::Async::Task.current?) reference = task.async { |current| begin yield ensure current.yield end } wait_all(reference) else ::Async::Reactor.run { |task| async { yield }.result }.wait end end |
#aware ⇒ Object
- public
-
Call behavior within an async context without additional nesting.
54 55 56 57 58 59 60 61 62 |
# File 'lib/is/async.rb', line 54 def aware if ::Async::Task.current? yield else await do yield end end end |
#defer ⇒ Object
- public
-
Yields control to allow other fibers to execute.
90 91 92 93 94 |
# File 'lib/is/async.rb', line 90 def defer if (task = ::Async::Task.current?) task.yield end end |
#resolve(value) ⇒ Object
- public
-
Resolves a potential future to a final result.
98 99 100 101 102 103 104 105 |
# File 'lib/is/async.rb', line 98 def resolve(value) case value when Core::Async::Future value.result else value end end |
#sleep(seconds) ⇒ Object
- public
-
Sleeps for
secondsin a proper async context.
66 67 68 69 70 |
# File 'lib/is/async.rb', line 66 def sleep(seconds) internal_await do |task| task.sleep(seconds) end end |
#timeout(seconds, &block) ⇒ Object
- public
-
Call asynchonous behavior in a proper async context, wrapped in a timeout.
Raises Core::Async::Timeout if execution exceeds seconds.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/is/async.rb', line 76 def timeout(seconds, &block) internal_await do |task| if seconds && seconds > 0 task.with_timeout(seconds, Core::Async::Timeout) do yield end else yield end end end |