From 41149ed335bd9cd8b3cc6d59312b21b2658b2485 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 5 May 2019 23:00:58 -0400 Subject: [PATCH] class: Share the module level signal system implementation. Avoid adding another copy. --- lib/gears/object.lua | 36 ++++++++++++++++++++++++++++++++++++ lib/naughty/core.lua | 33 +++++++-------------------------- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/gears/object.lua b/lib/gears/object.lua index a7be7f35c..b8b5d6458 100644 --- a/lib/gears/object.lua +++ b/lib/gears/object.lua @@ -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") diff --git a/lib/naughty/core.lua b/lib/naughty/core.lua index adf1cbe9a..802848b97 100644 --- a/lib/naughty/core.lua +++ b/lib/naughty/core.lua @@ -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