I am creating a slightly modified version of the OpenFL Timer found in openfl.utils
since it works well and I use it a lot for Windows and Neko targets. However, when reading the code of this file, I realised that the functions it uses are using the haxe.Timer
class using the constructor which according to the Haxe API is not available on C++ or Neko targets, so how is OpenFL doing this without compile errors? Is OpenFL magic or am I just missing something obvious?
Here is my modified version:
class Timer {
public var currentCount (default, null):Int;
public var delay (get, set):Float;
public var repeatCount (default, set):Int;
public var running (default, null):Bool;
private var __delay:Float;
private var __timer:HaxeTimer;
private var __timerID:Int;
private var __f:Void -> Void;
public function new (delay:Float, f:Void -> Void, repeatCount:Int = 0):Void {
if (Math.isNaN (delay) || delay < 0) {
throw new Error ("The delay specified is negative or not a finite number");
}
__delay = delay;
this.repeatCount = repeatCount;
__f = f;
running = false;
currentCount = 0;
}
public function reset ():Void {
if (running) {
stop ();
}
currentCount = 0;
}
public function start ():Void {
if (!running) {
running = true;
}
}
public function stop ():Void {
running = false;
if (__timer != null) {
__timer.stop ();
__timer = null;
}
}
// Getters & Setters
private function get_delay ():Float {
return __delay;
}
private function set_delay (value:Float):Float {
__delay = value;
if (running) {
stop ();
start ();
}
return __delay;
}
private function set_repeatCount (v:Int):Int {
if (running && v != 0 && v <= currentCount) {
stop ();
}
repeatCount = v;
return v;
}
private function timer_onTimer ():Void {
__f();
currentCount++;
if (repeatCount > 0 && currentCount >= repeatCount)
stop ();
}
}
I did read somewhere that haxe.Timer
became redundant on native targets beyond version 2.7 of the Haxe Compiler but can’t remember where, unless I was seeing things.