15 | ***************************************************************************** */function r(e,t,o,n){return new(o||(o=Promise))((function(s,i){function r(e){try{c(n.next(e))}catch(e){i(e)}}function a(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?s(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(r,a)}c((n=n.apply(e,t||[])).next())}))}function a(e,t){var o,n,s,i,r={label:0,sent:function(){if(1&s[0])throw s[1];return s[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(o)throw new TypeError("Generator is already executing.");for(;r;)try{if(o=1,n&&(s=2&i[0]?n.return:i[0]?n.throw||((s=n.return)&&s.call(n),0):n.next)&&!(s=s.call(n,i[1])).done)return s;switch(n=0,s&&(i=[2&i[0],s.value]),i[0]){case 0:case 1:s=i;break;case 4:return r.label++,{value:i[1],done:!1};case 5:r.label++,n=i[1],i=[0];continue;case 7:i=r.ops.pop(),r.trys.pop();continue;default:if(!(s=r.trys,(s=s.length>0&&s[s.length-1])||6!==i[0]&&2!==i[0])){r=0;continue}if(3===i[0]&&(!s||i[1]>s[0]&&i[1]<s[3])){r.label=i[1];break}if(6===i[0]&&r.label<s[1]){r.label=s[1],s=i;break}if(s&&r.label<s[2]){r.label=s[2],r.ops.push(i);break}s[2]&&r.ops.pop(),r.trys.pop();continue}i=t.call(e,r)}catch(e){i=[6,e],n=0}finally{o=s=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}}Object.create;Object.create;var c,h="0.7.4";!function(e){e[e.CLOSED=WebSocket.CLOSED]="CLOSED",e[e.CLOSING=WebSocket.CLOSING]="CLOSING",e[e.CONNECTING=WebSocket.CONNECTING]="CONNECTING",e[e.OPEN=WebSocket.OPEN]="OPEN"}(c||(c={}));var u=function(){function e(){this.autoReconnect=!0,this.log=s.Brolog.instance(),this.CONNECT_TIMEOUT=1e4,this.ENDPOINT="wss://api.chatie.io/v0/websocket/token/",this.PROTOCOL="web|0.0.1",this.sendBuffer=[],this.log.verbose("IoService","constructor()")}return Object.defineProperty(e.prototype,"readyState",{get:function(){return this._readyState.asObservable()},enumerable:!1,configurable:!0}),e.prototype.init=function(){return r(this,void 0,void 0,(function(){var e,t=this;return a(this,(function(n){switch(n.label){case 0:if(this.log.verbose("IoService","init()"),this.state)throw new Error("re-init");this.snapshot={readyState:c.CLOSED,event:null},this._readyState=new o.BehaviorSubject(c.CLOSED),this.state=new i.StateSwitch("IoService",this.log),this.state.setLog(this.log),n.label=1;case 1:return n.trys.push([1,4,,5]),[4,this.initStateDealer()];case 2:return n.sent(),[4,this.initRxSocket()];case 3:return n.sent(),[3,5];case 4:throw e=n.sent(),this.log.silly("IoService","init() exception: %s",e.message),e;case 5:return this.readyState.subscribe((function(e){t.log.silly("IoService","init() readyState.subscribe(%s)",c[e]),t.snapshot.readyState=e})),this.event.subscribe((function(e){t.log.silly("IoService","init() event.subscribe({name:%s})",e.name),t.snapshot.event=e})),[2]}}))}))},e.prototype.token=function(e){if(this.log.silly("IoService","token(%s)",e),!e)return this._token;this._token=e},e.prototype.start=function(){return r(this,void 0,void 0,(function(){var e;return a(this,(function(t){switch(t.label){case 0:if(this.log.verbose("IoService","start() with token:%s",this._token),!this._token)throw new Error("start() without token");if(this.state.on())throw new Error("state is already ON");if(this.state.pending())throw new Error("state is pending");this.state.on("pending"),this.autoReconnect=!0,t.label=1;case 1:return t.trys.push([1,3,,4]),[4,this.connectRxSocket()];case 2:return t.sent(),this.state.on(!0),[3,4];case 3:return e=t.sent(),this.log.warn("IoService","start() failed:%s",e.message),this.state.off(!0),[3,4];case 4:return[2]}}))}))},e.prototype.stop=function(){return r(this,void 0,void 0,(function(){return a(this,(function(e){switch(e.label){case 0:if(this.log.verbose("IoService","stop()"),this.state.off()){if(this.log.warn("IoService","stop() state is already off"),this.state.pending())throw new Error("state pending() is true");return[2]}if(this.state.off("pending"),this.autoReconnect=!1,!this._websocket)throw new Error("no websocket");return[4,this.socketClose(1e3,"IoService.stop()")];case 1:return e.sent(),this.state.off(!0),[2]}}))}))},e.prototype.restart=function(){return r(this,void 0,void 0,(function(){var e;return a(this,(function(t){switch(t.label){case 0:this.log.verbose("IoService","restart()"),t.label=1;case 1:return t.trys.push([1,4,,5]),[4,this.stop()];case 2:return t.sent(),[4,this.start()];case 3:return t.sent(),[3,5];case 4:throw e=t.sent(),this.log.error("IoService","restart() error:%s",e.message),e;case 5:return[2]}}))}))},e.prototype.initStateDealer=function(){var e=this;this.log.verbose("IoService","initStateDealer()");this.readyState.pipe(n.filter((function(e){return e===c.OPEN}))).subscribe((function(t){return e.stateOnOpen()}))},e.prototype.initRxSocket=function(){return r(this,void 0,void 0,(function(){var e,t=this;return a(this,(function(s){if(this.log.verbose("IoService","initRxSocket()"),this.event)throw new Error("re-init is not permitted");return this.moObserver={next:this.socketSend.bind(this),error:this.socketClose.bind(this),complete:this.socketClose.bind(this)},e=new o.Observable((function(e){return t.log.verbose("IoService","initRxSocket() Observable.create()"),t.mtObserver=e,t.socketClose.bind(t)})),this.event=o.Subject.create(this.moObserver,e.pipe(n.share())),[2]}))}))},e.prototype.connectRxSocket=function(){return r(this,void 0,void 0,(function(){var e,t=this;return a(this,(function(o){if(this.log.verbose("IoService","connectRxSocket()"),this._websocket)throw new Error("already has a websocket");if(this.state.off())throw new Error("switch state is off");if(!this.state.pending())throw new Error("switch state is already ON");return this._websocket=new WebSocket(this.endPoint(),this.PROTOCOL),this.socketUpdateState(),e=new Promise((function(e,o){t.log.verbose("IoService","connectRxSocket() Promise()");var n=setTimeout((function(){t._websocket=null;var e=new Error("rxSocket connect timeout after "+Math.round(t.CONNECT_TIMEOUT/1e3));o(e)}),t.CONNECT_TIMEOUT);t._websocket.onopen=function(o){t.log.verbose("IoService","connectRxSocket() Promise() WebSocket.onOpen() resolve()"),t.socketUpdateState(),clearTimeout(n),e()}})),this._websocket.onmessage=this.socketOnMessage.bind(this),this._websocket.onerror=this.socketOnError.bind(this),this._websocket.onclose=this.socketOnClose.bind(this),[2,e]}))}))},e.prototype.endPoint=function(){var e=this.ENDPOINT+this._token;return this.log.verbose("IoService","endPoint() => %s",e),e},e.prototype.stateOnOpen=function(){this.log.verbose("IoService","stateOnOpen()"),this.socketSendBuffer(),this.rpcUpdate("from stateOnOpen()")},e.prototype.rpcDing=function(e){return r(this,void 0,void 0,(function(){var t;return a(this,(function(o){return this.log.verbose("IoService","ding(%s)",e),t={name:"ding",payload:e},this.event.next(t),[2]}))}))},e.prototype.rpcUpdate=function(e){return r(this,void 0,void 0,(function(){return a(this,(function(t){return this.event.next({name:"update",payload:e}),[2]}))}))},e.prototype.socketClose=function(e,t){return r(this,void 0,void 0,(function(){var o=this;return a(this,(function(s){switch(s.label){case 0:if(this.log.verbose("IoService","socketClose()"),!this._websocket)throw new Error("no websocket");return this._websocket.close(e,t),this.socketUpdateState(),[4,new Promise((function(e){o.readyState.pipe(n.filter((function(e){return e===c.CLOSED}))).subscribe(e)}))];case 1:return s.sent(),[2]}}))}))},e.prototype.socketSend=function(e){this.log.silly("IoService","socketSend({name:%s, payload:%s})",e.name,e.payload),this._websocket||this.log.silly("IoService","socketSend() no _websocket");var t=JSON.stringify(e);this.sendBuffer.push(t),this.socketSendBuffer()},e.prototype.socketSendBuffer=function(){if(this.log.silly("IoService","socketSendBuffer() length:%s",this.sendBuffer.length),!this._websocket)throw new Error("socketSendBuffer(): no _websocket");if(this._websocket.readyState===WebSocket.OPEN)for(;this.sendBuffer.length;){var e=this.sendBuffer.shift();this.log.silly("IoService","socketSendBuffer() sending(%s)",e),this._websocket.send(e)}else this.log.warn("IoService","socketSendBuffer() readyState is not OPEN, send job delayed.")},e.prototype.socketUpdateState=function(){var e;this.log.verbose("IoService","socketUpdateState() is %s",c[null===(e=this._websocket)||void 0===e?void 0:e.readyState]),this._websocket?this._readyState.next(this._websocket.readyState):this.log.error("IoService","socketUpdateState() no _websocket")},e.prototype.socketOnMessage=function(e){this.log.verbose("IoService","onMessage({data: %s})",e.data);var t=e.data,o={name:"raw",payload:t};try{var n=JSON.parse(t);o.name=n.name,o.payload=n.payload}catch(e){this.log.warn("IoService","onMessage parse message fail. save as RAW")}this.mtObserver.next(o)},e.prototype.socketOnError=function(e){this.log.silly("IoService","socketOnError(%s)",e)},e.prototype.socketOnClose=function(e){var t=this;this.log.verbose("IoService","socketOnClose({code:%s, reason:%s, returnValue:%s})",e.code,e.reason,e.returnValue),this.socketUpdateState(),this.autoReconnect?(this.state.on("pending"),setTimeout((function(){return r(t,void 0,void 0,(function(){var e;return a(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,this.connectRxSocket()];case 1:return t.sent(),this.state.on(!0),[3,3];case 2:return e=t.sent(),this.log.warn("IoService","socketOnClose() autoReconnect() exception: %s",e),this.state.off(!0),[3,3];case 3:return[2]}}))}))}),1e3)):this.state.off(!0),this._websocket=null,e.wasClean||this.log.warn("IoService","socketOnClose() event.wasClean FALSE")},e}(),l=function(){function e(e,o){this.log=e,this.ngZone=o,this.message=new t.EventEmitter,this.scan=new t.EventEmitter,this.login=new t.EventEmitter,this.logout=new t.EventEmitter,this.error=new t.EventEmitter,this.heartbeat=new t.EventEmitter,this.timerSub=null,this.counter=0,this.timestamp=new Date,this.log.verbose("WechatyComponent","constructor() v%s",h)}return Object.defineProperty(e.prototype,"token",{get:function(){return this._token},set:function(e){this.log.verbose("WechatyComponent","set token(%s)",e);var t=(e||"").trim();if(this._token!==t){if(this._token=t,!this.ioService)return this.log.silly("WechatyComponent","set token() skip token init value"),void this.log.silly("WechatyComponent","set token() because ioService will do it inside ngOnInit()");this.log.silly("WechatyComponent","set token(%s) reloading ioService now...",t),this.ioService.token(this.token),this.ioService.restart()}else this.log.silly("WechatyComponent","set token(%s) not new",t)},enumerable:!1,configurable:!0}),e.prototype.ngOnInit=function(){return r(this,void 0,void 0,(function(){return a(this,(function(e){switch(e.label){case 0:return this.log.verbose("WechatyComponent","ngOnInit() with token: "+this.token),this.ioService=new u,[4,this.ioService.init()];case 1:return e.sent(),this.ioService.event.subscribe(this.onIo.bind(this)),this.log.silly("WechatyComponent","ngOnInit() ioService.event.subscribe()-ed"),this.token?(this.ioService.token(this.token),[4,this.ioService.start()]):[3,3];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},e.prototype.ngOnDestroy=function(){this.log.verbose("WechatyComponent","ngOnDestroy()"),this.endTimer(),this.ioService&&this.ioService.stop()},e.prototype.onIo=function(e){switch(this.log.silly("WechatyComponent","onIo#%d(%s)",this.counter++,e.name),this.timestamp=new Date,e.name){case"scan":this.scan.emit(e.payload);break;case"login":this.login.emit(e.payload);break;case"logout":this.logout.emit(e.payload);break;case"message":this.message.emit(e.payload);break;case"error":this.error.emit(e.payload);break;case"ding":case"dong":case"raw":this.heartbeat.emit(e.name+"["+e.payload+"]");break;case"heartbeat":this.heartbeat.emit(e.payload);break;case"sys":this.log.silly("WechatyComponent","onIo(%s): %s",e.name,e.payload);break;default:this.log.warn("WechatyComponent","onIo() unknown event name: %s[%s]",e.name,e.payload)}},e.prototype.reset=function(e){this.log.verbose("WechatyComponent","reset(%s)",e);var t={name:"reset",payload:e};if(!this.ioService)throw new Error("no ioService");this.ioService.event.next(t)},e.prototype.shutdown=function(e){this.log.verbose("WechatyComponent","shutdown(%s)",e);var t={name:"shutdown",payload:e};if(!this.ioService)throw new Error("no ioService");this.ioService.event.next(t)},e.prototype.startSyncMessage=function(){this.log.verbose("WechatyComponent","startSyncMessage()");if(!this.ioService)throw new Error("no ioService");this.ioService.event.next({name:"botie",payload:{args:["message"],source:"return this.syncMessage(message)"}})},e.prototype.startTimer=function(){var e=this;this.log.verbose("WechatyComponent","startTimer()"),this.ender=new o.Subject,this.ngZone.runOutsideAngular((function(){e.timer=o.interval(3e3).pipe(n.tap((function(t){e.log.verbose("do"," %d",t)})),n.takeUntil(e.ender),n.share())})),this.timerSub=this.timer.subscribe((function(t){if(e.counter=t,!e.ioService)throw new Error("no ioService");e.ioService.rpcDing(e.counter)}))},e.prototype.endTimer=function(){this.log.verbose("WechatyComponent","endTimer()"),this.timerSub&&(this.timerSub.unsubscribe(),this.timerSub=null),this.ender&&this.ender.next(null)},e.prototype.logoff=function(e){this.log.silly("WechatyComponent","logoff(%s)",e);var t={name:"logout",payload:e};this.ioService.event.next(t)},Object.defineProperty(e.prototype,"readyState",{get:function(){return this.ioService.readyState},enumerable:!1,configurable:!0}),e}();l.decorators=[{type:t.Component,args:[{selector:"wechaty",template:"<ng-content></ng-content>"}]}],l.ctorParameters=function(){return[{type:s.Brolog},{type:t.NgZone}]},l.propDecorators={message:[{type:t.Output}],scan:[{type:t.Output}],login:[{type:t.Output}],logout:[{type:t.Output}],error:[{type:t.Output}],heartbeat:[{type:t.Output}],token:[{type:t.Input}]};var p=function(){};p.decorators=[{type:t.NgModule,args:[{id:"wechaty",declarations:[l],exports:[l]}]}],e.VERSION=h,e.WechatyComponent=l,e.WechatyModule=p,e.ɵa=l,Object.defineProperty(e,"__esModule",{value:!0})}));
|