Make textclock garbage-collectable

This fixes the textclock-specific part of the test that the previous commit
added.

To fix this, gears.timer.weak_start_new() is used. This function creates a timer
that is automatically stopped when its callback function is garbage collected.
The callback function is saved as a member of the texbox widget that is the
"widget behind the textclock". Thus, the timer can only be stopped after the
widget is garbage-collected, but the timer does not keep the widget alive.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-09-27 12:38:57 +02:00
parent 901c8f680a
commit e9377c4808
1 changed files with 5 additions and 4 deletions

View File

@ -31,13 +31,14 @@ function textclock.new(format, timeout)
local timeout = timeout or 60
local w = textbox()
local t = timer { timeout = timeout }
t:connect_signal("timeout", function()
local t
function w._textclock_update_cb()
w:set_markup(DateTime.new_now_local():format(format))
t.timeout = calc_timeout(timeout)
t:again()
end)
t:start()
return true -- Continue the timer
end
t = timer.weak_start_new(timeout, w._textclock_update_cb)
t:emit_signal("timeout")
return w
end