CallbackRegistry

The CallbackRegistry class provides an easy mechanism to register and dispatch callbacks. It can be used in two different modes: either as a list or with keys.

List

A list callback registry calls the callbacks in the order they were registered.

local MyModule = select(2, ...).MyModule
local CallbackRegistry = MyModule:From("LibTSMUtil"):IncludeClassType("CallbackRegistry")

local function Callback1(value)
   print("CALLBACK1", value)
end
local function Callback2(value)
   print("CALLBACK2", value)
end

local registry = CallbackRegistry.NewList()
registry:Add(Callback1)
registry:Add(Callback2)

registry:CallAll(98)
-- CALLBACK 1  98
-- CALLBACK 2  98

registry:Remove(Callback2)

registry:CallAll(14)
-- CALLBACK 1  14

Keys

A keys callback registry calls the callbacks in an arbitrary order, but allows calling a specific callback.

local MyModule = select(2, ...).MyModule
local CallbackRegistry = MyModule:From("LibTSMUtil"):IncludeClassType("CallbackRegistry")

local function Callback1(value)
   print("CALLBACK1", value)
end
local function Callback2(value)
   print("CALLBACK2", value)
end

local registry = CallbackRegistry.NewWithKeys()
registry:Add(Callback1, "1")
registry:Add(Callback2, "2")

registry:CallAll(98)
-- CALLBACK 1  98
-- CALLBACK 2  98

registry:Call("1", 14)
-- CALLBACK 1  14

API

class CallbackRegistry: Class
staticmethod NewList(executionTimeLabel?: string): CallbackRegistry

Creates a new callback registry which maintains a list of callbacks.

staticmethod NewWithKeys(executionTimeLabel?: string): CallbackRegistry

Creates a new callback registry which maintains a keyed table of callbacks.

Add(self: CallbackRegistry, func: function, key?: string)

Adds a callback into the registry.

Parameters:
  • func (function) – The callback function

  • key? (string) – The key (required if and only if the registry was created via NewWithKeys())

Remove(self: CallbackRegistry, funcOrKey: string | function)

Removes a callback from the registry.

Parameters:

funcOrKey (string | function) – The callback function for registries created via NewList() or the key if created via NewWithkeys()

HasCallback(self: CallbackRegistry, funcOrKey: string | function): boolean

Checks if a callback is already within the registry.

Parameters:

funcOrKey (string | function) – The callback function for registries created via NewList() or the key if created via NewWithkeys()

IsEmpty(self: CallbackRegistry): boolean

Returns whether or not the registry is empty.

Wipe(self: CallbackRegistry)

Removes all callbacks from the registry.

Call(self: CallbackRegistry, key: string, ...: any): (...: unknown)

Calls a specific registered callback by its key and passes through its returns.

NOTE: This registry must have been created via NewWithKeys() and without an execution time label

Parameters:
  • key (string) – The key

  • ... (any) – Arguments to pass to the callbacks

CallAll(self: CallbackRegistry, ...: any)

Calls all registered callbacks.

Parameters:

... (any) – Arguments to pass to the callbacks