Skip to content

Commit 7f0318e

Browse files
committed
ImageLoader: immediately stops loaders when changing source, instead of waiting for validation, and consolidates cleanup code (closes #1188)
1 parent aaf6b80 commit 7f0318e

File tree

1 file changed

+53
-63
lines changed

1 file changed

+53
-63
lines changed

source/feathers/controls/ImageLoader.as

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ package feathers.controls
408408
{
409409
this.image.visible = false;
410410
}
411+
this.cleanupLoaders(true);
411412
this._lastURL = null;
412413
if(this._source is Texture)
413414
{
@@ -1520,36 +1521,6 @@ package feathers.controls
15201521
else if(sourceURL != this._lastURL)
15211522
{
15221523
this._lastURL = sourceURL;
1523-
1524-
if(this.urlLoader)
1525-
{
1526-
this.urlLoader.removeEventListener(flash.events.Event.COMPLETE, rawDataLoader_completeHandler);
1527-
this.urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, rawDataLoader_ioErrorHandler);
1528-
this.urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, rawDataLoader_securityErrorHandler);
1529-
try
1530-
{
1531-
this.urlLoader.close();
1532-
}
1533-
catch(error:Error)
1534-
{
1535-
//no need to do anything in response
1536-
}
1537-
}
1538-
1539-
if(this.loader)
1540-
{
1541-
this.loader.contentLoaderInfo.removeEventListener(flash.events.Event.COMPLETE, loader_completeHandler);
1542-
this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
1543-
this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler);
1544-
try
1545-
{
1546-
this.loader.close();
1547-
}
1548-
catch(error:Error)
1549-
{
1550-
//no need to do anything in response
1551-
}
1552-
}
15531524

15541525
if(this._textureCache && !this._isRestoringTexture && this._textureCache.hasTexture(sourceURL))
15551526
{
@@ -1803,6 +1774,52 @@ package feathers.controls
18031774
this._isTextureOwner = false;
18041775
}
18051776

1777+
/**
1778+
* @private
1779+
*/
1780+
protected function cleanupLoaders(close:Boolean):void
1781+
{
1782+
if(this.urlLoader)
1783+
{
1784+
this.urlLoader.removeEventListener(flash.events.Event.COMPLETE, rawDataLoader_completeHandler);
1785+
this.urlLoader.removeEventListener(ProgressEvent.PROGRESS, rawDataLoader_progressHandler);
1786+
this.urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, rawDataLoader_ioErrorHandler);
1787+
this.urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, rawDataLoader_securityErrorHandler);
1788+
if(close)
1789+
{
1790+
try
1791+
{
1792+
this.urlLoader.close();
1793+
}
1794+
catch(error:Error)
1795+
{
1796+
//no need to do anything in response
1797+
}
1798+
}
1799+
this.urlLoader = null;
1800+
}
1801+
1802+
if(this.loader)
1803+
{
1804+
this.loader.contentLoaderInfo.removeEventListener(flash.events.Event.COMPLETE, loader_completeHandler);
1805+
this.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loader_progressHandler);
1806+
this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
1807+
this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler);
1808+
if(close)
1809+
{
1810+
try
1811+
{
1812+
this.loader.close();
1813+
}
1814+
catch(error:Error)
1815+
{
1816+
//no need to do anything in response
1817+
}
1818+
}
1819+
this.loader = null;
1820+
}
1821+
}
1822+
18061823
/**
18071824
* @private
18081825
*/
@@ -2108,10 +2125,7 @@ package feathers.controls
21082125
protected function loader_completeHandler(event:flash.events.Event):void
21092126
{
21102127
var bitmap:Bitmap = Bitmap(this.loader.content);
2111-
this.loader.contentLoaderInfo.removeEventListener(flash.events.Event.COMPLETE, loader_completeHandler);
2112-
this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
2113-
this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler);
2114-
this.loader = null;
2128+
this.cleanupLoaders(false);
21152129

21162130
var bitmapData:BitmapData = bitmap.bitmapData;
21172131

