-
Notifications
You must be signed in to change notification settings - Fork 4
/
LoadingManager.js
69 lines (54 loc) · 2.01 KB
/
LoadingManager.js
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
/**
* Created by Primoz on 17.3.2016.
* Source: Three.js
*/
/**
* Class representing an observer for Loaders. It's instance may be used to monitor multiple loaders.
* @param {function} onLoad Will be called when the Loader X notifies that the item I finished loading
* @param {function} onProgress Will be called when the Loader X sends progress notification during the loading of item I.
* @param {function} onError Will be called when the Loader X encounters an error during the loading.
* @constructor Stores reference to functions passed to constructor and defines loader notification functions
* @name LoadingManager
*
*/
export class LoadingManager {
constructor(onLoad, onProgress, onError) {
// Store scope for nested functions
var scope = this;
var isLoading = false, itemsLoaded = 0, itemsTotal = 0;
this.onStart = undefined;
// Locally store given callback functions
this.onLoad = onLoad;
this.onProgress = onProgress;
this.onError = onError;
// Loaders should call this function to notify the observer that item started loading
// This function may be called multiple times by same or different loader
this.itemStart = function (url) {
itemsTotal++;
if (isLoading === false && scope.onStart !== undefined) {
scope.onStart(url, itemsLoaded, itemsTotal);
}
isLoading = true;
};
// Loaders should call this function to notify the observer that item finished loading
// This function should be called by the same loader that started the loading
this.itemEnd = function (url) {
itemsLoaded++;
if (scope.onProgress !== undefined) {
scope.onProgress(url, itemsLoaded, itemsTotal);
}
if (itemsLoaded === itemsTotal) {
isLoading = false;
if (scope.onLoad !== undefined) {
scope.onLoad(url, itemsLoaded, itemsTotal);
}
}
};
// Loaders should call this function to notify the observer that an error occurred during the loading
this.itemError = function (url) {
if (scope.onError !== undefined) {
scope.onError(url);
}
};
}
};