@chatie/angular
Advanced tools
Comparing version 0.1.31 to 0.1.32
@@ -7,3 +7,3 @@ (function (global, factory) { | ||
const version = "0.1.31"; | ||
const version = "0.1.32"; | ||
@@ -317,4 +317,10 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
socketOnClose(event) { | ||
this.log.verbose('IoService', 'socketOnClose(%s)', event); | ||
/** | ||
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent | ||
* code: 1006 CLOSE_ABNORMAL | ||
* - Reserved. Used to indicate that a connection was closed abnormally | ||
* (that is, with no close frame being sent) when a status code is expected. | ||
*/ | ||
socketOnClose(closeEvent) { | ||
this.log.verbose('IoService', 'socketOnClose({code:%s, reason:%s, returnValue:%s})', closeEvent.code, closeEvent.reason, closeEvent.returnValue); | ||
this.socketUpdateState(); | ||
@@ -336,3 +342,3 @@ /** | ||
this._websocket = null; | ||
if (!event.wasClean) { | ||
if (!closeEvent.wasClean) { | ||
this.log.warn('IoService', 'socketOnClose() event.wasClean FALSE'); | ||
@@ -339,0 +345,0 @@ // TODO emit error |
@@ -34,3 +34,3 @@ var __generator = (this && this.__generator) || function (thisArg, body) { | ||
'use strict'; | ||
var version = "0.1.31"; | ||
var version = "0.1.32"; | ||
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -419,5 +419,11 @@ return new (P || (P = Promise))(function (resolve, reject) { | ||
}; | ||
IoService.prototype.socketOnClose = function (event) { | ||
/** | ||
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent | ||
* code: 1006 CLOSE_ABNORMAL | ||
* - Reserved. Used to indicate that a connection was closed abnormally | ||
* (that is, with no close frame being sent) when a status code is expected. | ||
*/ | ||
IoService.prototype.socketOnClose = function (closeEvent) { | ||
var _this = this; | ||
this.log.verbose('IoService', 'socketOnClose(%s)', event); | ||
this.log.verbose('IoService', 'socketOnClose({code:%s, reason:%s, returnValue:%s})', closeEvent.code, closeEvent.reason, closeEvent.returnValue); | ||
this.socketUpdateState(); | ||
@@ -446,3 +452,3 @@ /** | ||
this._websocket = null; | ||
if (!event.wasClean) { | ||
if (!closeEvent.wasClean) { | ||
this.log.warn('IoService', 'socketOnClose() event.wasClean FALSE'); | ||
@@ -449,0 +455,0 @@ // TODO emit error |
@@ -1,1 +0,1 @@ | ||
var __generator=this&&this.__generator||function(e,t){function o(e){return function(t){return n([e,t])}}function n(o){if(s)throw new TypeError("Generator is already executing.");for(;a;)try{if(s=1,i&&(r=i[2&o[0]?"return":o[0]?"throw":"next"])&&!(r=r.call(i,o[1])).done)return r;switch(i=0,r&&(o=[0,r.value]),o[0]){case 0:case 1:r=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,i=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(r=a.trys,!(r=r.length>0&&r[r.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){a.label=o[1];break}if(6===o[0]&&a.label<r[1]){a.label=r[1],r=o;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(o);break}r[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],i=0}finally{s=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}var s,i,r,c,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return c={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c};!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs/Rx"),require("brolog"),require("state-switch")):"function"==typeof define&&define.amd?define(["exports","@angular/core","rxjs/Rx","brolog","state-switch"],t):t((e.chatie=e.chatie||{},e.chatie.wechaty=e.chatie.wechaty||{}),e.ng.core,e.rxjs_Rx,e.brolog,e.stateSwitch)}(this,function(e,t,o,n,s){"use strict";var i="0.1.31",r=function(e,t,o,n){return new(o||(o=Promise))(function(s,i){function r(e){try{a(n.next(e))}catch(e){i(e)}}function c(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){e.done?s(e.value):new o(function(t){t(e.value)}).then(r,c)}a((n=n.apply(e,t||[])).next())})};!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"}(e.ReadyState||(e.ReadyState={}));var c=function(){function t(){this.version=i,this.autoReconnect=!0,this.log=n.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() v%s",this.version)}return Object.defineProperty(t.prototype,"readyState",{get:function(){return this._readyState.asObservable()},enumerable:!0,configurable:!0}),t.prototype.init=function(){return r(this,void 0,void 0,function(){var t,n=this;return __generator(this,function(i){switch(i.label){case 0:if(this.log.verbose("IoService","init()"),this.stateSwitch)throw new Error("re-init");this._readyState=new o.BehaviorSubject(e.ReadyState.CLOSED),this.stateSwitch=new s.StateSwitch("IoService","close",this.log),this.stateSwitch.setLog(this.log),i.label=1;case 1:return i.trys.push([1,4,,5]),[4,this.initStateDealer()];case 2:return i.sent(),[4,this.initRxSocket()];case 3:return i.sent(),[3,5];case 4:throw t=i.sent(),this.log.silly("IoService","init() exception: %s",t.message),t;case 5:return this.snapshot={readyState:e.ReadyState.CLOSED,socket:null},this.readyState.subscribe(function(t){n.log.silly("IoService","init() readyState.subscribe(%s)",e.ReadyState[t]),n.snapshot.readyState=t}),this.socket.subscribe(function(e){n.log.silly("IoService","init() socket.subscribe(%s)",e),n.snapshot.socket=e}),[2]}})})},t.prototype.token=function(e){return this.log.silly("IoService","token(%s)",e),e&&(this._token=e),this._token},t.prototype.start=function(){return r(this,void 0,void 0,function(){var e;return __generator(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("open"===this.stateSwitch.target())throw new Error("stateSwitch target is already `open`");if(this.stateSwitch.inprocess())throw new Error("stateSwitch inprocess() is true");this.stateSwitch.target("open"),this.stateSwitch.current("open",!1),this.autoReconnect=!0,t.label=1;case 1:return t.trys.push([1,3,,4]),[4,this.connectRxSocket()];case 2:return t.sent(),this.stateSwitch.current("open",!0),[3,4];case 3:return e=t.sent(),this.log.warn("IoService","start() failed:%s",e.message),this.stateSwitch.target("close"),this.stateSwitch.current("close",!0),[3,4];case 4:return[2]}})})},t.prototype.stop=function(){return r(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:if(this.log.verbose("IoService","stop()"),"close"===this.stateSwitch.target()){if(this.log.warn("IoService","stop() stateSwitch target is already `close`"),this.stateSwitch.inprocess())throw new Error("stateSwitch inprocess() is true");return[2]}if(this.stateSwitch.target("close"),this.stateSwitch.current("close",!1),this.autoReconnect=!1,!this._websocket)throw new Error("no websocket");return[4,this.socketClose(1e3,"IoService.stop()")];case 1:return e.sent(),this.stateSwitch.current("close",!0),[2]}})})},t.prototype.restart=function(){return r(this,void 0,void 0,function(){var e;return __generator(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]}})})},t.prototype.initStateDealer=function(){var t=this;this.log.verbose("IoService","initStateDealer()"),this.readyState.filter(function(t){return t===e.ReadyState.OPEN}).subscribe(function(e){return t.statusOnOpen()})},t.prototype.initRxSocket=function(){var e=this;if(this.log.verbose("IoService","initRxSocket()"),this.socket)throw new Error("re-init is not permitted");this.moObserver={next:this.socketSend.bind(this),error:this.socketClose.bind(this),complete:this.socketClose.bind(this)};var t=o.Observable.create(function(t){return e.log.verbose("IoService","initRxSocket() Observable.create()"),e.mtObserver=t,e.socketClose.bind(e)});this.socket=o.Subject.create(this.moObserver,t)},t.prototype.connectRxSocket=function(){return r(this,void 0,void 0,function(){var e,t=this;return __generator(this,function(o){if(this.log.verbose("IoService","connectRxSocket()"),this._websocket)throw new Error("already has a websocket");if("open"!==this.stateSwitch.target()||"open"!==this.stateSwitch.current()||this.stateSwitch.stable())throw new Error("switch state not right");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(){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]})})},t.prototype.endPoint=function(){var e=this.ENDPOINT+this._token;return this.log.verbose("IoService","endPoint() => %s",e),e},t.prototype.statusOnOpen=function(){this.log.verbose("IoService","statusOnOpen()"),this.socketSendBuffer();var e={name:"update",payload:"onOpen"};this.socket.next(e)},t.prototype.ding=function(e){return r(this,void 0,void 0,function(){var t;return __generator(this,function(o){return this.log.verbose("IoService","ding(%s)",e),t={name:"ding",payload:e},this.socket.next(t),[2]})})},t.prototype.socketClose=function(t,o){return r(this,void 0,void 0,function(){var n,s=this;return __generator(this,function(i){switch(i.label){case 0:if(this.log.verbose("IoService","socketClose()"),!this._websocket)throw new Error("no websocket");return this._websocket.close(t,o),this.socketUpdateState(),n=new Promise(function(t){s.readyState.filter(function(t){return t===e.ReadyState.CLOSED}).subscribe(t)}),[4,n];case 1:return i.sent(),this._websocket=null,[2]}})})},t.prototype.socketSend=function(t){if(this.log.silly("IoService","socketSend({name:%s, payload:%s})",t.name,t.payload),this._websocket||this.log.silly("IoService","socketSend() no _websocket"),t){this.log.silly("IoService","socketSend() buf len: %d",this.sendBuffer.length);var o=JSON.stringify(t);this.sendBuffer.push(o)}this.snapshot.readyState===e.ReadyState.OPEN&&this.socketSendBuffer()},t.prototype.socketSendBuffer=function(){if(this.log.silly("IoService","socketSendBuffer() length:%s",this.sendBuffer.length),!this._websocket)throw new Error("socketSendBuffer(): no _websocket");for(;this.sendBuffer.length;){this.log.silly("IoService","socketSendBuffer() length: %d",this.sendBuffer.length);var e=this.sendBuffer.shift();this._websocket.send(e)}},t.prototype.socketUpdateState=function(){this.log.verbose("IoService","socketUpdateState() is %s",e.ReadyState[this._websocket.readyState]),this._readyState.next(this._websocket.readyState)},t.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)},t.prototype.socketOnError=function(e){this.log.silly("IoService","socketOnError(%s)",e)},t.prototype.socketOnClose=function(e){var t=this;this.log.verbose("IoService","socketOnClose(%s)",e),this.socketUpdateState(),this.autoReconnect?(this.stateSwitch.current("open",!1),setTimeout(function(){return r(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,this.connectRxSocket()];case 1:return e.sent(),this.stateSwitch.current("open",!0),[2]}})})},1e3)):(this.stateSwitch.target("close"),this.stateSwitch.current("close",!0)),this._websocket=null,e.wasClean||this.log.warn("IoService","socketOnClose() event.wasClean FALSE")},t}(),a=function(e,t,o,n){return new(o||(o=Promise))(function(s,i){function r(e){try{a(n.next(e))}catch(e){i(e)}}function c(e){try{a(n.throw(e))}catch(e){i(e)}}function a(e){e.done?s(e.value):new o(function(t){t(e.value)}).then(r,c)}a((n=n.apply(e,t||[])).next())})},h=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.version=i,this.counter=0,this.timestamp=new Date,this.log.verbose("WechatyComponent","constructor() v%s",this.version)}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();return this._token===t?void this.log.silly("WechatyComponent","set token(%s) not new",t):(this._token=t,this.ioService?(this.log.silly("WechatyComponent","set token(%s) reloading ioService now...",t),this.ioService.token(this.token),void this.ioService.restart()):(this.log.silly("WechatyComponent","set token() skip token init value"),void this.log.silly("WechatyComponent","set token() because ioService will do it inside ngOnInit()")))},enumerable:!0,configurable:!0}),e.prototype.ngOnInit=function(){return a(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this.log.verbose("WechatyComponent","ngOninit() with token: "+this.token),this.ioService=new c,[4,this.ioService.init()];case 1:return e.sent(),this.token?(this.ioService.token(this.token),[4,this.ioService.start()]):[3,3];case 2:e.sent(),e.label=3;case 3:return this.ioService.socket.subscribe(this.onIo.bind(this)),[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.socket.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.socket.next(t)},e.prototype.startTimer=function(){var e=this;this.log.verbose("WechatyComponent","startTimer()"),this.ender=new o.Subject,this.ngZone.runOutsideAngular(function(){e.timer=o.Observable.interval(3e3).do(function(t){e.log.verbose("do"," %d",t)}).takeUntil(e.ender).share()}),this.timerSub=this.timer.subscribe(function(t){if(e.counter=t,!e.ioService)throw new Error("no ioService");e.ioService.ding(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.socket.next(t)},Object.defineProperty(e.prototype,"readyState",{get:function(){return this.ioService.readyState},enumerable:!0,configurable:!0}),e}();h.decorators=[{type:t.Component,args:[{selector:"wechaty",template:"<ng-content></ng-content>"}]}],h.ctorParameters=function(){return[{type:n.Brolog},{type:t.NgZone}]},h.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 u=function(){function e(){}return e}();u.decorators=[{type:t.NgModule,args:[{id:"wechaty",declarations:[h],exports:[h]}]}],u.ctorParameters=function(){return[]},e.WechatyModule=u,e.WechatyComponent=h,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
var __generator=this&&this.__generator||function(e,t){function o(e){return function(t){return n([e,t])}}function n(o){if(s)throw new TypeError("Generator is already executing.");for(;a;)try{if(s=1,r&&(i=r[2&o[0]?"return":o[0]?"throw":"next"])&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[0,i.value]),o[0]){case 0:case 1:i=o;break;case 4:return a.label++,{value:o[1],done:!1};case 5:a.label++,r=o[1],o=[0];continue;case 7:o=a.ops.pop(),a.trys.pop();continue;default:if(i=a.trys,!(i=i.length>0&&i[i.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){a.label=o[1];break}if(6===o[0]&&a.label<i[1]){a.label=i[1],i=o;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(o);break}i[2]&&a.ops.pop(),a.trys.pop();continue}o=t.call(e,a)}catch(e){o=[6,e],r=0}finally{s=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}var s,r,i,c,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return c={next:o(0),throw:o(1),return:o(2)},"function"==typeof Symbol&&(c[Symbol.iterator]=function(){return this}),c};!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs/Rx"),require("brolog"),require("state-switch")):"function"==typeof define&&define.amd?define(["exports","@angular/core","rxjs/Rx","brolog","state-switch"],t):t((e.chatie=e.chatie||{},e.chatie.wechaty=e.chatie.wechaty||{}),e.ng.core,e.rxjs_Rx,e.brolog,e.stateSwitch)}(this,function(e,t,o,n,s){"use strict";var r="0.1.32",i=function(e,t,o,n){return new(o||(o=Promise))(function(s,r){function i(e){try{a(n.next(e))}catch(e){r(e)}}function c(e){try{a(n.throw(e))}catch(e){r(e)}}function a(e){e.done?s(e.value):new o(function(t){t(e.value)}).then(i,c)}a((n=n.apply(e,t||[])).next())})};!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"}(e.ReadyState||(e.ReadyState={}));var c=function(){function t(){this.version=r,this.autoReconnect=!0,this.log=n.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() v%s",this.version)}return Object.defineProperty(t.prototype,"readyState",{get:function(){return this._readyState.asObservable()},enumerable:!0,configurable:!0}),t.prototype.init=function(){return i(this,void 0,void 0,function(){var t,n=this;return __generator(this,function(r){switch(r.label){case 0:if(this.log.verbose("IoService","init()"),this.stateSwitch)throw new Error("re-init");this._readyState=new o.BehaviorSubject(e.ReadyState.CLOSED),this.stateSwitch=new s.StateSwitch("IoService","close",this.log),this.stateSwitch.setLog(this.log),r.label=1;case 1:return r.trys.push([1,4,,5]),[4,this.initStateDealer()];case 2:return r.sent(),[4,this.initRxSocket()];case 3:return r.sent(),[3,5];case 4:throw t=r.sent(),this.log.silly("IoService","init() exception: %s",t.message),t;case 5:return this.snapshot={readyState:e.ReadyState.CLOSED,socket:null},this.readyState.subscribe(function(t){n.log.silly("IoService","init() readyState.subscribe(%s)",e.ReadyState[t]),n.snapshot.readyState=t}),this.socket.subscribe(function(e){n.log.silly("IoService","init() socket.subscribe(%s)",e),n.snapshot.socket=e}),[2]}})})},t.prototype.token=function(e){return this.log.silly("IoService","token(%s)",e),e&&(this._token=e),this._token},t.prototype.start=function(){return i(this,void 0,void 0,function(){var e;return __generator(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("open"===this.stateSwitch.target())throw new Error("stateSwitch target is already `open`");if(this.stateSwitch.inprocess())throw new Error("stateSwitch inprocess() is true");this.stateSwitch.target("open"),this.stateSwitch.current("open",!1),this.autoReconnect=!0,t.label=1;case 1:return t.trys.push([1,3,,4]),[4,this.connectRxSocket()];case 2:return t.sent(),this.stateSwitch.current("open",!0),[3,4];case 3:return e=t.sent(),this.log.warn("IoService","start() failed:%s",e.message),this.stateSwitch.target("close"),this.stateSwitch.current("close",!0),[3,4];case 4:return[2]}})})},t.prototype.stop=function(){return i(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:if(this.log.verbose("IoService","stop()"),"close"===this.stateSwitch.target()){if(this.log.warn("IoService","stop() stateSwitch target is already `close`"),this.stateSwitch.inprocess())throw new Error("stateSwitch inprocess() is true");return[2]}if(this.stateSwitch.target("close"),this.stateSwitch.current("close",!1),this.autoReconnect=!1,!this._websocket)throw new Error("no websocket");return[4,this.socketClose(1e3,"IoService.stop()")];case 1:return e.sent(),this.stateSwitch.current("close",!0),[2]}})})},t.prototype.restart=function(){return i(this,void 0,void 0,function(){var e;return __generator(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]}})})},t.prototype.initStateDealer=function(){var t=this;this.log.verbose("IoService","initStateDealer()"),this.readyState.filter(function(t){return t===e.ReadyState.OPEN}).subscribe(function(e){return t.statusOnOpen()})},t.prototype.initRxSocket=function(){var e=this;if(this.log.verbose("IoService","initRxSocket()"),this.socket)throw new Error("re-init is not permitted");this.moObserver={next:this.socketSend.bind(this),error:this.socketClose.bind(this),complete:this.socketClose.bind(this)};var t=o.Observable.create(function(t){return e.log.verbose("IoService","initRxSocket() Observable.create()"),e.mtObserver=t,e.socketClose.bind(e)});this.socket=o.Subject.create(this.moObserver,t)},t.prototype.connectRxSocket=function(){return i(this,void 0,void 0,function(){var e,t=this;return __generator(this,function(o){if(this.log.verbose("IoService","connectRxSocket()"),this._websocket)throw new Error("already has a websocket");if("open"!==this.stateSwitch.target()||"open"!==this.stateSwitch.current()||this.stateSwitch.stable())throw new Error("switch state not right");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(){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]})})},t.prototype.endPoint=function(){var e=this.ENDPOINT+this._token;return this.log.verbose("IoService","endPoint() => %s",e),e},t.prototype.statusOnOpen=function(){this.log.verbose("IoService","statusOnOpen()"),this.socketSendBuffer();var e={name:"update",payload:"onOpen"};this.socket.next(e)},t.prototype.ding=function(e){return i(this,void 0,void 0,function(){var t;return __generator(this,function(o){return this.log.verbose("IoService","ding(%s)",e),t={name:"ding",payload:e},this.socket.next(t),[2]})})},t.prototype.socketClose=function(t,o){return i(this,void 0,void 0,function(){var n,s=this;return __generator(this,function(r){switch(r.label){case 0:if(this.log.verbose("IoService","socketClose()"),!this._websocket)throw new Error("no websocket");return this._websocket.close(t,o),this.socketUpdateState(),n=new Promise(function(t){s.readyState.filter(function(t){return t===e.ReadyState.CLOSED}).subscribe(t)}),[4,n];case 1:return r.sent(),this._websocket=null,[2]}})})},t.prototype.socketSend=function(t){if(this.log.silly("IoService","socketSend({name:%s, payload:%s})",t.name,t.payload),this._websocket||this.log.silly("IoService","socketSend() no _websocket"),t){this.log.silly("IoService","socketSend() buf len: %d",this.sendBuffer.length);var o=JSON.stringify(t);this.sendBuffer.push(o)}this.snapshot.readyState===e.ReadyState.OPEN&&this.socketSendBuffer()},t.prototype.socketSendBuffer=function(){if(this.log.silly("IoService","socketSendBuffer() length:%s",this.sendBuffer.length),!this._websocket)throw new Error("socketSendBuffer(): no _websocket");for(;this.sendBuffer.length;){this.log.silly("IoService","socketSendBuffer() length: %d",this.sendBuffer.length);var e=this.sendBuffer.shift();this._websocket.send(e)}},t.prototype.socketUpdateState=function(){this.log.verbose("IoService","socketUpdateState() is %s",e.ReadyState[this._websocket.readyState]),this._readyState.next(this._websocket.readyState)},t.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)},t.prototype.socketOnError=function(e){this.log.silly("IoService","socketOnError(%s)",e)},t.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.stateSwitch.current("open",!1),setTimeout(function(){return i(t,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return[4,this.connectRxSocket()];case 1:return e.sent(),this.stateSwitch.current("open",!0),[2]}})})},1e3)):(this.stateSwitch.target("close"),this.stateSwitch.current("close",!0)),this._websocket=null,e.wasClean||this.log.warn("IoService","socketOnClose() event.wasClean FALSE")},t}(),a=function(e,t,o,n){return new(o||(o=Promise))(function(s,r){function i(e){try{a(n.next(e))}catch(e){r(e)}}function c(e){try{a(n.throw(e))}catch(e){r(e)}}function a(e){e.done?s(e.value):new o(function(t){t(e.value)}).then(i,c)}a((n=n.apply(e,t||[])).next())})},h=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.version=r,this.counter=0,this.timestamp=new Date,this.log.verbose("WechatyComponent","constructor() v%s",this.version)}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();return this._token===t?void this.log.silly("WechatyComponent","set token(%s) not new",t):(this._token=t,this.ioService?(this.log.silly("WechatyComponent","set token(%s) reloading ioService now...",t),this.ioService.token(this.token),void this.ioService.restart()):(this.log.silly("WechatyComponent","set token() skip token init value"),void this.log.silly("WechatyComponent","set token() because ioService will do it inside ngOnInit()")))},enumerable:!0,configurable:!0}),e.prototype.ngOnInit=function(){return a(this,void 0,void 0,function(){return __generator(this,function(e){switch(e.label){case 0:return this.log.verbose("WechatyComponent","ngOninit() with token: "+this.token),this.ioService=new c,[4,this.ioService.init()];case 1:return e.sent(),this.token?(this.ioService.token(this.token),[4,this.ioService.start()]):[3,3];case 2:e.sent(),e.label=3;case 3:return this.ioService.socket.subscribe(this.onIo.bind(this)),[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.socket.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.socket.next(t)},e.prototype.startTimer=function(){var e=this;this.log.verbose("WechatyComponent","startTimer()"),this.ender=new o.Subject,this.ngZone.runOutsideAngular(function(){e.timer=o.Observable.interval(3e3).do(function(t){e.log.verbose("do"," %d",t)}).takeUntil(e.ender).share()}),this.timerSub=this.timer.subscribe(function(t){if(e.counter=t,!e.ioService)throw new Error("no ioService");e.ioService.ding(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.socket.next(t)},Object.defineProperty(e.prototype,"readyState",{get:function(){return this.ioService.readyState},enumerable:!0,configurable:!0}),e}();h.decorators=[{type:t.Component,args:[{selector:"wechaty",template:"<ng-content></ng-content>"}]}],h.ctorParameters=function(){return[{type:n.Brolog},{type:t.NgZone}]},h.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 u=function(){function e(){}return e}();u.decorators=[{type:t.NgModule,args:[{id:"wechaty",declarations:[h],exports:[h]}]}],u.ctorParameters=function(){return[]},e.WechatyModule=u,e.WechatyComponent=h,Object.defineProperty(e,"__esModule",{value:!0})}); |
@@ -77,3 +77,9 @@ import { Observable, Subject } from 'rxjs/Rx'; | ||
private socketOnError(event); | ||
private socketOnClose(event); | ||
/** | ||
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent | ||
* code: 1006 CLOSE_ABNORMAL | ||
* - Reserved. Used to indicate that a connection was closed abnormally | ||
* (that is, with no close frame being sent) when a status code is expected. | ||
*/ | ||
private socketOnClose(closeEvent); | ||
} |
@@ -312,4 +312,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
socketOnClose(event) { | ||
this.log.verbose('IoService', 'socketOnClose(%s)', event); | ||
/** | ||
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent | ||
* code: 1006 CLOSE_ABNORMAL | ||
* - Reserved. Used to indicate that a connection was closed abnormally | ||
* (that is, with no close frame being sent) when a status code is expected. | ||
*/ | ||
socketOnClose(closeEvent) { | ||
this.log.verbose('IoService', 'socketOnClose({code:%s, reason:%s, returnValue:%s})', closeEvent.code, closeEvent.reason, closeEvent.returnValue); | ||
this.socketUpdateState(); | ||
@@ -331,3 +337,3 @@ /** | ||
this._websocket = null; | ||
if (!event.wasClean) { | ||
if (!closeEvent.wasClean) { | ||
this.log.warn('IoService', 'socketOnClose() event.wasClean FALSE'); | ||
@@ -334,0 +340,0 @@ // TODO emit error |
{ | ||
"name": "@chatie/angular", | ||
"version": "0.1.31", | ||
"version": "0.1.32", | ||
"description": "Chatie Web Component NgModule for Angular", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
190312
2078