cc-haxe/src/kernel/Timer.hx

38 lines
671 B
Haxe
Raw Normal View History

2021-12-20 00:55:30 +00:00
package kernel;
import cc.OS;
2022-12-19 20:06:23 +00:00
using tink.CoreApi;
2021-12-20 00:55:30 +00:00
/**
Wrapper class for using timer.
**/
class Timer {
private final timerID:Int;
2022-02-21 00:50:19 +00:00
private final callback:Callback<Noise>;
2022-02-21 14:17:38 +00:00
private final timerLink:CallbackLink;
2021-12-20 00:55:30 +00:00
/**
Create new timer with timeout in seconds.
**/
2022-03-01 11:54:36 +00:00
public function new(timeout:Float, callback:Callback<Noise>) {
2021-12-20 00:55:30 +00:00
timerID = OS.startTimer(timeout);
2022-02-21 00:50:19 +00:00
this.callback = callback;
2021-12-20 00:55:30 +00:00
2022-02-21 14:35:37 +00:00
timerLink = KernelEvents.instance.onTimer.handle(timerID -> {
if (this.timerID == timerID) {
2022-02-21 00:50:19 +00:00
callback.invoke(null);
2022-02-21 14:17:38 +00:00
timerLink.cancel();
2021-12-20 00:55:30 +00:00
}
});
}
/**
Cancle timer.
**/
public function cancle() {
OS.cancelTimer(timerID);
2022-02-21 14:17:38 +00:00
timerLink.cancel();
2021-12-20 00:55:30 +00:00
}
}