2023-06-01 22:41:47 +02:00
|
|
|
--- Signal utilities.
|
2022-08-16 10:38:33 +02:00
|
|
|
--
|
|
|
|
-- This is a reworked implementation of the original signal module
|
|
|
|
-- from the hump library (https://github.com/vrld/hump).
|
|
|
|
-- See README.ACKNOWLEDGEMENT for detailed information.
|
|
|
|
--
|
|
|
|
-- It offers functionality to register for and publish signals.
|
|
|
|
-- Implements the Observer pattern.
|
|
|
|
--
|
2023-06-01 22:41:47 +02:00
|
|
|
-- @module gear.signal
|
2022-08-16 10:38:33 +02:00
|
|
|
-- @copyright 2010-2013 Matthias Richter
|
|
|
|
-- @copyright 2022 The DoubleFourteen Code Forge
|
|
|
|
-- @author Matthias Richter, Lorenzo Cogotti
|
|
|
|
|
|
|
|
local Signal = {}
|
|
|
|
Signal.__index = function(self, key)
|
|
|
|
return Signal[key] or (function()
|
|
|
|
local t = {}
|
|
|
|
rawset(self, key, t)
|
|
|
|
return t
|
|
|
|
end)()
|
|
|
|
end
|
|
|
|
|
2023-06-01 22:41:47 +02:00
|
|
|
|
|
|
|
function Signal:new()
|
|
|
|
return setmetatable({}, self)
|
|
|
|
end
|
|
|
|
|
2022-08-16 10:38:33 +02:00
|
|
|
function Signal:register(s, f)
|
|
|
|
self[s][f] = f
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
|
|
|
|
function Signal:emit(s, ...)
|
|
|
|
for f in pairs(self[s]) do
|
|
|
|
f(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Signal:remove(s, ...)
|
|
|
|
local f = {...}
|
|
|
|
for i = 1,select('#', ...) do
|
|
|
|
self[s][f[i]] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Signal:clear(...)
|
|
|
|
local s = {...}
|
|
|
|
for i = 1,select('#', ...) do
|
|
|
|
self[s[i]] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-01 22:41:47 +02:00
|
|
|
function Signal:emitpattern(p, ...)
|
2022-08-16 10:38:33 +02:00
|
|
|
for s in pairs(self) do
|
|
|
|
if s:match(p) then self:emit(s, ...) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-01 22:41:47 +02:00
|
|
|
function Signal:registerpattern(p, f)
|
2022-08-16 10:38:33 +02:00
|
|
|
for s in pairs(self) do
|
|
|
|
if s:match(p) then self:register(s, f) end
|
|
|
|
end
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
|
2023-06-01 22:41:47 +02:00
|
|
|
function Signal:removepattern(p, ...)
|
2022-08-16 10:38:33 +02:00
|
|
|
for s in pairs(self) do
|
|
|
|
if s:match(p) then self:remove(s, ...) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-01 22:41:47 +02:00
|
|
|
function Signal:clearpattern(p)
|
2022-08-16 10:38:33 +02:00
|
|
|
for s in pairs(self) do
|
|
|
|
if s:match(p) then self[s] = {} end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-01 22:41:47 +02:00
|
|
|
function Signal:clearall()
|
|
|
|
for s in pairs(self) do
|
|
|
|
self[s] = {}
|
|
|
|
end
|
2022-08-16 10:38:33 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- default instance
|
2023-06-01 22:41:47 +02:00
|
|
|
local default = Signal:new()
|
2022-08-16 10:38:33 +02:00
|
|
|
|
|
|
|
-- module forwards calls to default instance
|
|
|
|
local module = {}
|
|
|
|
for k in pairs(Signal) do
|
|
|
|
if k ~= "__index" then
|
|
|
|
module[k] = function(...) return default[k](default, ...) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return Signal
|