Browse Source

merge asys.Timer into haxe.Timer

Simon Krajewski 6 years ago
parent
commit
3fa47f53b3

+ 0 - 57
std/asys/Timer.hx

@@ -1,57 +0,0 @@
-package asys;
-
-private typedef Native =
-	#if doc_gen
-	Void;
-	#elseif eval
-	eval.uv.Timer;
-	#elseif hl
-	hl.uv.Timer;
-	#elseif neko
-	neko.uv.Timer;
-	#else
-	#error "timer not supported on this platform"
-	#end
-
-class Timer {
-	public static function delay(f:() -> Void, timeMs:Int):Timer {
-		var t = new Timer(timeMs);
-		t.run = function() {
-			t.stop();
-			f();
-		};
-		return t;
-	}
-
-	public static function measure<T>(f:()->T, ?pos:haxe.PosInfos):T {
-		var t0 = stamp();
-		var r = f();
-		haxe.Log.trace((stamp() - t0) + "s", pos);
-		return r;
-	}
-
-	public static function stamp():Float {
-		// TODO: libuv?
-		return Sys.time();
-	}
-
-	var native:Native;
-
-	public function new(timeMs:Int) {
-		native = new Native(timeMs, () -> run());
-	}
-
-	public dynamic function run():Void {}
-
-	public function stop():Void {
-		native.close((err) -> {});
-	}
-
-	public function ref():Void {
-		native.ref();
-	}
-
-	public function unref():Void {
-		native.unref();
-	}
-}

+ 1 - 1
std/asys/net/Server.hx

@@ -202,5 +202,5 @@ class Server {
 	var native:NativeStream;
 	var native:NativeStream;
 	var nativeSocket:NativeSocket;
 	var nativeSocket:NativeSocket;
 	var nativePipe:NativePipe;
 	var nativePipe:NativePipe;
-	var listenDefer:asys.Timer;
+	var listenDefer:haxe.Timer;
 }
 }

+ 3 - 3
std/asys/net/Socket.hx

@@ -367,7 +367,7 @@ class Socket extends Duplex {
 		native.unref();
 		native.unref();
 	}
 	}
 
 
-	var connectDefer:asys.Timer;
+	var connectDefer:haxe.Timer;
 	var native:NativeStream;
 	var native:NativeStream;
 	var nativeSocket:NativeSocket;
 	var nativeSocket:NativeSocket;
 	var nativePipe:NativePipe;
 	var nativePipe:NativePipe;
