Class: Ferrum::Interceptable::Chain
- Inherits:
-
Object
- Object
- Ferrum::Interceptable::Chain
- Defined in:
- lib/ferrum/interceptable/chain.rb,
sig/ferrum/interceptable/chain.rbs
Overview
An ordered chain of handlers for an event that has to be answered exactly once, e.g. a paused request or a dialog. Handlers run in the order they were added and the chain stops at the first one that handles the event object, so a handler that doesn't want the object just returns. An object nobody handled is passed to the default action, so the chain listens to the event from the start, handlers or not.
Defined Under Namespace
Modules: _Handleable
Instance Method Summary collapse
-
#add {|arg0| ... } ⇒ Integer
Adds a handler to the end of the chain.
-
#initialize(client, event, build:, default:) ⇒ Chain
constructor
A new instance of Chain.
-
#remove(id) ⇒ void
Removes a handler.
Constructor Details
#initialize(client, event, build:, default:) ⇒ Chain
Returns a new instance of Chain.
26 27 28 29 30 31 32 33 |
# File 'lib/ferrum/interceptable/chain.rb', line 26 def initialize(client, event, build:, default:) @event = event @build = build @default = default @handlers = Concurrent::Hash.new @sequence = Concurrent::AtomicFixnum.new client.on(event) { |params| run(params) } end |
Instance Method Details
#add {|arg0| ... } ⇒ Integer
Adds a handler to the end of the chain.
41 42 43 |
# File 'lib/ferrum/interceptable/chain.rb', line 41 def add(&block) @sequence.increment.tap { |id| @handlers[id] = block } end |
#remove(id) ⇒ void
This method returns an undefined value.
Removes a handler.
53 54 55 |
# File 'lib/ferrum/interceptable/chain.rb', line 53 def remove(id) @handlers.delete(id) end |