[connect] fixes
This commit is contained in:
60
src/main/hw/Timer.hx
Normal file
60
src/main/hw/Timer.hx
Normal file
@@ -0,0 +1,60 @@
|
||||
package hw;
|
||||
|
||||
#if cpp
|
||||
|
||||
import haxe.Log;
|
||||
import haxe.PosInfos;
|
||||
import cpp.vm.Thread;
|
||||
|
||||
class Timer {
|
||||
|
||||
private var sleep:Float;
|
||||
private var stopped:Bool;
|
||||
|
||||
public function new(time_ms:Int) {
|
||||
this.sleep = time_ms / 1000.0;
|
||||
this.stopped = false;
|
||||
Thread.create(function() {
|
||||
while (!stopped) {
|
||||
Sys.sleep(sleep);
|
||||
try {
|
||||
run();
|
||||
} catch (error:Dynamic) {
|
||||
trace(hw.log.BaseLogger.LoggerUtil.printError(error));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function stop() {
|
||||
stopped = true;
|
||||
}
|
||||
|
||||
public dynamic function run() {}
|
||||
|
||||
public static function delay(f:Void -> Void, time_ms:Int) {
|
||||
var t = new Timer(time_ms);
|
||||
t.run = function() {
|
||||
t.stop();
|
||||
f();
|
||||
};
|
||||
return t;
|
||||
}
|
||||
|
||||
public static function measure<T>(f:Void -> T, ?pos:PosInfos):T {
|
||||
var t0 = stamp();
|
||||
var r = f();
|
||||
Log.trace((stamp() - t0) + "s", pos);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static inline function stamp():Float {
|
||||
return Sys.time();
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
typedef Timer = haxe.Timer;
|
||||
|
||||
#end
|
||||
Reference in New Issue
Block a user