resource-loader
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "resource-loader", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "./src/index.js", | ||
@@ -5,0 +5,0 @@ "description": "A generic asset loader, made with web games in mind.", |
@@ -36,2 +36,9 @@ var async = require('async'), | ||
/** | ||
* Loading state of the loader, true if it is currently loading resources. | ||
* | ||
* @member {boolean} | ||
*/ | ||
this.loading = false; | ||
/** | ||
* The percentage of total progress that a single resource represents. | ||
@@ -112,2 +119,3 @@ * | ||
* @alias enqueue | ||
* @param name {string} The name of the resource to load. | ||
* @param url {string} The url for this resource, relative to the baseUrl of this loader. | ||
@@ -121,5 +129,11 @@ * @param [options] {object} The options for the load. | ||
*/ | ||
Loader.prototype.add = Loader.prototype.enqueue = function (url, options) { | ||
this.queue.push(new Resource(this.baseUrl + url, options)); | ||
Loader.prototype.add = Loader.prototype.enqueue = function (name, url, options) { | ||
var resource = new Resource(name, this.baseUrl + url, options); | ||
this.queue.push(resource); | ||
if (this.loading) { | ||
this.loadResource(resource); | ||
} | ||
return this; | ||
@@ -165,2 +179,3 @@ }; | ||
this.progress = 0; | ||
this.loading = false; | ||
}; | ||
@@ -191,6 +206,6 @@ | ||
if (parallel !== false) { | ||
async.each(this.queue, this._boundLoadResource, this._onComplete); | ||
async.each(this.queue, this._boundLoadResource, this._boundOnComplete); | ||
} | ||
else { | ||
async.eachSeries(this.queue, this._boundLoadResource, this._onComplete); | ||
async.eachSeries(this.queue, this._boundLoadResource, this._boundOnComplete); | ||
} | ||
@@ -224,5 +239,9 @@ | ||
Loader.prototype._onComplete = function () { | ||
this.emit('complete'); | ||
this.emit('complete', this.queue.reduce(_mapQueue, {})); | ||
}; | ||
function _mapQueue(obj, res) { | ||
obj[res.name] = res; | ||
} | ||
/** | ||
@@ -242,3 +261,3 @@ * Called each time a resources is loaded. | ||
if (resource.error) { | ||
this.emit('error', resource); | ||
this.emit('error', resource.error, resource); | ||
} | ||
@@ -245,0 +264,0 @@ else { |
@@ -8,2 +8,3 @@ var EventEmitter2 = require('eventemitter2').EventEmitter2; | ||
* @class | ||
* @param name {string} The name of the resource to load. | ||
* @param url {string|string[]} The url for this resource, for audio/video loads you can pass an array of sources. | ||
@@ -16,3 +17,3 @@ * @param [options] {object} The options for the load. | ||
*/ | ||
function Resource(url, options) { | ||
function Resource(name, url, options) { | ||
EventEmitter2.call(this); | ||
@@ -22,3 +23,15 @@ | ||
if (typeof name !== 'string' || typeof url !== 'string') { | ||
throw new Error('Both name and url are required for constructing a resource.'); | ||
} | ||
/** | ||
* The name of this resource. | ||
* | ||
* @member {string} | ||
* @readonly | ||
*/ | ||
this.name = name; | ||
/** | ||
* The url used to load this resource. | ||
@@ -267,10 +280,15 @@ * | ||
xhr.addEventListener('load', this._xhrOnLoad = function () { | ||
if (self.xhrType === Resource.XHR_RESPONSE_TYPE.TEXT) { | ||
self.data = xhr.responseText; | ||
if (xhr.status === 200) { | ||
if (self.xhrType === Resource.XHR_RESPONSE_TYPE.TEXT) { | ||
self.data = xhr.responseText; | ||
} | ||
else if (self.xhrType === Resource.XHR_RESPONSE_TYPE.DOCUMENT) { | ||
self.data = xhr.responseXML || xhr.response; | ||
} | ||
else { | ||
self.data = xhr.response; | ||
} | ||
} | ||
else if (self.xhrType === Resource.XHR_RESPONSE_TYPE.DOCUMENT) { | ||
self.data = xhr.responseXML || xhr.response; | ||
} | ||
else { | ||
self.data = xhr.response; | ||
self.error = new Error(xhr.responseText); | ||
} | ||
@@ -312,4 +330,4 @@ | ||
*/ | ||
Resource.prototype._onError = function (err) { | ||
this.error = err; | ||
Resource.prototype._onError = function (event) { | ||
this.error = new Error('Failed to load element using ' + event.target.nodeName); | ||
this.complete(); | ||
@@ -316,0 +334,0 @@ }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51825
1114