AwesomeFiles/config/awesome/modules/animation/subscribable.lua
2022-07-11 20:06:55 -05:00

35 lines
931 B
Lua
Executable file

local gobject = require("gears.object")
-- Kidna copying awesotre's stores on a surface level for added compatibility
local function subscribable(args)
local ret = gobject{}
local subscribed = {}
-- Subscrubes a function to the object so that it's called when `fire` is
-- Calls subscribe_callback if it exists as well
function ret:subscribe(func)
local id = tostring(func):gsub("function: ", "")
subscribed[id] = func
if self.subscribe_callback then self.subscribe_callback(func) end
end
-- Unsubscribes a function and calls unsubscribe_callback if it exists
function ret:unsubscribe(func)
if not func then
subscribed = {}
else
local id = tostring(func):gsub("function: ", "")
subscribed[id] = nil
end
if self.unsubscribe_callback then self.unsubscribe_callback(func) end
end
function ret:fire(...) for _, func in pairs(subscribed) do func(...) end end
return ret
end
return subscribable