Class: Concurrent::Actor::Behaviour::Linking
- Defined in:
- lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb
Overview
Links the actor to other actors and sends actor’s events to them, like: ‘:terminated`, `:paused`, `:resumed`, errors, etc. Linked actor needs to handle those messages.
listener = AdHoc.spawn name: :listener do
lambda do ||
case
when Reference
if .ask!(:linked?)
<< :unlink
else
<< :link
end
else
puts "got event #{.inspect} from #{envelope.sender}"
end
end
end
an_actor = AdHoc.spawn name: :an_actor, supervise: true, behaviour_definition: Behaviour.restarting_behaviour_definition do
lambda { || raise 'failed'}
end
# link the actor
listener.ask(an_actor).wait
an_actor.ask(:fail).wait
# unlink the actor
listener.ask(an_actor).wait
an_actor.ask(:fail).wait
an_actor << :terminate!
produces only two events, other events happened after unlinking
got event #<RuntimeError: failed> from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
got event :reset from #<Concurrent::Actor::Reference /an_actor (Concurrent::Actor::Utils::AdHoc)>
Instance Attribute Summary
Attributes inherited from Abstract
Instance Method Summary collapse
-
#initialize(core, subsequent, core_options) ⇒ Linking
constructor
A new instance of Linking.
- #link(ref) ⇒ Object
- #on_envelope(envelope) ⇒ Object
- #on_event(public, event) ⇒ Object
- #unlink(ref) ⇒ Object
Methods inherited from Abstract
#broadcast, #pass, #reject_envelope
Methods included from InternalDelegations
#behaviour, #behaviour!, #children, #context, #dead_letter_routing, #log, #redirect, #terminate!, #terminated?
Methods included from PublicDelegations
#context_class, #executor, #name, #parent, #path, #reference
Methods included from TypeCheck
#Child!, #Child?, #Match!, #Match?, #Type!, #Type?
Constructor Details
#initialize(core, subsequent, core_options) ⇒ Linking
Returns a new instance of Linking.
45 46 47 48 49 |
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 45 def initialize(core, subsequent, ) super core, subsequent, @linked = Set.new @linked.add Actor.current if [:link] != false end |
Instance Method Details
#link(ref) ⇒ Object
66 67 68 69 |
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 66 def link(ref) @linked.add(ref) true end |
#on_envelope(envelope) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 51 def on_envelope(envelope) case envelope. when :link link envelope.sender when :unlink unlink envelope.sender when :linked? @linked.include? envelope.sender when :linked @linked.to_a else pass envelope end end |
#on_event(public, event) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 76 def on_event(public, event) event_name, _ = event @linked.each { |a| a << event } if public @linked.clear if event_name == :terminated super public, event end |
#unlink(ref) ⇒ Object
71 72 73 74 |
# File 'lib/concurrent-ruby-edge/concurrent/actor/behaviour/linking.rb', line 71 def unlink(ref) @linked.delete(ref) true end |