Class: Ferrum::Interceptable::Chain

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(client, event, build:, default:) ⇒ Chain

Returns a new instance of Chain.

Parameters:

  • client (#on) —

    Where the event comes from.

  • event (String) —

    The CDP event name.

  • build (#call) —

    Turns the event params into the object handlers receive. The object has to respond to handled?.

  • default (#call) —

    Called with the object when no handler handled it.

  • build: (^(Hash[String, untyped]) -> T)
  • default: (^(T) -> untyped)


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.

Yields:

Yield Parameters:

  • arg0 (T)

Yield Returns:

  • (Object)

Returns:

  • (Integer) —

    The id to pass to #remove.



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.

Parameters:

  • id (Integer) —

    The id returned by #add.



53
54
55
# File 'lib/ferrum/interceptable/chain.rb', line 53

def remove(id)
  @handlers.delete(id)
end