Class: Textus::Hooks::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/hooks/dispatcher.rb

Constant Summary collapse

HOOK_TIMEOUT_SECONDS =
2

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



10
11
12
13
# File 'lib/textus/hooks/dispatcher.rb', line 10

def initialize
  @subscribers = Hash.new { |h, k| h[k] = [] }
  @error_handlers = []
end

Instance Method Details

#on_error(&block) ⇒ Object

Register an error callback invoked when a user hook raises. Used by Infra::AuditSubscriber to record an “event_error” audit row.



17
18
19
# File 'lib/textus/hooks/dispatcher.rb', line 17

def on_error(&block)
  @error_handlers << block
end

#publish(event, **kwargs) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/textus/hooks/dispatcher.rb', line 25

def publish(event, **kwargs)
  key = kwargs[:key] || "-"
  @subscribers[event.to_sym].each do |sub|
    next unless match?(sub[:keys], key)

    invoke(event, sub, key, kwargs)
  end
end

#subscribe(event, name, keys: nil, &block) ⇒ Object



21
22
23
# File 'lib/textus/hooks/dispatcher.rb', line 21

def subscribe(event, name, keys: nil, &block)
  @subscribers[event.to_sym] << { name: name.to_sym, callable: block, keys: keys }
end