<commit_before>package lime.app;


import lime.app.Event;
import lime.Assets;

#if (js && html5)
import js.html.Image;
import js.html.SpanElement;
import js.Browser;
import lime.net.URLLoader;
import lime.net.URLRequest;
#elseif flash
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.ProgressEvent;
import flash.Lib;
#end


class Preloader #if flash extends Sprite #end {
	
	
	public var complete:Bool;
	public var onComplete = new Event<Void->Void> ();
	public var onProgress = new Event<Int->Int->Void> ();
	
	#if (js && html5)
	public static var images = new Map<String, Image> ();
	public static var loaders = new Map<String, URLLoader> ();
	private var loaded = 0;
	private var total = 0;
	#end
	
	
	public function new () {
		
		#if flash
			
			super ();
			
		#end
		
		onProgress.add (update);
		
	}
	
	
	public function create (config:Config):Void {
		
		#if flash
			
			Lib.current.addChild (this);
			
			Lib.current.loaderInfo.addEventListener (flash.events.Event.COMPLETE, loaderInfo_onComplete);
			Lib.current.loaderInfo.addEventListener (flash.events.Event.INIT, loaderInfo_onInit);
			Lib.current.loaderInfo.addEventListener (ProgressEvent.PROGRESS, loaderInfo_onProgress);
			Lib.current.addEventListener (flash.events.Event.ENTER_FRAME, current_onEnter);
			
		#end
		
		#if (!flash && !html5)
			
			start ();
			
		#end
		
	}
	
	
	public function load (urls:Array<String>, types:Array<AssetType>):Void {
		
		#if (js && html5)
			
			var url = null;
			
			for (i in 0...urls.length) {
				
				url = urls[i];
				
				switch (types[i]) {
					
					case IMAGE:
						
						var image = new Image ();
						images.set (url, image);
						image.onload = image_onLoad;
						image.src = url;
						total++;
					
					case BINARY:
						
						var loader = new URLLoader ();
						loader.dataFormat = BINARY;
						loaders.set (url, loader);
						total++;
					
					case TEXT:
						
						var loader = new URLLoader ();
						loaders.set (url, loader);
						total++;
					
					case FONT:
						
						total++;
						loadFont (url);
					
					default:
					
				}
				
			}
			
			for (url in loaders.keys ()) {
				
				var loader = loaders.get (url);
				loader.onComplete.add (loader_onComplete);
				loader.load (new URLRequest (url));
				
			}
			
			if (total == 0) {
				
				start ();
				
			}
			
		#end
		
	}
	
	
	#if (js && html5)
	private function loadFont (font:String):Void {
		
		if (untyped (Browser.document).fonts && untyped (Browser.document).fonts.load) {
			
			untyped (Browser.document).fonts.load ("1em '" + font + "'").then (function (_) {
				
				loaded ++;
				onProgress.dispatch (loaded, total);
				
				if (loaded == total) {
					
					start ();
					
				}
				
			});
			
		} else {
			
			var node:SpanElement = cast Browser.document.createElement ("span");
			node.innerHTML = "giItT1WQy@!-/#";
			var style = node.style;
			style.position = "absolute";
			style.left = "-10000px";
			style.top = "-10000px";
			style.fontSize = "300px";
			style.fontFamily = "sans-serif";
			style.fontVariant = "normal";
			style.fontStyle = "normal";
			style.fontWeight = "normal";
			style.letterSpacing = "0";
			Browser.document.body.appendChild (node);
			
			var width = node.offsetWidth;
			style.fontFamily = "'" + font + "', sans-serif";
			
			var interval:Null<Int> = null;
			var found = false;
			
			var checkFont = function () {
				
				if (node.offsetWidth != width) {
					
					// Test font was still not available yet, try waiting one more interval?
					if (!found) {
						
						found = true;
						return false;
						
					}
					
					loaded ++;
					
					if (interval != null) {
						
						Browser.window.clearInterval (interval);
						
					}
					
					node.parentNode.removeChild (node);
					node = null;
					
					onProgress.dispatch (loaded, total);
					
					if (loaded == total) {
						
						start ();
						
					}
					
					return true;
					
				}
				
				return false;
				
			}
			
			if (!checkFont ()) {
				
				interval = Browser.window.setInterval (checkFont, 50);
				
			}
			
		}
		
	}
	#end
	
	
	private function start ():Void {
		
		complete = true;
		
		#if flash
		if (Lib.current.contains (this)) {
			
			Lib.current.removeChild (this);
			
		}
		#end
		
		onComplete.dispatch ();
		
	}
	
	
	private function update (loaded:Int, total:Int):Void {
		
		
		
	}
	
	
	
	
	// Event Handlers
	
	
	
	
	#if (js && html5)
	private function image_onLoad (_):Void {
		
		loaded++;
		
		onProgress.dispatch (loaded, total);
		
		if (loaded == total) {
			
			start ();
			
		}
		
	}
	
	
	private function loader_onComplete (loader:URLLoader):Void {
		
		loaded++;
		
		onProgress.dispatch (loaded, total);
		
		if (loaded == total) {
			
			start ();
			
		}
		
	}
	#end
	
	
	#if flash
	private function current_onEnter (event:flash.events.Event):Void {
		
		if (!complete && Lib.current.loaderInfo.bytesLoaded == Lib.current.loaderInfo.bytesTotal) {
			
			complete = true;
			onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
			
		}
		
		if (complete) {
			
			Lib.current.removeEventListener (flash.events.Event.ENTER_FRAME, current_onEnter);
			Lib.current.loaderInfo.removeEventListener (flash.events.Event.COMPLETE, loaderInfo_onComplete);
			Lib.current.loaderInfo.removeEventListener (flash.events.Event.INIT, loaderInfo_onInit);
			Lib.current.loaderInfo.removeEventListener (ProgressEvent.PROGRESS, loaderInfo_onProgress);
			
			start ();
			
		}
		
	}
	
	
	private function loaderInfo_onComplete (event:flash.events.Event):Void {
		
		complete = true;
		onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
		
	}
	
	
	private function loaderInfo_onInit (event:flash.events.Event):Void {
		
		onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
		
	}
	
	
	private function loaderInfo_onProgress (event:flash.events.ProgressEvent):Void {
		
		onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
		
	}
	#end
	
	
}
<commit_msg>Fix case of repeated preloader URLs<commit_after>package lime.app;


