resource-loader
Advanced tools
Comparing version 1.3.0 to 1.3.1
{ | ||
"name": "resource-loader", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"main": "./src/index.js", | ||
@@ -5,0 +5,0 @@ "description": "A generic asset loader, made with web games in mind.", |
@@ -10,5 +10,4 @@ module.exports = require('./Loader'); | ||
parsing: { | ||
json: require('./middlewares/parsing/json'), | ||
blob: require('./middlewares/parsing/blob') | ||
} | ||
}; |
@@ -9,3 +9,3 @@ var async = require('async'), | ||
* @class | ||
* @param baseUrl {string} The base url for all resources loaded by this loader. | ||
* @param [baseUrl=''] {string} The base url for all resources loaded by this loader. | ||
* @param [concurrency=10] {number} The number of resources to load concurrently. | ||
@@ -61,3 +61,3 @@ */ | ||
/** | ||
* The `loadResource` function bound with this object context. | ||
* The `_loadResource` function bound with this object context. | ||
* | ||
@@ -67,3 +67,3 @@ * @private | ||
*/ | ||
this._boundLoadResource = this.loadResource.bind(this); | ||
this._boundLoadResource = this._loadResource.bind(this); | ||
@@ -79,2 +79,10 @@ /** | ||
/** | ||
* The `_onLoad` function bound with this object context. | ||
* | ||
* @private | ||
* @member {function} | ||
*/ | ||
this._boundOnLoad = this._onLoad.bind(this); | ||
/** | ||
* The resource buffer that fills until `load` is called to start loading resources. | ||
@@ -228,4 +236,9 @@ * | ||
// add base url if this isn't a data url | ||
if (url.indexOf('data:') !== 0) { | ||
url = this.baseUrl + url; | ||
} | ||
// create the store the resource | ||
this.resources[name] = new Resource(name, this.baseUrl + url, options); | ||
this.resources[name] = new Resource(name, url, options); | ||
@@ -334,11 +347,13 @@ if (typeof cb === 'function') { | ||
* @fires progress | ||
* @private | ||
*/ | ||
Loader.prototype.loadResource = function (resource, cb) { | ||
Loader.prototype._loadResource = function (resource, dequeue) { | ||
var self = this; | ||
resource._dequeue = dequeue; | ||
this._runMiddleware(resource, this._beforeMiddleware, function () { | ||
// resource.on('progress', self.emit.bind(self, 'progress')); | ||
resource.once('complete', self._onLoad.bind(self, resource, cb)); | ||
resource.load(); | ||
resource.load(self._boundOnLoad); | ||
}); | ||
@@ -365,3 +380,3 @@ }; | ||
*/ | ||
Loader.prototype._onLoad = function (resource, cb) { | ||
Loader.prototype._onLoad = function (resource) { | ||
this.progress += this._progressChunk; | ||
@@ -378,9 +393,9 @@ | ||
// run middleware, this *must* happen before dequeue so sub-assets get added properly | ||
this._runMiddleware(resource, this._afterMiddleware, function () { | ||
resource.emit('afterMiddleware', resource); | ||
}); | ||
if (cb) { | ||
cb(); | ||
} | ||
}); | ||
// remove this resource from the async queue | ||
resource._dequeue(); | ||
}; | ||
@@ -387,0 +402,0 @@ |
@@ -1,2 +0,3 @@ | ||
var Resource = require('../../Resource'); | ||
var Resource = require('../../Resource'), | ||
b64 = require('../../b64'); | ||
@@ -9,8 +10,25 @@ window.URL = window.URL || window.webkitURL; | ||
return function (resource, next) { | ||
// if this was an XHR load | ||
if (!resource.data) { | ||
return next(); | ||
} | ||
// if this was an XHR load of a blob | ||
if (resource.xhr && resource.xhrType === Resource.XHR_RESPONSE_TYPE.BLOB) { | ||
// if content type says this is an image, then we need to transform the blob into an Image object | ||
if (resource.data.type.indexOf('image') === 0) { | ||
// if there is no blob support we probably got a binary string back | ||
if (!window.Blob || typeof resource.data === 'string') { | ||
var type = resource.xhr.getResponseHeader('content-type'); | ||
// this is an image, convert the binary string into a data url | ||
if (type && type.indexOf('image') === 0) { | ||
resource.data = new Image(); | ||
resource.data.src = 'data:' + type + ';base64,' + b64.encodeBinary(resource.xhr.responseText); | ||
next(); | ||
} | ||
} | ||
// if content type says this is an image, then we should transform the blob into an Image object | ||
else if (resource.data.type.indexOf('image') === 0) { | ||
var src = URL.createObjectURL(resource.data); | ||
resource.blob = resource.data; | ||
resource.data = new Image(); | ||
@@ -17,0 +35,0 @@ resource.data.src = src; |
@@ -88,2 +88,11 @@ var EventEmitter = require('eventemitter3').EventEmitter, | ||
/** | ||
* The `dequeue` method that will be used a storage place for the async queue dequeue method | ||
* used privately by the loader. | ||
* | ||
* @member {function} | ||
* @private | ||
*/ | ||
this._dequeue = null; | ||
/** | ||
* The `complete` function bound to this resource's context. | ||
@@ -182,6 +191,12 @@ * | ||
* @fires start | ||
* @param [callback] {function} Optional callback to call once the resource is loaded. | ||
*/ | ||
Resource.prototype.load = function () { | ||
Resource.prototype.load = function (cb) { | ||
this.emit('start', this); | ||
// if a callback is set, listen for complete event | ||
if (cb) { | ||
this.once('complete', cb); | ||
} | ||
// if unset, determine the value | ||
@@ -278,4 +293,10 @@ if (typeof this.crossOrigin !== 'string') { | ||
// set the responseType | ||
xhr.responseType = this.xhrType; | ||
// load json as text and parse it ourselves. We do this because some browsers | ||
// *cough* safari *cough* can't deal with it. | ||
if (this.xhrType === Resource.XHR_RESPONSE_TYPE.JSON || this.xhrType === Resource.XHR_RESPONSE_TYPE.DOCUMENT) { | ||
xhr.responseType = Resource.XHR_RESPONSE_TYPE.TEXT; | ||
} | ||
else { | ||
xhr.responseType = this.xhrType; | ||
} | ||
@@ -410,8 +431,31 @@ xhr.addEventListener('error', this._boundXhrOnError, false); | ||
if (xhr.status === 200) { | ||
// if text, just return it | ||
if (this.xhrType === Resource.XHR_RESPONSE_TYPE.TEXT) { | ||
this.data = xhr.responseText; | ||
} | ||
// if json, parse into json object | ||
else if (this.xhrType === Resource.XHR_RESPONSE_TYPE.JSON) { | ||
try { | ||
this.data = JSON.parse(xhr.responseText); | ||
} catch(e) { | ||
this.error = new Error('Error trying to parse loaded json:', e); | ||
} | ||
} | ||
// if xml, parse into an xml document or div element | ||
else if (this.xhrType === Resource.XHR_RESPONSE_TYPE.DOCUMENT) { | ||
this.data = xhr.responseXML || xhr.response; | ||
try { | ||
if (window.DOMParser) { | ||
var domparser = new DOMParser(); | ||
this.data = domparser.parseFromString(xhr.responseText, 'text/xml'); | ||
} | ||
else { | ||
var div = document.createElement('div'); | ||
div.innerHTML = xhr.responseText; | ||
this.data = div; | ||
} | ||
} catch (e) { | ||
this.error = new Error('Error trying to parse loaded xml:', e); | ||
} | ||
} | ||
// other types just return the response | ||
else { | ||
@@ -418,0 +462,0 @@ this.data = xhr.response; |
54865
1224