<commit_before>package lime._backend.native;


import lime.app.Application;
import lime.app.Config;
import lime.audio.AudioManager;
import lime.graphics.ConsoleRenderContext;
import lime.graphics.GLRenderContext;
import lime.graphics.RenderContext;
import lime.graphics.Renderer;
import lime.system.System;
import lime.ui.Window;

@:access(lime.app.Application)
@:access(lime.graphics.Renderer)


class NativeApplication {
	
	
	private var keyEventInfo = new KeyEventInfo ();
	private var mouseEventInfo = new MouseEventInfo ();
	private var renderEventInfo = new RenderEventInfo (RENDER);
	private var touchEventInfo = new TouchEventInfo ();
	private var updateEventInfo = new UpdateEventInfo ();
	private var windowEventInfo = new WindowEventInfo ();
	
	public var handle:Dynamic;
	
	private var initialized:Bool;
	private var parent:Application;
	
	
	public function new (parent:Application):Void {
		
		this.parent = parent;
		
		AudioManager.init ();
		
	}
	
	
	public function create (config:Config):Void {
		
		parent.config = config;
		
		handle = lime_application_create (null);
		
		if (config != null) {
			
			var window = new Window (config);
			var renderer = new Renderer (window);
			parent.addWindow (window);
			parent.addRenderer (renderer);
			
		}
		
	}
	
	
	public function exec ():Int {
		
		lime_key_event_manager_register (handleKeyEvent, keyEventInfo);
		lime_mouse_event_manager_register (handleMouseEvent, mouseEventInfo);
		lime_render_event_manager_register (handleRenderEvent, renderEventInfo);
		lime_touch_event_manager_register (handleTouchEvent, touchEventInfo);
		lime_update_event_manager_register (handleUpdateEvent, updateEventInfo);
		lime_window_event_manager_register (handleWindowEvent, windowEventInfo);
		
		// TODO: add parent.init
		
		#if nodejs
		
		lime_application_init (handle);
		
		var prevTime = untyped __js__ ('Date.now ()');
		var eventLoop = function () {
			
			var active = lime_application_update (handle);
			
			if (!active) {
				
				var result = lime_application_quit (handle);
				__cleanup ();
				Sys.exit (result);
				
			}
			
			var time =  untyped __js__ ('Date.now ()');
			if (time - prevTime <= 16) {
				
				untyped setTimeout (eventLoop, 0);
				
			}
			else {
				
				untyped setImmediate (eventLoop);
				
			}
			
			prevTime = time;
			
		}
		
		untyped setImmediate (eventLoop);
		
		#elseif (cpp || neko)
		
		return lime_application_exec (handle);
		
		#else
		
		return 0;
		
		#end<commit_msg>Always use setImmediate on Node.js<commit_after>package lime._backend.native;


import lime.app.Application;
import lime.app.Config;
import lime.audio.AudioManager;
import lime.graphics.ConsoleRenderContext;
import lime.graphics.GLRenderContext;
import lime.graphics.RenderContext;
import lime.graphics.Renderer;
import lime.system.System;
import lime.ui.Window;

@:access(lime.app.Application)
@:access(lime.graphics.Renderer)


class NativeApplication {
	
	
	private var keyEventInfo = new KeyEventInfo ();
	private var mouseEventInfo = new MouseEventInfo ();
	private var renderEventInfo = new RenderEventInfo (RENDER);
	private var touchEventInfo = new TouchEventInfo ();
	private var updateEventInfo = new UpdateEventInfo ();
	private var windowEventInfo = new WindowEventInfo ();
	
	public var handle:Dynamic;
	
	private var initialized:Bool;
	private var parent:Application;
	
	
	public function new (parent:Application):Void {
		
		this.parent = parent;
		
		AudioManager.init ();
		
	}
	
	
	public function create (config:Config):Void {
		
		parent.config = config;
		
		handle = lime_application_create (null);
		
		if (config != null) {
			
			var window = new Window (config);
			var renderer = new Renderer (window);
			parent.addWindow (window);
			parent.addRenderer (renderer);
			
		}
		
	}
	
	
	public function exec ():Int {
		
		lime_key_event_manager_register (handleKeyEvent, keyEventInfo);
		lime_mouse_event_manager_register (handleMouseEvent, mouseEventInfo);
		lime_render_event_manager_register (handleRenderEvent, renderEventInfo);
		lime_touch_event_manager_register (handleTouchEvent, touchEventInfo);
		lime_update_event_manager_register (handleUpdateEvent, updateEventInfo);
		lime_window_event_manager_register (handleWindowEvent, windowEventInfo);
		
		// TODO: add parent.init
		
		#if nodejs
		
		lime_application_init (handle);
		
		var eventLoop = function () {
			
			var active = lime_application_update (handle);
			
			if (!active) {
				
				var result = lime_application_quit (handle);
				__cleanup ();
				Sys.exit (result);
				
			}
			
			untyped setImmediate (eventLoop);
			
		}
		
		untyped setImmediate (eventLoop);
		return 0;
		
		#elseif (cpp || neko)
		
		return lime_application_exec (handle);
		
		#else
		
		return 0;
		
		#end
{'fraction_non_alphanumeric': 0.09201425277719556, 'fraction_numerical': 0.0012575979878432194, 'mean_word_length': 3.934850051706308, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '920492', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1390, 'n_words': 342}