Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Consider use of DispatcherAdapter #19

Open
ThomasBurleson opened this issue May 28, 2013 · 0 comments
Open

Consider use of DispatcherAdapter #19

ThomasBurleson opened this issue May 28, 2013 · 0 comments

Comments

@ThomasBurleson
Copy link
Contributor

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant