LoadItem

Kind of class: public class
Package:
Inherits from:
Known subclasses:
Version: 02/13/10
Author: Aaron Clinger
Classpath: org.casalib.load.LoadItem
File last modified: Tuesday, 12 July 2011, 14:51:29
► View source▼ Hide source
/*
    CASA Lib for ActionScript 3.0
    Copyright (c) 2011, Aaron Clinger & Contributors of CASA Lib
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    
    - Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    
    - Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    
    - Neither the name of the CASA Lib nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/
package org.casalib.load {
    import flash.errors.IOError;
    import flash.events.Event;
    import flash.events.HTTPStatusEvent;
    import flash.events.IEventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;
    import flash.utils.getTimer;
    import org.casalib.errors.ArguementTypeError;
    import org.casalib.events.LoadEvent;
    import org.casalib.events.RetryEvent;
    import org.casalib.math.Percent;
    import org.casalib.process.Process;
    import org.casalib.util.LoadUtil;
    import org.casalib.util.StringUtil;
    
    
    [Event(name="complete", type="org.casalib.events.LoadEvent")]
    [Event(name="ioError", type="flash.events.IOErrorEvent")]
    [Event(name="open", type="flash.events.Event")]
    [Event(name="progress", type="org.casalib.events.LoadEvent")]
    [Event(name="retry", type="org.casalib.events.RetryEvent")]
    [Event(name="start", type="org.casalib.events.LoadEvent")]
    [Event(name="stop", type="org.casalib.events.LoadEvent")]
    
    /**
        Base class used by load classes. LoadItem is not designed to be used on its own and needs to be extended to function.
        
        @author Aaron Clinger
        @version 02/13/10
    */
    public class LoadItem extends Process {
        protected var _attempts:uint;
        protected var _Bps:int;
        protected var _dispatcher:IEventDispatcher;
        protected var _errrored:Boolean;
        protected var _httpStatus:uint;
        protected var _latency:uint;
        protected var _loaded:Boolean;
        protected var _loadItem:*;
        protected var _preventCache:Boolean;
        protected var _progress:Percent;
        protected var _request:URLRequest;
        protected var _retries:uint;
        protected var _startTime:uint;
        protected var _time:uint;
        protected var _url:String;
        
        
        /**
            Defines the load object and file location.
            
            @param load: The load object.
            @param request: A String or an URLRequest reference to the file you wish to load.
            @throws ArguementTypeError if you pass a type other than a String or an URLRequest to parameter request.
            @throws Error if you try to load an empty String or URLRequest.
        */
        public function LoadItem(load:*, request:*) {
            super();
            
            this._createRequest(request);
            
            this._loadItem = load;
            this._retries  = 2;
            this._Bps      = -1;
            this._progress = new Percent();
        }
        
        /**
            Begins the loading process.
            
            @sends LoadEvent#START - Dispatched when a load is started.
        */
        override public function start():void {
            if (this.loading)
                return;
            
            super.start();
            
            this._loaded    = false;
            this._errrored  = false;
            this._startTime = getTimer();
            this._attempts  = 0;
            this._progress  = new Percent();
            this._Bps       = -1;
            this._time      = 0;
            
            if (this._preventCache) {
                const cache:String = 'casaCache=' + (new Date()).time;
                
                this._request.url = (this._request.url.indexOf('?') == -1) ? this._request.url + '?' + cache : this._request.url + '&' + cache;
            }
            
            this._load();
            
            this.dispatchEvent(this._createDefinedLoadEvent(LoadEvent.START));
        }
        
        /**
            Cancels the currently loading file from completing.
            
            @sends LoadEvent#STOP - Dispatched if the load is stopped during the loading process.
        */
        override public function stop():void {
            if (!this.loading || this.loaded)
                return;
            
            if (this.bytesTotal == this.bytesLoaded && this.bytesLoaded > 0)
                return;
            
            super.stop();
            
            try {
                this._loadItem.close();
            } catch (error:IOError) {}
            
            this.dispatchEvent(this._createDefinedLoadEvent(LoadEvent.STOP));
        }
        
        /**
            Specifies if a random value name/value pair should be appended to the query string to help prevent caching true, or not append false; defaults to false.
        */
        public function get preventCache():Boolean {
            return this._preventCache;
        }
        
        public function set preventCache(cache:Boolean):void {
            this._preventCache = cache;
        }
        
        /**
            The total number of bytes of the requested file.
        */
        public function get bytesTotal():Number {
            return (this._loadItem.bytesTotal == 0 && this.bytesLoaded != 0) ? Number.POSITIVE_INFINITY : this._loadItem.bytesTotal;
        }
        
        /**
            The number of bytes loaded of the requested file.
        */
        public function get bytesLoaded():uint {
            return this._loadItem.bytesLoaded;
        }
        
        /**
            The percent that the requested file has loaded.
        */
        public function get progress():Percent {
            return this._progress.clone();
        }
        
        /**
            The number of additional times the file has attempted to load after {@link #start start} was called.
        */
        public function get attempts():uint {
            return this._attempts;
        }
        
        /**
            The number of additional load retries the class should attempt before failing; defaults to 2 additional retries / 3 total load attempts.
        */
        public function get retries():uint {
            return this._retries;
        }
        
        public function set retries(amount:uint):void {
            this._retries = amount;
        }
        
        /**
            The URLRequest reference to the requested file.
        */
        public function get urlRequest():URLRequest {
            return this._request;
        }
        
        /**
            The URL of the requested file.
        */
        public function get url():String {
            return this._url;
        }
        
        /**
            Determines if the requested file is loading true, or if it isn't currently loading false.
        */
        public function get loading():Boolean {
            return this.running;
        }
        
        /**
            Determines if the requested file has loaded true, or hasn't finished loading false.
        */
        public function get loaded():Boolean {
            return this._loaded;
        }
        
        /**
            Determines if the requested file could not complete because of an error true, or hasn't encountered an error false.
        */
        public function get errored():Boolean {
            return this._errrored;
        }
        
        /**
            The current download speed of the requested file in bytes per second.
        */
        public function get Bps():int {
            return this._Bps;
        }
        
        /**
            The current time duration in milliseconds the load has taken.
        */
        public function get time():uint {
            return this._time;
        }
        
        /**
            The time in milliseconds that the server took to respond.
        */
        public function get latency():uint {
            return this._latency;
        }
        
        /**
            The HTTP status code returned by the server; or 0  if no status has/can been received or the load is a stream.
        */
        public function get httpStatus():uint {
            return this._httpStatus;
        }
        
        override public function destroy():void {
            this._dispatcher.removeEventListener(Event.COMPLETE, this._onComplete);
            this._dispatcher.removeEventListener(Event.OPEN, this._onOpen);
            this._dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, this._onLoadError);
            this._dispatcher.removeEventListener(ProgressEvent.PROGRESS, this._onProgress);
            
            super.destroy();
        }
        
        protected function _initListeners(dispatcher:IEventDispatcher):void {
            this._dispatcher = dispatcher;
            
            this._dispatcher.addEventListener(Event.COMPLETE, this._onComplete, false, 0, true);
            this._dispatcher.addEventListener(Event.OPEN, this._onOpen, false, 0, true);
            this._dispatcher.addEventListener(IOErrorEvent.IO_ERROR, this._onLoadError, false, 0, true);
            this._dispatcher.addEventListener(ProgressEvent.PROGRESS, this._onProgress, false, 0, true);
        }
        
        protected function _load():void {
            this._loadItem.load(this._request);
        }
        
        protected function _createRequest(request:*):void {
            if (request is String) {
                request = StringUtil.trim(request);
                
                if (request == '')
                    throw new Error('Cannot load an empty reference/String');
                
                request = new URLRequest(request);
            } else if (!(request is URLRequest))
                throw new ArguementTypeError('request');
            
            this._request = request;
            this._url     = this._request.url;
        }
        
        /**
            @sends RetryEvent#RETRY - Dispatched if the download attempt failed and the class is going to attempt to download the file again.
            @sends IOErrorEvent#IO_ERROR - Dispatched if requested file cannot be loaded and the download terminates.
        */
        protected function _onLoadError(error:Event):void {
            if (++this._attempts <= this._retries) {
                var retry:RetryEvent = new RetryEvent(RetryEvent.RETRY);
                retry.attempts       = this._attempts;
                
                this.dispatchEvent(retry);
                
                this._load();
            } else {
                this._errrored = true;
                
                super._complete();
                
                this.dispatchEvent(error);
            }
        }
        
        /**
            @sends Event#OPEN - Dispatched when a load operation starts.
        */
        protected function _onOpen(e:Event):void {
            this._latency = getTimer() - this._startTime;
            
            this.dispatchEvent(e);
        }
        
        protected function _onHttpStatus(e:HTTPStatusEvent):void {
            this._httpStatus = e.status;
            
            this.dispatchEvent(e);
        }
        
        protected function _onProgress(progress:ProgressEvent):void {
            this._calculateLoadProgress();
        }
        
        /**
            @sends LoadEvent#PROGRESS - Dispatched as data is received during the download process.
        */
        protected function _calculateLoadProgress():void {
            var currentTime:int = getTimer();
            
            this._Bps  = LoadUtil.calculateBps(this.bytesLoaded, this._startTime, currentTime);
            this._time = currentTime - this._startTime;
            
            this._progress.decimalPercentage = Math.min(this.bytesLoaded / this.bytesTotal, 1);
            
            this.dispatchEvent(this._createDefinedLoadEvent(LoadEvent.PROGRESS));
        }
        
        /**
            @sends LoadEvent#COMPLETE - Dispatched when file has completely loaded.
        */
        protected function _onComplete(complete:Event = null):void {
            this._complete();
            
            this.dispatchEvent(this._createDefinedLoadEvent(LoadEvent.COMPLETE));
        }
        
        protected function _createDefinedLoadEvent(type:String):LoadEvent {
            var loadEvent:LoadEvent = new LoadEvent(type);
            loadEvent.attempts      = this.attempts;
            loadEvent.Bps           = this.Bps;
            loadEvent.bytesLoaded   = this.bytesLoaded;
            loadEvent.bytesTotal    = this.bytesTotal;
            loadEvent.latency       = this.latency;
            loadEvent.progress      = this.progress;
            loadEvent.retries       = this.retries;
            loadEvent.time          = this.time;
            
            return loadEvent;
        }
        
        override protected function _complete():void {
            var currentTime:int              = getTimer();
            this._Bps                        = LoadUtil.calculateBps(this.bytesTotal, this._startTime, currentTime);
            this._time                       = currentTime - this._startTime;
            this._loaded                     = true;
            this._progress.decimalPercentage = 1;
            
            super._complete();
        }
    }
}
Base class used by load classes. LoadItem is not designed to be used on its own and needs to be extended to function.
Events broadcasted to listeners
  • LoadEvent with type: START - Dispatched when a load is started.
  • LoadEvent with type: STOP - Dispatched if the load is stopped during the loading process.
  • RetryEvent with type: RETRY - Dispatched if the download attempt failed and the class is going to attempt to download the file again.
  • IOErrorEvent with type: IO_ERROR - Dispatched if requested file cannot be loaded and the download terminates.
  • Event with type: OPEN - Dispatched when a load operation starts.
  • LoadEvent with type: PROGRESS - Dispatched as data is received during the download process.
  • LoadEvent with type: COMPLETE - Dispatched when file has completely loaded.

Summary

Constructor
  • LoadItem (load = *, request = *)
    • Defines the load object and file location.
Instance properties
  • preventCache : Boolean
    • Specifies if a random value name/value pair should be appended to the query string to help prevent caching true, or not append false; defaults to false.
  • bytesTotal : Number
    • The total number of bytes of the requested file.
  • bytesLoaded : uint
    • The number of bytes loaded of the requested file.
  • progress : Percent
    • The percent that the requested file has loaded.
  • attempts : uint
    • The number of additional times the file has attempted to load after start was called.
  • retries : uint
    • The number of additional load retries the class should attempt before failing; defaults to 2 additional retries / 3 total load attempts.
  • urlRequest : URLRequest
    • The URLRequest reference to the requested file.
  • url : String
    • The URL of the requested file.
  • loading : Boolean
    • Determines if the requested file is loading true, or if it isn't currently loading false.
  • loaded : Boolean
    • Determines if the requested file has loaded true, or hasn't finished loading false.
  • errored : Boolean
    • Determines if the requested file could not complete because of an error true, or hasn't encountered an error false.
  • Bps : int
    • The current download speed of the requested file in bytes per second.
  • time : uint
    • The current time duration in milliseconds the load has taken.
  • latency : uint
    • The time in milliseconds that the server took to respond.
  • httpStatus : uint
    • The HTTP status code returned by the server; or 0 if no status has/can been received or the load is a stream.
