Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
190 lines (143 sloc) 4.72 KB
package ClosureTimers
import NoWurst
import TimerUtils
import Real
import Wurstunit
/** Execute an action after a certain time.
The callback object is automatically destroyed.
Must be used on a timer acquired with `getTimer()`
Example use:
| someTimer.doAfter(10.0) ->
| print("10 seconds later")
*/
public function timer.doAfter(real timeToWait, CallbackSingle cb) returns CallbackSingle
cb.start(this, timeToWait)
return cb
/** Execute an action after a certain time.
The callback object is automatically destroyed.
Example use:
| doAfter(10.0) ->
| print("10 seconds later")
*/
public function doAfter(real timeToWait, CallbackSingle cb) returns CallbackSingle
return getTimer().doAfter(timeToWait, cb)
/** Execute an action with a 0-second timer delay.
The callback object is destroyed afterwards.
*/
public function nullTimer(CallbackSingle cb) returns CallbackSingle
return doAfter(0, cb)
/** Execute an action periodically.
The callback has to be destroyed manually.
Must be used on a timer acquired with `getTimer()`
Example use:
| someTimer.doPeriodically(0.5) cb ->
| if i > 10
| destroy cb
*/
public function timer.doPeriodically(real time, CallbackPeriodic cb) returns CallbackPeriodic
cb.start(this, time)
return cb
/** Execute an action periodically.
The callback has to be destroyed manually.
Example use:
| doPeriodically(0.5) cb ->
| if i > 10
| destroy cb
*/
public function doPeriodically(real time, CallbackPeriodic cb) returns CallbackPeriodic
return getTimer().doPeriodically(time, cb)
/** execute an action periodically, with a limited amount of calls
The callback object is destroyed after the action has been executed callAmount times.
Must be used on a timer acquired with `getTimer()`
Example use:
| someTimer.doPeriodicallyCounted(0.5, 100) cb ->
| doSomething()
*/
public function timer.doPeriodicallyCounted(real time, int callAmount, CallbackCounted cb) returns CallbackCounted
cb.start(this, time, callAmount)
return cb
/** execute an action periodically, with a limited amount of calls
The callback object is destroyed after the action has been executed callAmount times.
Example use:
| doPeriodicallyCounted(0.5, 100) cb ->
| doSomething()
*/
public function doPeriodicallyCounted(real time, int callAmount, CallbackCounted cb) returns CallbackCounted
return getTimer().doPeriodicallyCounted(time, callAmount, cb)
/** execute an action periodically, with a limited duration
The callback object is destroyed afterwards.
Must be used on a timer acquired with `getTimer()`
Example use:
| someTimer.doPeriodicallyCounted(0.5, 10.) ->
| doSomething()
*/
public function timer.doPeriodicallyTimed(real interval, real timerDuration, CallbackCounted cb) returns CallbackCounted
return this.doPeriodicallyCounted(interval, (timerDuration / interval + 0.5).toInt(), cb)
/** execute an action periodically, with a limited duration
The callback object is destroyed afterwards.
Example use:
| doPeriodicallyCounted(0.5, 10.) ->
| doSomething()
*/
public function doPeriodicallyTimed(real interval, real timerDuration, CallbackCounted cb) returns CallbackCounted
return getTimer().doPeriodicallyTimed(interval, timerDuration, cb)
//Timer Stuff
public abstract class CallbackSingle
private timer t
abstract function call()
function start(timer whichTimer, real time)
t = whichTimer
..setData(this castTo int)
..start(time, () -> staticCallback())
private static function staticCallback()
timer t = GetExpiredTimer()
thistype cb = t.getData() castTo thistype
cb.call()
destroy cb
ondestroy
t.release()
public abstract class CallbackPeriodic
private timer t
protected abstract function call(thistype cb)
function start(timer whichTimer, real time)
t = whichTimer
..setData(this castTo int)
..startPeriodic(time, function staticCallback)
private static function staticCallback()
let cb = (GetExpiredTimer().getData() castTo thistype)
cb.call(cb)
ondestroy
t.release()
public abstract class CallbackCounted
private var count = 0
private timer t
protected abstract function call(thistype cb)
function start(timer whichTimer, real time, int callAmount)
count = callAmount
t = whichTimer
..setData(this castTo int)
..startPeriodic(time, function staticCallback)
function isLast() returns boolean
return count == 1
function getCount() returns int
return count
function stop()
count = 1
private static function staticCallback()
(GetExpiredTimer().getData() castTo thistype).callAndCount()
private function callAndCount()
call(this)
count--
if count <= 0
destroy this
ondestroy
t.release()
var x = 200
@Test function testDoAfter()
doAfter(0.1) ->
x += 50
doAfter(0.2) ->
x *= 2
doAfter(0.3) ->
x = x div 2
x.assertEquals(250)