import{debugError,helper}from"../common/helper.js";export class PipeTransport{constructor(e,s){this._pipeWrite=e,this._pendingMessage="",this._eventListeners=[helper.addEventListener(s,"data",(e=>this._dispatch(e))),helper.addEventListener(s,"close",(()=>{this.onclose&&this.onclose.call(null)})),helper.addEventListener(s,"error",debugError),helper.addEventListener(e,"error",debugError)],this.onmessage=null,this.onclose=null}send(e){this._pipeWrite.write(e),this._pipeWrite.write("\0")}_dispatch(e){let s=e.indexOf("\0");if(-1===s)return void(this._pendingMessage+=e.toString());const t=this._pendingMessage+e.toString(void 0,0,s);this.onmessage&&this.onmessage.call(null,t);let i=s+1;for(s=e.indexOf("\0",i);-1!==s;)this.onmessage&&this.onmessage.call(null,e.toString(void 0,i,s)),i=s+1,s=e.indexOf("\0",i);this._pendingMessage=e.toString(void 0,i)}close(){this._pipeWrite=null,helper.removeEventListeners(this._eventListeners)}}