import lime.app.Event;
import lime.Assets;

#if (js && html5)
import js.html.Image;
import js.html.SpanElement;
import js.Browser;
import lime.net.URLLoader;
import lime.net.URLRequest;
#elseif flash
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.ProgressEvent;
import flash.Lib;
#end


class Preloader #if flash extends Sprite #end {
	
	
	public var complete:Bool;
	public var onComplete = new Event<Void->Void> ();
	public var onProgress = new Event<Int->Int->Void> ();
	
	#if (js && html5)
	public static var images = new Map<String, Image> ();
	public static var loaders = new Map<String, URLLoader> ();
	private var loaded = 0;
	private var total = 0;
	#end
	
	
	public function new () {
		
		#if flash
			
			super ();
			
		#end
		
		onProgress.add (update);
		
	}
	
	
	public function create (config:Config):Void {
		
		#if flash
			
			Lib.current.addChild (this);
			
			Lib.current.loaderInfo.addEventListener (flash.events.Event.COMPLETE, loaderInfo_onComplete);
			Lib.current.loaderInfo.addEventListener (flash.events.Event.INIT, loaderInfo_onInit);
			Lib.current.loaderInfo.addEventListener (ProgressEvent.PROGRESS, loaderInfo_onProgress);
			Lib.current.addEventListener (flash.events.Event.ENTER_FRAME, current_onEnter);
			
		#end
		
		#if (!flash && !html5)
			
			start ();
			
		#end
		
	}
	
	
	public function load (urls:Array<String>, types:Array<AssetType>):Void {
		
		#if (js && html5)
			
			var url = null;
			
			for (i in 0...urls.length) {
				
				url = urls[i];
				
				switch (types[i]) {
					
					case IMAGE:
						
						if (!images.exists (url)) {
							
							var image = new Image ();
							images.set (url, image);
							image.onload = image_onLoad;
							image.src = url;
							total++;
							
						}
					
					case BINARY:
						
						if (!loaders.exists (url)) {
							
							var loader = new URLLoader ();
							loader.dataFormat = BINARY;
							loaders.set (url, loader);
							total++;
							
						}
					
					case TEXT:
						
						if (!loaders.exists (url)) {
							
							var loader = new URLLoader ();
							loaders.set (url, loader);
							total++;
							
						}
					
					case FONT:
						
						total++;
						loadFont (url);
					
					default:
					
				}
				
			}
			
			for (url in loaders.keys ()) {
				
				var loader = loaders.get (url);
				loader.onComplete.add (loader_onComplete);
				loader.load (new URLRequest (url));
				
			}
			
			if (total == 0) {
				
				start ();
				
			}
			
		#end
		
	}
	
	
	#if (js && html5)
	private function loadFont (font:String):Void {
		
		if (untyped (Browser.document).fonts && untyped (Browser.document).fonts.load) {
			
			untyped (Browser.document).fonts.load ("1em '" + font + "'").then (function (_) {
				
				loaded ++;
				onProgress.dispatch (loaded, total);
				
				if (loaded == total) {
					
					start ();
					
				}
				
			});
			
		} else {
			
			var node:SpanElement = cast Browser.document.createElement ("span");
			node.innerHTML = "giItT1WQy@!-/#";
			var style = node.style;
			style.position = "absolute";
			style.left = "-10000px";
			style.top = "-10000px";
			style.fontSize = "300px";
			style.fontFamily = "sans-serif";
			style.fontVariant = "normal";
			style.fontStyle = "normal";
			style.fontWeight = "normal";
			style.letterSpacing = "0";
			Browser.document.body.appendChild (node);
			
			var width = node.offsetWidth;
			style.fontFamily = "'" + font + "', sans-serif";
			
			var interval:Null<Int> = null;
			var found = false;
			
			var checkFont = function () {
				
				if (node.offsetWidth != width) {
					
					// Test font was still not available yet, try waiting one more interval?
					if (!found) {
						
						found = true;
						return false;
						
					}
					
					loaded ++;
					
					if (interval != null) {
						
						Browser.window.clearInterval (interval);
						
					}
					
					node.parentNode.removeChild (node);
					node = null;
					
					onProgress.dispatch (loaded, total);
					
					if (loaded == total) {
						
						start ();
						
					}
					
					return true;
					
				}
				
				return false;
				
			}
			
			if (!checkFont ()) {
				
				interval = Browser.window.setInterval (checkFont, 50);
				
			}
			
		}
		
	}
	#end
	
	
	private function start ():Void {
		
		complete = true;
		
		#if flash
		if (Lib.current.contains (this)) {
			
			Lib.current.removeChild (this);
			
		}
		#end
		
		onComplete.dispatch ();
		
	}
	
	
	private function update (loaded:Int, total:Int):Void {
		
		
		
	}
	
	
	
	
	// Event Handlers
	
	
	
	
	#if (js && html5)
	private function image_onLoad (_):Void {
		
		loaded++;
		
		onProgress.dispatch (loaded, total);
		
		if (loaded == total) {
			
			start ();
			
		}
		
	}
	
	
	private function loader_onComplete (loader:URLLoader):Void {
		
		loaded++;
		
		onProgress.dispatch (loaded, total);
		
		if (loaded == total) {
			
			start ();
			
		}
		
	}
	#end
	
	
	#if flash
	private function current_onEnter (event:flash.events.Event):Void {
		
		if (!complete && Lib.current.loaderInfo.bytesLoaded == Lib.current.loaderInfo.bytesTotal) {
			
			complete = true;
			onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
			
		}
		
		if (complete) {
			
			Lib.current.removeEventListener (flash.events.Event.ENTER_FRAME, current_onEnter);
			Lib.current.loaderInfo.removeEventListener (flash.events.Event.COMPLETE, loaderInfo_onComplete);
			Lib.current.loaderInfo.removeEventListener (flash.events.Event.INIT, loaderInfo_onInit);
			Lib.current.loaderInfo.removeEventListener (ProgressEvent.PROGRESS, loaderInfo_onProgress);
			
			start ();
			
		}
		
	}
	
	
	private function loaderInfo_onComplete (event:flash.events.Event):Void {
		
		complete = true;
		onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
		
	}
	
	
	private function loaderInfo_onInit (event:flash.events.Event):Void {
		
		onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
		
	}
	
	
	private function loaderInfo_onProgress (event:flash.events.ProgressEvent):Void {
		
		onProgress.dispatch (Lib.current.loaderInfo.bytesLoaded, Lib.current.loaderInfo.bytesTotal);
		
	}
	#end
	
	
}

{'fraction_non_alphanumeric': 0.12381867691814832, 'fraction_numerical': 0.004485023226013135, 'mean_word_length': 2.3694009713977335, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '5247753', 'n_tokens_mistral': 5504, 'n_tokens_neox': 3890, 'n_words': 846}