Created
February 24, 2012 16:50
-
-
Save Stray/1901979 to your computer and use it in GitHub Desktop.
Example of promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modules.common.net | |
{ | |
import flash.display.DisplayObject; | |
import org.osflash.signals.Signal; | |
public class ResourcePromise implements IPromise, IResourcePromise | |
{ | |
protected var _result:DisplayObject; | |
protected var _errorMessage:String; | |
protected var _loaded:Signal = new Signal(DisplayObject); | |
protected var _error:Signal = new Signal(String); | |
protected var _resource:Resource; | |
public function ResourcePromise(resource:Resource) | |
{ | |
_resource = resource; | |
} | |
public function get resource():Resource | |
{ | |
return _resource; | |
} | |
public function fulfill(content:DisplayObject):void | |
{ | |
_result = content; | |
_loaded.dispatch(_result); | |
} | |
public function handleError(errorMessage:String):void | |
{ | |
_errorMessage = errorMessage; | |
_error.dispatch(_errorMessage); | |
} | |
public function onLoaded(handler:Function):IPromise | |
{ | |
_loaded.addOnce(handler); | |
if(_result) | |
handler(_result); | |
return this; | |
} | |
public function onError(handler:Function):IPromise | |
{ | |
_error.add(handler); | |
if(_errorMessage) | |
handler(_errorMessage); | |
return this; | |
} | |
public function clear():void | |
{ | |
_loaded.removeAll(); | |
_error.removeAll(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment