Module: GenServer
- Defined in:
- lib/gen_server.rb,
lib/gen_server/version.rb,
lib/gen_server/registry.rb,
lib/gen_server/registry/info.rb
Defined Under Namespace
Classes: Registry
Constant Summary collapse
- VERSION =
'0.0.1'
Class Method Summary collapse
- .call(pid, message) ⇒ Object
- .cast(pid, message) ⇒ Object
- .receive(state) ⇒ Object
- .start_link(klass, state = []) ⇒ Object
Instance Method Summary collapse
Class Method Details
.call(pid, message) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/gen_server.rb', line 48 def call(pid, ) Registry.actor(pid).send [:call, Ractor.current, , Registry.klass(pid)] Ractor.receive => [:ok, response] response end |
.cast(pid, message) ⇒ Object
42 43 44 45 46 |
# File 'lib/gen_server.rb', line 42 def cast(pid, ) Registry.actor(pid).send [:cast, , Registry.klass(pid)] :ok end |
.receive(state) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gen_server.rb', line 30 def receive(state) case Ractor.receive in [:cast, , klass] klass.allocate.handle_cast(, state) => [:noreply, new_state] receive(new_state) in [:call, sender, , klass] klass.allocate.handle_call(, sender, state) => [:reply, reply, new_state] sender.send [:ok, reply] receive(new_state) end end |
.start_link(klass, state = []) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gen_server.rb', line 18 def start_link(klass, state = []) pid = SecureRandom.uuid actor = Ractor.new(state, name: pid) do |state| GenServer.receive(state) end Registry[pid] = Registry::Info.new(actor:, klass:) [:ok, pid] end |
Instance Method Details
#handle_call ⇒ Object
14 |
# File 'lib/gen_server.rb', line 14 def handle_call(...) = [:reply, nil, nil] |
#handle_cast ⇒ Object
15 |
# File 'lib/gen_server.rb', line 15 def handle_cast(...) = [:noreply, nil] |
#init ⇒ Object
13 |
# File 'lib/gen_server.rb', line 13 def init(...) = nil |
#initialize ⇒ Object
9 10 11 |
# File 'lib/gen_server.rb', line 9 def initialize(...) init(...) end |