Coverage Report - org.vostokframework.domain.loading.states.fileloader.LoadingFileLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadingFileLoader
100%
51/51
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2011 (c) Flávio Silva, flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  1
 package org.vostokframework.domain.loading.states.fileloader
 30  
 {
 31  
         import org.vostokframework.domain.loading.ILoaderStateTransition;
 32  
         import org.vostokframework.domain.loading.events.LoaderErrorEvent;
 33  
         import org.vostokframework.domain.loading.events.LoaderEvent;
 34  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent;
 35  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmEvent;
 36  
 
 37  
         import flash.events.ProgressEvent;
 38  
 
 39  
         /**
 40  
          * description
 41  
          * 
 42  
          * @author Flávio Silva
 43  
          */
 44  
         public class LoadingFileLoader extends FileLoaderState
 45  
         {
 46  
                 
 47  
                 override public function get isLoading():Boolean { return true; }
 48  
                 
 49  
                 override public function get openedConnections():int { return 1; }
 50  
                 
 51  
                 //TODO:note: explain that this object starts its logic as soon as it is instanciated
 52  
                 /**
 53  
                  * description
 54  
                  * 
 55  
                  * @param name
 56  
                  * @param ordinal
 57  
                  */
 58  
                 public function LoadingFileLoader(loader:ILoaderStateTransition, algorithm:IFileLoadingAlgorithm)
 59  
                 {
 60  1
                         super(algorithm);
 61  
                         
 62  1
                         setLoader(loader);
 63  1
                         _load();
 64  1
                 }
 65  
                 
 66  
                 override public function load():void
 67  
                 {
 68  
                         //do nothing
 69  1
                 }
 70  
                 
 71  
                 override protected function doDispose():void
 72  
                 {
 73  1
                         removeAlgorithmListeners();
 74  1
                 }
 75  
                 
 76  
                 private function addAlgorithmListeners():void
 77  
                 {
 78  1
                         algorithm.addEventListener(FileLoadingAlgorithmEvent.COMPLETE, completeHandler, false, 0, true);
 79  1
                         algorithm.addEventListener(FileLoadingAlgorithmEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
 80  1
                         algorithm.addEventListener(FileLoadingAlgorithmEvent.INIT, initHandler, false, 0, true);
 81  1
                         algorithm.addEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false, 0, true);
 82  1
                         algorithm.addEventListener(FileLoadingAlgorithmEvent.OPEN, openHandler, false, 0, true);
 83  1
                         algorithm.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
 84  1
                 }
 85  
                 
 86  
                 private function removeAlgorithmListeners():void
 87  
                 {
 88  1
                         algorithm.removeEventListener(FileLoadingAlgorithmEvent.COMPLETE, completeHandler, false);
 89  1
                         algorithm.removeEventListener(FileLoadingAlgorithmEvent.HTTP_STATUS, httpStatusHandler, false);
 90  1
                         algorithm.removeEventListener(FileLoadingAlgorithmEvent.INIT, initHandler, false);
 91  1
                         algorithm.removeEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false);
 92  1
                         algorithm.removeEventListener(FileLoadingAlgorithmEvent.OPEN, openHandler, false);
 93  1
                         algorithm.removeEventListener(ProgressEvent.PROGRESS, progressHandler, false);
 94  1
                 }
 95  
                 
 96  
                 private function _load():void
 97  
                 {
 98  1
                         validateDisposal();
 99  
                         
 100  1
                         addAlgorithmListeners();
 101  1
                         algorithm.load();
 102  1
                 }
 103  
                 
 104  
                 ////////////////////////////////////////////////////////////////////////
 105  
                 ////////////////////////// ALGORITHM LISTENERS /////////////////////////
 106  
                 ////////////////////////////////////////////////////////////////////////
 107  
                 
 108  
                 private function completeHandler(event:FileLoadingAlgorithmEvent):void
 109  
                 {
 110  1
                         validateDisposal();
 111  1
                         removeAlgorithmListeners();
 112  
                         
 113  1
                         loader.setState(new CompleteFileLoader(loader, algorithm));
 114  1
                         loader.dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE, event.data));
 115  1
                         dispose();
 116  1
                 }
 117  
                 
 118  
                 private function failedHandler(event:FileLoadingAlgorithmErrorEvent):void
 119  
                 {
 120  1
                         validateDisposal();
 121  1
                         removeAlgorithmListeners();
 122  
                         
 123  1
                         loader.setState(new FailedFileLoader(loader, algorithm));
 124  1
                         loader.dispatchEvent(new LoaderErrorEvent(LoaderErrorEvent.FAILED, event.errors));
 125  1
                         dispose();
 126  1
                 }
 127  
                 
 128  
                 private function httpStatusHandler(event:FileLoadingAlgorithmEvent):void
 129  
                 {
 130  1
                         validateDisposal();
 131  
                         
 132  1
                         var $event:LoaderEvent = new LoaderEvent(LoaderEvent.HTTP_STATUS);
 133  1
                         $event.httpStatus = event.httpStatus;
 134  
                         
 135  1
                         loader.dispatchEvent($event);
 136  1
                 }
 137  
                 
 138  
                 private function initHandler(event:FileLoadingAlgorithmEvent):void
 139  
                 {
 140  1
                         validateDisposal();
 141  1
                         loader.dispatchEvent(new LoaderEvent(LoaderEvent.INIT, event.data));
 142  1
                 }
 143  
                 
 144  
                 private function openHandler(event:FileLoadingAlgorithmEvent):void
 145  
                 {
 146  1
                         validateDisposal();
 147  1
                         loader.dispatchEvent(new LoaderEvent(LoaderEvent.OPEN, event.data, event.latency));
 148  1
                 }
 149  
                 //TODO:pensar sobre criar LoaderEvent.PROGRESS
 150  
                 //ja esta redisparando aqui
 151  
                 //nao muda muito criar um novo evento e a clonagem interna do player para redisparar
 152  
                 private function progressHandler(event:ProgressEvent):void
 153  
                 {
 154  1
                         loader.dispatchEvent(event);
 155  1
                 }
 156  
                 
 157  
         }
 158  
 
 159  
 }