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.as3collections.IList; |
32 | |
import org.as3collections.lists.ArrayList; |
33 | |
import org.vostokframework.domain.loading.LoadError; |
34 | |
import org.vostokframework.domain.loading.LoadErrorType; |
35 | |
import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm; |
36 | |
import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent; |
37 | |
import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmEvent; |
38 | |
|
39 | |
import flash.events.TimerEvent; |
40 | |
import flash.utils.Timer; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class LatencyTimeoutFileLoadingAlgorithm extends FileLoadingAlgorithmBehavior |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
private static const LATENCY_TIMEOUT_ERROR_MESSAGE:String = "Internal VostokFramework Latency Timeout Error: Timeout of $LATENCY_TIMEOUT milliseconds."; |
53 | |
|
54 | |
private var _latencyTimeout:Number; |
55 | |
private var _timer:Timer; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public function LatencyTimeoutFileLoadingAlgorithm(wrapAlgorithm:IFileLoadingAlgorithm, latencyTimeout:Number = 12000) |
65 | |
{ |
66 | 1 | super(wrapAlgorithm); |
67 | |
|
68 | 1 | if (latencyTimeout < 1000) throw new ArgumentError("Argument <latencyTimeout> must be greater than 999. Received: <" + latencyTimeout + ">"); |
69 | |
|
70 | 1 | _latencyTimeout = latencyTimeout; |
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 | super.load(); |
97 | 1 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
override public function stop(): void |
105 | |
{ |
106 | 1 | validateDisposal(); |
107 | 1 | stopTimer(); |
108 | 1 | super.stop(); |
109 | 1 | } |
110 | |
|
111 | |
override protected function doDispose():void |
112 | |
{ |
113 | 1 | removeWrappedAlgorithmListeners(); |
114 | 1 | stopTimer(); |
115 | |
|
116 | 1 | _timer = null; |
117 | 1 | } |
118 | |
|
119 | |
private function addWrappedAlgorithmListeners():void |
120 | |
{ |
121 | 1 | wrappedAlgorithm.addEventListener(FileLoadingAlgorithmEvent.OPEN, openHandler, false, int.MAX_VALUE, true); |
122 | 1 | wrappedAlgorithm.addEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false, int.MAX_VALUE, true); |
123 | 1 | } |
124 | |
|
125 | |
private function createErrorMessage():String |
126 | |
{ |
127 | 1 | return LATENCY_TIMEOUT_ERROR_MESSAGE.replace("$LATENCY_TIMEOUT", _latencyTimeout); |
128 | |
} |
129 | |
|
130 | |
private function createTimer():void |
131 | |
{ |
132 | 1 | _timer = new Timer(_latencyTimeout); |
133 | 1 | } |
134 | |
|
135 | |
private function failedHandler(event:FileLoadingAlgorithmErrorEvent):void |
136 | |
{ |
137 | 0 | validateDisposal(); |
138 | 0 | stopTimer(); |
139 | 0 | } |
140 | |
|
141 | |
private function openHandler(event:FileLoadingAlgorithmEvent):void |
142 | |
{ |
143 | 1 | validateDisposal(); |
144 | 1 | stopTimer(); |
145 | 1 | } |
146 | |
|
147 | |
private function startTimer():void |
148 | |
{ |
149 | 1 | _timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true); |
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 timeout():void |
161 | |
{ |
162 | 1 | validateDisposal(); |
163 | 1 | removeWrappedAlgorithmListeners(); |
164 | 1 | stop(); |
165 | |
|
166 | 1 | var errors:IList = new ArrayList(); |
167 | 1 | var error:LoadError = new LoadError(LoadErrorType.LATENCY_TIMEOUT_ERROR, createErrorMessage()); |
168 | 1 | errors.add(error); |
169 | |
|
170 | 1 | dispatchEvent(new FileLoadingAlgorithmErrorEvent(FileLoadingAlgorithmErrorEvent.FAILED, errors)); |
171 | 1 | } |
172 | |
|
173 | |
private function timerHandler(event:TimerEvent):void |
174 | |
{ |
175 | 1 | stopTimer(); |
176 | 1 | timeout(); |
177 | 1 | } |
178 | |
|
179 | |
private function removeWrappedAlgorithmListeners():void |
180 | |
{ |
181 | 1 | wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmEvent.OPEN, openHandler, false); |
182 | 1 | wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false); |
183 | 1 | } |
184 | |
|
185 | |
} |
186 | |
|
187 | |
} |