add 'not found' cached image on failure
[potlatch2.git] / net / systemeD / potlatch2 / utils / CachedDataLoader.as
1 package net.systemeD.potlatch2.utils {
2
3         import flash.events.*;
4         import flash.net.*;
5         import flash.utils.ByteArray;
6         import flash.display.BitmapData;
7         import mx.graphics.codec.PNGEncoder;
8
9     import net.systemeD.halcyon.ExtendedURLLoader;
10
11         public class CachedDataLoader {
12
13         private static var allData:Object = {};
14         private static var requestsMade:Object = {};
15         private static var missingImage:ByteArray = null;
16         
17                 public static function loadData(url:String, onLoadHandler:Function = null):ByteArray {
18                     var data:ByteArray = allData[url];
19                     if ( data != null )
20                         return data;
21                     
22                     var requests:Array = requestsMade[url];
23                     if ( requests == null ) {
24                         requests = [];
25                         requestsMade[url] = requests;
26
27                     var loader:ExtendedURLLoader = new ExtendedURLLoader();
28                     loader.info = url;
29                 loader.addEventListener(Event.COMPLETE, imageLoaded);
30                 loader.addEventListener(IOErrorEvent.IO_ERROR, imageLoadFailed);
31                 loader.dataFormat = URLLoaderDataFormat.BINARY;
32                 loader.load(new URLRequest(url));
33                     }
34                     requests.push(onLoadHandler);
35                     
36                     return allData[url];
37                 }
38
39         private static function imageLoaded(event:Event):void {
40             var loader:ExtendedURLLoader = ExtendedURLLoader(event.target);
41             var url:String = loader.info as String;
42             allData[url] = loader.data;
43             dispatchEvents(url);
44         }
45         
46         private static function imageLoadFailed(event:Event):void {
47             var loader:ExtendedURLLoader = ExtendedURLLoader(event.target);
48             var url:String = loader.info as String;
49             
50             allData[url] = getMissingImage();
51             dispatchEvents(url);
52         }
53         
54         private static function dispatchEvents(url:String):void {
55             var requests:Array = requestsMade[url];
56             for each ( var handler:Function in requests ) {
57                 handler(url, allData[url]);
58             }
59             
60             delete requestsMade[url];
61         }
62         
63         private static function getMissingImage():ByteArray {
64             if ( missingImage == null ) {
65                 var bitmap:BitmapData = new BitmapData(16, 16, false);
66                 for ( var i:uint = 0; i < 16; i++ ) {
67                     bitmap.setPixel(i, i, 0xff0000);
68                     bitmap.setPixel(15-i, i, 0xff0000);
69                 }
70                 missingImage = new PNGEncoder().encode(bitmap);
71             }
72             return missingImage;
73         }
74         }
75 }