1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
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 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class DelayableFileLoadingAlgorithm extends FileLoadingAlgorithmBehavior |
45 | |
{ |
46 | |
|
47 | |
|
48 | |
|
49 | |
private var _delayAfterError:Number; |
50 | |
private var _currentDelay:Number; |
51 | |
private var _initialDelay:Number; |
52 | |
private var _timer:Timer; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
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 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
override public function cancel(): void |
81 | |
{ |
82 | 1 | validateDisposal(); |
83 | 1 | stopTimer(); |
84 | 1 | super.cancel(); |
85 | 1 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
override public function load(): void |
93 | |
{ |
94 | 1 | validateDisposal(); |
95 | 1 | startTimer(); |
96 | 1 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
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 | |
} |