added EndOfLoop
This commit is contained in:
parent
f85ff77728
commit
f92d777ee1
24
src/kernel/EndOfLoop.hx
Normal file
24
src/kernel/EndOfLoop.hx
Normal 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;
|
||||
}
|
||||
}
|
@ -228,6 +228,8 @@ class KernelEvents {
|
||||
this.onWebsocketMessageTrigger.trigger({url: event[2], message: event[3], isBinary: event[4]});
|
||||
case "websocket_success":
|
||||
this.onWebsocketSuccessTrigger.trigger({url: event[2], handle: event[3]});
|
||||
case "endofloop":
|
||||
EndOfLoop.run();
|
||||
default:
|
||||
Log.error('Unknown event: $eventName');
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user