local function createSignal()
local connections = {}
local suspendedConnections = {}
local firing = false
local function subscribe(_self, callback)
assert(typeof(callback) == "function", "Can only subscribe to signals with a function.")
local connection = {
callback = callback,
disconnected = false,
}
if firing and not connections[callback] then
suspendedConnections[callback] = connection
end
connections[callback] = connection
local function disconnect()
assert(not connection.disconnected, "Listeners can only be disconnected once.")
connection.disconnected = true
connections[callback] = nil
suspendedConnections[callback] = nil
end
return disconnect
end
local function fire(_self, ...)
firing = true
for callback, connection in pairs(connections) do
if not connection.disconnected and not suspendedConnections[callback] then
callback(...)
end
end
firing = false
for callback, _ in pairs(suspendedConnections) do
suspendedConnections[callback] = nil
end
end
return {
subscribe = subscribe,
fire = fire,
}
end
return createSignal