LimitOp.js 733 B

1234567891011121314151617181920212223
  1. var Op = require("../Op");
  2. /** The $limit operator; opts is the number of Objects to allow before preventing further data to pass through. **/
  3. var LimitOp = module.exports = (function(){
  4. // CONSTRUCTOR
  5. var base = Op, proto, klass = function LimitOp(opts){
  6. this.n = 0;
  7. base.call(this, opts);
  8. };
  9. proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  10. // PROTOTYPE MEMBERS
  11. proto.write = function writeUnlessLimitted(obj){
  12. if(this.n++ < this.opts)
  13. this.queue(obj);
  14. //else this.end(); //TODO: this should work but we need to hook up the end event to preceeding things in the pipeline for it to function
  15. };
  16. proto.reset = function resetLimitter(){
  17. this.n = 0;
  18. };
  19. return klass;
  20. })();