Coverage Report - org.vostokframework.domain.loading.states.fileloader.algorithms.DelayableFileLoadingAlgorithm
 
Classes in this File Line Coverage Branch Coverage Complexity
DelayableFileLoadingAlgorithm
94%
47/50
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.algorithms
 30  
 {
 31  
         import org.vostokframework.domain.loading.LoadError;
 32  
         import org.vostokframework.domain.loading.LoadErrorType;
 33  
         import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm;
 34  
         import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent;
 35  
 
 36  
         import flash.events.TimerEvent;
 37  
         import flash.utils.Timer;
 38  
 
 39  
         /**
 40  
          * description
 41  
          * 
 42  
          * @author Flávio Silva
 43  
          */
 44  
         public class DelayableFileLoadingAlgorithm extends FileLoadingAlgorithmBehavior
 45  
         {
 46  
                 /**
 47  
                  * @private
 48  
                   */
 49  
                 private var _delayAfterError:Number;
 50  
                 private var _currentDelay:Number;
 51  
                 private var _initialDelay:Number;
 52  
                 private var _timer:Timer;
 53  
                 
 54  
                 /**
 55  
                  * description
 56  
                  * 
 57  
                  * @param loader
 58  
                  * @param request
 59  
                  * @param context
 60  
                  */
 61  
                 public function DelayableFileLoadingAlgorithm(wrapAlgorithm:IFileLoadingAlgorithm, initialDelay:Number = 10, delayAfterError:Number = 5000)
 62  
                 {
 63  1
                         super(wrapAlgorithm);
 64  
                         
 65  1
                         if (initialDelay < 0) throw new ArgumentError("Argument <initialDelay> must not be a negative number. Received: <" + initialDelay + ">");
 66  1
                         if (delayAfterError < 0) throw new ArgumentError("Argument <delayAfterError> must not be a negative number. Received: <" + delayAfterError + ">");
 67  
                         
 68  1
                         _initialDelay = initialDelay;
 69  1
                         _delayAfterError = delayAfterError;
 70  1
                         _currentDelay = _initialDelay;
 71  1
                         createTimer();
 72  1
                         addWrappedAlgorithmListeners();
 73  1
                 }
 74  
                 
 75  
                 /**
 76  
                  * description
 77  
                  * 
 78  
                  * @return
 79  
                   */
 80  
                 override public function cancel(): void
 81  
                 {
 82  1
                         validateDisposal();
 83  1
                         stopTimer();
 84  1
                         super.cancel();
 85  1
                 }
 86  
                 
 87  
                 /**
 88  
                  * description
 89  
                  * 
 90  
                  * @return
 91  
                   */
 92  
                 override public function load(): void
 93  
                 {
 94  1
                         validateDisposal();
 95  1
                         startTimer();
 96  1
                 }
 97  
                 
 98  
                 /**
 99  
                  * description
 100  
                  * 
 101  
                  * @return
 102  
                   */
 103  
                 override public function stop(): void
 104  
                 {
 105  1
                         validateDisposal();
 106  1
                         stopTimer();
 107  1
                         super.stop();
 108  1
                 }
 109  
                 
 110  
                 override protected function doDispose():void
 111  
                 {
 112  1
                         removeWrappedAlgorithmListeners();
 113  1
                         stopTimer();
 114  
                         
 115  1
                         _timer = null;
 116  1
                 }
 117  
                 
 118  
                 private function addWrappedAlgorithmListeners():void
 119  
                 {
 120  1
                         wrappedAlgorithm.addEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false, int.MAX_VALUE, true);
 121  1
                 }
 122  
                 
 123  
                 private function createTimer():void
 124  
                 {
 125  1
                         _timer = new Timer(_initialDelay);
 126  1
                 }
 127  
                 
 128  
                 private function failedHandler(event:FileLoadingAlgorithmErrorEvent):void
 129  
                 {
 130  1
                         validateDisposal();
 131  1
                         stopTimer();
 132  
                         
 133  1
                         _currentDelay = _delayAfterError;
 134  
                         
 135  1
                         if (event.errors && !event.errors.isEmpty())
 136  
                         {
 137  0
                                 var lastError:LoadError = event.errors.getAt(event.errors.size() - 1);
 138  
                                 
 139  0
                                 if (lastError.type.equals(LoadErrorType.LATENCY_TIMEOUT_ERROR))
 140  
                                 {
 141  0
                                         _currentDelay = _initialDelay;
 142  
                                 }
 143  
                         }
 144  1
                 }
 145  
                 
 146  
                 private function startTimer():void
 147  
                 {
 148  1
                         _timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
 149  1
                         _timer.delay = _currentDelay;
 150  1
                         _timer.start();
 151  1
                 }
 152  
                 
 153  
                 private function stopTimer():void
 154  
                 {
 155  1
                         _timer.stop();
 156  1
                         _timer.reset();
 157  1
                         _timer.removeEventListener(TimerEvent.TIMER, timerHandler, false);
 158  1
                 }
 159  
                 
 160  
                 private function timerHandler(event:TimerEvent):void
 161  
                 {
 162  1
                         stopTimer();
 163  1
                         super.load();
 164  1
                 }
 165  
                 
 166  
                 private function removeWrappedAlgorithmListeners():void
 167  
                 {
 168  1
                         wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false);
 169  1
                 }
 170  
                 
 171  
         }
 172  
 
 173  
 }