diff --git a/sys/apis/event.lua b/sys/apis/event.lua index 5fe4d87..a71ace8 100644 --- a/sys/apis/event.lua +++ b/sys/apis/event.lua @@ -5,9 +5,8 @@ local Event = { uid = 1, -- unique id for handlers routines = { }, -- coroutines types = { }, -- event handlers - timers = { }, -- named timers terminate = false, - free = { }, + free = { }, -- allocated unused coroutines } -- Use a pool of coroutines for event handlers @@ -118,24 +117,6 @@ function Event.off(h) 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) return Event.addRoutine(function() while true do @@ -146,29 +127,17 @@ function Event.onInterval(interval, fn) end 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) - repeat - local e = { os.pullEvent() } - if e[1] == event then - return table.unpack(e) + local handler + + handler = Event.on('timer', function(t, id) + if timerId == id then + fn(t, id) + Event.off(handler) end - until e[1] == 'timer' and e[2] == timerId + end) + + return handler end -- Set a handler for the terminate event. Within the function, return diff --git a/sys/apis/ui/components/Notification.lua b/sys/apis/ui/components/Notification.lua index f71408b..901a68c 100644 --- a/sys/apis/ui/components/Notification.lua +++ b/sys/apis/ui/components/Notification.lua @@ -35,8 +35,12 @@ function UI.Notification:success(value, timeout) end function UI.Notification:cancel() + if self.timer then + Event.off(self.timer) + self.timer = nil + end + if self.canvas then - Event.cancelNamedTimer('notificationTimer') self.enabled = false self.canvas:removeLayer() self.canvas = nil @@ -60,7 +64,7 @@ function UI.Notification:display(value, timeout) self:write(2, k, v) end - Event.addNamedTimer('notificationTimer', timeout or 3, false, function() + self.timer = Event.onTimeout(timeout or 3, function() self:cancel() self:sync() end) diff --git a/sys/apis/ui/components/StatusBar.lua b/sys/apis/ui/components/StatusBar.lua index 183014a..af1d2c4 100644 --- a/sys/apis/ui/components/StatusBar.lua +++ b/sys/apis/ui/components/StatusBar.lua @@ -62,10 +62,9 @@ function UI.StatusBar:getValue(name) end function UI.StatusBar:timedStatus(status, timeout) - timeout = timeout or 3 self:write(2, 1, Util.widthify(status, self.width-2), self.backgroundColor) - Event.addNamedTimer('statusTimer', timeout, false, function() - if self.parent.enabled then + Event.on(timeout or 3, function() + if self.enabled then self:draw() self:sync() end