timer in neko

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

timer in neko

Ian Martins
  why isn't there a haxe.Timer for neko?



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: timer in neko

Ian Martins
Ian Martins wrote:
>  why isn't there a haxe.Timer for neko?
>
here is a timer implementation for neko if you want one.


? neko_timer.patch
Index: std/haxe/Timer.hx
===================================================================
RCS file: /cvsroot/haxe/std/haxe/Timer.hx,v
retrieving revision 1.24
diff -u -p -r1.24 Timer.hx
--- std/haxe/Timer.hx 27 Apr 2009 13:52:33 -0000 1.24
+++ std/haxe/Timer.hx 28 Apr 2009 17:10:21 -0000
@@ -25,7 +25,7 @@
 package haxe;
 
 class Timer {
- #if (neko || php)
+ #if (php)
  #else
 
  private var id : Null<Int>;
@@ -33,6 +33,8 @@ class Timer {
  #if js
  private static var arr = new Array<Timer>();
  private var timerId : Int;
+ #elseif neko
+ private var runThread : neko.vm.Thread;
  #end
 
  public function new( time_ms : Int ){
@@ -46,12 +48,17 @@ class Timer {
  id = arr.length;
  arr[id] = this;
  timerId = untyped window.setInterval("haxe.Timer.arr["+id+"].run();",time_ms);
+ #elseif neko
+ var me = this;
+ runThread = neko.vm.Thread.create(function() { me.runLoop(time_ms); });
  #end
  }
 
  public function stop() {
+ #if( php || flash9 || flash || js )
  if( id == null )
  return;
+ #end
  #if flash9
  untyped __global__["flash.utils.clearInterval"](id);
  #elseif flash
@@ -66,6 +73,9 @@ class Timer {
  p--;
  arr = arr.slice(0,p+1);
  }
+ #elseif neko
+ run = function() {};
+ runThread.sendMessage("stop");
  #end
  id = null;
  }
@@ -73,6 +83,27 @@ class Timer {
  public dynamic function run() {
  }
 
+ #if neko
+ private function runLoop(time_ms)
+ {
+  var shouldStop = false;
+  while( !shouldStop )
+  {
+    try
+    {
+      neko.Sys.sleep(time_ms/1000);
+      run();
+
+      var msg = neko.vm.Thread.readMessage(false);
+      if( msg == "stop" )
+ shouldStop = true;
+    }
+    catch( ex:Dynamic )
+    {}
+  }
+ }
+ #end
+
  public static function delay( f : Void -> Void, time_ms : Int ) {
  var t = new haxe.Timer(time_ms);
  t.run = function() {

--
haXe - an open source web programming language
http://haxe.org