Class: DiscordRDA::AsyncRuntime
- Inherits:
-
Object
- Object
- DiscordRDA::AsyncRuntime
- Defined in:
- lib/discord_rda/core/async_runtime.rb
Overview
Wrapper around Ruby’s Fiber scheduler for async operations. Provides a simple interface for running concurrent tasks.
Class Method Summary collapse
-
.await_all(*tasks) ⇒ Array
Run multiple tasks concurrently and wait for all.
-
.await_any(*tasks) ⇒ Object
Run tasks concurrently and return first result.
-
.run { ... } ⇒ void
Run a block within the async runtime.
Instance Method Summary collapse
-
#after(delay) { ... } ⇒ Timers::Timer
Create a timer that fires after a delay.
-
#async { ... } ⇒ Async::Task
Schedule a task to run asynchronously.
-
#every(interval) { ... } ⇒ void
Create a periodic timer.
-
#initialize ⇒ AsyncRuntime
constructor
Initialize a new async runtime.
-
#run ⇒ void
Run the event loop until all tasks complete This is a blocking call.
-
#run_forever ⇒ void
Run the event loop indefinitely Used for long-running applications like bots.
-
#stop ⇒ void
Stop all running tasks.
Constructor Details
#initialize ⇒ AsyncRuntime
Initialize a new async runtime
17 18 19 |
# File 'lib/discord_rda/core/async_runtime.rb', line 17 def initialize @tasks = [] end |
Class Method Details
.await_all(*tasks) ⇒ Array
Run multiple tasks concurrently and wait for all
88 89 90 91 92 93 94 |
# File 'lib/discord_rda/core/async_runtime.rb', line 88 def await_all(*tasks) Async do tasks.map do |task| Async { task.call }.wait end end.wait end |
.await_any(*tasks) ⇒ Object
Run tasks concurrently and return first result
99 100 101 102 103 104 105 |
# File 'lib/discord_rda/core/async_runtime.rb', line 99 def await_any(*tasks) Async do |parent_task| tasks.map do |task| parent_task.async { task.call } end.first.wait end.wait end |
.run { ... } ⇒ void
This method returns an undefined value.
Run a block within the async runtime
81 82 83 |
# File 'lib/discord_rda/core/async_runtime.rb', line 81 def run(&block) Async(&block) end |
Instance Method Details
#after(delay) { ... } ⇒ Timers::Timer
Create a timer that fires after a delay
57 58 59 60 61 62 |
# File 'lib/discord_rda/core/async_runtime.rb', line 57 def after(delay, &block) Async::Reactor.run do sleep(delay) block.call end end |
#async { ... } ⇒ Async::Task
Schedule a task to run asynchronously
24 25 26 27 28 |
# File 'lib/discord_rda/core/async_runtime.rb', line 24 def async(&block) task = Async(&block) @tasks << task task end |
#every(interval) { ... } ⇒ void
This method returns an undefined value.
Create a periodic timer
68 69 70 71 72 73 74 75 |
# File 'lib/discord_rda/core/async_runtime.rb', line 68 def every(interval, &block) Async do loop do block.call sleep(interval) end end end |
#run ⇒ void
This method returns an undefined value.
Run the event loop until all tasks complete This is a blocking call
33 34 35 |
# File 'lib/discord_rda/core/async_runtime.rb', line 33 def run # Handled by Async reactor automatically end |
#run_forever ⇒ void
This method returns an undefined value.
Run the event loop indefinitely Used for long-running applications like bots
40 41 42 43 44 |
# File 'lib/discord_rda/core/async_runtime.rb', line 40 def run_forever Async::Reactor.run do |task| task.yield end end |
#stop ⇒ void
This method returns an undefined value.
Stop all running tasks
48 49 50 51 |
# File 'lib/discord_rda/core/async_runtime.rb', line 48 def stop @tasks.each(&:stop) @tasks.clear end |