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 |
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 | |
|
41 | |
|
42 | |
|
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 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
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 | |
|
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 | |
|
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 | |
|
150 | |
|
151 | |
|
152 | |
private function progressHandler(event:ProgressEvent):void |
153 | |
{ |
154 | 1 | loader.dispatchEvent(event); |
155 | 1 | } |
156 | |
|
157 | |
} |
158 | |
|
159 | |
} |