001package votorola.g.web.gwt; // Copyright 2012, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import com.google.gwt.user.client.rpc.AsyncCallback;
004import votorola.g.hold.*;
005
006
007/** A wrapper that disables an asynchronous callback when a spool is unwinding.
008  */
009public final class SpooledAsyncCallback<T> implements AsyncCallback<T>
010{
011
012
013    /** Creates an SpooledAsyncCallback.
014      *
015      *     @param spool the spool to use.
016      *     @param callback the callback to guard.
017      */
018    public static <T> SpooledAsyncCallback<T> wrap( final Spool spool,
019      final AsyncCallback<T> callback )
020    {
021        return new SpooledAsyncCallback<T>( spool, callback );
022    }
023
024
025
026    private SpooledAsyncCallback( Spool _spool, AsyncCallback<T> _callback )
027    {
028        spool = _spool;
029        callback = _callback;
030    }
031
032
033
034   // - A s y n c - C a l l b a c k ------------------------------------------------------
035
036
037    public void onFailure( final Throwable x )
038    {
039        if( !spool.isUnwinding() ) callback.onFailure( x );
040    }
041
042
043
044    public void onSuccess( final T result )
045    {
046        if( !spool.isUnwinding() ) callback.onSuccess( result );
047    }
048
049
050
051//// P r i v a t e ///////////////////////////////////////////////////////////////////////
052
053
054    private final AsyncCallback<T> callback;
055
056
057
058    private final Spool spool;
059
060
061}