UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

18 lines (15 loc) 4.76 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var n=function(){return(n=Object.assign||function(n){for(var t,e=1,o=arguments.length;e<o;e++)for(var c in t=arguments[e])Object.prototype.hasOwnProperty.call(t,c)&&(n[c]=t[c]);return n}).apply(this,arguments)};var t,e=new(function(){function e(){this.connectionUrl=null,this.token=null,this.sequence=1,this.connectFailCount=0,this.stop=!1,this.platform=""}return e.prototype.initialize=function(e,o){var c=this,i={forceConnection:!0,connectionUrl:this.connectionUrl,webSocketConnector:WebSocket},s=Object.assign({},i,o),r=s.connectionUrl,l=s.forceConnection,a=s.webSocketConnector,u=s.platform,p=function(n,t){var e={};for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&t.indexOf(o)<0&&(e[o]=n[o]);if(null!=n&&"function"==typeof Object.getOwnPropertySymbols){var c=0;for(o=Object.getOwnPropertySymbols(n);c<o.length;c++)t.indexOf(o[c])<0&&Object.prototype.propertyIsEnumerable.call(n,o[c])&&(e[o[c]]=n[o[c]])}return e}(s,["connectionUrl","forceConnection","webSocketConnector","platform"]);return u&&(this.platform=u),l&&(this.stop=!1),new Promise((function(i,s){if(c.conn)i();else{if(null==r)return console.log("websocket must have connection url"),void s(new Error("websocket must have connection url"));0===c.connectFailCount&&console.log("websocket connecting to "+r),t=a,c.connectingCallback&&c.connectingCallback();var l,f=/^(?:https?|wss?):(?:\/\/)?[^/]*/.exec(r);if(!f){var h="websocket failed to parse origin from "+r;return console.warn(h),void s(new Error(h))}if(l=f[0],"android"===u){var b=l.split(":"),C=b[2];"80"!==C&&"443"!==C||(l=b[0]+":"+b[1])}c.conn=new t(r,[],n({headers:{origin:l}},p||{})),c.connectionUrl=r,c.token=e,c.conn.onopen=function(){e&&c.sendMessage("authentication_challenge",{token:e}),c.connectFailCount>0?(console.log("websocket re-established connection"),c.reconnectCallback&&c.reconnectCallback()):c.firstConnectCallback&&c.firstConnectCallback(),c.connectFailCount=0,i()},c.conn.onclose=function(){c.conn=void 0,c.sequence=1,0===c.connectFailCount&&console.log("websocket closed"),c.connectFailCount++,c.closeCallback&&c.closeCallback(c.connectFailCount);var n=3e3;c.connectFailCount>7&&(n=3e3*c.connectFailCount)>3e5&&(n=3e5),c.connectionTimeout&&clearTimeout(c.connectionTimeout),c.connectionTimeout=setTimeout((function(){c.stop?clearTimeout(c.connectionTimeout):c.initialize(e,o)}),n)},c.conn.onerror=function(n){c.connectFailCount<=1&&(console.log("websocket error"),console.log(n)),c.errorCallback&&c.errorCallback(n)},c.conn.onmessage=function(n){var t=JSON.parse(n.data);t.seq_reply?t.error&&console.warn(t):c.eventCallback&&c.eventCallback(t)}}}))},e.prototype.setConnectingCallback=function(n){this.connectingCallback=n},e.prototype.setEventCallback=function(n){this.eventCallback=n},e.prototype.setFirstConnectCallback=function(n){this.firstConnectCallback=n},e.prototype.setReconnectCallback=function(n){this.reconnectCallback=n},e.prototype.setErrorCallback=function(n){this.errorCallback=n},e.prototype.setCloseCallback=function(n){this.closeCallback=n},e.prototype.close=function(n){void 0===n&&(n=!1),this.stop=n,this.connectFailCount=0,this.sequence=1,this.conn&&this.conn.readyState===t.OPEN&&(this.conn.onclose=function(){},this.conn.close(),this.conn=void 0,console.log("websocket closed"))},e.prototype.sendMessage=function(n,e){var o={action:n,seq:this.sequence++,data:e};this.conn&&this.conn.readyState===t.OPEN?this.conn.send(JSON.stringify(o)):this.conn&&this.conn.readyState!==t.CLOSED||(this.conn=void 0,this.initialize(this.token,{platform:this.platform}))},e.prototype.userTyping=function(n,t){this.sendMessage("user_typing",{channel_id:n,parent_id:t})},e.prototype.getStatuses=function(){this.sendMessage("get_statuses",null)},e.prototype.getStatusesByIds=function(n){this.sendMessage("get_statuses_by_ids",{user_ids:n})},e}());exports.default=e; //# sourceMappingURL=mattermost.websocket_client.js.map