<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -130,9 +130,8 @@
       // Remove all media items
       while (itemsHash.length &gt; 0) {
         var deadItem:MediaItem = itemsHash.removeIndex(0)[0];
-        Output.assert(mediaItems.contains(deadItem),
-          &quot;Removing item not in mediaItems?&quot; + deadItem);
-        mediaItems.removeChild(deadItem);
+        if (mediaItems &amp;&amp; mediaItems.contains(deadItem))
+          mediaItems.removeChild(deadItem);
       }
 
       // Reset media items to 0</diff>
      <filename>fbair/nile/renderers/AttachmentRenderer.mxml</filename>
    </modified>
    <modified>
      <diff>@@ -75,7 +75,7 @@
     import mx.events.FlexEvent;
 
     // Max number we want to show before we permalink
-    public static const MaxComments:int = 6;
+    private static const MaxComments:int = 6;
 
     // Tracks whether we've been revealed and are getting ALL comments
     private var revealed:Boolean = false;
@@ -93,11 +93,11 @@
 
     // Watches when our count has changed, to refetch
     private var countWatcher:ChangeWatcher;
-    
+
     // comments[0] =&gt; Newest
     // comments[comments.length-1] = Oldest
     // All the CommentRenderers, keyed by id
-    public var commentsHash:HashArray = new HashArray();
+    private var commentsHash:HashArray = new HashArray();
 
     // List of comment-ids we have absolutely deleted
     //   We hold on to make sure a quick refresh doesn't revive them.
@@ -119,23 +119,23 @@
         Depot.put(event.target);
     }
 
+    // Marks an id as immune to the next deletion round
+    public function markImmune(commentData:Object):void {
+      immunes[commentData.id] = true;
+    }
+
     // Creates new CommentRenderer and adds to display
     // commentData =&gt; {fromid:int, text:String}
-    public function addComment(commentData:Object,
-                               immune:Boolean = false,
-                               firstBatch:Boolean = false):void {
-      if (commentsHash.hasKey(commentData.id)) return;
+    public function addComment(commentData:Object):void {
+      if (!revealed || commentsHash.hasKey(commentData.id)) return;
 
       // Pass on our can_remove var
       commentData.can_remove = data.comments.can_remove;
 
-      // Mark in immunity if immune
-      if (immune) immunes[commentData.id] = true;
-
       // Create renderer
       var renderer:CommentRenderer = Depot.get(CommentRenderer);
       renderer.data = commentData;
-      renderer.animateIn = !firstBatch;
+      renderer.animateIn = !firstPull;
 
       // Determine position of comment based on timestamp
       var entry_position:int = 0;
@@ -164,22 +164,19 @@
       if (revealed) return;
       revealed = true;
 
-      // If we have more than max, then display link
-      allCommentsContainer.visible = (data.comments.count &gt; MaxComments);
-
       // Ok we need to check how many stories we have, and if we need more
       if (data.comments.count &lt;= 3) {
         loadingIndicator.visible = false;
         commentsBoxContainer.animateIn = false;
 
-        populateComments(data.comments.posts);
+        populateComments(data.comments.comment_list);
       } else {
         commentsBoxContainer.visible = false;
         commentsBoxContainer.animateIn = true;
 
         if (data.comments.count &gt; 0) {
           // If data is already in the cache, then just show it
-          if (CommentCache.hasFreshComments(data.post_id))
+          if (CommentCache.hasFreshComments(data.post_id, data.comments.count))
             commentsFetched(new FBEvent(CommentCache.COMMENTS_FETCHED,
               CommentCache.commentCache[data.post_id]));
           // Fetch data, and show loader
@@ -191,11 +188,22 @@
           }
         }
       }
-      
+
       // Now lets bind to the count to see when to refetch
       if (countWatcher) countWatcher.unwatch();
       countWatcher = BindingUtils.bindSetter(function(new_count:int):void {
-        fetchComments();
+        if (new_count &lt;= 0) return;
+        if (new_count &lt;= 3) populateComments(data.comments.comment_list);
+        else {
+          if (CommentCache.hasFreshComments(data.post_id, data.comments.count))
+            commentsFetched(new FBEvent(CommentCache.COMMENTS_FETCHED,
+              CommentCache.commentCache[data.post_id]));
+          else
+            fetchComments();
+        }
+
+        // If we have more than max, then display link
+        allCommentsContainer.visible = (new_count &gt; MaxComments);
       }, data, [&quot;comments&quot;, &quot;count&quot;]);
     }
 
@@ -204,7 +212,6 @@
       var comment:CommentRenderer = event.target as CommentRenderer;
       removeCommentAtIndex(commentsHash.indexAtKey(comment.data.id));
       data.comments.count--;
-      allCommentsContainer.visible = (data.comments.count &gt; MaxComments);
     }
 
     // Called when we want to get some comments, yo
@@ -224,6 +231,7 @@
       populateComments(event.data);
     }
 
+    // Populate our comments with given array
     private function populateComments(newComments:*):void {
       // If comments isn't an Array, then there are no comments
       if (!(newComments is Array)) newComments = new Array();
@@ -238,21 +246,31 @@
 
       // Add our comments
       for each (var newComment:Object in commentsArray)
-        addComment(newComment, false, firstPull);
+        addComment(newComment);
 
       // Remove extraneous comments
       removeAllCommentsExcept(commentsArray);
 
       // Remove overflow, if any.
+      removeExcessComments();
+
+      // Mark latest update
+      markLatestUpdate();
+
+      // Not the first anymore
+      firstPull = false;
+    }
+
+    // We remove any stories above our max here
+    public function removeExcessComments():void {
       while (commentsHash.length &gt; MaxComments)
         removeCommentAtIndex(0);
+    }
 
-      // Mark latest update
+    // Mark latest update
+    public function markLatestUpdate():void {
       latestUpdate = (commentsHash.length == 0 ? 0 :
         commentsHash.getAt(0).data.time);
-        
-      // Not the first anymore
-      firstPull = false;
     }
 
     // We call this to narrow our comment view down to the group
@@ -313,6 +331,10 @@
       commentsHash = new HashArray();
       zombies = new Object();
       immunes = new Object();
+
+      // Put this back in the non revealed state
+      commentsBoxContainer.visible = false;
+      commentsBoxContainer.animateIn = false;
     }
   ]]&gt;&lt;/mx:Script&gt;
 &lt;/mx:VBox&gt;</diff>
      <filename>fbair/nile/renderers/CommentsRenderer.mxml</filename>
    </modified>
    <modified>
      <diff>@@ -210,18 +210,27 @@
     private function commentCreated(event:FBEvent):void {
       if (event.data.post_id != data.post_id) return;
       
-      // Iterate our comment count
+      // Get a copy of our data
+      var commentData:Object = ObjectUtil.copy(event.data);
+      
+      // Iterate our comment count, and put in comment_list
       data.comments.count++;
+      if (data.comments.count == 1)
+        data.comments.comment_list = [commentData];
+      else
+        data.comments.comment_list.push(commentData);
 
+      // Mark this as immune for next go around
+      commentsRenderer.markImmune(commentData);
+      
       // Add this comment to our CommentsRenderer
-      commentsRenderer.addComment(ObjectUtil.copy(event.data), true);
-      commentsRenderer.allCommentsContainer.visible =
-        (data.comments.count &gt; CommentsRenderer.MaxComments);
+      commentsRenderer.addComment(commentData);
 
       // Remove overflow, if any.
-      while (commentsRenderer.commentsHash.length &gt;
-        CommentsRenderer.MaxComments)
-        commentsRenderer.removeCommentAtIndex(0);
+      commentsRenderer.removeExcessComments();
+      
+      // Mark latest update
+      commentsRenderer.markLatestUpdate();
     }
   ]]&gt;&lt;/mx:Script&gt;
 &lt;/util:AnimatedCanvas&gt;</diff>
      <filename>fbair/nile/renderers/UFI.mxml</filename>
    </modified>
    <modified>
      <diff>@@ -58,8 +58,10 @@ package fbair.util {
     private static var queuedRequests:Object = new Object;
 
     // Whether we have fresh comments for a given id
-    public static function hasFreshComments(post_id:String):Boolean {
+    public static function hasFreshComments(post_id:String,
+                                            comment_count:int):Boolean {
       return commentCache[post_id] &amp;&amp;
+        commentCache[post_id].length == comment_count &amp;&amp;
         (new Date()).time - fetchTime &lt; CommentLifeSpan;
     }
 </diff>
      <filename>fbair/util/CommentCache.as</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>fcb3f5a824f26967188348017c5a76be6457df62</id>
    </parent>
  </parents>
  <author>
    <name>Justin Bishop</name>
    <email>jubishop@gmail.com</email>
  </author>
  <url>http://github.com/jubishop/Facebook-for-Adobe-AIR/commit/6e16123763eb447eb70063e8825507e5c698b064</url>
  <id>6e16123763eb447eb70063e8825507e5c698b064</id>
  <committed-date>2009-05-19T18:23:29-07:00</committed-date>
  <authored-date>2009-05-19T18:23:29-07:00</authored-date>
  <message>cleaning nascent bugs everywhere.</message>
  <tree>9cdd472df172d9a7cc6b8c6b53b6411131aef492</tree>
  <committer>
    <name>Justin Bishop</name>
    <email>jubishop@gmail.com</email>
  </committer>
</commit>
