Skip to content

Instantly share code, notes, and snippets.

@trxcllnt
Created July 26, 2011 20:13
Show Gist options
  • Save trxcllnt/1107886 to your computer and use it in GitHub Desktop.
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.
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