document signal class

This commit is contained in:
Lera Elvoé 2025-02-14 20:04:38 +03:00
parent d17e670f27
commit 784e654f07
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc

View File

@ -1,17 +1,26 @@
local util = require "util"
---@class Signal
local Signal = {
_connections = {}
}
local Signal = {}
local util = require "util"
Signal.__index = Signal
---Connects f to this signal. When the signal is emmitted, this function will be called.
---@param f function
function Signal:connect(f)
util.list_insert_once(self._connections, f)
end
---Disconnects f from this signal.
---@param f function
function Signal:disconnect(f)
util.list_remove_value(self._connections, f)
end
---Emits the signal, calling the connecting functions with the provided params.
---@param ... any
function Signal:emit(...)
-- don't care about order
for _, v in pairs(self._connections) do
@ -19,6 +28,8 @@ function Signal:emit(...)
end
end
---Constructs a new signal.
---@return Signal
function Signal.new()
local s = {
_connections = {},