2021-12-20 00:55:30 +00:00
|
|
|
package kernel;
|
|
|
|
|
2022-02-21 00:50:19 +00:00
|
|
|
using tink.CoreApi;
|
|
|
|
|
2021-12-20 00:55:30 +00:00
|
|
|
import cc.OS;
|
|
|
|
|
|
|
|
/**
|
|
|
|
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-02-21 14:35:37 +00:00
|
|
|
public function new(timeout:Int, 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
|
|
|
}
|
|
|
|
}
|