Class: Ruff::Standard::Async::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/ruff/standard/async.rb

Constant Summary collapse

Waiting =

are ADT-like classes which have only getter method. These are used internally.

type 'a promise = | Waiting of ('a, unit) continuation list | Done of 'a

Util::ADT.create
Done =
Util::ADT.create

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstance

makes a new instance.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruff/standard/async.rb', line 55

def initialize
  # delegates effect instances.
  @eff = OpenStruct.new(
    async: Ruff.instance,
    yield: Ruff.instance,
    await: Ruff.instance
  )

  # is a proc queue.
  @q = Util::FnStack.new
end

Instance Attribute Details

#effObject (readonly)

You can reimplement the handler using these effect instances with accessing Async.async , Async.yield , and Async.await .



104
105
106
# File 'lib/ruff/standard/async.rb', line 104

def eff
  @eff
end

Instance Method Details

#async(th) ⇒ Promise<A>

is a smart method to invoke the effect operation Async.async .

Parameters:

  • th (Proc<(), A>)

    is a thunk asynchronously computed.

Returns:

  • (Promise<A>)

    with with , returns Promise<A> with running concurrently .



72
73
74
# File 'lib/ruff/standard/async.rb', line 72

def async(th)
  @eff.async.perform th
end

#await(p) ⇒ A

is a smart method to invoke the effect operation Async.await .

Parameters:

  • p (Promise<A>)

    is a promise to run.

Returns:

  • (A)

    with with returns the result of promise computation.



88
89
90
# File 'lib/ruff/standard/async.rb', line 88

def await(p)
  @eff.await.perform p
end

#with(&th) ⇒ ()!{e}

Returns unit but still has the possibility to invoke effects e .

Parameters:

  • th (Proc<(), _A!{Async.async, Async.yield, Async.await, e}>)

    is a thunk returning _A with te possibility to invoke effects, including Async.async , Async.yield and Async.await .

Returns:

  • (()!{e})

    returns unit but still has the possibility to invoke effects e .



98
99
100
# File 'lib/ruff/standard/async.rb', line 98

define_method :with do |&th|
  fork(Util::Ref.new(Waiting.new([])), th)
end

#yield()

is a smart method to invoke the effect operation Async.yield .

Returns:

  • (())

    with with , yields the control to another task.



79
80
81
# File 'lib/ruff/standard/async.rb', line 79

def yield
  @eff.yield.perform
end