001package votorola.g.web.gwt; // Copyright 2011-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.jsonp.client.JsonpRequest;
004
005
006/** A guard to enforce non-overlapping JSONP request/response exchanges.  It ensures that
007  * only a single response is ever pending.
008  */
009public final class JSONPAtomizer<R>
010{
011
012
013    /** Nulls the pending request if it matches the one specified.
014      *
015      *     @see #pendingRequest()
016      */
017    public void clearPendingRequest( JsonpRequest<R> oldRequest )
018    {
019        if( oldRequest.equals( pendingRequest )) pendingRequest = null;
020    }
021
022
023
024    /** The serial request to the server that is currently pending, or null if there is
025      * none.  Requesters are required 1) to call this method on receiving a response to a
026      * request in order to confirm that it is recorded as pending.  If it is, then 2)
027      * clear the record and 3) act on the response; otherwise do nothing.
028      *
029      *     @see #clearPendingRequest(JsonpRequest)
030      *     @see #setPendingRequest(JsonpRequest)
031      */
032    public JsonpRequest<R> pendingRequest() { return pendingRequest; }
033
034
035        private JsonpRequest<R> pendingRequest;
036
037
038
039    /** Records the start of a new request to the server and cancels any previous request
040      * that is pending.  Requesters are required to call this prior to sending each
041      * serial request.
042      *
043      *     @see #pendingRequest()
044      */
045    public void setPendingRequest( JsonpRequest<R> newRequest )
046    {
047        if( newRequest == null ) throw new NullPointerException(); // should use clearPendingRequest()
048
049        if( pendingRequest != null ) pendingRequest.cancel(); // just to be tidy
050        pendingRequest = newRequest;
051    }
052
053
054}