Class: ReactorActorM::RactorActor

Inherits:
Object
  • Object
show all
Defined in:
lib/ractor_actor.rb

Overview

A shared state actor driven by Ruby Ractors.

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ RactorActor

Returns a new instance of RactorActor.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ractor_actor.rb', line 28

def initialize(data)
  @ractor = Ractor.new do
    state = nil
    loop do
      incoming = Ractor.recv
      case incoming.method
      when :get
        incoming.backchannel.send state
      when :set
        state = incoming.obj
        incoming.backchannel.send true
      when :kill
        break
      end
    end
  end

  @ractor.send RactorMessage.new :set, data
  _ = @value
end

Instance Method Details

#get_value_with_timeout(timeout) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/ractor_actor.rb', line 61

def get_value_with_timeout(timeout)
  m = RactorMessage.new :get
  @ractor.send m
  val = nil
  Timeout.timeout(timeout) do
    val = m.backchannel.take
  end
  val
end

#killObject



71
72
73
74
75
# File 'lib/ractor_actor.rb', line 71

def kill
  m = RactorMessage.new :kill
  @ractor.send m
  _ = m.backchannel.take
end

#valueObject



49
50
51
52
53
# File 'lib/ractor_actor.rb', line 49

def value
  m = RactorMessage.new :get
  @ractor.send m
  m.backchannel.take
end

#value=(new_value) ⇒ Object



55
56
57
58
59
# File 'lib/ractor_actor.rb', line 55

def value=(new_value)
  m = RactorMessage.new :set, new_value
  @ractor.send m
  _ = m.backchannel.take
end