/* globals SubscribeWithScroll, InfiniteScroll */

SubscribeWithScroll = class {
  /**
   * Initializes the SubscribeWithScroll
   * @param  {Object} options
   *  {
   *  	pub: String,
   *  	limit: Number,
   *  	increment: Number,
   *  	template: Blaze.TemplateInstance,
   *  	threshold: String,
   *  	params: Function,
   *  	collection: Mongo.Collection
   *  }
   * @return {SubscribeWithScroll}
   */
  constructor(options){
    this.pub = options.pub;
    this.collection = options.collection;
    this.limit = new ReactiveVar(options.limit);
    this.increment = options.increment;
    this.template = options.template || Meteor;
    this.params = options.params || function(){};
    this.infiniteScroll =
      new InfiniteScroll(options.threshold, options.template);
    this.events = $({});

    return this;
  }

  destroy(){
    this.infiniteScroll.destroy();
    if(this.computation){
      this.computation.stop();
    }
  }

  onEnd(fn){
    return this.events.on('end', fn);
  }

  run(){
    this.computation = Tracker.autorun(() => {
      let params = _.extend(this.params(), {
        limit: this.limit.get()
      });

      let countBefore = this.collection.find().count();
      this.template.subscribe(this.pub, params, () => {
        let count = this.collection.find().count();
        if(countBefore === count){
          this.events.trigger('end');
        }
      });
    });

    this.infiniteScroll.onInfinite(() => {
      this.limit.set( this.limit.get() + this.increment );
    });

    this.infiniteScroll.run();
  }
};

@@ -39,16 +39,24 @@
 teScroll
+, jQuery
  */%0A%0ASub
@@ -168,15 +168,91 @@
   * 
+@return %7BSubscribeWithScroll%7D%0A   */%0A  constructor(options)%7B%0A    check(options,
  %7B%0A  
- *
   %09p
@@ -265,18 +265,16 @@
 ring,%0A  
- *
   %09limit
@@ -275,28 +275,33 @@
 %09limit: 
-Numb
+Match.Integ
 er,%0A  
- *
   %09incre
@@ -310,20 +310,25 @@
 nt: 
-Numb
+Match.Integ
 er,%0A  
- *
   %09t
@@ -336,16 +336,31 @@
 mplate: 
+Match.Optional(
 Blaze.Te
@@ -373,22 +373,21 @@
 Instance
+)
 ,%0A  
- *
   %09thres
@@ -396,20 +396,39 @@
 ld: 
-String,%0A 
+Match.OneOf(String, jQuery),%0A
   
-*
   %09p
@@ -438,16 +438,31 @@
 ms: 
+Match.Optional(
 Function
 ,%0A  
@@ -461,14 +461,13 @@
 tion
+)
 ,%0A  
- *
   %09c
@@ -501,77 +501,13 @@
 %0A   
-*  %7D%0A   * @return %7BSubscribeWithScroll%7D%0A   */%0A  constructor(options)%7B
+ %7D);%0A
 %0A   
@@ -622,16 +622,60 @@
 limit);%0A
+    this.hasEnded = new ReactiveVar(false);%0A
     this
@@ -1210,16 +1210,22 @@
 params()
+ %7C%7C %7B%7D
 , %7B%0A    
@@ -1500,16 +1500,51 @@
 'end');%0A
+          this.hasEnded.set(true);%0A
         

Add check and new method hasEnded