Explorar el Código

auto register default loop on getDefault(), auto stop it when processing finished

ncannasse hace 7 años
padre
commit
2542f915cf
Se han modificado 1 ficheros con 14 adiciones y 12 borrados
  1. 14 12
      std/hl/uv/Loop.hx

+ 14 - 12
std/hl/uv/Loop.hx

@@ -23,21 +23,23 @@ abstract Loop(hl.Abstract<"uv_loop">) {
 	@:hlNative("uv","stop") public function stop() : Void {
 	}
 
-	@:hlNative("uv","default_loop")
 	public static function getDefault() : Loop {
-		return null;
+		var def = default_loop();
+		if( loopEvent == null )
+			loopEvent = haxe.MainLoop.add(function() {
+				// if no more things to process, stop
+				if( def.run(NoWait) == 0 ) {
+					loopEvent.stop();
+					loopEvent = null;
+				}
+			});
+		return def;
 	}
 
-	/**
-		Register the default loop into the main haxe loop.
-		Should be called at least once unless you want to integrate the loop update yourself.
-	**/
-	public static function register() {
-		if( initDone ) return;
-		initDone = true;
-		var def = getDefault();
-		haxe.MainLoop.add(function() def.run(NoWait));
+	@:hlNative("uv", "default_loop") static function default_loop() : Loop {
+		return null;
 	}
-	static var initDone = false;
+
+	static var loopEvent : haxe.MainLoop.MainEvent;
 
 }