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.IIterator; |
32 | |
import org.as3collections.IList; |
33 | |
import org.as3collections.lists.ArrayList; |
34 | |
import org.as3collections.lists.TypedList; |
35 | |
import org.vostokframework.domain.loading.LoadError; |
36 | |
import org.vostokframework.domain.loading.LoadErrorType; |
37 | |
import org.vostokframework.domain.loading.states.fileloader.IFileLoadingAlgorithm; |
38 | |
import org.vostokframework.domain.loading.states.fileloader.algorithms.events.FileLoadingAlgorithmErrorEvent; |
39 | |
|
40 | |
import flash.events.Event; |
41 | |
import flash.events.EventDispatcher; |
42 | |
import flash.events.IEventDispatcher; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public class MaxAttemptsFileLoadingAlgorithm extends FileLoadingAlgorithmBehavior |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
private var _dispatcher:IEventDispatcher; |
55 | |
private var _errors:IList; |
56 | |
private var _maxAttempts:int; |
57 | |
private var _performedAttempts:int; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public function MaxAttemptsFileLoadingAlgorithm(wrapAlgorithm:IFileLoadingAlgorithm, maxAttempts:int = 1) |
67 | |
{ |
68 | 1 | super(wrapAlgorithm); |
69 | |
|
70 | 1 | if (maxAttempts < 1) throw new ArgumentError("Argument <maxAttempts> must be greater than zero. Received: <" + maxAttempts + ">"); |
71 | |
|
72 | 1 | _maxAttempts = maxAttempts; |
73 | 1 | _dispatcher = new EventDispatcher(this); |
74 | 1 | _errors = new TypedList(new ArrayList(), LoadError); |
75 | |
|
76 | 1 | addWrappedAlgorithmListeners(); |
77 | 1 | } |
78 | |
|
79 | |
override public function addEventListener(type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void |
80 | |
{ |
81 | 1 | validateDisposal(); |
82 | |
|
83 | 1 | if (type == FileLoadingAlgorithmErrorEvent.FAILED) |
84 | |
{ |
85 | 1 | _dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference); |
86 | |
} |
87 | |
else |
88 | |
{ |
89 | 1 | super.addEventListener(type, listener, useCapture, priority, useWeakReference); |
90 | |
} |
91 | 1 | } |
92 | |
|
93 | |
override public function dispatchEvent(event : Event) : Boolean |
94 | |
{ |
95 | 1 | validateDisposal(); |
96 | |
|
97 | 1 | if (event.type == FileLoadingAlgorithmErrorEvent.FAILED) |
98 | |
{ |
99 | 1 | return _dispatcher.dispatchEvent(event); |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | return super.dispatchEvent(event); |
104 | |
} |
105 | |
} |
106 | |
|
107 | |
override public function hasEventListener(type : String) : Boolean |
108 | |
{ |
109 | 0 | validateDisposal(); |
110 | |
|
111 | 0 | if (type == FileLoadingAlgorithmErrorEvent.FAILED) |
112 | |
{ |
113 | 0 | return _dispatcher.hasEventListener(type); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 0 | return super.hasEventListener(type); |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
override public function load(): void |
127 | |
{ |
128 | 1 | validateDisposal(); |
129 | |
|
130 | 1 | if (isExaustedAttempts() || hasSomeFatalError()) |
131 | |
{ |
132 | 1 | failed(); |
133 | 1 | return; |
134 | |
} |
135 | |
|
136 | 1 | wrappedAlgorithm.load(); |
137 | 1 | } |
138 | |
|
139 | |
override public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void |
140 | |
{ |
141 | 1 | validateDisposal(); |
142 | |
|
143 | 1 | if (type == FileLoadingAlgorithmErrorEvent.FAILED) |
144 | |
{ |
145 | 1 | _dispatcher.removeEventListener(type, listener, useCapture); |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | 1 | super.removeEventListener(type, listener, useCapture); |
150 | |
} |
151 | 1 | } |
152 | |
|
153 | |
override public function willTrigger(type : String) : Boolean |
154 | |
{ |
155 | 0 | validateDisposal(); |
156 | |
|
157 | 0 | if (type == FileLoadingAlgorithmErrorEvent.FAILED) |
158 | |
{ |
159 | 0 | return _dispatcher.willTrigger(type); |
160 | |
} |
161 | |
else |
162 | |
{ |
163 | 0 | return super.willTrigger(type); |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
override protected function doDispose():void |
168 | |
{ |
169 | 1 | removeWrappedAlgorithmListeners(); |
170 | 1 | } |
171 | |
|
172 | |
private function addWrappedAlgorithmListeners():void |
173 | |
{ |
174 | 1 | wrappedAlgorithm.addEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false, 0, true); |
175 | 1 | } |
176 | |
|
177 | |
private function failed():void |
178 | |
{ |
179 | 1 | validateDisposal(); |
180 | 1 | removeWrappedAlgorithmListeners(); |
181 | |
|
182 | 1 | dispatchEvent(new FileLoadingAlgorithmErrorEvent(FileLoadingAlgorithmErrorEvent.FAILED, _errors)); |
183 | 1 | } |
184 | |
|
185 | |
private function failedHandler(event:FileLoadingAlgorithmErrorEvent):void |
186 | |
{ |
187 | 1 | validateDisposal(); |
188 | |
|
189 | 1 | _errors.addAll(event.errors); |
190 | 1 | _performedAttempts++; |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | 1 | load(); |
207 | 1 | } |
208 | |
|
209 | |
private function hasSomeFatalError():Boolean |
210 | |
{ |
211 | 1 | if (!_errors || _errors.isEmpty()) return false; |
212 | |
|
213 | 1 | var it:IIterator = _errors.iterator(); |
214 | |
var error:LoadError; |
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | 1 | while (it.hasNext()) |
221 | |
{ |
222 | 1 | error = it.next(); |
223 | 1 | if (error.type.equals(LoadErrorType.SECURITY_ERROR) |
224 | 1 | || error.type.equals(LoadErrorType.IO_ERROR)) return true; |
225 | |
} |
226 | |
|
227 | 0 | return false; |
228 | |
} |
229 | |
|
230 | |
private function isExaustedAttempts():Boolean |
231 | |
{ |
232 | 1 | return _performedAttempts >= _maxAttempts; |
233 | |
} |
234 | |
|
235 | |
private function removeWrappedAlgorithmListeners():void |
236 | |
{ |
237 | 1 | wrappedAlgorithm.removeEventListener(FileLoadingAlgorithmErrorEvent.FAILED, failedHandler, false); |
238 | 1 | } |
239 | |
|
240 | |
} |
241 | |
|
242 | |
} |