class: Share the module level signal system implementation.

Avoid adding another copy.
This commit is contained in:
Emmanuel Lepage Vallee 2019-05-05 23:00:58 -04:00
parent c10312b511
commit 41149ed335
2 changed files with 43 additions and 26 deletions

View File

@ -134,6 +134,42 @@ function object:emit_signal(name, ...)
end
end
function object._setup_class_signals(t)
local conns = {}
function t.connect_signal(name, func)
assert(name)
conns[name] = conns[name] or {}
table.insert(conns[name], func)
end
--- Emit a notification signal.
-- @tparam string name The signal name.
-- @param ... The signal callback arguments
function t.emit_signal(name, ...)
assert(name)
for _, func in pairs(conns[name] or {}) do
func(...)
end
end
--- Disconnect a signal from a source.
-- @tparam string name The name of the signal
-- @tparam function func The attached function
-- @treturn boolean If the disconnection was successful
function t.disconnect_signal(name, func)
for k, v in ipairs(conns[name] or {}) do
if v == func then
table.remove(conns[name], k)
return true
end
end
return false
end
return conns
end
local function get_miss(self, key)
local class = rawget(self, "_class")

View File

@ -14,9 +14,10 @@
-- Package environment
local capi = { screen = screen }
local gdebug = require("gears.debug")
local screen = require("awful.screen")
local gtable = require("gears.table")
local gdebug = require("gears.debug")
local screen = require("awful.screen")
local gtable = require("gears.table")
local gobject = require("gears.object")
local naughty = {}
@ -249,7 +250,7 @@ function naughty.suspend()
properties.suspended = true
end
local conns = {}
local conns = gobject._setup_class_signals(naughty)
--- Connect a global signal on the notification engine.
--
@ -261,41 +262,21 @@ local conns = {}
--
-- @tparam string name The name of the signal
-- @tparam function func The function to attach
-- @staticfct naughty.connect_signal
-- @usage naughty.connect_signal("added", function(notif)
-- -- do something
-- end)
-- @staticfct naughty.connect_signal
function naughty.connect_signal(name, func)
assert(name)
conns[name] = conns[name] or {}
table.insert(conns[name], func)
end
--- Emit a notification signal.
-- @tparam string name The signal name.
-- @param ... The signal callback arguments
-- @staticfct naughty.emit_signal
function naughty.emit_signal(name, ...)
assert(name)
for _, func in pairs(conns[name] or {}) do
func(...)
end
end
--- Disconnect a signal from a source.
-- @tparam string name The name of the signal
-- @tparam function func The attached function
-- @treturn boolean If the disconnection was successful
-- @staticfct naughty.disconnect_signal
function naughty.disconnect_signal(name, func)
for k, v in ipairs(conns[name] or {}) do
if v == func then
table.remove(conns[name], k)
return true
end
end
return false
end
-- @treturn boolean If the disconnection was successful
local function resume()
properties.suspended = false