@@ -376,7 +376,7 @@ class Socket extends Duplex {
 	var connectStarted = false;
 	var connectStarted = false;
 	var serverSpawn:Bool = false;
 	var serverSpawn:Bool = false;
 	var timeoutTime:Int = 0;
 	var timeoutTime:Int = 0;
-	var timeoutTimer:asys.Timer;
+	var timeoutTimer:haxe.Timer;
 
 
 	function new() {
 	function new() {
 		super();
 		super();
@@ -443,7 +443,7 @@ class Socket extends Duplex {
 			timeoutTimer.stop();
 			timeoutTimer.stop();
 		timeoutTimer = null;
 		timeoutTimer = null;
 		if (timeoutTime != 0) {
 		if (timeoutTime != 0) {
-			timeoutTimer = asys.Timer.delay(timeoutTrigger, timeoutTime);
+			timeoutTimer = haxe.Timer.delay(timeoutTrigger, timeoutTime);
 			timeoutTimer.unref();
 			timeoutTimer.unref();
 		}
 		}
 	}
 	}

+ 28 - 1
std/haxe/Timer.hx

@@ -22,6 +22,15 @@
 
 
 package haxe;
 package haxe;
 
 
+#if (target.asys)
+private typedef Native =
+	#if eval
+	eval.uv.Timer;
+	#else
+	#error "Missing asys implementation"
+	#end
+#end
+
 /**
 /**
 	The `Timer` class allows you to create asynchronous timers on platforms that
 	The `Timer` class allows you to create asynchronous timers on platforms that
 	support events.
 	support events.
@@ -42,6 +51,8 @@ class Timer {
 	#elseif java
 	#elseif java
 	private var timer:java.util.Timer;
 	private var timer:java.util.Timer;
 	private var task:java.util.TimerTask;
 	private var task:java.util.TimerTask;
+	#elseif (target.asys)
+	private var native:Native;
 	#else
 	#else
 	private var event:MainLoop.MainEvent;
 	private var event:MainLoop.MainEvent;
 	#end
 	#end
@@ -69,6 +80,8 @@ class Timer {
 		#elseif java
 		#elseif java
 		timer = new java.util.Timer();
 		timer = new java.util.Timer();
 		timer.scheduleAtFixedRate(task = new TimerTask(this), haxe.Int64.ofInt(time_ms), haxe.Int64.ofInt(time_ms));
 		timer.scheduleAtFixedRate(task = new TimerTask(this), haxe.Int64.ofInt(time_ms), haxe.Int64.ofInt(time_ms));
+		#elseif (target.asys)
+		native = new Native(time_ms, () -> run());
 		#else
 		#else
 		var dt = time_ms / 1000;
 		var dt = time_ms / 1000;
 		event = MainLoop.add(function() {
 		event = MainLoop.add(function() {
@@ -103,6 +116,8 @@ class Timer {
 			timer = null;
 			timer = null;
 		}
 		}
 		task = null;
 		task = null;
+		#elseif (target.asys)
+		native.close((err) -> {});
 		#else
 		#else
 		if (event != null) {
 		if (event != null) {
 			event.stop();
 			event.stop();
@@ -121,12 +136,24 @@ class Timer {
 		var timer = new haxe.Timer(1000); // 1000ms delay
 		var timer = new haxe.Timer(1000); // 1000ms delay
 		timer.run = function() { ... }
 		timer.run = function() { ... }
 		```
 		```
-		
+
 		Once bound, it can still be rebound to different functions until `this`
 		Once bound, it can still be rebound to different functions until `this`
 		Timer is stopped through a call to `this.stop`.
 		Timer is stopped through a call to `this.stop`.
 	**/
 	**/
 	public dynamic function run() {}
 	public dynamic function run() {}
 
 
+	public function ref() {
+		#if (target.asys)
+		native.ref();
+		#end
+	}
+
+	public function unref() {
+		#if (target.asys)
+		native.unref();
+		#end
+	}
+
 	/**
 	/**
 		Invokes `f` after `time_ms` milliseconds.
 		Invokes `f` after `time_ms` milliseconds.
 
 

+ 2 - 2
std/haxe/async/Defer.hx

@@ -5,7 +5,7 @@ class Defer {
 		Schedules the given function to run during the next processing tick.
 		Schedules the given function to run during the next processing tick.
 		Convenience shortcut for `Timer.delay(f, 0)`.
 		Convenience shortcut for `Timer.delay(f, 0)`.
 	**/
 	**/
-	public static inline function nextTick(f:() -> Void):asys.Timer {
-		return asys.Timer.delay(f, 0);
+	public static inline function nextTick(f:() -> Void):haxe.Timer {
+		return haxe.Timer.delay(f, 0);
 	}
 	}
 }
 }

+ 1 - 1
std/haxe/io/Readable.hx

@@ -62,7 +62,7 @@ class Readable implements IReadable {
 	public var done(default, null) = false;
 	public var done(default, null) = false;
 
 
 	var buffer = new List<Bytes>();
 	var buffer = new List<Bytes>();
-	var deferred:asys.Timer;
+	var deferred:haxe.Timer;
 	var willEof = false;
 	var willEof = false;
 
 
 	@:dox(show)
 	@:dox(show)

+ 1 - 1
std/haxe/io/Writable.hx

@@ -23,7 +23,7 @@ class Writable implements IWritable {
 
 
 	var willDrain = false;
 	var willDrain = false;
 	var willFinish = false;
 	var willFinish = false;
-	var deferred:asys.Timer;
+	var deferred:haxe.Timer;
 	var buffer = new List<Bytes>();
 	var buffer = new List<Bytes>();
 
 
 	// for use by implementing classes
 	// for use by implementing classes

+ 1 - 1
tests/asys/impl/SlowSource.hx

@@ -15,7 +15,7 @@ class SlowSource extends Readable {
 		if (data.length > 0) {
 		if (data.length > 0) {
 			var nextChunk = data.shift();
 			var nextChunk = data.shift();
 			var nextEof = data.length == 0;
 			var nextEof = data.length == 0;
-			asys.Timer.delay(() -> asyncRead([nextChunk], nextEof), 10);
+			haxe.Timer.delay(() -> asyncRead([nextChunk], nextEof), 10);
 		}
 		}
 		return None;
 		return None;
 	}
 	}