UNPKG

ng2-signalr

Version:
16 lines (14 loc) 14.3 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("rxjs"),require("@angular/core")):"function"==typeof define&&define.amd?define("ng2-signalr",["exports","rxjs","@angular/core"],n):n((e=e||self)["ng2-signalr"]={},e.rxjs,e.ng.core)}(this,(function(e,n,t){"use strict"; /*! ***************************************************************************** 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 r=function(e,n){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t])})(e,n)};Object.create;function o(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function i(e,n){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var r,o,i=t.call(e),s=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)s.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(t=i.return)&&t.call(i)}finally{if(o)throw o.error}}return s}function s(){for(var e=[],n=0;n<arguments.length;n++)e=e.concat(i(arguments[n]));return e}Object.create;var u=function(e){function n(n){var t=e.call(this)||this;if(t.event=n,null==n||""===n)throw new Error("Failed to create BroadcastEventListener. Argument 'event' can not be empty");return t}return function(e,n){function t(){this.constructor=e}r(e,n),e.prototype=null===n?Object.create(n):(t.prototype=n.prototype,new t)}(n,e),n}(n.Subject),a=function(){function e(e,n,t){this._mockErrors$=e,this._mockStatus$=n,this._listeners=t}return Object.defineProperty(e.prototype,"errors",{get:function(){return this._mockErrors$},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"status",{get:function(){return this._mockStatus$.asObservable()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"id",{get:function(){return"xxxxxxxx-xxxx-xxxx-xxxxxxxxx"},enumerable:!1,configurable:!0}),e.prototype.stop=function(){},e.prototype.start=function(){return Promise.resolve(null)},e.prototype.invoke=function(e){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return Promise.resolve(null)},e.prototype.listen=function(e){this._listeners[e.event]=e},e.prototype.listenFor=function(e){var n=new u(e);return this.listen(n),n},e.prototype.listenForRaw=function(e){var n=new u(e);return this._listeners[n.event]=n,n},e.prototype.stopListening=function(e){delete this._listeners[e.event]},e}(),c=function(){function e(){this._errors$=new n.Subject,this._status$=new n.Subject,this._listeners={},this._object=new a(this._errors$,this._status$,this._listeners)}return Object.defineProperty(e.prototype,"mock",{get:function(){return this._object},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"errors$",{get:function(){return this._errors$},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"status$",{get:function(){return this._status$},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"listeners",{get:function(){return this._listeners},enumerable:!1,configurable:!0}),e}(),l=function(){function e(e){if(null==e||e<0)throw new Error("Failed to create ConnectionStatus. Argument 'name' can not be null or empty.");this._value=e}return Object.defineProperty(e.prototype,"value",{get:function(){return this._value},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"name",{get:function(){return e.names[Number(this._value.toString())]},enumerable:!1,configurable:!0}),e.prototype.toString=function(){return this.name},e.prototype.equals=function(e){return null!=e&&this._value===e.value},e}();l.names=["connecting","connected","reconnecting","","disconnected"];var f=function(){function e(){}return Object.defineProperty(e,"connecting",{get:function(){return e.statuses[0]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"connected",{get:function(){return e.statuses[1]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"reconnecting",{get:function(){return e.statuses[2]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"disconnected",{get:function(){return e.statuses[3]},enumerable:!1,configurable:!0}),e}();f.statuses=[new l(0),new l(1),new l(2),new l(4)];var g=function(){function e(e,n,t,r){this._enabledLogging=!0,this._jProxy=n,this._jConnection=e,this._zone=t,this._errors=this.wireUpErrorsAsObservable(),this._status=this.wireUpStatusEventsAsObservable(),this._configuration=r,this._enabledLogging=r.logging,this._listeners={}}return Object.defineProperty(e.prototype,"errors",{get:function(){return this._errors},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"status",{get:function(){return this._status},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"enabledLogging",{get:function(){return this._enabledLogging},set:function(e){this._enabledLogging=e},enumerable:!1,configurable:!0}),e.prototype.start=function(){var e=this,n=this.convertTransports(this._configuration.transport);return new Promise((function(t,r){e._jConnection.start({jsonp:e._configuration.jsonp,pingInterval:e._configuration.pingInterval,transport:n,withCredentials:e._configuration.withCredentials}).done((function(){e.log("Connection established, ID: "+e._jConnection.id),e.log("Connection established, Transport: "+e._jConnection.transport.name),t(e)})).fail((function(n){e.log("Could not connect"),r("Failed to connect. Error: "+n.message)}))}))},e.prototype.stop=function(){this._jConnection.stop()},Object.defineProperty(e.prototype,"id",{get:function(){return this._jConnection.id},enumerable:!1,configurable:!0}),e.prototype.invoke=function(e){for(var n=this,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];if(null==e)throw new Error("SignalRConnection: Failed to invoke. Argument 'method' can not be null");this.log("SignalRConnection. Start invoking '"+e+"'...");var o=new Promise((function(r,o){var i;(i=n._jProxy).invoke.apply(i,s([e],t)).done((function(t){n.log("'"+e+"' invoked succesfully. Resolving promise..."),r(t),n.log("Promise resolved.")})).fail((function(t){n.log("Invoking '"+e+"' failed. Rejecting promise..."),o(t),n.log("Promise rejected.")}))}));return o},e.prototype.listen=function(e){var n=this;if(null==e)throw new Error("Failed to listen. Argument 'listener' can not be null");this.setListener((function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];n.run((function(){var r=null;t.length>0&&(r=t[0]),n.log("SignalRConnection.proxy.on invoked. Calling listener next() ..."),e.next(r),n.log("listener next() called.")}),n._configuration.executeEventsInZone)}),e)},e.prototype.stopListening=function(e){var n,t;if(null==e)throw new Error("Failed to listen. Argument 'listener' can not be null");this.log("SignalRConnection: Stopping listening to server event with name "+e.event),this._listeners[e.event]||(this._listeners[e.event]=[]);try{for(var r=o(this._listeners[e.event]),i=r.next();!i.done;i=r.next()){var s=i.value;this._jProxy.off(e.event,s)}}catch(e){n={error:e}}finally{try{i&&!i.done&&(t=r.return)&&t.call(r)}finally{if(n)throw n.error}}this._listeners[e.event]=[]},e.prototype.listenFor=function(e){if(null==e||""===e)throw new Error("Failed to listen. Argument 'event' can not be empty");var n=new u(e);return this.listen(n),n},e.prototype.listenForRaw=function(e){var n=this;if(null==e||""===e)throw new Error("Failed to listen. Argument 'event' can not be empty");var t=new u(e);return this.setListener((function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];n.run((function(){e.length,n.log("SignalRConnection.proxy.on invoked. Calling listener next() ..."),t.next(e),n.log("listener next() called.")}),n._configuration.executeEventsInZone)}),t),t},e.prototype.setListener=function(e,n){this.log("SignalRConnection: Starting to listen to server event with name "+n.event),this._jProxy.on(n.event,e),null==this._listeners[n.event]&&(this._listeners[n.event]=[]),this._listeners[n.event].push(e)},e.prototype.convertTransports=function(e){return e instanceof Array?e.map((function(e){return e.name})):e.name},e.prototype.wireUpErrorsAsObservable=function(){var e=this,t=new n.Subject;return this._jConnection.error((function(n){e.run((function(){return t.next(n)}),e._configuration.executeErrorsInZone)})),t},e.prototype.wireUpStatusEventsAsObservable=function(){var e=this,t=new n.Subject;return this._jConnection.stateChanged((function(n){e.run((function(){return t.next(new l(n.newState))}),e._configuration.executeStatusChangeInZone)})),t.asObservable()},e.prototype.onBroadcastEventReceived=function(e){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];this.log("SignalRConnection.proxy.on invoked. Calling listener next() ...");var r=null;n.length>0&&(r=n[0]),this.run((function(){e.next(r)}),this._configuration.executeEventsInZone),this.log("listener next() called.")},e.prototype.log=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];!1!==this.enabledLogging&&console.log(e.join(", "))},e.prototype.run=function(e,n){n?this._zone.run((function(){return e()})):this._zone.runOutsideAngular((function(){return e()}))},e}(),p=function(){function e(e){if(null==e||""===e)throw new Error("Failed to create ConnectionTransport. Argument 'name' can not be null or empty.");this._name=e}return Object.defineProperty(e.prototype,"name",{get:function(){return this._name},enumerable:!1,configurable:!0}),e.prototype.toString=function(){return this._name},e.prototype.equals=function(e){return null!=e&&this._name===e.name},e}(),h=function(){function e(){}return Object.defineProperty(e,"foreverFrame",{get:function(){return e.transports[0]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"longPolling",{get:function(){return e.transports[1]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"serverSentEvents",{get:function(){return e.transports[2]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"webSockets",{get:function(){return e.transports[3]},enumerable:!1,configurable:!0}),Object.defineProperty(e,"auto",{get:function(){return e.transports[4]},enumerable:!1,configurable:!0}),e}();h.transports=[new p("foreverFrame"),new p("longPolling"),new p("serverSentEvents"),new p("webSockets"),new p("auto")];var b=function(){this.hubName=null,this.logging=!1,this.qs=null,this.url=null,this.jsonp=!1,this.withCredentials=!1,this.transport=h.auto,this.executeEventsInZone=!0,this.executeErrorsInZone=!1,this.executeStatusChangeInZone=!0,this.pingInterval=3e5},v=new t.InjectionToken("SIGNALR_JCONNECTION_TOKEN"),y=function(){function e(e,n,t){this._configuration=e,this._zone=n,this._jHubConnectionFn=t}return e.prototype.createConnection=function(e){var n=this.merge(e||{});this.logConfiguration(n);var t=this._jHubConnectionFn(n.url);t.logging=n.logging,t.qs=n.qs;var r=t.createHubProxy(n.hubName);return r.on("noOp",(function(){})),new g(t,r,this._zone,n)},e.prototype.connect=function(e){return this.createConnection(e).start()},e.prototype.logConfiguration=function(e){try{var n=JSON.stringify(e.qs),t=JSON.stringify(e.transport);e.logging&&(this.log("Creating connecting with..."),this.log("configuration:[url: '"+e.url+"'] ..."),this.log("configuration:[hubName: '"+e.hubName+"'] ..."),this.log("configuration:[qs: '"+n+"'] ..."),this.log("configuration:[transport: '"+t+"'] ..."))}catch(e){}},e.prototype.log=function(e){console.log(e)},e.prototype.merge=function(e){var n=new b;return n.hubName=e.hubName||this._configuration.hubName,n.url=e.url||this._configuration.url,n.qs=e.qs||this._configuration.qs,n.logging=this._configuration.logging,n.jsonp=e.jsonp||this._configuration.jsonp,n.withCredentials=e.withCredentials||this._configuration.withCredentials,n.transport=e.transport||this._configuration.transport,n.executeEventsInZone=e.executeEventsInZone||this._configuration.executeEventsInZone,n.executeErrorsInZone=e.executeErrorsInZone||this._configuration.executeErrorsInZone,n.executeStatusChangeInZone=e.executeStatusChangeInZone||this._configuration.executeStatusChangeInZone,n.pingInterval=e.pingInterval||this._configuration.pingInterval,n},e}();y.decorators=[{type:t.Injectable}],y.ctorParameters=function(){return[{type:b},{type:t.NgZone},{type:void 0,decorators:[{type:t.Inject,args:[v]}]}]};var d=new t.InjectionToken("SIGNALR_CONFIGURATION");function m(e,n){var t=function(){var e=function(){var e=window.jQuery;if(null==e)throw new Error("Signalr failed to initialize. Script 'jquery.js' is missing. Please make sure to include jquery script.");return e}().hubConnection;if(null==e)throw new Error("Signalr failed to initialize. Script 'jquery.signalR.js' is missing. Please make sure to include 'jquery.signalR.js' script.");return e}();return new y(e,n,t)}var _=y,j=function(){function e(){}return e.forRoot=function(n){return{ngModule:e,providers:[{provide:d,useFactory:n},{deps:[d,t.NgZone],provide:y,useFactory:m}]}},e.forChild=function(){throw new Error("forChild method not implemented")},e}();j.decorators=[{type:t.NgModule,args:[{providers:[{provide:y,useValue:_}]}]}],e.BroadcastEventListener=u,e.ConnectionStatus=l,e.ConnectionStatuses=f,e.ConnectionTransport=p,e.ConnectionTransports=h,e.SignalR=y,e.SignalRConfiguration=b,e.SignalRConnection=g,e.SignalRConnectionMock=a,e.SignalRConnectionMockManager=c,e.SignalRModule=j,e.ɵa=m,e.ɵb=v,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=ng2-signalr.umd.min.js.map