UNPKG

angular2-actioncable

Version:

Simple and flexible integration for ActionCable and Angular applications

3 lines (2 loc) 16.8 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("actioncable")):"function"==typeof define&&define.amd?define(["exports","@angular/core","actioncable"],e):e(t["angular2-actioncable"]={},t.ng.core,t.actioncable)}(this,function(t,e,r){"use strict";var n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)};function o(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}function a(t){return"function"==typeof t}var i=!1,s={Promise:undefined,set useDeprecatedSynchronousErrorHandling(t){t&&(new Error).stack;i=t},get useDeprecatedSynchronousErrorHandling(){return i}};function u(t){setTimeout(function(){throw t})}var h={closed:!0,next:function(t){},error:function(t){if(s.useDeprecatedSynchronousErrorHandling)throw t;u(t)},complete:function(){}},p=Array.isArray||function(t){return t&&"number"==typeof t.length};function l(t){return null!=t&&"object"==typeof t}var c,b={e:{}};function d(){try{return c.apply(this,arguments)}catch(t){return b.e=t,b}}function f(t){return c=t,d}function y(t){return Error.call(this),this.message=t?t.length+" errors occurred during unsubscription:\n"+t.map(function(t,e){return e+1+") "+t.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=t,this}y.prototype=Object.create(Error.prototype);var v=y,_=function(){function n(t){this.closed=!1,this._parent=null,this._parents=null,this._subscriptions=null,t&&(this._unsubscribe=t)}var t;return n.prototype.unsubscribe=function(){var t,e=!1;if(!this.closed){var r=this._parent,n=this._parents,i=this._unsubscribe,o=this._subscriptions;this.closed=!0,this._parent=null,this._parents=null,this._subscriptions=null;for(var s=-1,c=n?n.length:0;r;)r.remove(this),r=++s<c&&n[s]||null;if(a(i))f(i).call(this)===b&&(e=!0,t=t||(b.e instanceof v?w(b.e.errors):[b.e]));if(p(o))for(s=-1,c=o.length;++s<c;){var u=o[s];if(l(u))if(f(u.unsubscribe).call(u)===b){e=!0,t=t||[];var h=b.e;h instanceof v?t=t.concat(w(h.errors)):t.push(h)}}if(e)throw new v(t)}},n.prototype.add=function(t){if(!t||t===n.EMPTY)return n.EMPTY;if(t===this)return this;var e=t;switch(typeof t){case"function":e=new n(t);case"object":if(e.closed||"function"!=typeof e.unsubscribe)return e;if(this.closed)return e.unsubscribe(),e;if("function"!=typeof e._addParent){var r=e;(e=new n)._subscriptions=[r]}break;default:throw new Error("unrecognized teardown "+t+" added to Subscription.")}return(this._subscriptions||(this._subscriptions=[])).push(e),e._addParent(this),e},n.prototype.remove=function(t){var e=this._subscriptions;if(e){var r=e.indexOf(t);-1!==r&&e.splice(r,1)}},n.prototype._addParent=function(t){var e=this._parent,r=this._parents;e&&e!==t?r?-1===r.indexOf(t)&&r.push(t):this._parents=[t]:this._parent=t},n.EMPTY=((t=new n).closed=!0,t),n}();function w(t){return t.reduce(function(t,e){return t.concat(e instanceof v?e.errors:e)},[])}var S="function"==typeof Symbol&&"function"==typeof Symbol["for"]?Symbol["for"]("rxSubscriber"):"@@rxSubscriber",E=function(s){function i(t,e,r){var n=s.call(this)||this;switch(n.syncErrorValue=null,n.syncErrorThrown=!1,n.syncErrorThrowable=!1,n.isStopped=!1,n._parentSubscription=null,arguments.length){case 0:n.destination=h;break;case 1:if(!t){n.destination=h;break}if("object"==typeof t){if(function i(t){return t instanceof E||"_addParentTeardownLogic"in t&&t[S]}(t)){var o=t[S]();n.syncErrorThrowable=o.syncErrorThrowable,(n.destination=o)._addParentTeardownLogic(n)}else n.syncErrorThrowable=!0,n.destination=new m(n,t);break}default:n.syncErrorThrowable=!0,n.destination=new m(n,t,e,r)}return n}return o(i,s),i.prototype[S]=function(){return this},i.create=function(t,e,r){var n=new i(t,e,r);return n.syncErrorThrowable=!1,n},i.prototype.next=function(t){this.isStopped||this._next(t)},i.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t),this._unsubscribeParentSubscription())},i.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete(),this._unsubscribeParentSubscription())},i.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,s.prototype.unsubscribe.call(this))},i.prototype._next=function(t){this.destination.next(t)},i.prototype._error=function(t){this.destination.error(t),this.unsubscribe()},i.prototype._complete=function(){this.destination.complete(),this.unsubscribe()},i.prototype._addParentTeardownLogic=function(t){t!==this&&(this._parentSubscription=this.add(t))},i.prototype._unsubscribeParentSubscription=function(){null!==this._parentSubscription&&this._parentSubscription.unsubscribe()},i.prototype._unsubscribeAndRecycle=function(){var t=this._parent,e=this._parents;return this._parent=null,this._parents=null,this.unsubscribe(),this.closed=!1,this.isStopped=!1,this._parent=t,this._parents=e,this._parentSubscription=null,this},i}(_),m=function(c){function t(t,e,r,n){var i,o=c.call(this)||this;o._parentSubscriber=t;var s=o;return a(e)?i=e:e&&(i=e.next,r=e.error,n=e.complete,e!==h&&(a((s=Object.create(e)).unsubscribe)&&o.add(s.unsubscribe.bind(s)),s.unsubscribe=o.unsubscribe.bind(o))),o._context=s,o._next=i,o._error=r,o._complete=n,o}return o(t,c),t.prototype.next=function(t){if(!this.isStopped&&this._next){var e=this._parentSubscriber;s.useDeprecatedSynchronousErrorHandling&&e.syncErrorThrowable?this.__tryOrSetError(e,this._next,t)&&this.unsubscribe():this.__tryOrUnsub(this._next,t)}},t.prototype.error=function(t){if(!this.isStopped){var e=this._parentSubscriber,r=s.useDeprecatedSynchronousErrorHandling;if(this._error)r&&e.syncErrorThrowable?this.__tryOrSetError(e,this._error,t):this.__tryOrUnsub(this._error,t),this.unsubscribe();else if(e.syncErrorThrowable)r?(e.syncErrorValue=t,e.syncErrorThrown=!0):u(t),this.unsubscribe();else{if(this.unsubscribe(),r)throw t;u(t)}}},t.prototype.complete=function(){var t=this;if(!this.isStopped){var e=this._parentSubscriber;if(this._complete){var r=function(){return t._complete.call(t._context)};s.useDeprecatedSynchronousErrorHandling&&e.syncErrorThrowable?this.__tryOrSetError(e,r):this.__tryOrUnsub(r),this.unsubscribe()}else this.unsubscribe()}},t.prototype.__tryOrUnsub=function(t,e){try{t.call(this._context,e)}catch(r){if(this.unsubscribe(),s.useDeprecatedSynchronousErrorHandling)throw r;u(r)}},t.prototype.__tryOrSetError=function(t,e,r){if(!s.useDeprecatedSynchronousErrorHandling)throw new Error("bad call");try{e.call(this._context,r)}catch(n){return s.useDeprecatedSynchronousErrorHandling?(t.syncErrorValue=n,t.syncErrorThrown=!0):(u(n),!0)}return!1},t.prototype._unsubscribe=function(){var t=this._parentSubscriber;this._context=null,this._parentSubscriber=null,t.unsubscribe()},t}(E);var g="function"==typeof Symbol&&Symbol.observable||"@@observable";function x(){}var T=function(){function r(t){this._isScalar=!1,t&&(this._subscribe=t)}return r.prototype.lift=function(t){var e=new r;return e.source=this,e.operator=t,e},r.prototype.subscribe=function(t,e,r){var n=this.operator,i=function o(t,e,r){if(t){if(t instanceof E)return t;if(t[S])return t[S]()}return t||e||r?new E(t,e,r):new E(h)}(t,e,r);if(n?n.call(i,this.source):i._addParentTeardownLogic(this.source||s.useDeprecatedSynchronousErrorHandling&&!i.syncErrorThrowable?this._subscribe(i):this._trySubscribe(i)),s.useDeprecatedSynchronousErrorHandling&&i.syncErrorThrowable&&(i.syncErrorThrowable=!1,i.syncErrorThrown))throw i.syncErrorValue;return i},r.prototype._trySubscribe=function(t){try{return this._subscribe(t)}catch(e){s.useDeprecatedSynchronousErrorHandling&&(t.syncErrorThrown=!0,t.syncErrorValue=e),t.error(e)}},r.prototype.forEach=function(i,t){var e=this;return new(t=O(t))(function(t,r){var n;n=e.subscribe(function(t){try{i(t)}catch(e){r(e),n&&n.unsubscribe()}},r,t)})},r.prototype._subscribe=function(t){var e=this.source;return e&&e.subscribe(t)},r.prototype[g]=function(){return this},r.prototype.pipe=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return 0===t.length?this:function r(e){return e?1===e.length?e[0]:function t(t){return e.reduce(function(t,e){return e(t)},t)}:x}(t)(this)},r.prototype.toPromise=function(t){var n=this;return new(t=O(t))(function(t,e){var r;n.subscribe(function(t){return r=t},function(t){return e(t)},function(){return t(r)})})},r.create=function(t){return new r(t)},r}();function O(t){if(t||(t=s.Promise||Promise),!t)throw new Error("no Promise impl found");return t}function P(){return Error.call(this),this.message="object unsubscribed",this.name="ObjectUnsubscribedError",this}P.prototype=Object.create(Error.prototype);var j=P,D=function(n){function t(t,e){var r=n.call(this)||this;return r.subject=t,r.subscriber=e,r.closed=!1,r}return o(t,n),t.prototype.unsubscribe=function(){if(!this.closed){this.closed=!0;var t=this.subject,e=t.observers;if(this.subject=null,e&&0!==e.length&&!t.isStopped&&!t.closed){var r=e.indexOf(this.subscriber);-1!==r&&e.splice(r,1)}}},t}(_),C=function(r){function t(t){var e=r.call(this,t)||this;return e.destination=t,e}return o(t,r),t}(E),A=function(e){function t(){var t=e.call(this)||this;return t.observers=[],t.closed=!1,t.isStopped=!1,t.hasError=!1,t.thrownError=null,t}return o(t,e),t.prototype[S]=function(){return new C(this)},t.prototype.lift=function(t){var e=new V(this,this);return e.operator=t,e},t.prototype.next=function(t){if(this.closed)throw new j;if(!this.isStopped)for(var e=this.observers,r=e.length,n=e.slice(),i=0;i<r;i++)n[i].next(t)},t.prototype.error=function(t){if(this.closed)throw new j;this.hasError=!0,this.thrownError=t,this.isStopped=!0;for(var e=this.observers,r=e.length,n=e.slice(),i=0;i<r;i++)n[i].error(t);this.observers.length=0},t.prototype.complete=function(){if(this.closed)throw new j;this.isStopped=!0;for(var t=this.observers,e=t.length,r=t.slice(),n=0;n<e;n++)r[n].complete();this.observers.length=0},t.prototype.unsubscribe=function(){this.isStopped=!0,this.closed=!0,this.observers=null},t.prototype._trySubscribe=function(t){if(this.closed)throw new j;return e.prototype._trySubscribe.call(this,t)},t.prototype._subscribe=function(t){if(this.closed)throw new j;return this.hasError?(t.error(this.thrownError),_.EMPTY):this.isStopped?(t.complete(),_.EMPTY):(this.observers.push(t),new D(this,t))},t.prototype.asObservable=function(){var t=new T;return t.source=this,t},t.create=function(t,e){return new V(t,e)},t}(T),V=function(n){function t(t,e){var r=n.call(this)||this;return r.destination=t,r.source=e,r}return o(t,n),t.prototype.next=function(t){var e=this.destination;e&&e.next&&e.next(t)},t.prototype.error=function(t){var e=this.destination;e&&e.error&&this.destination.error(t)},t.prototype.complete=function(){var t=this.destination;t&&t.complete&&this.destination.complete()},t.prototype._subscribe=function(t){return this.source?this.source.subscribe(t):_.EMPTY},t}(A);var H=function(n){function t(t,e){var r=n.call(this,t,e)||this;return r.scheduler=t,r.work=e,r.pending=!1,r}return o(t,n),t.prototype.schedule=function(t,e){if(void 0===e&&(e=0),this.closed)return this;this.state=t;var r=this.id,n=this.scheduler;return null!=r&&(this.id=this.recycleAsyncId(n,r,e)),this.pending=!0,this.delay=e,this.id=this.id||this.requestAsyncId(n,this.id,e),this},t.prototype.requestAsyncId=function(t,e,r){return void 0===r&&(r=0),setInterval(t.flush.bind(t,this),r)},t.prototype.recycleAsyncId=function(t,e,r){if(void 0===r&&(r=0),null!==r&&this.delay===r&&!1===this.pending)return e;clearInterval(e)},t.prototype.execute=function(t,e){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var r=this._execute(t,e);if(r)return r;!1===this.pending&&null!=this.id&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},t.prototype._execute=function(t,e){var r=!1,n=undefined;try{this.work(t)}catch(i){r=!0,n=!!i&&i||new Error(i)}if(r)return this.unsubscribe(),n},t.prototype._unsubscribe=function(){var t=this.id,e=this.scheduler,r=e.actions,n=r.indexOf(this);this.work=null,this.state=null,this.pending=!1,this.scheduler=null,-1!==n&&r.splice(n,1),null!=t&&(this.id=this.recycleAsyncId(e,t,null)),this.delay=null},t}(function(r){function t(t,e){return r.call(this)||this}return o(t,r),t.prototype.schedule=function(t,e){return void 0===e&&(e=0),this},t}(_)),U=function(){function r(t,e){void 0===e&&(e=r.now),this.SchedulerAction=t,this.now=e}return r.prototype.schedule=function(t,e,r){return void 0===e&&(e=0),new this.SchedulerAction(this,t).schedule(r,e)},r.now=function(){return Date.now()},r}(),k=function(n){function i(t,e){void 0===e&&(e=U.now);var r=n.call(this,t,function(){return i.delegate&&i.delegate!==r?i.delegate.now():e()})||this;return r.actions=[],r.active=!1,r.scheduled=undefined,r}return o(i,n),i.prototype.schedule=function(t,e,r){return void 0===e&&(e=0),i.delegate&&i.delegate!==this?i.delegate.schedule(t,e,r):n.prototype.schedule.call(this,t,e,r)},i.prototype.flush=function(t){var e=this.actions;if(this.active)e.push(t);else{var r;this.active=!0;do{if(r=t.execute(t.state,t.delay))break}while(t=e.shift());if(this.active=!1,r){for(;t=e.shift();)t.unsubscribe();throw r}}},i}(U);var I=new k(H);Error.prototype;var M=function(){function t(t,e){this.dueTime=t,this.scheduler=e}return t.prototype.call=function(t,e){return e.subscribe(new Y(t,this.dueTime,this.scheduler))},t}(),Y=function(i){function t(t,e,r){var n=i.call(this,t)||this;return n.dueTime=e,n.scheduler=r,n.debouncedSubscription=null,n.lastValue=null,n.hasValue=!1,n}return o(t,i),t.prototype._next=function(t){this.clearDebounce(),this.lastValue=t,this.hasValue=!0,this.add(this.debouncedSubscription=this.scheduler.schedule(z,this.dueTime,this))},t.prototype._complete=function(){this.debouncedNext(),this.destination.complete()},t.prototype.debouncedNext=function(){if(this.clearDebounce(),this.hasValue){var t=this.lastValue;this.lastValue=null,this.hasValue=!1,this.destination.next(t)}},t.prototype.clearDebounce=function(){var t=this.debouncedSubscription;null!==t&&(this.remove(t),t.unsubscribe(),this.debouncedSubscription=null)},t}(E);function z(t){t.debouncedNext()}var L=r,q=function(){function t(t,e){var r=this;this.url=t,this.params=e,this.disconnectedSource=new A,this.baseCable=L.createConsumer(this.buildUrl(t,e)),e instanceof Function&&this.disconnected().subscribe(function(){r.baseCable.url=L.createWebSocketURL(r.buildUrl(t,e))})}return t.prototype.channel=function(t,e){var r=this;void 0===e&&(e={});var n=new R(this,t,e);return n.disconnected().subscribe(function(t){return r.disconnectedSource.next(t)}),n},t.prototype.disconnected=function(){return this.disconnectedSource.asObservable().pipe(function t(e,r){return void 0===r&&(r=I),function(t){return t.lift(new M(e,r))}}(100))},t.prototype.disconnect=function(){this.baseCable.disconnect()},t.prototype.buildUrl=function(t,e){return e instanceof Function&&(e=e()),e?[t,Object.keys(e).map(function(t){return encodeURIComponent(t)+"="+encodeURIComponent(e[t])}).join("&")].join("?"):t},t}(),R=function(){function t(t,e,r){void 0===r&&(r={});var n=this;this.cable=t,this.name=e,this.params=r,this.initializedSource=new A,this.connectedSource=new A,this.disconnectedSource=new A,this.rejectedSource=new A,this.eventTypes=["initialized","connected","disconnected","rejected"];var i=Object.assign({},r,{channel:e});this.messages=new T(function(e){var t={received:function(t){return e.next(t)}};return n.eventTypes.forEach(function(e){t[e]=function(t){return n[e+"Source"].next(t)}}),n.baseChannel=n.cable.baseCable.subscriptions.create(i,t),function(){return n.unsubscribe()}})}return t.prototype.received=function(){return this.messages},t.prototype.initialized=function(){return this.initializedSource.asObservable()},t.prototype.connected=function(){return this.connectedSource.asObservable()},t.prototype.disconnected=function(){return this.disconnectedSource.asObservable()},t.prototype.rejected=function(){return this.rejectedSource.asObservable()},t.prototype.send=function(t){this.baseChannel.send(t)},t.prototype.perform=function(t,e){this.baseChannel.perform(t,e)},t.prototype.unsubscribe=function(){var e=this;this.baseChannel.unsubscribe(),this.eventTypes.forEach(function(t){return e[t+"Source"].complete()})},t}(),N=function(){function t(){this.cables={}}return t.prototype.cable=function(t,e){return this.cables.hasOwnProperty(t)||(this.cables[t]=new q(t,e)),this.cables[t]},t.prototype.disconnect=function(t){this.cables.hasOwnProperty(t)&&(this.cables[t].disconnect(),delete this.cables[t])},t}();N.decorators=[{type:e.Injectable}],N.ctorParameters=function(){return[]},t.Cable=q,t.Channel=R,t.ActionCableService=N,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=angular2-actioncable.umd.min.js.map