Neko and haxe.Timer.delayed ()

As every Haxe developer knows, you can use haxe.Timer.delayed()to delay calling a function for some time. But this feature does not exist at all for Neko. Is there a way to achieve the same results?

+3
source share
4 answers

Be sure to check this out, but

function delayed(f, time) {
   neko.vm.Thread.create(function() {
       neko.Sys.sleep(time);
       f();
   });
}

may be the closest. The only drawback is that the application becomes multithreaded, which can lead to serious problems.

+4
source

I thought about your problem, and I think the best way is to create your own Neko Timer class. I made a Timer class for you:

NekoTimer.hx

package;
import neko.Sys;

    class NekoTimer 
    {
    private static var threadActive:Bool = false;
    private static var timersList:Array<TimerInfo> = new Array<TimerInfo>();
    private static var timerInterval:Float = 0.1;

    public static function addTimer(interval:Int, callMethod:Void->Void):Int
    {
        //setup timer thread if not yet active
        if (!threadActive) setupTimerThread();

        //add the given timer
        return timersList.push(new TimerInfo(interval, callMethod, Sys.time() * 1000)) - 1;
    }

    public static function delTimer(id:Int):Void
    {
        timersList.splice(id, 1);
    }

    private static function setupTimerThread():Void
    {
        threadActive = true;
        neko.vm.Thread.create(function() {
            while (true) {
                Sys.sleep(timerInterval);
                for (timer in timersList) {
                    if (Sys.time() * 1000 - timer.lastCallTimestamp >= timer.interval) {
                        timer.callMethod();
                        timer.lastCallTimestamp = Sys.time() * 1000;
                    }
                }
            }
        });
    }
}

private class TimerInfo
{
    public var interval:Int;
    public var callMethod:Void->Void;
    public var lastCallTimestamp:Float;

    public function new(interval:Int, callMethod:Void->Void, lastCallTimestamp:Float) {
        this.interval = interval;
        this.callMethod = callMethod;
        this.lastCallTimestamp = lastCallTimestamp;
    }
}

Name it as follows:

package ;

import neko.Lib;

class Main 
{
    private var timerId:Int;

    public function new()
    {
        trace("setting up timer...");
        timerId = NekoTimer.addTimer(5000, timerCallback);
        trace(timerId);

        //idle main app
        while (true) { }
    }

    private function timerCallback():Void
    {
        trace("it now 5 seconds later");
        NekoTimer.delTimer(timerId);
        trace("removed timer");
    }

    //neko constructor
    static function main() 
    {
        new Main();
    }
}

Hope this helps.

: 100 . , .

+1

I also used a class and I found one problem. Since this is not completely real time, he sleeps an interval, calls a function, and sleeps again. Thus, depending on how long the function has been running, it ticks slower or faster.

I solved this by replacing line 39 as follows:

//timer.lastCallTimestamp = Sys.time() * 1000;
timer.lastCallTimestamp = timer.lastCallTimestamp + timer.interval;
+1
source

Yes, I don’t know anything except what you mentioned in your first answer. On Linux, you can use SIGALARM - but it does not look trivial, 100 percent pure C code, and you need to handle it with great care to avoid a virtual machine crash.

0
source

Source: https://habr.com/ru/post/1698584/


All Articles