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:
parent
a3c37382be
commit
625b9c8901
|
@ -134,7 +134,8 @@ function object:emit_signal(name, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function object._setup_class_signals(t)
|
function object._setup_class_signals(t, args)
|
||||||
|
args = args or {}
|
||||||
local conns = {}
|
local conns = {}
|
||||||
|
|
||||||
function t.connect_signal(name, func)
|
function t.connect_signal(name, func)
|
||||||
|
@ -143,6 +144,18 @@ function object._setup_class_signals(t)
|
||||||
table.insert(conns[name], func)
|
table.insert(conns[name], func)
|
||||||
end
|
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.
|
--- Emit a notification signal.
|
||||||
-- @tparam string name The signal name.
|
-- @tparam string name The signal name.
|
||||||
-- @param ... The signal callback arguments
|
-- @param ... The signal callback arguments
|
||||||
|
|
Loading…
Reference in New Issue