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
168 lines (151 loc) · 5.98 KB
/
Copy pathloader.js
File metadata and controls
168 lines (151 loc) · 5.98 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
157
158
159
160
161
162
163
164
165
166
167
168
Object.assign(pc, function () {
'use strict';
/**
* @constructor
* @name pc.ResourceLoader
* @classdesc 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 = {};
};
Object.assign(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.
* @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.
* @param {String} type The type of resource.
* @param {*} data The raw resource data.
* @returns {*} The parsed resource data.
*/
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 its asset data
* or any other asset data.
* @param {pc.Asset} asset The asset to patch.
* @param {pc.AssetRegistry} assets The asset registry.
*/
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.
* @param {String} url The URL of the resource to get from the cache.
* @param {String} type The type of the resource.
* @returns {*} The resource loaded from the cache.
*/
getFromCache: function (url, type) {
if (this._cache[url + type]) {
return this._cache[url + type];
}
},
/**
* @function
* @name pc.ResourceLoader#destroy
* @description Destroys the resource loader.
*/
destroy: function () {
this._handlers = {};
this._requests = {};
this._cache = {};
}
});
return {
ResourceLoader: ResourceLoader
};
}());