1
0
mirror of https://github.com/kepler155c/opus synced 2025-01-03 20:30:28 +00:00

Event cleanup

This commit is contained in:
kepler155c@gmail.com 2019-02-24 06:58:26 -05:00
parent 8ae3b33374
commit 687fc05445
3 changed files with 18 additions and 46 deletions

View File

@ -5,9 +5,8 @@ local Event = {
uid = 1, -- unique id for handlers uid = 1, -- unique id for handlers
routines = { }, -- coroutines routines = { }, -- coroutines
types = { }, -- event handlers types = { }, -- event handlers
timers = { }, -- named timers
terminate = false, terminate = false,
free = { }, free = { }, -- allocated unused coroutines
} }
-- Use a pool of coroutines for event handlers -- Use a pool of coroutines for event handlers
@ -118,24 +117,6 @@ function Event.off(h)
end end
end end
local function addTimer(interval, recurring, fn)
local timerId = os.startTimer(interval)
local handler
handler = Event.on('timer', function(t, id)
if timerId == id then
fn(t, id)
if recurring then
timerId = os.startTimer(interval)
else
Event.off(handler)
end
end
end)
return handler
end
function Event.onInterval(interval, fn) function Event.onInterval(interval, fn)
return Event.addRoutine(function() return Event.addRoutine(function()
while true do while true do
@ -146,29 +127,17 @@ function Event.onInterval(interval, fn)
end end
function Event.onTimeout(timeout, fn) function Event.onTimeout(timeout, fn)
return addTimer(timeout, false, fn)
end
function Event.addNamedTimer(name, interval, recurring, fn)
Event.cancelNamedTimer(name)
Event.timers[name] = addTimer(interval, recurring, fn)
end
function Event.cancelNamedTimer(name)
local timer = Event.timers[name]
if timer then
Event.off(timer)
end
end
function Event.waitForEvent(event, timeout)
local timerId = os.startTimer(timeout) local timerId = os.startTimer(timeout)
repeat local handler
local e = { os.pullEvent() }
if e[1] == event then handler = Event.on('timer', function(t, id)
return table.unpack(e) if timerId == id then
fn(t, id)
Event.off(handler)
end end
until e[1] == 'timer' and e[2] == timerId end)
return handler
end end
-- Set a handler for the terminate event. Within the function, return -- Set a handler for the terminate event. Within the function, return

View File

@ -35,8 +35,12 @@ function UI.Notification:success(value, timeout)
end end
function UI.Notification:cancel() function UI.Notification:cancel()
if self.timer then
Event.off(self.timer)
self.timer = nil
end
if self.canvas then if self.canvas then
Event.cancelNamedTimer('notificationTimer')
self.enabled = false self.enabled = false
self.canvas:removeLayer() self.canvas:removeLayer()
self.canvas = nil self.canvas = nil
@ -60,7 +64,7 @@ function UI.Notification:display(value, timeout)
self:write(2, k, v) self:write(2, k, v)
end end
Event.addNamedTimer('notificationTimer', timeout or 3, false, function() self.timer = Event.onTimeout(timeout or 3, function()
self:cancel() self:cancel()
self:sync() self:sync()
end) end)

View File

@ -62,10 +62,9 @@ function UI.StatusBar:getValue(name)
end end
function UI.StatusBar:timedStatus(status, timeout) function UI.StatusBar:timedStatus(status, timeout)
timeout = timeout or 3
self:write(2, 1, Util.widthify(status, self.width-2), self.backgroundColor) self:write(2, 1, Util.widthify(status, self.width-2), self.backgroundColor)
Event.addNamedTimer('statusTimer', timeout, false, function() Event.on(timeout or 3, function()
if self.parent.enabled then if self.enabled then
self:draw() self:draw()
self:sync() self:sync()
end end