summaryrefslogtreecommitdiffstats
path: root/platform/javascript/js/engine/preloader.js
blob: 8641646e6e1dd5b475651b759f6ec0d43ccaf033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var Preloader = /** @constructor */ function() { // eslint-disable-line no-unused-vars

	const loadXHR = function(resolve, reject, file, tracker, attempts) {
		const xhr = new XMLHttpRequest();
		tracker[file] = {
			total: 0,
			loaded: 0,
			final: false,
		};
		xhr.onerror = function() {
			if (attempts <= 1) {
				reject(new Error("Failed loading file '" + file + "'"));
			} else {
				setTimeout(function () {
					loadXHR(resolve, reject, file, tracker, attempts - 1);
				}, 1000);
			}
		};
		xhr.onabort = function() {
			tracker[file].final = true;
			reject(new Error("Loading file '" + file + "' was aborted."));
		};
		xhr.onloadstart = function(ev) {
			tracker[file].total = ev.total;
			tracker[file].loaded = ev.loaded;
		};
		xhr.onprogress = function(ev) {
			tracker[file].loaded = ev.loaded;
			tracker[file].total = ev.total;
		};
		xhr.onload = function() {
			if (xhr.status >= 400) {
				if (xhr.status < 500 || attempts <= 1) {
					reject(new Error("Failed loading file '" + file + "': " + xhr.statusText));
					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;

	const animateProgress = function() {

		var loaded = 0;
		var total = 0;
		var totalIsValid = true;
		var progressIsFinal = true;

		Object.keys(loadingFiles).forEach(function(file) {
			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;
			if (typeof progressFunc === 'function')
				progressFunc(loaded, total);
		}
		if (!progressIsFinal)
			requestAnimationFrame(animateProgress);
	}

	this.animateProgress = animateProgress;

	this.setProgressFunc = function(callback) {
		progressFunc = callback;
	}


	this.loadPromise = function(file) {
		return new Promise(function(resolve, reject) {
			loadXHR(resolve, reject, file, loadingFiles, DOWNLOAD_ATTEMPTS_MAX);
		});
	}

	this.preloadedFiles = [];
	this.preload = function(pathOrBuffer, destPath) {
		let buffer = null;
		if (typeof pathOrBuffer === 'string') {
			var me = this;
			return this.loadPromise(pathOrBuffer).then(function(xhr) {
				me.preloadedFiles.push({
					path: destPath || pathOrBuffer,
					buffer: xhr.response
				});
				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,
				buffer: pathOrBuffer
			});
			return Promise.resolve();
		} else {
			return Promise.reject(new Error("Invalid object for preloading"));
		}
	};
};