UNPKG

ng2-signalr

Version:
1 lines 14.7 kB
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports,require("rxjs/Subject"),require("@angular/core")):typeof define==="function"&&define.amd?define(["exports","rxjs/Subject","@angular/core"],factory):factory((global.ng=global.ng||{},global.ng["ng2-signalr"]=global.ng["ng2-signalr"]||{}),global.rxjs_Subject,global.ng.core)})(this,function(exports,rxjs_Subject,_angular_core){"use strict";var __extends=undefined&&undefined.__extends||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p];function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)};var BroadcastEventListener=function(_super){__extends(BroadcastEventListener,_super);function BroadcastEventListener(event){_super.call(this);this.event=event;if(event==null||event===""){throw new Error("Failed to create BroadcastEventListener. Argument 'event' can not be empty")}}return BroadcastEventListener}(rxjs_Subject.Subject);var SignalRConnectionMock=function(){function SignalRConnectionMock(_mockErrors$,_mockStatus$,_listeners){this._mockErrors$=_mockErrors$;this._mockStatus$=_mockStatus$;this._listeners=_listeners}Object.defineProperty(SignalRConnectionMock.prototype,"errors",{get:function(){return this._mockErrors$},enumerable:true,configurable:true});Object.defineProperty(SignalRConnectionMock.prototype,"status",{get:function(){return this._mockStatus$},enumerable:true,configurable:true});Object.defineProperty(SignalRConnectionMock.prototype,"id",{get:function(){return"xxxxxxxx-xxxx-xxxx-xxxxxxxxx"},enumerable:true,configurable:true});SignalRConnectionMock.prototype.stop=function(){};SignalRConnectionMock.prototype.start=function(){return Promise.resolve(null)};SignalRConnectionMock.prototype.invoke=function(method){var parameters=[];for(var _i=1;_i<arguments.length;_i++){parameters[_i-1]=arguments[_i]}return Promise.resolve(null)};SignalRConnectionMock.prototype.listen=function(listener){this._listeners[listener.event]=listener};SignalRConnectionMock.prototype.listenFor=function(event){var listener=new BroadcastEventListener(event);this.listen(listener);return listener};return SignalRConnectionMock}();var SignalRConnectionMockManager=function(){function SignalRConnectionMockManager(){this._errors$=new rxjs_Subject.Subject;this._status$=new rxjs_Subject.Subject;this._listeners={};this._object=new SignalRConnectionMock(this._errors$,this._status$,this._listeners)}Object.defineProperty(SignalRConnectionMockManager.prototype,"mock",{get:function(){return this._object},enumerable:true,configurable:true});Object.defineProperty(SignalRConnectionMockManager.prototype,"errors$",{get:function(){return this._errors$},enumerable:true,configurable:true});Object.defineProperty(SignalRConnectionMockManager.prototype,"status$",{get:function(){return this._status$},enumerable:true,configurable:true});Object.defineProperty(SignalRConnectionMockManager.prototype,"listeners",{get:function(){return this._listeners},enumerable:true,configurable:true});return SignalRConnectionMockManager}();var ConnectionStatus=function(){function ConnectionStatus(value){if(value==null||value<0){throw new Error("Failed to create ConnectionStatus. Argument 'name' can not be null or empty.")}this._value=value}Object.defineProperty(ConnectionStatus.prototype,"value",{get:function(){return this._value},enumerable:true,configurable:true});Object.defineProperty(ConnectionStatus.prototype,"name",{get:function(){return ConnectionStatus.names[Number.parseInt(this._value.toString())]},enumerable:true,configurable:true});ConnectionStatus.prototype.toString=function(){return this.name};ConnectionStatus.prototype.equals=function(other){if(other==null){return false}return this._value===other.value};ConnectionStatus.names=["connecting","connected","reconnecting","","disconnected"];return ConnectionStatus}();var ConnectionStatuses=function(){function ConnectionStatuses(){}Object.defineProperty(ConnectionStatuses,"connecting",{get:function(){return ConnectionStatuses.statuses[0]},enumerable:true,configurable:true});Object.defineProperty(ConnectionStatuses,"connected",{get:function(){return ConnectionStatuses.statuses[1]},enumerable:true,configurable:true});Object.defineProperty(ConnectionStatuses,"reconnecting",{get:function(){return ConnectionStatuses.statuses[2]},enumerable:true,configurable:true});Object.defineProperty(ConnectionStatuses,"disconnected",{get:function(){return ConnectionStatuses.statuses[3]},enumerable:true,configurable:true});ConnectionStatuses.statuses=[new ConnectionStatus(0),new ConnectionStatus(1),new ConnectionStatus(2),new ConnectionStatus(4)];return ConnectionStatuses}();var SignalRConnection=function(){function SignalRConnection(jConnection,jProxy,zone,configuration){this._jProxy=jProxy;this._jConnection=jConnection;this._zone=zone;this._errors=this.wireUpErrorsAsObservable();this._status=this.wireUpStatusEventsAsObservable();this._configuration=configuration}Object.defineProperty(SignalRConnection.prototype,"errors",{get:function(){return this._errors},enumerable:true,configurable:true});Object.defineProperty(SignalRConnection.prototype,"status",{get:function(){return this._status},enumerable:true,configurable:true});SignalRConnection.prototype.start=function(){var _this=this;var jTransports=this.convertTransports(this._configuration.transport);var $promise=new Promise(function(resolve,reject){_this._jConnection.start({jsonp:_this._configuration.jsonp,transport:jTransports,withCredentials:_this._configuration.withCredentials}).done(function(){console.log("Connection established, ID: "+_this._jConnection.id);console.log("Connection established, Transport: "+_this._jConnection.transport.name);resolve(_this)}).fail(function(error){console.log("Could not connect");reject("Failed to connect. Error: "+error.message)})});return $promise};SignalRConnection.prototype.stop=function(){this._jConnection.stop()};Object.defineProperty(SignalRConnection.prototype,"id",{get:function(){return this._jConnection.id},enumerable:true,configurable:true});SignalRConnection.prototype.invoke=function(method){var _this=this;var parameters=[];for(var _i=1;_i<arguments.length;_i++){parameters[_i-1]=arguments[_i]}if(method==null){throw new Error("SignalRConnection: Failed to invoke. Argument 'method' can not be null")}this.log("SignalRConnection. Start invoking '"+method+"'...");var $promise=new Promise(function(resolve,reject){(_a=_this._jProxy).invoke.apply(_a,[method].concat(parameters)).done(function(result){_this.log("'"+method+"' invoked succesfully. Resolving promise...");resolve(result);_this.log("Promise resolved.")}).fail(function(err){console.log("Invoking '"+method+"' failed. Rejecting promise...");reject(err);console.log("Promise rejected.")});var _a});return $promise};SignalRConnection.prototype.listen=function(listener){var _this=this;if(listener==null){throw new Error("Failed to listen. Argument 'listener' can not be null")}this.log("SignalRConnection: Starting to listen to server event with name "+listener.event);this._jProxy.on(listener.event,function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i-0]=arguments[_i]}_this._zone.run(function(){var casted=null;if(args.length>0){casted=args[0]}_this.log("SignalRConnection.proxy.on invoked. Calling listener next() ...");listener.next(casted);_this.log("listener next() called.")})})};SignalRConnection.prototype.listenFor=function(event){if(event==null||event===""){throw new Error("Failed to listen. Argument 'event' can not be empty")}var listener=new BroadcastEventListener(event);this.listen(listener);return listener};SignalRConnection.prototype.convertTransports=function(transports){if(transports instanceof Array){return transports.map(function(t){return t.name})}return transports.name};SignalRConnection.prototype.wireUpErrorsAsObservable=function(){var sError=new rxjs_Subject.Subject;this._jConnection.error(function(error){sError.next(error)});return sError};SignalRConnection.prototype.wireUpStatusEventsAsObservable=function(){var _this=this;var sStatus=new rxjs_Subject.Subject;this._jConnection.stateChanged(function(change){_this._zone.run(function(){sStatus.next(new ConnectionStatus(change.newState))})});return sStatus};SignalRConnection.prototype.onBroadcastEventReceived=function(listener){var args=[];for(var _i=1;_i<arguments.length;_i++){args[_i-1]=arguments[_i]}this.log("SignalRConnection.proxy.on invoked. Calling listener next() ...");var casted=null;if(args.length>0){casted=args[0]}this._zone.run(function(){listener.next(casted)});this.log("listener next() called.")};SignalRConnection.prototype.log=function(){var args=[];for(var _i=0;_i<arguments.length;_i++){args[_i-0]=arguments[_i]}if(this._jConnection.logging===false){return}console.log(args.join(", "))};return SignalRConnection}();var ConnectionTransport=function(){function ConnectionTransport(name){if(name==null||name===""){throw new Error("Failed to create ConnectionTransport. Argument 'name' can not be null or empty.")}this._name=name}Object.defineProperty(ConnectionTransport.prototype,"name",{get:function(){return this._name},enumerable:true,configurable:true});ConnectionTransport.prototype.toString=function(){return this._name};ConnectionTransport.prototype.equals=function(other){if(other==null){return false}return this._name===other.name};return ConnectionTransport}();var ConnectionTransports=function(){function ConnectionTransports(){}Object.defineProperty(ConnectionTransports,"foreverFrame",{get:function(){return ConnectionTransports.transports[0]},enumerable:true,configurable:true});Object.defineProperty(ConnectionTransports,"longPolling",{get:function(){return ConnectionTransports.transports[1]},enumerable:true,configurable:true});Object.defineProperty(ConnectionTransports,"serverSentEvents",{get:function(){return ConnectionTransports.transports[2]},enumerable:true,configurable:true});Object.defineProperty(ConnectionTransports,"webSockets",{get:function(){return ConnectionTransports.transports[3]},enumerable:true,configurable:true});Object.defineProperty(ConnectionTransports,"auto",{get:function(){return ConnectionTransports.transports[4]},enumerable:true,configurable:true});ConnectionTransports.transports=[new ConnectionTransport("foreverFrame"),new ConnectionTransport("longPolling"),new ConnectionTransport("serverSentEvents"),new ConnectionTransport("webSockets"),new ConnectionTransport("auto")];return ConnectionTransports}();var SignalRConfiguration=function(){function SignalRConfiguration(){this.hubName=null;this.logging=false;this.qs=null;this.url=null;this.jsonp=false;this.withCredentials=false;this.transport=ConnectionTransports.auto}return SignalRConfiguration}();var SIGNALR_CONFIGURATION=new _angular_core.OpaqueToken("SIGNALR_CONFIGURATION");var SIGNALR_JCONNECTION_TOKEN=new _angular_core.OpaqueToken("SIGNALR_JCONNECTION_TOKEN");var SignalR=function(){function SignalR(configuration,zone,jHubConnectionFn){this._configuration=configuration;this._zone=zone;this._jHubConnectionFn=jHubConnectionFn}SignalR.prototype.createConnection=function(options){var status;var configuration=this.merge(options?options:{});try{var serializedQs=JSON.stringify(configuration.qs);var serializedTransport=JSON.stringify(configuration.transport);if(configuration.logging){console.log("Creating connecting with...");console.log("configuration:[url: '"+configuration.url+"'] ...");console.log("configuration:[hubName: '"+configuration.hubName+"'] ...");console.log("configuration:[qs: '"+serializedQs+"'] ...");console.log("configuration:[transport: '"+serializedTransport+"'] ...")}}catch(err){}var jConnection=this._jHubConnectionFn(configuration.url);jConnection.logging=configuration.logging;jConnection.qs=configuration.qs;var jProxy=jConnection.createHubProxy(configuration.hubName);jProxy.on("noOp",function(){});var hubConnection=new SignalRConnection(jConnection,jProxy,this._zone,configuration);return hubConnection};SignalR.prototype.connect=function(options){return this.createConnection(options).start()};SignalR.prototype.merge=function(overrides){var merged=new SignalRConfiguration;merged.hubName=overrides.hubName||this._configuration.hubName;merged.url=overrides.url||this._configuration.url;merged.qs=overrides.qs||this._configuration.qs;merged.logging=this._configuration.logging;merged.jsonp=overrides.jsonp||this._configuration.jsonp;merged.withCredentials=overrides.withCredentials||this._configuration.withCredentials;merged.transport=overrides.transport||this._configuration.transport;return merged};SignalR.decorators=[{type:_angular_core.Injectable}];SignalR.ctorParameters=function(){return[{type:SignalRConfiguration},{type:_angular_core.NgZone},{type:Function,decorators:[{type:_angular_core.Inject,args:[SIGNALR_JCONNECTION_TOKEN]}]}]};return SignalR}();var SIGNALR_CONFIGURATION$1=new _angular_core.OpaqueToken("SIGNALR_CONFIGURATION");function createSignalr$1(configuration,zone){var jConnectionFn=getJConnectionFn$1();return new SignalR(configuration,zone,jConnectionFn)}function getJConnectionFn$1(){var jQuery=getJquery$1();var hubConnectionFn=window.jQuery.hubConnection;if(hubConnectionFn==null){throw new Error("Signalr failed to initialize. Script 'jquery.signalR.js' is missing. Please make sure to include 'jquery.signalR.js' script.")}return hubConnectionFn}function getJquery$1(){var jQuery=window.jQuery;if(jQuery==null){throw new Error("Signalr failed to initialize. Script 'jquery.js' is missing. Please make sure to include jquery script.")}return jQuery}var SignalRModule$1=function(){function SignalRModule(){}SignalRModule.forRoot=function(getSignalRConfiguration){return{ngModule:SignalRModule,providers:[{provide:SIGNALR_CONFIGURATION$1,useFactory:getSignalRConfiguration},{deps:[SIGNALR_CONFIGURATION$1,_angular_core.NgZone],provide:SignalR,useFactory:createSignalr$1}]}};SignalRModule.forChild=function(){throw new Error("forChild method not implemented")};SignalRModule.decorators=[{type:_angular_core.NgModule,args:[{providers:[{provide:SignalR,useValue:SignalR}]}]}];SignalRModule.ctorParameters=function(){return[]};return SignalRModule}();exports.SignalRConnectionMock=SignalRConnectionMock;exports.SignalRConnectionMockManager=SignalRConnectionMockManager;exports.ConnectionStatus=ConnectionStatus;exports.ConnectionStatuses=ConnectionStatuses;exports.BroadcastEventListener=BroadcastEventListener;exports.SignalRConnection=SignalRConnection;exports.ConnectionTransport=ConnectionTransport;exports.ConnectionTransports=ConnectionTransports;exports.SignalR=SignalR;exports.SignalRConfiguration=SignalRConfiguration;exports.SignalRModule=SignalRModule$1;Object.defineProperty(exports,"__esModule",{value:true})});