Module: Is::Async

Included in:
Core::Async::Reactor
Defined in:
lib/is/async.rb

Overview

public

Makes Ruby objects async-aware.

Instance Method Summary collapse

Instance Method Details

#asyncObject

public

Call asynchronous behavior in a proper async context.



14
15
16
17
18
# File 'lib/is/async.rb', line 14

def async
  ::Async::Reactor.run do
    yield
  end
end

#awaitObject

public

Call asynchronous behavior synchronously in a proper async context.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/is/async.rb', line 22

def await
  if (task = ::Async::Task.current?)
    reference = task.async {
      yield
    }

    wait_all(reference)
  else
    ::Async::Reactor.run {
      yield
    }.wait
  end
end

#deferObject

public

Yields control to allow other fibers to execute.



62
63
64
65
66
# File 'lib/is/async.rb', line 62

def defer
  if (task = ::Async::Task.current?)
    task.yield
  end
end

#sleep(seconds) ⇒ Object

public

Sleeps for seconds in a proper async context.



38
39
40
41
42
# File 'lib/is/async.rb', line 38

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.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/is/async.rb', line 48

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