2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const Preloader = /** @constructor */ function () { // eslint-disable-line no-unused-vars
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									const loadXHR = function (resolve, reject, file, tracker, attempts) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										const xhr = new XMLHttpRequest();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										tracker[file] = {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											total: 0,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											loaded: 0,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											final: false,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										xhr.onerror = function () {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (attempts <= 1) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												reject(new Error(`Failed loading file '${file}'`));
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											} else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												setTimeout(function () {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
													loadXHR(resolve, reject, file, tracker, attempts - 1);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												}, 1000);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										xhr.onabort = function () {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tracker[file].final = true;
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											reject(new Error(`Loading file '${file}' was aborted.`));
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										xhr.onloadstart = function (ev) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tracker[file].total = ev.total;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tracker[file].loaded = ev.loaded;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										xhr.onprogress = function (ev) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tracker[file].loaded = ev.loaded;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											tracker[file].total = ev.total;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										xhr.onload = function () {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (xhr.status >= 400) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												if (xhr.status < 500 || attempts <= 1) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
													reject(new Error(`Failed loading file '${file}': ${xhr.statusText}`));
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
													xhr.abort();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												} else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
													setTimeout(function () {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
														loadXHR(resolve, reject, file, tracker, attempts - 1);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
													}, 1000);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											} else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												tracker[file].final = true;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												resolve(xhr);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										// Make request.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										xhr.open('GET', file);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (!file.endsWith('.js')) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											xhr.responseType = 'arraybuffer';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										xhr.send();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									const DOWNLOAD_ATTEMPTS_MAX = 4;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									const loadingFiles = {};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									const lastProgress = { loaded: 0, total: 0 };
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									let progressFunc = null;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									const animateProgress = function () {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										let loaded = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										let total = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										let totalIsValid = true;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										let progressIsFinal = true;
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										Object.keys(loadingFiles).forEach(function (file) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											const stat = loadingFiles[file];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (!stat.final) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												progressIsFinal = false;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											if (!totalIsValid || stat.total === 0) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												totalIsValid = false;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												total = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											} else {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												total += stat.total;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											loaded += stat.loaded;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (loaded !== lastProgress.loaded || total !== lastProgress.total) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											lastProgress.loaded = loaded;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											lastProgress.total = total;
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											if (typeof progressFunc === 'function') {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												progressFunc(loaded, total);
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											}
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										if (!progressIsFinal) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											requestAnimationFrame(animateProgress);
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									this.animateProgress = animateProgress;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									this.setProgressFunc = function (callback) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										progressFunc = callback;
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									this.loadPromise = function (file) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return new Promise(function (resolve, reject) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											loadXHR(resolve, reject, file, loadingFiles, DOWNLOAD_ATTEMPTS_MAX);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										});
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									this.preloadedFiles = [];
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									this.preload = function (pathOrBuffer, destPath) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										let buffer = null;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (typeof pathOrBuffer === 'string') {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
											const me = this;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											return this.loadPromise(pathOrBuffer).then(function (xhr) {
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												me.preloadedFiles.push({
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
													path: destPath || pathOrBuffer,
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
													buffer: xhr.response,
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
												});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												return Promise.resolve();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										} else if (pathOrBuffer instanceof ArrayBuffer) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											buffer = new Uint8Array(pathOrBuffer);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										} else if (ArrayBuffer.isView(pathOrBuffer)) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											buffer = new Uint8Array(pathOrBuffer.buffer);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										if (buffer) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											this.preloadedFiles.push({
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
												path: destPath,
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
												buffer: pathOrBuffer,
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
											});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
											return Promise.resolve();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										}
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-23 13:27:13 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										return Promise.reject(new Error('Invalid object for preloading'));
							 | 
						
					
						
							
								
									
										
										
										
											2020-11-19 16:54:07 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 |