Instance methods
  • start () : void
    • Begins the loading process.
  • stop () : void
    • Cancels the currently loading file from completing.
  • destroy () : void

Constructor

LoadItem

function LoadItem(load = *, request = *)

Defines the load object and file location.

Parameters
load :The load object.
request:A String or an URLRequest reference to the file you wish to load.
Throws
  • ArguementTypeError if you pass a type other than a String or an URLRequest to parameter request.
  • Error if you try to load an empty String or URLRequest.

Instance properties

Bps

Bps:int(read)

The current download speed of the requested file in bytes per second.

attempts

attempts:uint(read)

The number of additional times the file has attempted to load after start was called.

bytesLoaded

bytesLoaded:uint(read)

The number of bytes loaded of the requested file.

bytesTotal

bytesTotal:Number(read)

The total number of bytes of the requested file.

errored

errored:Boolean(read)

Determines if the requested file could not complete because of an error true, or hasn't encountered an error false.

httpStatus

httpStatus:uint(read)

The HTTP status code returned by the server; or 0 if no status has/can been received or the load is a stream.

latency

latency:uint(read)

The time in milliseconds that the server took to respond.

loaded

loaded:Boolean(read)

Determines if the requested file has loaded true, or hasn't finished loading false.

loading

loading:Boolean(read)

Determines if the requested file is loading true, or if it isn't currently loading false.

preventCache

preventCache:Boolean(read,write)

Specifies if a random value name/value pair should be appended to the query string to help prevent caching true, or not append false; defaults to false.

progress

progress:Percent(read)

The percent that the requested file has loaded.

retries

retries:uint(read,write)

The number of additional load retries the class should attempt before failing; defaults to 2 additional retries / 3 total load attempts.

time

time:uint(read)

The current time duration in milliseconds the load has taken.

url

url:String(read)

The URL of the requested file.

urlRequest

urlRequest:URLRequest(read)

The URLRequest reference to the requested file.

Instance methods

destroy

override function destroy() : void

Removes any event listeners and stops all internal processes to help allow for prompt garbage collection. Always call before deleting last object pointer.

Overrides

start

override function start() : void

Begins the loading process.

Overrides
Events broadcasted to listeners
  • LoadEvent with type: START - Dispatched when a load is started.

stop

override function stop() : void

Cancels the currently loading file from completing.

Overrides
Events broadcasted to listeners
  • LoadEvent with type: STOP - Dispatched if the load is stopped during the loading process.