forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
156 lines (139 loc) · 5.43 KB
/
loader.js
File metadata and controls
156 lines (139 loc) · 5.43 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
pc.extend(pc, function () {
'use strict';
/**
* @name pc.ResourceLoader
* @class Load resource data, potentially from remote sources. Caches resource on load to prevent multiple requests
* Add ResourceHandlers to handle different types of resources
*/
var ResourceLoader = function () {
this._handlers = {};
this._requests = {};
this._cache = {};
};
ResourceLoader.prototype = {
/**
* @function
* @name pc.ResourceLoader#addHandler
* @description Add a handler for a resource type. Handler should support: load(url, callback) and open(url, data).
* Handlers can optionally support patch(asset, assets) to handle dependencies on other assets
* @param {String} type The name of the type that the handler will load
* @param {pc.ResourceHandler} handler An instance of a resource handler supporting load() and open().
* @example
* var loader = new ResourceLoader();
* loader.addHandler("json", new pc.JsonHandler());
*/
addHandler: function (type, handler) {
this._handlers[type] = handler;
handler._loader = this;
},
removeHandler: function (type) {
delete this._handlers[type];
},
getHandler: function (type) {
return this._handlers[type];
},
/**
* @function
* @name pc.ResourceLoader#load
* @description Make a request for a resource from a remote URL. Parse the returned data using the handler for the specified type
* When loaded and parsed use the callback to return an instance of the resource. Handles multiple requests for
* @param {String} url The URL of the resource to load
* @param {String} type The type of resource expected
* @param {Function} callback The callback used when the resource is loaded or an error occurs. Passed (err, resource) where err is null if there are no errors
* @example
* app.loader.load("../path/to/texture.png", "texture", function (err, texture) {
* // use texture here
* });
*/
load: function(url, type, callback, asset) {
var handler = this._handlers[type];
if (!handler) {
var err = "No handler for asset type: " + type;
callback(err);
return;
}
var key = url + type;
if (this._cache[key] !== undefined) {
// in cache
callback(null, this._cache[key]);
} else if (this._requests[key]) {
// existing request
this._requests[key].push(callback);
} else {
// new request
this._requests[key] = [callback];
handler.load(url, function (err, data, extra) {
// make sure key exists because loader
// might have been destroyed by now
if (!this._requests[key])
return;
var i, len = this._requests[key].length;
if (!err) {
var resource = handler.open(url, data, asset);
this._cache[key] = resource;
for (i = 0; i < len; i++)
this._requests[key][i](null, resource, extra);
} else {
for (i = 0; i < len; i++)
this._requests[key][i](err);
}
delete this._requests[key];
}.bind(this), asset);
}
},
/**
* @function
* @name pc.ResourceLoader#open
* @description Convert raw resource data into a resource instance. e.g. take 3D model format JSON and return a pc.Model.
*/
open: function (type, data) {
var handler = this._handlers[type];
if (!handler) {
console.warn("No resource handler found for: " + type);
return data;
}
return handler.open(null, data);
},
/**
* @function
* @name pc.ResourceLoader#patch
* @description Perform any operations on a resource, that requires a dependency on it's asset data or any other asset data
*/
patch: function (asset, assets) {
var handler = this._handlers[asset.type];
if (!handler) {
console.warn("No resource handler found for: " + asset.type);
return;
}
if (handler.patch) {
handler.patch(asset, assets);
}
},
clearCache: function (url, type) {
delete this._cache[url + type];
},
/**
* @function
* @name pc.ResourceLoader#getFromCache
* @description Check cache for resource from a URL. If present return the cached value
*/
getFromCache: function (url, type) {
if (this._cache[url + type]) {
return this._cache[url + type];
}
},
/**
* @function
* @name pc.ResourceLoader#destroy
* @description Destroys resource loader
*/
destroy: function () {
this._handlers = {};
this._requests = {};
this._cache = {};
}
};
return {
ResourceLoader: ResourceLoader
};
}());