/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","app/core/config","vendor/npm/rxjs/Observable"],function(a){var b,c,d,e,f;return{setters:[function(a){b=a},function(a){c=a},function(a){d=a}],execute:function(){e=function(){function a(){this.observers={}}return a.prototype.getWebSocketUrl=function(){var a=window.location;return("https:"===a.protocol?"wss://":"ws://")+a.host+c["default"].appSubUrl+"/ws"},a.prototype.getConnection=function(){var a=this;return this.initPromise?this.initPromise:this.conn&&1===this.conn.readyState?Promise.resolve(this.conn):(this.initPromise=new Promise(function(b,c){console.log("Live: connecting..."),a.conn=new WebSocket(a.getWebSocketUrl()),a.conn.onclose=function(b){console.log("Live: websocket onclose",b),c({message:"Connection closed"}),a.initPromise=null,setTimeout(a.reconnect.bind(a),2e3)},a.conn.onmessage=function(b){a.handleMessage(b.data)},a.conn.onerror=function(b){a.initPromise=null,c({message:"Connection error"}),console.log("Live: websocket error",b)},a.conn.onopen=function(c){console.log("opened"),a.initPromise=null,b(a.conn)}}),this.initPromise)},a.prototype.handleMessage=function(a){if(a=JSON.parse(a),!a.stream)return void console.log("Error: stream message without stream!",a);var b=this.observers[a.stream];return b?void b.next(a):void this.removeObserver(a.stream,null)},a.prototype.reconnect=function(){var a=this;0!==b["default"].keys(this.observers).length&&(console.log("LiveSrv: Reconnecting"),this.getConnection().then(function(c){b["default"].each(a.observers,function(b,c){a.send({action:"subscribe",stream:c})})}))},a.prototype.send=function(a){this.conn.send(JSON.stringify(a))},a.prototype.addObserver=function(a,b){var c=this;this.observers[a]=b,this.getConnection().then(function(b){c.send({action:"subscribe",stream:a})})},a.prototype.removeObserver=function(a,b){var c=this;console.log("unsubscribe",a),delete this.observers[a],this.getConnection().then(function(b){c.send({action:"unsubscribe",stream:a})})},a.prototype.subscribe=function(a){var b=this;return console.log("LiveSrv.subscribe: "+a),d.Observable.create(function(c){return b.addObserver(a,c),function(){b.removeObserver(a,c)}})},a}(),a("LiveSrv",e),f=new e,a("liveSrv",f)}}});