CC-Archived / promise-as3

Promises/A+ compliant implementation in ActionScript 3.0
168 stars 55 forks source link

Consider use of DispatcherAdapter #19

Open ThomasBurleson opened 11 years ago

ThomasBurleson commented 11 years ago

Often we use AS3 EventDispatchers with custom events. Developers add listeners to dispatchers for two (2) scenarios:

1) Listeners to respond to repeated occurrence of events (e.g. mouseDown, click, etc). or 2) Once-only listeners to respond to a single occurrence of an event (e.g. Authentication, etc)

In the case of adding listeners for once-only actions (2 above), having the ability to easily use Promise(s) instead of adding and removing listeners can dramatically simplify developer code:

Consider the sample scenario below:

    function loginUser( userName:String, password:String ):Promise    
    {                                                                 
         var authenticator : Authenticator = new Authenticator;                                                                             
             authenticator.loginUser( userName, password );           

        return Promise.when( new EventIntercepter (                   
                 authenticator,                                       
                 AuthenticationEvent.AUTHENTICATED, 'session',        
                 AuthenticationEvent.NOT_ALLOWED,   'details'         
        ));                                                           
    }                                                                 

    loginUser(                                                        
         'ThomasB',                                                   
         "superSecretPassword"                                        
    )                                                                 
    .then(                                                                                                                                  
        function onLoginOK( session:Object ):void {                   
            // Save the session information and continue login process
        },                                                                                                                                  
        function onLoginFailed( fault:Object  ):void {                
           // Report the login failure and request another attempt    
        }                                                                                                                                   
    );                                                                

If this seems valuable, please review the EventIntercepter and DispatcherAdapter classes in

https://github.com/ThomasBurleson/promise-as3/commit/8e7bd76b88afd666df37572c66e077d08d7fe3fe