thx.core
thx.core copied to clipboard
create Timer for neko/cpp when NME/OpenFL are not used
From Hugh on the ML:
I guess the first question is do you want sync or async timer callbacks? Sync is probably the most "cross platform" way.
I think the haxe timer class could easily be extended for non event driver apps by having a function "processTimers()". So like this:
public static function main() {
var count = 10;
Timer.delay(function() trace("Hi"), 1000 );
Timer.processTimers();
}
And a sync processTimers can be created without any threads, by creating a list of pending timers.
public static function processTimers() {
while(true) {
var nextTimer = findNextTimer(); // Get one with least due time
if (nextTimer==null)
break;
var delay = nextTimer.due - stamp();
if (delay>0)
sleep(delay);
nextTimer.run();
}
}
Whether you want to automagically call processTimers() via macro or otherwise is a minor matter, but explicit calling is not that hard.