package votorola.s.gwt.scene.diff;// Copyright 2010-2011. Michael Allan. Christian Weilbach. 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.json.client.*; import com.google.gwt.user.client.rpc.AsyncCallback; import java.util.ArrayList; import java.util.List; import java.util.Date; import votorola.a.web.gwt.*; import votorola.s.gwt.stage.Stage; import votorola.s.gwt.scene.*; import votorola.s.gwt.scene.feed.*; import votorola.g.hold.*; /** * A feed for diff bites. * * @see Live example of a DiffFeed * (left) */ public final class DiffFeed extends Feed implements Hold { /** * Constructs a DiffFeed. Call {@linkplain #release release}() when done * with it. */ public DiffFeed() { 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); // newest last list.add(bite); } private static final int LIST_MAX_SIZE = 200; private static final int LISTU_MAX_SIZE = LIST_MAX_SIZE * 100; // unlike prefix for this legacy code, change also stripPrefix() and // parameters private static final String REQUEST_URL = App.getServletContextLocation() + "/wap?wCall=dfyDiffFeed"; // 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 { private Date lastAccess = new Date(0); private int INTERVAL = 600000; // ten mins Ticker() { spool.add(new Hold() { public void release() { cancel(); } }); run(); // tick eagerly } JsArray biteBuffer // received from server, not yet displayed in // feed list = JavaScriptObject.createArray().cast(); boolean isResponsePending; boolean isScrapingBottom; public void onFailure(final Throwable throwable) { if (spool.isUnwinding()) return; final boolean toRetry = Window.confirm("Feed request failed: " + throwable + ". Ready to retry."); if (toRetry) isResponsePending = false; else cancel(); } private final native JsArray stripPrefix( JavaScriptObject prefixJs) /*-{ return prefixJs["dfy"]; }-*/; public void onSuccess(final JavaScriptObject prefixJs) { if (spool.isUnwinding()) return; JsArray moreBites = null; moreBites = stripPrefix(prefixJs); if (moreBites == null) { return; } if (biteBuffer.length() > 0) { assert false; return; } biteBuffer = moreBites; isResponsePending = false; run(); } private BiteJS pendingBite = null; public boolean triggerBite(final BiteJS bite) { final Date now = new Date(); final long targetTime = bite.sentDate().getTime() + INTERVAL - now.getTime(); if (targetTime > 0) { // show in INTERVAL delay on all clients pendingBite = bite; schedule((int) targetTime); return false; } else { addBite(bite); return true; } } public void addBite(final BiteJS bite) { if (bite == null) return; // System.out.println( "Added bite " + bite.message().title() ); addTrim(scoper.unList(), bite, LISTU_MAX_SIZE); if (Scenes.i().scene().inScope(bite)) { addTrim(getList(), bite, LIST_MAX_SIZE); } pendingBite = null; } public @Override void run() { if (spool.isUnwinding()) return; // though probably impossible, as ticker would be // cancelled final Date now = new Date(); addBite(pendingBite); // empty the buffer while (biteBuffer.length() > 0) { if (!triggerBite(biteBuffer.shift())) return; } if (!isResponsePending) { if ((now.getTime() - lastAccess.getTime()) < INTERVAL) { final long targetTime = lastAccess.getTime() + INTERVAL - now.getTime(); schedule((int) targetTime); return; } final StringBuilder ub = new StringBuilder(); ub.append(REQUEST_URL); if (Stage.i().getActorName() != null) { ub.append("&hUsers=").append(Stage.i().getActorName()); } if (Stage.i().getPollName() != null) { ub.append("&hPoll=").append(Stage.i().getPollName()); } App.i().jsonpWAP().requestObject(ub.toString(), Ticker.this); lastAccess = now; isResponsePending = true; } } } }