Skip to content

Commit f770aa1

Browse files
author
Lanny McNie
committed
Modified the asynchronous resultFormatter function to no longer use an anonymous function, and also handle error conditions when formatting tag images. This case happens if the loader gets a result that is not an image.
1 parent c212941 commit f770aa1

1 file changed

Lines changed: 50 additions & 25 deletions

File tree

src/preloadjs/loaders/ImageLoader.js

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,31 +125,56 @@ this.createjs = this.createjs || {};
125125
* @private
126126
*/
127127
p._formatResult = function (loader) {
128-
var _this = this;
129-
return function (done) {
130-
var tag = _this._tag;
131-
var URL = window.URL || window.webkitURL;
132-
133-
if (!_this._preferXHR) {
134-
//document.body.removeChild(tag);
135-
} else if (URL) {
136-
var objURL = URL.createObjectURL(loader.getResult(true));
137-
tag.src = objURL;
138-
tag.onload = function () {
139-
URL.revokeObjectURL(_this.src);
140-
}
141-
} else {
142-
tag.src = loader.getItem().src;
143-
}
144-
145-
if (tag.complete) {
146-
done(tag);
147-
} else {
148-
tag.onload = function () {
149-
done(this);
150-
}
151-
}
152-
};
128+
return this._formatImage;
129+
};
130+
131+
/**
132+
* The asynchronous image formatter function. This is required because images have
133+
* a short delay before they are ready.
134+
* @method _formatImage
135+
* @param {Function} successCallback The method to call when the result has finished formatting
136+
* @param {Function} errorCallback The method to call if an error occurs during formatting
137+
* @private
138+
*/
139+
p._formatImage = function (successCallback, errorCallback) {
140+
var tag = this._tag;
141+
var URL = window.URL || window.webkitURL;
142+
143+
if (!this._preferXHR) {
144+
//document.body.removeChild(tag);
145+
} else if (URL) {
146+
var objURL = URL.createObjectURL(this.getResult(true));
147+
tag.src = objURL;
148+
149+
tag.addEventListener("load", this._cleanUpURL, false);
150+
tag.addEventListener("error", this._cleanUpURL, false);
151+
} else {
152+
tag.src = loader.getItem().src;
153+
}
154+
155+
if (tag.complete) {
156+
successCallback(tag);
157+
} else {
158+
tag.addEventListener("load", createjs.proxy(function(event) {
159+
successCallback(this._tag);
160+
}, this), false);
161+
tag.addEventListener("error", createjs.proxy(function(event) {
162+
errorCallback(this._tag);
163+
}, this), false);
164+
}
165+
};
166+
167+
/**
168+
* Clean up the ObjectURL, the tag is done with it. Note that this function is run
169+
* as an event listener without a proxy/closure, as it doesn't require it - so do not
170+
* include any functionality that requires scope without changing it.
171+
* @method _cleanUpURL
172+
* @param event
173+
* @private
174+
*/
175+
p._cleanUpURL = function (event) {
176+
var URL = window.URL || window.webkitURL;
177+
URL.revokeObjectURL(event.target.src);
153178
};
154179

155180
createjs.ImageLoader = createjs.promote(ImageLoader, "AbstractLoader");

0 commit comments

Comments
 (0)