| 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.application |
| 30 | |
{ |
| 31 | |
import org.as3coreaddendum.errors.IllegalStateError; |
| 32 | |
import org.vostokframework.VostokFramework; |
| 33 | |
import org.vostokframework.VostokIdentification; |
| 34 | |
import org.vostokframework.application.cache.CachedAssetData; |
| 35 | |
import org.vostokframework.application.cache.CachedAssetDataRepository; |
| 36 | |
import org.vostokframework.application.events.AssetLoadingEvent; |
| 37 | |
import org.vostokframework.application.events.GlobalLoadingEvent; |
| 38 | |
import org.vostokframework.application.events.QueueLoadingEvent; |
| 39 | |
import org.vostokframework.application.monitoring.ILoadingMonitor; |
| 40 | |
import org.vostokframework.application.monitoring.LoadingMonitorRepository; |
| 41 | |
import org.vostokframework.application.monitoring.monitors.CompositeLoadingMonitor; |
| 42 | |
import org.vostokframework.application.monitoring.monitors.LoadingMonitorDispatcher; |
| 43 | |
import org.vostokframework.application.monitoring.monitors.LoadingMonitorWrapper; |
| 44 | |
import org.vostokframework.application.monitoring.monitors.dispatchers.GlobalLoadingMonitorDispatcher; |
| 45 | |
import org.vostokframework.application.services.AssetService; |
| 46 | |
import org.vostokframework.domain.assets.Asset; |
| 47 | |
import org.vostokframework.domain.loading.GlobalLoadingSettings; |
| 48 | |
import org.vostokframework.domain.loading.ILoader; |
| 49 | |
import org.vostokframework.domain.loading.ILoaderFactory; |
| 50 | |
import org.vostokframework.domain.loading.ILoadingSettingsFactory; |
| 51 | |
import org.vostokframework.domain.loading.LoadPriority; |
| 52 | |
import org.vostokframework.domain.loading.LoaderRepository; |
| 53 | |
import org.vostokframework.domain.loading.loaders.VostokLoaderFactory; |
| 54 | |
import org.vostokframework.domain.loading.settings.LoadingSettings; |
| 55 | |
import org.vostokframework.domain.loading.settings.LoadingSettingsFactory; |
| 56 | |
|
| 57 | |
import flash.errors.IllegalOperationError; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public class LoadingContext |
| 65 | |
{ |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private static const GLOBAL_QUEUE_LOADER_ID:String = "GlobalQueueLoader"; |
| 70 | |
private static var _instance:LoadingContext = new LoadingContext(); |
| 71 | |
|
| 72 | |
private var _cachedAssetDataRepository:CachedAssetDataRepository; |
| 73 | |
private var _loadingSettingsFactory:ILoadingSettingsFactory; |
| 74 | |
private var _loadingSettingsRepository:LoadingSettingsRepository; |
| 75 | |
private var _globalLoadingSettings:GlobalLoadingSettings; |
| 76 | |
private var _globalQueueLoader:ILoader; |
| 77 | |
private var _globalQueueLoadingMonitor:ILoadingMonitor; |
| 78 | |
private var _globalQueueLoadingMonitorWrapper:LoadingMonitorWrapper; |
| 79 | |
private var _loaderFactory:ILoaderFactory; |
| 80 | |
private var _loaderRepository:LoaderRepository; |
| 81 | |
private var _loadingMonitorRepository:LoadingMonitorRepository; |
| 82 | |
|
| 83 | |
private var _maxConcurrentQueues:int; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
private static var _created :Boolean = false; |
| 89 | |
|
| 90 | |
{ |
| 91 | |
_created = true; |
| 92 | |
} |
| 93 | |
|
| 94 | |
public function get cachedAssetDataRepository(): CachedAssetDataRepository { return _cachedAssetDataRepository; } |
| 95 | |
|
| 96 | |
public function get loadingSettingsFactory(): ILoadingSettingsFactory { return _loadingSettingsFactory; } |
| 97 | |
|
| 98 | |
public function get loadingSettingsRepository(): LoadingSettingsRepository { return _loadingSettingsRepository; } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public function get globalLoadingSettings(): GlobalLoadingSettings { return _globalLoadingSettings; } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
public function get globalQueueLoader(): ILoader { return _globalQueueLoader; } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public function get globalQueueLoadingMonitor(): ILoadingMonitor { return _globalQueueLoadingMonitorWrapper; } |
| 113 | |
|
| 114 | |
public function get loaderRepository(): LoaderRepository { return _loaderRepository; } |
| 115 | |
|
| 116 | |
public function get loadingMonitorRepository(): LoadingMonitorRepository { return _loadingMonitorRepository; } |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public function get maxConcurrentQueues(): int { return _maxConcurrentQueues; } |
| 122 | |
|
| 123 | |
public function get loaderFactory(): ILoaderFactory { return _loaderFactory; } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
public function LoadingContext() |
| 129 | 1 | { |
| 130 | 1 | if (_created) throw new IllegalOperationError("<LoadingContext> is a singleton class and should be accessed only by its <getInstance> method."); |
| 131 | |
|
| 132 | 1 | _globalLoadingSettings = GlobalLoadingSettings.getInstance(); |
| 133 | 1 | _maxConcurrentQueues = 3; |
| 134 | |
|
| 135 | 1 | _cachedAssetDataRepository = new CachedAssetDataRepository(); |
| 136 | 1 | _loadingSettingsFactory = new LoadingSettingsFactory(); |
| 137 | 1 | _loadingSettingsRepository = new LoadingSettingsRepository(); |
| 138 | 1 | _loaderFactory = new VostokLoaderFactory(loadingSettingsFactory); |
| 139 | 1 | _loaderRepository = new LoaderRepository(); |
| 140 | 1 | _loadingMonitorRepository = new LoadingMonitorRepository(); |
| 141 | |
|
| 142 | 1 | _globalQueueLoadingMonitorWrapper = new LoadingMonitorWrapper(); |
| 143 | |
|
| 144 | 1 | createGlobalQueueLoader(); |
| 145 | 1 | } |
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public static function getInstance(): LoadingContext |
| 153 | |
{ |
| 154 | 1 | return _instance; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
public function setLoadingSettingsFactory(factory:ILoadingSettingsFactory): void |
| 163 | |
{ |
| 164 | 0 | if (!factory) throw new ArgumentError("Argument <factory> must not be null."); |
| 165 | 0 | _loadingSettingsFactory = factory; |
| 166 | 0 | } |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public function setLoadingSettingsRepository(repository:LoadingSettingsRepository): void |
| 174 | |
{ |
| 175 | 1 | if (!repository) throw new ArgumentError("Argument <repository> must not be null."); |
| 176 | |
|
| 177 | 1 | if (_loadingSettingsRepository) _loadingSettingsRepository.clear(); |
| 178 | 1 | _loadingSettingsRepository = repository; |
| 179 | 1 | } |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public function setGlobalQueueLoader(queueLoader:ILoader): void |
| 187 | |
{ |
| 188 | 1 | if (!queueLoader) throw new ArgumentError("Argument <queueLoader> must not be null."); |
| 189 | |
|
| 190 | 1 | var oldGlobalMonitor:ILoadingMonitor = _globalQueueLoadingMonitor; |
| 191 | 1 | var oldGlobalLoader:ILoader = _globalQueueLoader; |
| 192 | |
|
| 193 | 1 | if (oldGlobalLoader) loaderRepository.remove(oldGlobalLoader.identification); |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | 1 | _globalQueueLoader = queueLoader; |
| 217 | 1 | loaderRepository.add(_globalQueueLoader); |
| 218 | |
|
| 219 | |
|
| 220 | 1 | var globalQueueLoadingMonitorDispatcher:LoadingMonitorDispatcher = new GlobalLoadingMonitorDispatcher(_globalQueueLoader.identification.id, _globalQueueLoader.identification.locale); |
| 221 | 1 | var globalQueueLoadingMonitor:ILoadingMonitor = new CompositeLoadingMonitor(_globalQueueLoader, globalQueueLoadingMonitorDispatcher); |
| 222 | |
|
| 223 | 1 | _globalQueueLoadingMonitorWrapper.changeMonitor(globalQueueLoadingMonitor); |
| 224 | 1 | _globalQueueLoadingMonitorWrapper.addEventListener(GlobalLoadingEvent.COMPLETE, globalQueueLoaderCompleteHandler, false, int.MIN_VALUE, true); |
| 225 | 1 | _globalQueueLoadingMonitorWrapper.addEventListener(QueueLoadingEvent.COMPLETE, queueLoaderCompleteHandler, false, int.MIN_VALUE, true); |
| 226 | 1 | _globalQueueLoadingMonitorWrapper.addEventListener(QueueLoadingEvent.CANCELED, queueLoaderCanceledHandler, false, int.MIN_VALUE, true); |
| 227 | 1 | _globalQueueLoadingMonitorWrapper.addEventListener(AssetLoadingEvent.COMPLETE, assetCompleteHandler, false, int.MAX_VALUE, true); |
| 228 | |
|
| 229 | |
|
| 230 | 1 | _globalQueueLoadingMonitor = globalQueueLoadingMonitor; |
| 231 | |
|
| 232 | 1 | if (oldGlobalMonitor) oldGlobalMonitor.dispose(); |
| 233 | |
|
| 234 | 1 | if (oldGlobalLoader) |
| 235 | |
{ |
| 236 | |
|
| 237 | |
|
| 238 | |
try |
| 239 | |
{ |
| 240 | 1 | oldGlobalLoader.cancel(); |
| 241 | |
} |
| 242 | 1 | catch(error:Error) |
| 243 | |
{ |
| 244 | |
|
| 245 | |
} |
| 246 | |
finally |
| 247 | |
{ |
| 248 | 1 | oldGlobalLoader.dispose(); |
| 249 | |
} |
| 250 | |
} |
| 251 | 1 | } |
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public function setCachedAssetDataRepository(repository:CachedAssetDataRepository): void |
| 259 | |
{ |
| 260 | 1 | if (!repository) throw new ArgumentError("Argument <repository> must not be null."); |
| 261 | |
|
| 262 | 1 | if (_cachedAssetDataRepository) _cachedAssetDataRepository.clear(); |
| 263 | 1 | _cachedAssetDataRepository = repository; |
| 264 | 1 | } |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
public function setLoaderFactory(factory:ILoaderFactory): void |
| 272 | |
{ |
| 273 | 0 | if (!factory) throw new ArgumentError("Argument <factory> must not be null."); |
| 274 | 0 | _loaderFactory = factory; |
| 275 | 0 | } |
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
public function setLoaderRepository(repository:LoaderRepository): void |
| 283 | |
{ |
| 284 | 1 | if (!repository) throw new ArgumentError("Argument <repository> must not be null."); |
| 285 | |
|
| 286 | 1 | if (_loaderRepository) _loaderRepository.clear(); |
| 287 | 1 | _loaderRepository = repository; |
| 288 | 1 | } |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
public function setLoadingMonitorRepository(repository:LoadingMonitorRepository): void |
| 296 | |
{ |
| 297 | 1 | if (!repository) throw new ArgumentError("Argument <repository> must not be null."); |
| 298 | |
|
| 299 | 1 | if (_loadingMonitorRepository) _loadingMonitorRepository.clear(); |
| 300 | 1 | _loadingMonitorRepository = repository; |
| 301 | 1 | } |
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
public function setMaxConcurrentQueues(value:int): void |
| 309 | |
{ |
| 310 | 1 | if (value < 1) throw new ArgumentError("Argument <value> must be greater than zero."); |
| 311 | 1 | _maxConcurrentQueues = value; |
| 312 | 1 | } |
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
private function createGlobalQueueLoader(): void |
| 318 | |
{ |
| 319 | 1 | var identification:VostokIdentification = new VostokIdentification(GLOBAL_QUEUE_LOADER_ID, VostokFramework.CROSS_LOCALE_ID); |
| 320 | 1 | var queueLoader:ILoader = loaderFactory.createComposite(identification, loaderRepository, globalLoadingSettings, LoadPriority.MEDIUM, _maxConcurrentQueues); |
| 321 | |
|
| 322 | 1 | setGlobalQueueLoader(queueLoader); |
| 323 | 1 | } |
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
|
| 328 | |
private function globalQueueLoaderCompleteHandler(event:GlobalLoadingEvent):void |
| 329 | |
{ |
| 330 | 1 | trace("#####################################################"); |
| 331 | 1 | trace("LoadingContext::globalQueueLoaderCompleteHandler() - event.queueId: " + event.queueId); |
| 332 | |
|
| 333 | 1 | createGlobalQueueLoader(); |
| 334 | 1 | } |
| 335 | |
|
| 336 | |
private function queueLoaderCanceledHandler(event:QueueLoadingEvent):void |
| 337 | |
{ |
| 338 | 1 | trace("#####################################################"); |
| 339 | 1 | trace("LoadingContext::queueLoaderCanceledHandler() - event.queueId: " + event.queueId); |
| 340 | |
|
| 341 | 1 | var identification:VostokIdentification = new VostokIdentification(event.queueId, event.queueLocale); |
| 342 | |
|
| 343 | 1 | globalQueueLoadingMonitor.removeChild(identification); |
| 344 | |
|
| 345 | 1 | } |
| 346 | |
|
| 347 | |
private function queueLoaderCompleteHandler(event:QueueLoadingEvent):void |
| 348 | |
{ |
| 349 | 1 | trace("#####################################################"); |
| 350 | 1 | trace("LoadingContext::queueLoaderCompleteHandler() - event.queueId: " + event.queueId); |
| 351 | |
|
| 352 | 1 | var identification:VostokIdentification = new VostokIdentification(event.queueId, event.queueLocale); |
| 353 | |
|
| 354 | 1 | globalQueueLoadingMonitor.removeChild(identification); |
| 355 | |
|
| 356 | 1 | var loader:ILoader = globalQueueLoader.getChild(identification); |
| 357 | 1 | globalQueueLoader.removeChild(identification); |
| 358 | 1 | loader.dispose(); |
| 359 | 1 | } |
| 360 | |
|
| 361 | |
private function assetCompleteHandler(event:AssetLoadingEvent):void |
| 362 | |
{ |
| 363 | 1 | trace("#####################################################"); |
| 364 | 1 | trace("LoadingContext::assetCompleteHandler() - event.assetId: " + event.assetId); |
| 365 | |
|
| 366 | |
var errorMessage:String; |
| 367 | |
|
| 368 | 1 | var assetService:AssetService = new AssetService(); |
| 369 | 1 | if (!assetService.assetExists(event.assetId, event.assetLocale)) |
| 370 | |
{ |
| 371 | 0 | errorMessage = "It was expected that the Asset object was found:\n"; |
| 372 | 0 | errorMessage += "<assetId>: " + event.assetId + "\n"; |
| 373 | 0 | errorMessage += "<assetLocale>: " + event.assetLocale; |
| 374 | |
|
| 375 | 0 | throw new IllegalStateError(errorMessage); |
| 376 | |
} |
| 377 | |
|
| 378 | 1 | var asset:Asset = assetService.getAsset(event.assetId, event.assetLocale); |
| 379 | |
|
| 380 | 1 | var settings:LoadingSettings = _loadingSettingsRepository.find(asset); |
| 381 | 1 | if (!settings.cache.allowInternalCache) return; |
| 382 | |
|
| 383 | 1 | var src:String = asset.src; |
| 384 | |
|
| 385 | 1 | var queueLoader:ILoader = globalQueueLoader.getParent(asset.identification); |
| 386 | 1 | if (!queueLoader) |
| 387 | |
{ |
| 388 | 0 | errorMessage = "It was expected that the parent ILoader object for the child ILoader object was found.\n"; |
| 389 | 0 | errorMessage += "child ILoader id: " + asset.identification.toString(); |
| 390 | |
|
| 391 | 0 | throw new IllegalOperationError(errorMessage); |
| 392 | |
} |
| 393 | |
|
| 394 | 1 | var cachedAssetData:CachedAssetData = new CachedAssetData(asset.identification, queueLoader.identification, event.assetData, event.assetType, src); |
| 395 | 1 | cachedAssetDataRepository.add(cachedAssetData); |
| 396 | 1 | } |
| 397 | |
|
| 398 | |
} |
| 399 | |
|
| 400 | |
} |