Created
July 26, 2011 20:13
-
-
Save trxcllnt/1107886 to your computer and use it in GitHub Desktop.
Asynchronous service example with signals as promises enabling functional and fluent API interaction.
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 | |
{ | |
public class MyService | |
{ | |
[Inject] | |
public var session:ISession; | |
private var startedSignal:ISignal; | |
public function startMyService():ISignal | |
{ | |
// If the started signal is null, initialize the service. | |
// | |
// When startedSignal has at least one item in its dispatch history, | |
// it will immediately invoke listeners as they're added. | |
if(startedSignal == null) | |
{ | |
startedSignal = new ReplaySignal(); | |
// ISession.start() is a potentially asynchronous operation, | |
// so it returns an ISignal promise. The ISession implementation | |
// is smart and uses a ReplaySignal itself, so it takes advantage | |
// of the same caching technique we employ here. | |
session.start().addOnce(function(... args):void{ | |
//Do miscellaneous service setup, open channels, whatever. | |
//Tell all callers of startMyService() the service is ready. | |
startedSignal.dispatch(); | |
}); | |
} | |
return startedSignal; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment