Module: AsyncCable::Util

Defined in:
lib/async_cable/util.rb

Class Method Summary collapse

Class Method Details

.create_task(parent = Async::Task.current?, schedule = false, &block) ⇒ Async::Task

Return created task.

Parameters:

  • parent (Async::Task, NilClass) (defaults to: Async::Task.current?)

    parent task for new one.

  • schedule (Boolean) (defaults to: false)

    run now if true, otherwise will be run at next reactor loop cycle.

Returns:

  • (Async::Task)

    return created task.



6
7
8
9
10
11
12
13
14
# File 'lib/async_cable/util.rb', line 6

def create_task(parent = Async::Task.current?, schedule = false, &block)
  task = Async::Task.new(parent, &block)
  if schedule
    Async::Task.current.reactor << task.fiber
  else
    task.run
  end
  task
end

.each_async(list, *args) ⇒ Object

Each yield will be executed within it’s own fiber.

Parameters:

  • list (Array)

    list that will be iterable.

  • args (Array)

    parent, schedule @see #create_task (optional).



19
20
21
22
23
# File 'lib/async_cable/util.rb', line 19

def each_async(list, *args)
  list.each do |item|
    create_task(*args) { yield item }
  end
end