 
     Posts.find()
 
+  Meteor.publish 'localCollection', ->
+    @autorun (computation) =>
+      LocalCollection.find({}, {sort: {i: 1}, limit: localCollectionLimit.get()}).observeChanges
+        addedBefore: (id, fields, before) =>
+          @added 'localCollection', id, fields
+        changed: (id, fields) =>
+          @changed 'localCollection', id, fields
+        removed: (id) =>
+          @removed 'localCollection', id
+
+      @ready()
+
+    return
+
   # We use our own insert method to not have latency compensation so that observeChanges
   # on the client really matches how databases changes on the server.
   Meteor.methods
-    'insertPost': (timestamp) ->
+    insertPost: (timestamp) ->
       check timestamp, Number
 
       Posts.insert
         timestamp: timestamp
 
-    'userAndProjection': (userId) ->
+    userAndProjection: (userId) ->
       user = Users.findOne userId,
         fields:
           posts: 1

Fields = new Mongo.Collection 'Fields_meteor_reactivepublish_tests'

if Meteor.isServer
  Meteor.publish null, ->
    Users.find()


      onError: @expect =>
        @assertTrue true

  testClientLocalCollection: [
    ->
      Meteor.call 'setLocalCollectionLimit', 10, @expect (error) =>
          @assertFalse error, error
  ,
    ->
      @assertSubscribeSuccessful 'localCollection', @expect()
  ,
    ->
      @assertEqual LocalCollection.find({}).fetch(), []

      for i in [0...10]
        Meteor.call 'insertLocalCollection', {i: i}, @expect (error, documentId) =>
          @assertFalse error, error
          @assertTrue documentId
  ,
    ->
      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 10

      Meteor.call 'setLocalCollectionLimit', 5, @expect (error) =>
        @assertFalse error, error

      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 5

      for i in [0...10]
        Meteor.call 'insertLocalCollection', {i: i}, @expect (error, documentId) =>
          @assertFalse error, error
          @assertTrue documentId
  ,
    ->
      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 5

      Meteor.call 'setLocalCollectionLimit', 15, @expect (error) =>
        @assertFalse error, error

      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 15
  ]

# Register the test case.
ClassyTestCase.addTest new ReactivePublishTestCase()
