package votorola.s.gwt.scene.dum; // Copyright 2010-2011, 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. import com.google.gwt.core.client.*; import com.google.gwt.user.cellview.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.rpc.AsyncCallback; import java.util.ArrayList; import java.util.List; import votorola.a.web.gwt.*; import votorola.s.gwt.scene.*; import votorola.s.gwt.scene.feed.*; import votorola.g.hold.*; import votorola.g.web.gwt.*; /** A feed for dummy bites. */ public final class DummyFeed extends Feed implements Hold { /** Constructs a DummyFeed. Call {@linkplain #release release}() when done with it. */ public DummyFeed() { super( new ArrayList( /*initial capacity*/LIST_MAX_SIZE )); new Ticker(); } // ```````````````````````````````````````````````````````````````````````````````````` // init for early use private final Spool spool = new Spool1(); // - H o l d -------------------------------------------------------------------------- public void release() { spool.unwind(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static void addTrim( final List list, final BiteJS bite, final int maxSize ) { while( list.size() >= maxSize ) list.remove( maxSize - 1 ); list.add( 0, bite ); } private static final int LIST_MAX_SIZE = 200; private static final int LISTU_MAX_SIZE = LIST_MAX_SIZE * 100; private static final String REQUEST_LOC = App.getServletContextLocation() + "/xfDum?form=json"; // callback parameter added automatically by JsonpRequestBuilder private final UnScoper scoper = new UnScoper( LIST_MAX_SIZE, new ArrayList(), spool ); // ==================================================================================== private final class Ticker extends Timer implements AsyncCallback> { Ticker() { spool.add( new Hold() { public void release() { cancel(); }}); run(); // tick eagerly scheduleRepeating( 5000/*ms*/ ); } JsArray biteBuffer // received from server, not yet displayed in feed list = JavaScriptObject.createArray().cast(); boolean isResponsePending; boolean isScrapingBottom; public void onFailure( final Throwable x ) { if( spool.isUnwinding() ) return; final boolean toRetry = Window.confirm( "Feed request failed: " + x + ". Ready to retry." ); if( toRetry ) isResponsePending = false; else cancel(); } public void onSuccess( final JsArray moreBites ) { if( spool.isUnwinding() ) return; if( biteBuffer.length() > 0 ) { assert false; return; } biteBuffer = moreBites; isResponsePending = false; if( isScrapingBottom ) run(); } public void run() { if( spool.isUnwinding() ) return; // though probably impossible, as ticker would be cancelled final int bN = biteBuffer.length(); isScrapingBottom = bN <= 0; if( !isScrapingBottom ) { final BiteJS bite = biteBuffer.shift(); addTrim( scoper.unList(), bite, LISTU_MAX_SIZE ); if( Scenes.i().scene().inScope( bite )) { addTrim( getList(), bite, LIST_MAX_SIZE ); } } if( bN <= 1/*just emptied or was empty*/ && !isResponsePending ) { App.i().jsonp().requestObject( REQUEST_LOC, Ticker.this ); isResponsePending = true; } } } }