aboutsummaryrefslogtreecommitdiff
path: root/.config/awesome/quarrel/animation/subscribable/init.lua
blob: 95427048f77094770e72e23c0c115bed05a1d2a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
-------------------------------------------
-- @author https://github.com/Kasper24
-- @copyright 2021-2025 Kasper24
-- Adopted from rubato
-------------------------------------------
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