Add ability to track connections
One interesting use case for this is to only do expensive operations if something is connected to the signal: -- item:connect_signal("connection",function(_,name,count) -- if name == "mouse::move" and count == 0 then -- widget:connect_signal("mouse::move",conn) -- end -- end) -- item:connect_signal("disconnection",function(_,name,count) -- if count == 0 then -- widget:connect_signal("mouse::move",conn) -- end -- end)
This commit is contained in:
parent
d0d4d5fcd4
commit
879a43fbc4
|
@ -11,12 +11,14 @@ local function setup_object(args)
|
||||||
function data:connect_signal(name,func)
|
function data:connect_signal(name,func)
|
||||||
signals[name] = signals[name] or {}
|
signals[name] = signals[name] or {}
|
||||||
table.insert(signals[name],func)
|
table.insert(signals[name],func)
|
||||||
|
data:emit_signal("connection",name,#signals[name])
|
||||||
end
|
end
|
||||||
|
|
||||||
function data:disconnect_signal(name,func)
|
function data:disconnect_signal(name,func)
|
||||||
for k,v in pairs(signals[name] or {}) do
|
for k,v in pairs(signals[name] or {}) do
|
||||||
if v == func then
|
if v == func then
|
||||||
signals[name][k] = nil
|
signals[name][k] = nil
|
||||||
|
data:emit_signal("disconnection",name,#signals[name])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue