From 3761ccb018d89596636ca68cadadde94eaa54d54 Mon Sep 17 00:00:00 2001
From: Robin Ward <robin.ward@gmail.com>
Date: Wed, 17 Jul 2013 16:50:20 -0400
Subject: [PATCH] FIX: Sometimes the total post count could be incorrect

---
 .../discourse/models/post_stream.js           |  5 +++--
 test/javascripts/models/post_stream_test.js   | 20 ++++++++++++++++++-
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/app/assets/javascripts/discourse/models/post_stream.js b/app/assets/javascripts/discourse/models/post_stream.js
index a258424d802..922998b4a93 100644
--- a/app/assets/javascripts/discourse/models/post_stream.js
+++ b/app/assets/javascripts/discourse/models/post_stream.js
@@ -359,7 +359,7 @@ Discourse.PostStream = Em.Object.extend({
   **/
   commitPost: function(post) {
     this.appendPost(post);
-    this.get('stream').pushObject(post.get('id'));
+    this.get('stream').addObject(post.get('id'));
     this.set('stagingPost', false);
   },
 
@@ -435,7 +435,7 @@ Discourse.PostStream = Em.Object.extend({
     var lastPostLoaded = this.get('lastPostLoaded');
 
     if (this.get('stream').indexOf(postId) === -1) {
-      this.get('stream').pushObject(postId);
+      this.get('stream').addObject(postId);
       if (lastPostLoaded) { this.appendMore(); }
     }
   },
@@ -602,6 +602,7 @@ Discourse.PostStream = Em.Object.extend({
     return this.get('stream').indexOf(post.get('id'));
   },
 
+
   /**
     @private
 
diff --git a/test/javascripts/models/post_stream_test.js b/test/javascripts/models/post_stream_test.js
index 477b17de97a..cdb7d85ad31 100644
--- a/test/javascripts/models/post_stream_test.js
+++ b/test/javascripts/models/post_stream_test.js
@@ -312,7 +312,6 @@ test("staging and committing a post", function() {
 
 });
 
-
 test('triggerNewPostInStream', function() {
   var postStream = buildStream(225566);
 
@@ -341,3 +340,22 @@ test('triggerNewPostInStream', function() {
   ok(postStream.appendMore.calledOnce, "delegates to appendMore because the last post is loaded");
 });
 
+
+test("comitting and triggerNewPostInStream race condition", function() {
+  var postStream = buildStream(4964);
+
+  postStream.appendPost(Discourse.Post.create({id: 1, post_number: 1}));
+  var user = Discourse.User.create({username: 'eviltrout', name: 'eviltrout', id: 321});
+  var stagedPost = Discourse.Post.create({ raw: 'hello world this is my new post' });
+
+  var result = postStream.stagePost(stagedPost, user);
+  equal(postStream.get('filteredPostsCount'), 0, "it has no filteredPostsCount yet");
+  stagedPost.set('id', 123);
+
+  this.stub(postStream, 'appendMore');
+  postStream.triggerNewPostInStream(123);
+  equal(postStream.get('filteredPostsCount'), 1, "it added the post");
+
+  postStream.commitPost(stagedPost);
+  equal(postStream.get('filteredPostsCount'), 1, "it does not add the same post twice");
+});
\ No newline at end of file