Add. Support onInterval method to EventConsumer class (#1747)

* Add. Support `onInterval` method to EventConsumer class

Usage
```Lua
-- execute action each 30 sec
events:onInterval(30*1000, function() end)

-- execute action once after 5 min
events:onIntervalOnce(5*60*1000, function() end)
```

* Fix. Remove timers

* Fix. Reset timer before callback

It allows stop timer inside callback.
Also it produce more accurate interval invocation
if callback take quite a long time.
E.g. Interval = 10 sec and callback took 5 sec then
if we reset timer after this callback then gap between
invocation will be 15 sec.

* Add. Timers now have TimeEvent class type.

Add. `reset` method to IntervalTimer class.

```Lua
events:onIntervalOnce(1000, function(self, timer)
  -- timer has type TimeEvent

  -- restart timer so it will be invoke again
  timer:restart()

  -- or reset new interval
  -- timer:reset(5000)
end)
```

* Fix. Typo in variable name
This commit is contained in:
Alexey Melnichuk
2016-07-13 18:43:51 +03:00
committed by FusionPBX
parent 44196ae739
commit 9076df2302
2 changed files with 175 additions and 25 deletions

View File

@@ -68,6 +68,12 @@ function IntervalTimer:rest()
return d
end
function IntervalTimer:reset(interval)
self._interval = interval
if self._begin then self:restart() end
return self
end
function IntervalTimer:stop()
if self:started() then
local d = self:elapsed()