added EndOfLoop

This commit is contained in:
Djeeberjr 2023-06-07 20:07:03 +02:00
parent f85ff77728
commit f92d777ee1
2 changed files with 26 additions and 0 deletions

24
src/kernel/EndOfLoop.hx Normal file
View File

@ -0,0 +1,24 @@
package kernel;
import cc.OS;
/**
Make sure that a function is called at the end of the current event loop.
Like setTimeout(func, 0) in JavaScript.
**/
class EndOfLoop {
private static var backlog:Array<Void -> Void> = [];
private static var isQueued = false;
public static function endOfLoop(func: Void -> Void) {
backlog.push(func);
if (!isQueued) { OS.queueEvent("endofloop", null); }
}
@:allow(kernel.KernelEvents)
private static function run() {
for (func in backlog) { func(); }
backlog = [];
isQueued = false;
}
}

View File

@ -228,6 +228,8 @@ class KernelEvents {
this.onWebsocketMessageTrigger.trigger({url: event[2], message: event[3], isBinary: event[4]}); this.onWebsocketMessageTrigger.trigger({url: event[2], message: event[3], isBinary: event[4]});
case "websocket_success": case "websocket_success":
this.onWebsocketSuccessTrigger.trigger({url: event[2], handle: event[3]}); this.onWebsocketSuccessTrigger.trigger({url: event[2], handle: event[3]});
case "endofloop":
EndOfLoop.run();
default: default:
Log.error('Unknown event: $eventName'); Log.error('Unknown event: $eventName');
} }