Simple load manager with cache


/ Published in: ActionScript 3
Save to your folder(s)

Simple manager that loads display objects via single method call and caches loaded objects


Copy this code and paste it in your HTML
  1. package {
  2. import flash.display.Loader;
  3. import flash.display.LoaderInfo;
  4. import flash.events.Event;
  5. import flash.events.IOErrorEvent;
  6. import flash.net.URLRequest;
  7. import flash.utils.Dictionary;
  8.  
  9. /**
  10.  * The simple load manager for multiple loaders.
  11.  * It doesn't handle progress event but caches each loaded content which is shared between clients
  12.  * Content should be allowed by flash player security
  13.  */
  14. public class SharedLoadManager
  15. {
  16. private static var _cache:Dictionary = new Dictionary;
  17. private static var _freeLoaders:Vector.<Loader>;
  18. private static var _activeLoaders:Dictionary = new Dictionary();
  19. private var _pendingHandlers:Dictionary = new Dictionary();
  20.  
  21. /**
  22. * start load display object
  23. * @param uri
  24. * @param loadHandler function called when the load complete,
  25. * if the object with such uri already have been loaded the function called immediately
  26. * the function takes one parameter of type Object
  27. * (which is a loaded content) on success or null on load error
  28. */
  29. public function load(uri:String,loadHandler:Function):void
  30. {
  31. if(_cache[uri] != null)
  32. {
  33. loadHandler(_cache[uri]);
  34. return;
  35. }
  36.  
  37. if(_pendingHandlers[uri] == null)
  38. {
  39. _pendingHandlers[uri] = new Vector.<Function>;
  40. }
  41. _pendingHandlers[uri].push(loadHandler);
  42.  
  43. if(_activeLoaders[uri] == null)
  44. {
  45. var loader:Loader = getLoader();
  46. _activeLoaders[loader] = uri;
  47. loader.load(new URLRequest(uri));
  48. }
  49. }
  50.  
  51. /**
  52. * cancel the loading which was previously initiated by the load() method
  53. * @param uri
  54. * @param loadHandler
  55. */
  56. public function cancel(uri:String, loadHandler:Function):void
  57. {
  58. if(_pendingHandlers[uri] != null)
  59. {
  60. for (var i:int=0;i<_pendingHandlers[uri].length;i++)
  61. {
  62. if(_pendingHandlers[uri][i] == loadHandler)
  63. {
  64. _pendingHandlers[uri].splice(i,1);
  65. break;
  66. }
  67. }
  68. if(_pendingHandlers[uri].length == 0)
  69. {
  70. _activeLoaders[uri].close();
  71. _activeLoaders[uri] = null;
  72. delete _activeLoaders[uri];
  73. }
  74. }
  75. }
  76.  
  77. /**
  78. * remove cached objects and free loaders
  79. * @param disposeLoaders if false than loaders doesn't disposed
  80. */
  81. public function clearCache(disposeLoaders:Boolean = true):void
  82. {
  83. for (var uri:String in _cache)
  84. {
  85. _cache[uri] = null;
  86. delete _cache[uri];
  87. }
  88. if(disposeLoaders && _freeLoaders != null)
  89. {
  90. while(_freeLoaders.length)
  91. {
  92. _freeLoaders.shift();
  93. }
  94. }
  95. }
  96.  
  97. private function getLoader():Loader
  98. {
  99. if(_freeLoaders != null && _freeLoaders.length > 0)
  100. {
  101. return _freeLoaders.shift();
  102. }
  103. var loader:Loader = new Loader();
  104. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoadHandler);
  105. loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorLoadHandler);
  106. return loader;
  107. }
  108.  
  109. private function errorLoadHandler(event:IOErrorEvent):void
  110. {
  111. handleResult(LoaderInfo(event.target));
  112. }
  113.  
  114. private function completeLoadHandler(event:Event):void
  115. {
  116. handleResult(LoaderInfo(event.target));
  117. }
  118.  
  119. private function handleResult(loaderInfo:LoaderInfo):void
  120. {
  121. var uri:String = _activeLoaders[loaderInfo.loader];
  122. var handlers:Vector.<Function> = _pendingHandlers[uri];
  123. if(handlers != null)
  124. {
  125. while(handlers.length)
  126. {
  127. var handler:Function = handlers.shift();
  128. handler(loaderInfo.content);
  129. }
  130. _pendingHandlers[uri] = null;
  131. delete _pendingHandlers[uri];
  132. }
  133. if(loaderInfo.content)
  134. {
  135. _cache[uri] = loaderInfo.content;
  136. loaderInfo.loader.unload();
  137. }
  138. _activeLoaders[loaderInfo.loader] = null;
  139. delete _activeLoaders[loaderInfo.loader];
  140. if(_freeLoaders == null)
  141. {
  142. _freeLoaders = new Vector.<Loader>;
  143. }
  144. _freeLoaders.push(loaderInfo.loader);
  145. }
  146. }
  147. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.