@@ -2153,12 +2167,7 @@ package feathers.controls
21532167
*/
21542168
protected function loader_ioErrorHandler(event:IOErrorEvent):void
21552169
{
2156-
this.loader.contentLoaderInfo.removeEventListener(flash.events.Event.COMPLETE, loader_completeHandler);
2157-
this.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loader_progressHandler);
2158-
this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
2159-
this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler);
2160-
this.loader = null;
2161-
2170+
this.cleanupLoaders(false);
21622171
this.cleanupTexture();
21632172
this.invalidate(INVALIDATION_FLAG_DATA);
21642173
this.dispatchEventWith(FeathersEventType.ERROR, false, event);
@@ -2170,12 +2179,7 @@ package feathers.controls
21702179
*/
21712180
protected function loader_securityErrorHandler(event:SecurityErrorEvent):void
21722181
{
2173-
this.loader.contentLoaderInfo.removeEventListener(flash.events.Event.COMPLETE, loader_completeHandler);
2174-
this.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loader_progressHandler);
2175-
this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loader_ioErrorHandler);
2176-
this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityErrorHandler);
2177-
this.loader = null;
2178-
2182+
this.cleanupLoaders(false);
21792183
this.cleanupTexture();
21802184
this.invalidate(INVALIDATION_FLAG_DATA);
21812185
this.dispatchEventWith(FeathersEventType.ERROR, false, event);
@@ -2188,11 +2192,7 @@ package feathers.controls
21882192
protected function rawDataLoader_completeHandler(event:flash.events.Event):void
21892193
{
21902194
var rawData:ByteArray = ByteArray(this.urlLoader.data);
2191-
this.urlLoader.removeEventListener(flash.events.Event.COMPLETE, rawDataLoader_completeHandler);
2192-
this.urlLoader.removeEventListener(ProgressEvent.PROGRESS, rawDataLoader_progressHandler);
2193-
this.urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, rawDataLoader_ioErrorHandler);
2194-
this.urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, rawDataLoader_securityErrorHandler);
2195-
this.urlLoader = null;
2195+
this.cleanupLoaders(false);
21962196

21972197
//only clear the texture if we're not restoring
21982198
if(!this._isRestoringTexture)
@@ -2226,12 +2226,7 @@ package feathers.controls
22262226
*/
22272227
protected function rawDataLoader_ioErrorHandler(event:ErrorEvent):void
22282228
{
2229-
this.urlLoader.removeEventListener(flash.events.Event.COMPLETE, rawDataLoader_completeHandler);
2230-
this.urlLoader.removeEventListener(ProgressEvent.PROGRESS, rawDataLoader_progressHandler);
2231-
this.urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, rawDataLoader_ioErrorHandler);
2232-
this.urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, rawDataLoader_securityErrorHandler);
2233-
this.urlLoader = null;
2234-
2229+
this.cleanupLoaders(false);
22352230
this.cleanupTexture();
22362231
this.invalidate(INVALIDATION_FLAG_DATA);
22372232
this.dispatchEventWith(FeathersEventType.ERROR, false, event);
@@ -2243,12 +2238,7 @@ package feathers.controls
22432238
*/
22442239
protected function rawDataLoader_securityErrorHandler(event:ErrorEvent):void
22452240
{
2246-
this.urlLoader.removeEventListener(flash.events.Event.COMPLETE, rawDataLoader_completeHandler);
2247-
this.urlLoader.removeEventListener(ProgressEvent.PROGRESS, rawDataLoader_progressHandler);
2248-
this.urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, rawDataLoader_ioErrorHandler);
2249-
this.urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, rawDataLoader_securityErrorHandler);
2250-
this.urlLoader = null;
2251-
2241+
this.cleanupLoaders(false);
22522242
this.cleanupTexture();
22532243
this.invalidate(INVALIDATION_FLAG_DATA);
22542244
this.dispatchEventWith(FeathersEventType.ERROR, false, event);

0 commit comments

Comments
 (0)