awesome/tests/examples/text/gears/object/signal.lua

32 lines
1007 B
Lua
Raw Normal View History

2016-05-17 06:59:48 +02:00
local gears = require("gears") --DOC_HIDE
local o = gears.object{}
-- Add a __tostring metamethod for prettier output --DOC_HIDE
getmetatable(o).__tostring = function() --DOC_HIDE
return "[obj]" --DOC_HIDE
end --DOC_HIDE
-- Function can be attached to signals
2016-05-17 06:59:48 +02:00
local function slot(obj, a, b, c)
print("In slot", obj, a, b, c)
end
2016-05-17 06:59:48 +02:00
o:connect_signal("my_signal", slot)
-- Emitting can be done without arguments. In that case, the object will be
-- implicitly added as an argument.
2016-05-17 06:59:48 +02:00
o:emit_signal "my_signal"
-- It is also possible to add as many random arguments are required.
2016-05-17 06:59:48 +02:00
o:emit_signal("my_signal", "foo", "bar", 42)
-- Finally, to allow the object to be garbage collected (the memory freed), it
-- is necessary to disconnect the signal or use `weak_connect_signal`
2016-05-17 06:59:48 +02:00
o:disconnect_signal("my_signal", slot)
-- This time, the `slot` wont be called as it is no longer connected.
2016-05-17 06:59:48 +02:00
o:emit_signal "my_signal"
--DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80