"use strict";var RingBuffer=require("./ring_buffer"),Functor=function(e,t){this._session=e,this._method=t,this._queue=new RingBuffer(Functor.QUEUE_SIZE),this._stopped=!1,this.pending=0};Functor.QUEUE_SIZE=8,Functor.prototype.call=function(e,t,s,o){if(!this._stopped){var r={error:e,message:t,callback:s,context:o,done:!1},n=!1,u=this;if(this._queue.push(r),r.error)return r.done=!0,this._stop(),this._flushQueue();var i=function(e,t){n^(n=!0)&&(e?(u._stop(),r.error=e,r.message=null):r.message=t,r.done=!0,u._flushQueue())};try{this._session[this._method](t,i)}catch(e){i(e)}}},Functor.prototype._stop=function(){this.pending=this._queue.length,this._stopped=!0},Functor.prototype._flushQueue=function(){for(var e,t=this._queue;t.length>0&&t.peek().done;)(e=t.shift()).error?(this.pending=0,t.clear()):this.pending-=1,e.callback.call(e.context,e.error,e.message)},module.exports=Functor;