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.
This commit is contained in:
Emmanuel Lepage Vallee 2020-03-15 04:02:31 -04:00
parent a3c37382be
commit 625b9c8901
1 changed files with 14 additions and 1 deletions

View File

@ -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