From 625b9c8901001014e0001dea3b94872693039868 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sun, 15 Mar 2020 04:02:31 -0400 Subject: [PATCH] object: Add a private API to optimize chain of responsibilities. Currently, emit_signal always call all connected function. However, it is very wasteful for some I/O intensive operations like icon lookup. This commit adds a trick (private API) to stop once a condition is met. It will also in the future be used for the permission system, but this is not yet implementd. --- lib/gears/object.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/gears/object.lua b/lib/gears/object.lua index b8b5d645..8b2411f7 100644 --- a/lib/gears/object.lua +++ b/lib/gears/object.lua @@ -134,7 +134,8 @@ function object:emit_signal(name, ...) end end -function object._setup_class_signals(t) +function object._setup_class_signals(t, args) + args = args or {} local conns = {} function t.connect_signal(name, func) @@ -143,6 +144,18 @@ function object._setup_class_signals(t) table.insert(conns[name], func) end + -- A variant of emit_signal which stops once a condition is met. + if args.allow_chain_of_responsibility then + function t._emit_signal_if(name, condition, ...) + assert(name) + for _, func in pairs(conns[name] or {}) do + if condition(...) then return end + func(...) + end + end + end + + --- Emit a notification signal. -- @tparam string name The signal name. -- @param ... The signal callback arguments