Module: Lich::Common::HookRegistry

Included in:
DownstreamHook, UpstreamHook
Defined in:
documented/common/hook_registry.rb

Overview

Shared behaviour for the down/upstream hook registries. DownstreamHook and UpstreamHook are otherwise near-identical apart from their backing storage and their per-direction run, so the registration/bookkeeping lives here and a fix (e.g. to source tracking) lands in one place.

An including class is +extend+ed with these as class methods and supplies its own storage via _hooks, _hook_sources, _hook_owners, _hook_persist and _hook_priorities, keeping its own run.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(registry) ⇒ void

This method returns an undefined value.

Initialize each registry's lock before it accepts registrations. Keep the same lock if the registry is extended again during a reload.

Parameters:

  • registry (Class) —

    the extending hook registry



20
21
22
# File 'documented/common/hook_registry.rb', line 20

def self.extended(registry)
  registry.instance_variable_set(:@hook_mutex, Mutex.new) unless registry.instance_variable_defined?(:@hook_mutex)
end

Instance Method Details

#add(name, action, persist: nil, priority: 0) ⇒ Proc, false

Registers action under name, recording the current script's name as the source (used by #sources for display), its object_id as the owner, and the declared persist disposition (used by #cleanup_on_death).

persist declares what should happen to the hook when the registering script dies:

* +true+  - keep it (it is meant to outlive the script, e.g. ;alias)
* +false+ - remove it (it is scoped to this script's lifetime)
* +nil+   - undeclared: kept for backwards compatibility, but the death
          path warns once so the author can declare intent.

Higher-priority hooks run first. Hooks with equal priority retain their registration order, preserving historical behaviour at the default of zero. A named replacement keeps its original equal-priority position.

Parameters:

  • name (String)
  • action (Proc)
  • persist (Boolean, nil) (defaults to: nil) —

    hook lifetime relative to the script

  • priority (Numeric) (defaults to: 0) —

    execution priority; higher values run first

Returns:

  • (Proc, false) —

    the stored proc, or false if action is not a Proc



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'documented/common/hook_registry.rb', line 44

def add(name, action, persist: nil, priority: 0)
  unless action.is_a?(Proc)
    echo "#{hook_label}: not a Proc (#{action})"
    return false
  end
  unless priority.is_a?(Numeric) && priority.real? &&
         (!priority.respond_to?(:finite?) || priority.finite?)
    echo "#{hook_label}: priority must be a finite real Numeric (#{priority.inspect})"
    return false
  end
  script = Script.current
  @hook_mutex.synchronize do
    _hook_sources[name] = (script&.name || "Unknown")
    _hook_owners[name]  = script&.object_id
    _hook_persist[name] = persist
    _hook_priorities[name] = priority
    _hooks[name] = action
  end
  action
end

#cleanup_on_death(owner_id) ⇒ Integer

Invoked from the ScriptDeath handler when a script dies. For each hook the script registered (matched by object_id, so a force: true sibling sharing its name is unaffected), removes the ones explicitly scoped to the script (+persist: false+), keeps explicitly persistent ones (+persist: true+), and leaves undeclared ones in place but warns once so the author can declare intent. Default behaviour is therefore unchanged (hooks persist) until a script opts in to persist: false.

Parameters:

  • owner_id (Integer) —

    the dying script's object_id

Returns:

  • (Integer) —

    the number of hooks removed



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'documented/common/hook_registry.rb', line 83

def cleanup_on_death(owner_id)
  removed    = 0
  undeclared = []
  @hook_mutex.synchronize do
    owned = _hook_owners.select { |_name, owner| owner == owner_id }.keys
    owned.each do |name|
      case _hook_persist[name]
      when false then (remove_hook(name); removed += 1)
      when true  then next
      else undeclared << name
      end
    end
  end
  warn_undeclared(undeclared)
  removed
end

#hook_sources ⇒ Hash{String => String}

Returns the live hook-name -> source map.

Returns:



116
117
118
# File 'documented/common/hook_registry.rb', line 116

def hook_sources
  _hook_sources
end

#list ⇒ Array<String>

Returns a copy of the registered hook names.

Returns:

  • (Array<String>) —

    a copy of the registered hook names



101
102
103
# File 'documented/common/hook_registry.rb', line 101

def list
  @hook_mutex.synchronize { _hooks.keys }
end

#ordered_hook_names ⇒ Array<String>

Snapshot names and priorities together with registration/removal excluded. There is no order cache: legacy direct edits to the live maps are visible on the next dispatch, but bypass synchronization; use add/remove for concurrent mutations. Sorting and callbacks run outside the lock.

New names and priority changes take effect on the next dispatch. Actions are read live before invocation: removed names are skipped, and a name replaced before its turn runs the new action at its old position for this pass, preserving historical replacement behaviour.

Returns:



130
131
132
133
134
135
# File 'documented/common/hook_registry.rb', line 130

def ordered_hook_names
  entries = @hook_mutex.synchronize do
    _hooks.keys.each_with_index.map { |name, index| [name, _hook_priorities.fetch(name, 0), index] }
  end
  entries.sort_by { |_name, priority, index| [-priority, index] }.map(&:first)
end

#remove(name) ⇒ Proc?

Removes the hook registered under name from every map.

Parameters:

Returns:

  • (Proc, nil) —

    the removed proc, if any



69
70
71
# File 'documented/common/hook_registry.rb', line 69

def remove(name)
  @hook_mutex.synchronize { remove_hook(name) }
end

#sources ⇒ void

This method returns an undefined value.

Prints a Hook -> Source table via Lich::Messaging.



107
108
109
110
111
112
113
# File 'documented/common/hook_registry.rb', line 107

def sources
  rows = @hook_mutex.synchronize { _hook_sources.to_a }
  info_table = Terminal::Table.new :headings => ['Hook', 'Source'],
                                   :rows     => rows,
                                   :style    => { :all_separators => true }
  Lich::Messaging.mono(info_table.to_s)
end