Class: DiscordRDA::AsyncRuntime

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

runtime = AsyncRuntime.new
runtime.async { perform_io_operation }
runtime.run

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAsyncRuntime

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

Parameters:

  • tasks (Array<Proc>)

    Tasks to run

Returns:

  • (Array)

    Results from all tasks



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

Parameters:

  • tasks (Array<Proc>)

    Tasks to run

Returns:

  • (Object)

    First completed 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

Yields:

  • The block to run



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

Parameters:

  • delay (Float)

    Delay in seconds

Yields:

  • The block to execute after the delay

Returns:

  • (Timers::Timer)

    The timer object



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

Yields:

  • The block to execute asynchronously

Returns:

  • (Async::Task)

    The scheduled task



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

Parameters:

  • interval (Float)

    Interval in seconds

Yields:

  • The block to execute periodically



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

#runvoid

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_forevervoid

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

#stopvoid

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