UNPKG

@andreasnicolaou/reactive-event-source

Version:

A lightweight reactive wrapper around EventSource using RxJS, providing automatic reconnection and buffering.

2 lines (1 loc) 29.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).reactiveEventSource={})}(this,function(t){"use strict";var e=function(t,n){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},e(t,n)};function n(t,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}function r(t,e,n,r){return new(n||(n=Promise))(function(o,i){function u(t){try{c(r.next(t))}catch(t){i(t)}}function s(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(u,s)}c((r=r.apply(t,e||[])).next())})}function o(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=s(0),u.throw=s(1),u.return=s(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function s(s){return function(c){return function(s){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,s[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&s[0]?r.return:s[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,s[1])).done)return o;switch(r=0,o&&(s=[2&s[0],o.value]),s[0]){case 0:case 1:o=s;break;case 4:return i.label++,{value:s[1],done:!1};case 5:i.label++,r=s[1],s=[0];continue;case 7:s=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==s[0]&&2!==s[0])){i=0;continue}if(3===s[0]&&(!o||s[1]>o[0]&&s[1]<o[3])){i.label=s[1];break}if(6===s[0]&&i.label<o[1]){i.label=o[1],o=s;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(s);break}o[2]&&i.ops.pop(),i.trys.pop();continue}s=e.call(t,i)}catch(t){s=[6,t],r=0}finally{n=o=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function i(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function u(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return u}function s(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}function c(t){return this instanceof c?(this.v=t,this):new c(t)}function a(t,e,n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r,o=n.apply(t,e||[]),i=[];return r=Object.create(("function"==typeof AsyncIterator?AsyncIterator:Object).prototype),u("next"),u("throw"),u("return",function(t){return function(e){return Promise.resolve(e).then(t,l)}}),r[Symbol.asyncIterator]=function(){return this},r;function u(t,e){o[t]&&(r[t]=function(e){return new Promise(function(n,r){i.push([t,e,n,r])>1||s(t,e)})},e&&(r[t]=e(r[t])))}function s(t,e){try{(n=o[t](e)).value instanceof c?Promise.resolve(n.value.v).then(a,l):f(i[0][2],n)}catch(t){f(i[0][3],t)}var n}function a(t){s("next",t)}function l(t){s("throw",t)}function f(t,e){t(e),i.shift(),i.length&&s(i[0][0],i[0][1])}}function l(t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e,n=t[Symbol.asyncIterator];return n?n.call(t):(t=i(t),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(n){e[n]=t[n]&&function(e){return new Promise(function(r,o){(function(t,e,n,r){Promise.resolve(r).then(function(e){t({value:e,done:n})},e)})(r,o,(e=t[n](e)).done,e.value)})}}}function f(t){return"function"==typeof t}function h(t){var e=t(function(t){Error.call(t),t.stack=(new Error).stack});return e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,e}"function"==typeof SuppressedError&&SuppressedError;var p=h(function(t){return function(e){t(this),this.message=e?e.length+" errors occurred during unsubscription:\n"+e.map(function(t,e){return e+1+") "+t.toString()}).join("\n "):"",this.name="UnsubscriptionError",this.errors=e}});function v(t,e){if(t){var n=t.indexOf(e);0<=n&&t.splice(n,1)}}var d=function(){function t(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}var e;return t.prototype.unsubscribe=function(){var t,e,n,r,o;if(!this.closed){this.closed=!0;var c=this._parentage;if(c)if(this._parentage=null,Array.isArray(c))try{for(var a=i(c),l=a.next();!l.done;l=a.next()){l.value.remove(this)}}catch(e){t={error:e}}finally{try{l&&!l.done&&(e=a.return)&&e.call(a)}finally{if(t)throw t.error}}else c.remove(this);var h=this.initialTeardown;if(f(h))try{h()}catch(t){o=t instanceof p?t.errors:[t]}var v=this._finalizers;if(v){this._finalizers=null;try{for(var d=i(v),b=d.next();!b.done;b=d.next()){var y=b.value;try{w(y)}catch(t){o=null!=o?o:[],t instanceof p?o=s(s([],u(o)),u(t.errors)):o.push(t)}}}catch(t){n={error:t}}finally{try{b&&!b.done&&(r=d.return)&&r.call(d)}finally{if(n)throw n.error}}}if(o)throw new p(o)}},t.prototype.add=function(e){var n;if(e&&e!==this)if(this.closed)w(e);else{if(e instanceof t){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(e)}},t.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},t.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},t.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&v(e,t)},t.prototype.remove=function(e){var n=this._finalizers;n&&v(n,e),e instanceof t&&e._removeParent(this)},t.EMPTY=((e=new t).closed=!0,e),t}(),b=d.EMPTY;function y(t){return t instanceof d||t&&"closed"in t&&f(t.remove)&&f(t.add)&&f(t.unsubscribe)}function w(t){f(t)?t():t.unsubscribe()}var m={Promise:void 0},S=function(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,s([t,e],u(n)))};function _(t){S(function(){throw t})}function x(){}function g(t){t()}var E=function(t){function e(e){var n=t.call(this)||this;return n.isStopped=!1,e?(n.destination=e,y(e)&&e.add(n)):n.destination=$,n}return n(e,t),e.create=function(t,e,n){return new j(t,e,n)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,t.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e}(d),O=function(){function t(t){this.partialObserver=t}return t.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(t){I(t)}},t.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(t){I(t)}else I(t)},t.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(t){I(t)}},t}(),j=function(t){function e(e,n,r){var o,i=t.call(this)||this;return o=f(e)||!e?{next:null!=e?e:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:e,i.destination=new O(o),i}return n(e,t),e}(E);function I(t){_(t)}var $={closed:!0,next:x,error:function(t){throw t},complete:x},A="function"==typeof Symbol&&Symbol.observable||"@@observable";function C(t){return t}var P=function(){function t(t){t&&(this._subscribe=t)}return t.prototype.lift=function(e){var n=new t;return n.source=this,n.operator=e,n},t.prototype.subscribe=function(t,e,n){var r,o=this,i=(r=t)&&r instanceof E||function(t){return t&&f(t.next)&&f(t.error)&&f(t.complete)}(r)&&y(r)?t:new j(t,e,n);return g(function(){var t=o,e=t.operator,n=t.source;i.add(e?e.call(i,n):n?o._subscribe(i):o._trySubscribe(i))}),i},t.prototype._trySubscribe=function(t){try{return this._subscribe(t)}catch(e){t.error(e)}},t.prototype.forEach=function(t,e){var n=this;return new(e=T(e))(function(e,r){var o=new j({next:function(e){try{t(e)}catch(t){r(t),o.unsubscribe()}},error:r,complete:e});n.subscribe(o)})},t.prototype._subscribe=function(t){var e;return null===(e=this.source)||void 0===e?void 0:e.subscribe(t)},t.prototype[A]=function(){return this},t.prototype.pipe=function(){for(var t,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return(0===(t=e).length?C:1===t.length?t[0]:function(e){return t.reduce(function(t,e){return e(t)},e)})(this)},t.prototype.toPromise=function(t){var e=this;return new(t=T(t))(function(t,n){var r;e.subscribe(function(t){return r=t},function(t){return n(t)},function(){return t(r)})})},t.create=function(e){return new t(e)},t}();function T(t){var e;return null!==(e=null!=t?t:m.Promise)&&void 0!==e?e:Promise}function L(t){return function(e){if(function(t){return f(null==t?void 0:t.lift)}(e))return e.lift(function(e){try{return t(e,this)}catch(t){this.error(t)}});throw new TypeError("Unable to lift unknown Observable type")}}function k(t,e,n,r,o){return new z(t,e,n,r,o)}var z=function(t){function e(e,n,r,o,i,u){var s=t.call(this,e)||this;return s.onFinalize=i,s.shouldUnsubscribe=u,s._next=n?function(t){try{n(t)}catch(t){e.error(t)}}:t.prototype._next,s._error=o?function(t){try{o(t)}catch(t){e.error(t)}finally{this.unsubscribe()}}:t.prototype._error,s._complete=r?function(){try{r()}catch(t){e.error(t)}finally{this.unsubscribe()}}:t.prototype._complete,s}return n(e,t),e.prototype.unsubscribe=function(){var e;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var n=this.closed;t.prototype.unsubscribe.call(this),!n&&(null===(e=this.onFinalize)||void 0===e||e.call(this))}},e}(E),R=h(function(t){return function(){t(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed"}}),M=function(t){function e(){var e=t.call(this)||this;return e.closed=!1,e.currentObservers=null,e.observers=[],e.isStopped=!1,e.hasError=!1,e.thrownError=null,e}return n(e,t),e.prototype.lift=function(t){var e=new U(this,this);return e.operator=t,e},e.prototype._throwIfClosed=function(){if(this.closed)throw new R},e.prototype.next=function(t){var e=this;g(function(){var n,r;if(e._throwIfClosed(),!e.isStopped){e.currentObservers||(e.currentObservers=Array.from(e.observers));try{for(var o=i(e.currentObservers),u=o.next();!u.done;u=o.next()){u.value.next(t)}}catch(t){n={error:t}}finally{try{u&&!u.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}}})},e.prototype.error=function(t){var e=this;g(function(){if(e._throwIfClosed(),!e.isStopped){e.hasError=e.isStopped=!0,e.thrownError=t;for(var n=e.observers;n.length;)n.shift().error(t)}})},e.prototype.complete=function(){var t=this;g(function(){if(t._throwIfClosed(),!t.isStopped){t.isStopped=!0;for(var e=t.observers;e.length;)e.shift().complete()}})},e.prototype.unsubscribe=function(){this.isStopped=this.closed=!0,this.observers=this.currentObservers=null},Object.defineProperty(e.prototype,"observed",{get:function(){var t;return(null===(t=this.observers)||void 0===t?void 0:t.length)>0},enumerable:!1,configurable:!0}),e.prototype._trySubscribe=function(e){return this._throwIfClosed(),t.prototype._trySubscribe.call(this,e)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var e=this,n=this,r=n.hasError,o=n.isStopped,i=n.observers;return r||o?b:(this.currentObservers=null,i.push(t),new d(function(){e.currentObservers=null,v(i,t)}))},e.prototype._checkFinalizedStatuses=function(t){var e=this,n=e.hasError,r=e.thrownError,o=e.isStopped;n?t.error(r):o&&t.complete()},e.prototype.asObservable=function(){var t=new P;return t.source=this,t},e.create=function(t,e){return new U(t,e)},e}(P),U=function(t){function e(e,n){var r=t.call(this)||this;return r.destination=e,r.source=n,r}return n(e,t),e.prototype.next=function(t){var e,n;null===(n=null===(e=this.destination)||void 0===e?void 0:e.next)||void 0===n||n.call(e,t)},e.prototype.error=function(t){var e,n;null===(n=null===(e=this.destination)||void 0===e?void 0:e.error)||void 0===n||n.call(e,t)},e.prototype.complete=function(){var t,e;null===(e=null===(t=this.destination)||void 0===t?void 0:t.complete)||void 0===e||e.call(t)},e.prototype._subscribe=function(t){var e,n;return null!==(n=null===(e=this.source)||void 0===e?void 0:e.subscribe(t))&&void 0!==n?n:b},e}(M),D=function(t){function e(e){var n=t.call(this)||this;return n._value=e,n}return n(e,t),Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),e.prototype._subscribe=function(e){var n=t.prototype._subscribe.call(this,e);return!n.closed&&e.next(this._value),n},e.prototype.getValue=function(){var t=this,e=t.hasError,n=t.thrownError,r=t._value;if(e)throw n;return this._throwIfClosed(),r},e.prototype.next=function(e){t.prototype.next.call(this,this._value=e)},e}(M),F={now:function(){return(F.delegate||Date).now()},delegate:void 0},N=function(t){function e(e,n,r){void 0===e&&(e=1/0),void 0===n&&(n=1/0),void 0===r&&(r=F);var o=t.call(this)||this;return o._bufferSize=e,o._windowTime=n,o._timestampProvider=r,o._buffer=[],o._infiniteTimeWindow=!0,o._infiniteTimeWindow=n===1/0,o._bufferSize=Math.max(1,e),o._windowTime=Math.max(1,n),o}return n(e,t),e.prototype.next=function(e){var n=this,r=n.isStopped,o=n._buffer,i=n._infiniteTimeWindow,u=n._timestampProvider,s=n._windowTime;r||(o.push(e),!i&&o.push(u.now()+s)),this._trimBuffer(),t.prototype.next.call(this,e)},e.prototype._subscribe=function(t){this._throwIfClosed(),this._trimBuffer();for(var e=this._innerSubscribe(t),n=this._infiniteTimeWindow,r=this._buffer.slice(),o=0;o<r.length&&!t.closed;o+=n?1:2)t.next(r[o]);return this._checkFinalizedStatuses(t),e},e.prototype._trimBuffer=function(){var t=this,e=t._bufferSize,n=t._timestampProvider,r=t._buffer,o=t._infiniteTimeWindow,i=(o?1:2)*e;if(e<1/0&&i<r.length&&r.splice(0,r.length-i),!o){for(var u=n.now(),s=0,c=1;c<r.length&&r[c]<=u;c+=2)s=c;s&&r.splice(0,s+1)}},e}(M),W=function(t){function e(e,n){return t.call(this)||this}return n(e,t),e.prototype.schedule=function(t,e){return this},e}(d),Y=function(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setInterval.apply(void 0,s([t,e],u(n)))},B=function(t){return clearInterval(t)},q=function(t){function e(e,n){var r=t.call(this,e,n)||this;return r.scheduler=e,r.work=n,r.pending=!1,r}return n(e,t),e.prototype.schedule=function(t,e){var n;if(void 0===e&&(e=0),this.closed)return this;this.state=t;var r=this.id,o=this.scheduler;return null!=r&&(this.id=this.recycleAsyncId(o,r,e)),this.pending=!0,this.delay=e,this.id=null!==(n=this.id)&&void 0!==n?n:this.requestAsyncId(o,this.id,e),this},e.prototype.requestAsyncId=function(t,e,n){return void 0===n&&(n=0),Y(t.flush.bind(t,this),n)},e.prototype.recycleAsyncId=function(t,e,n){if(void 0===n&&(n=0),null!=n&&this.delay===n&&!1===this.pending)return e;null!=e&&B(e)},e.prototype.execute=function(t,e){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;var n=this._execute(t,e);if(n)return n;!1===this.pending&&null!=this.id&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},e.prototype._execute=function(t,e){var n,r=!1;try{this.work(t)}catch(t){r=!0,n=t||new Error("Scheduled action threw falsy error")}if(r)return this.unsubscribe(),n},e.prototype.unsubscribe=function(){if(!this.closed){var e=this.id,n=this.scheduler,r=n.actions;this.work=this.state=this.scheduler=null,this.pending=!1,v(r,this),null!=e&&(this.id=this.recycleAsyncId(n,e,null)),this.delay=null,t.prototype.unsubscribe.call(this)}},e}(W),G=function(){function t(e,n){void 0===n&&(n=t.now),this.schedulerActionCtor=e,this.now=n}return t.prototype.schedule=function(t,e,n){return void 0===e&&(e=0),new this.schedulerActionCtor(this,t).schedule(n,e)},t.now=F.now,t}(),V=new(function(t){function e(e,n){void 0===n&&(n=G.now);var r=t.call(this,e,n)||this;return r.actions=[],r._active=!1,r}return n(e,t),e.prototype.flush=function(t){var e=this.actions;if(this._active)e.push(t);else{var n;this._active=!0;do{if(n=t.execute(t.state,t.delay))break}while(t=e.shift());if(this._active=!1,n){for(;t=e.shift();)t.unsubscribe();throw n}}},e}(G))(q),Z=new P(function(t){return t.complete()});function H(t){return t[t.length-1]}function J(t){return(e=H(t))&&f(e.schedule)?t.pop():void 0;var e}var K=function(t){return t&&"number"==typeof t.length&&"function"!=typeof t};function Q(t){return f(null==t?void 0:t.then)}function X(t){return f(t[A])}function tt(t){return Symbol.asyncIterator&&f(null==t?void 0:t[Symbol.asyncIterator])}function et(t){return new TypeError("You provided "+(null!==t&&"object"==typeof t?"an invalid object":"'"+t+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}var nt="function"==typeof Symbol&&Symbol.iterator?Symbol.iterator:"@@iterator";function rt(t){return f(null==t?void 0:t[nt])}function ot(t){return a(this,arguments,function(){var e,n,r;return o(this,function(o){switch(o.label){case 0:e=t.getReader(),o.label=1;case 1:o.trys.push([1,,9,10]),o.label=2;case 2:return[4,c(e.read())];case 3:return n=o.sent(),r=n.value,n.done?[4,c(void 0)]:[3,5];case 4:return[2,o.sent()];case 5:return[4,c(r)];case 6:return[4,o.sent()];case 7:return o.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}})})}function it(t){return f(null==t?void 0:t.getReader)}function ut(t){if(t instanceof P)return t;if(null!=t){if(X(t))return o=t,new P(function(t){var e=o[A]();if(f(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")});if(K(t))return r=t,new P(function(t){for(var e=0;e<r.length&&!t.closed;e++)t.next(r[e]);t.complete()});if(Q(t))return n=t,new P(function(t){n.then(function(e){t.closed||(t.next(e),t.complete())},function(e){return t.error(e)}).then(null,_)});if(tt(t))return st(t);if(rt(t))return e=t,new P(function(t){var n,r;try{for(var o=i(e),u=o.next();!u.done;u=o.next()){var s=u.value;if(t.next(s),t.closed)return}}catch(t){n={error:t}}finally{try{u&&!u.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}t.complete()});if(it(t))return st(ot(t))}var e,n,r,o;throw et(t)}function st(t){return new P(function(e){(function(t,e){var n,i,u,s;return r(this,void 0,void 0,function(){var r,c;return o(this,function(o){switch(o.label){case 0:o.trys.push([0,5,6,11]),n=l(t),o.label=1;case 1:return[4,n.next()];case 2:if((i=o.sent()).done)return[3,4];if(r=i.value,e.next(r),e.closed)return[2];o.label=3;case 3:return[3,1];case 4:return[3,11];case 5:return c=o.sent(),u={error:c},[3,11];case 6:return o.trys.push([6,,9,10]),i&&!i.done&&(s=n.return)?[4,s.call(n)]:[3,8];case 7:o.sent(),o.label=8;case 8:return[3,10];case 9:if(u)throw u.error;return[7];case 10:return[7];case 11:return e.complete(),[2]}})})})(t,e).catch(function(t){return e.error(t)})})}function ct(t,e,n,r,o){void 0===r&&(r=0),void 0===o&&(o=!1);var i=e.schedule(function(){n(),o?t.add(this.schedule(null,r)):this.unsubscribe()},r);if(t.add(i),!o)return i}function at(t,e){return void 0===e&&(e=0),L(function(n,r){n.subscribe(k(r,function(n){return ct(r,t,function(){return r.next(n)},e)},function(){return ct(r,t,function(){return r.complete()},e)},function(n){return ct(r,t,function(){return r.error(n)},e)}))})}function lt(t,e){return void 0===e&&(e=0),L(function(n,r){r.add(t.schedule(function(){return n.subscribe(r)},e))})}function ft(t,e){if(!t)throw new Error("Iterable cannot be null");return new P(function(n){ct(n,e,function(){var r=t[Symbol.asyncIterator]();ct(n,e,function(){r.next().then(function(t){t.done?n.complete():n.next(t.value)})},0,!0)})})}function ht(t,e){if(null!=t){if(X(t))return function(t,e){return ut(t).pipe(lt(e),at(e))}(t,e);if(K(t))return function(t,e){return new P(function(n){var r=0;return e.schedule(function(){r===t.length?n.complete():(n.next(t[r++]),n.closed||this.schedule())})})}(t,e);if(Q(t))return function(t,e){return ut(t).pipe(lt(e),at(e))}(t,e);if(tt(t))return ft(t,e);if(rt(t))return function(t,e){return new P(function(n){var r;return ct(n,e,function(){r=t[nt](),ct(n,e,function(){var t,e,o;try{e=(t=r.next()).value,o=t.done}catch(t){return void n.error(t)}o?n.complete():n.next(e)},0,!0)}),function(){return f(null==r?void 0:r.return)&&r.return()}})}(t,e);if(it(t))return function(t,e){return ft(ot(t),e)}(t,e)}throw et(t)}function pt(t,e){return e?ht(t,e):ut(t)}function vt(t,e){var n=f(t)?t:function(){return t};return new P(function(t){return t.error(n())})}function dt(t,e){return L(function(n,r){var o=0;n.subscribe(k(r,function(n){r.next(t.call(e,n,o++))}))})}var bt=Array.isArray;function yt(t){return dt(function(e){return function(t,e){return bt(e)?t.apply(void 0,s([],u(e))):t(e)}(t,e)})}function wt(t,e,n){return void 0===n&&(n=1/0),f(e)?wt(function(n,r){return dt(function(t,o){return e(n,t,r,o)})(ut(t(n,r)))},n):("number"==typeof e&&(n=e),L(function(e,r){return function(t,e,n,r,o,i,u){var s=[],c=0,a=0,l=!1,f=function(){!l||s.length||c||e.complete()},h=function(t){c++;var o=!1;ut(n(t,a++)).subscribe(k(e,function(t){e.next(t)},function(){o=!0},void 0,function(){if(o)try{c--;for(var t=function(){var t=s.shift();u||h(t)};s.length&&c<r;)t();f()}catch(t){e.error(t)}}))};return t.subscribe(k(e,function(t){return c<r?h(t):s.push(t)},function(){l=!0,f()})),function(){}}(e,r,t,n)}))}var mt=["addListener","removeListener"],St=["addEventListener","removeEventListener"],_t=["on","off"];function xt(t,e,n,r){if(f(n)&&(r=n,n=void 0),r)return xt(t,e,n).pipe(yt(r));var o=u(function(t){return f(t.addEventListener)&&f(t.removeEventListener)}(t)?St.map(function(r){return function(o){return t[r](e,o,n)}}):function(t){return f(t.addListener)&&f(t.removeListener)}(t)?mt.map(gt(t,e)):function(t){return f(t.on)&&f(t.off)}(t)?_t.map(gt(t,e)):[],2),i=o[0],s=o[1];if(!i&&K(t))return wt(function(t){return xt(t,e,n)})(ut(t));if(!i)throw new TypeError("Invalid event target");return new P(function(t){var e=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return t.next(1<e.length?e:e[0])};return i(e),function(){return s(e)}})}function gt(t,e){return function(n){return function(r){return t[n](e,r)}}}function Et(t,e,n){return void 0===t&&(t=0),void 0===n&&(n=V),new P(function(e){var r,o=(r=t)instanceof Date&&!isNaN(r)?+t-n.now():t;o<0&&(o=0);var i=0;return n.schedule(function(){e.closed||(e.next(i++),e.complete())},o)})}function Ot(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var n=J(t),r=function(t,e){return"number"==typeof H(t)?t.pop():e}(t,1/0),o=t;return o.length?1===o.length?ut(o[0]):function(t){return void 0===t&&(t=1/0),wt(C,t)}(r)(pt(o,n)):Z}function jt(t){return L(function(e,n){var r,o=null,i=!1;o=e.subscribe(k(n,void 0,void 0,function(u){r=ut(t(u,jt(t)(e))),o?(o.unsubscribe(),o=null,r.subscribe(n)):i=!0})),i&&(o.unsubscribe(),o=null,r.subscribe(n))})}function It(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return t.length?L(function(e,n){var r;(r=s([e],u(t)),function(t){for(var e=[],n=function(n){e.push(ut(r[n]).subscribe(k(t,function(r){if(e){for(var o=0;o<e.length;o++)o!==n&&e[o].unsubscribe();e=null}t.next(r)})))},o=0;e&&!t.closed&&o<r.length;o++)n(o)})(n)}):C}function $t(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];if(!0!==e){if(!1!==e){var o=new j({next:function(){o.unsubscribe(),t()}});return ut(e.apply(void 0,s([],u(n)))).subscribe(o)}}else t()}function At(t,e){return L(function(n,r){var o=null,i=0,u=!1,s=function(){return u&&!o&&r.complete()};n.subscribe(k(r,function(n){null==o||o.unsubscribe();var u=0,c=i++;ut(t(n,c)).subscribe(o=k(r,function(t){return r.next(e?e(n,t,c,u++):t)},function(){o=null,s()}))},function(){u=!0,s()}))})}function Ct(t){return L(function(e,n){ut(t).subscribe(k(n,function(){return n.complete()},x)),!n.closed&&e.subscribe(n)})}class Pt extends Error{attempt;constructor(t,e=-1){super(`EventSource Error: ${t}${e>-1?` - Attempt: ${e}`:""}`),this.name="EventSourceError",this.attempt=e}}t.EventSourceError=Pt,t.ReactiveEventSource=class{destroy$=new M;eventListenerCleanup=new Map;eventSource$;eventSubjects=new Map;lastEventSource;options;readyStateSubject$=new D(2);subscriptions=new Map;url;constructor(t,e){this.url=t,this.options={maxRetries:3,initialDelay:1e3,maxDelay:1e4,connectionTimeout:15e3,withCredentials:!1,...e},this.eventSource$=this.createEventSource()}get readyState(){return this.readyStateSubject$.value}get readyState$(){return this.readyStateSubject$.asObservable()}get withCredentials(){return this.options.withCredentials??!1}get URL(){return this.url.toString()}close(){this.subscriptions.forEach(t=>t.unsubscribe()),this.subscriptions.clear(),this.eventListenerCleanup.forEach(t=>t()),this.eventListenerCleanup.clear(),this.lastEventSource?.close(),this.readyStateSubject$.next(2),this.eventSubjects.forEach(t=>{t.closed||t.complete()}),this.eventSubjects.clear(),this.destroy$.next(),this.destroy$.complete(),this.readyStateSubject$.complete()}on(t="message"){if(!this.eventSubjects.has(t)){const e=new N(1);this.eventSubjects.set(t,e),this.setupEventListener(t,e)}return this.eventSubjects.get(t).asObservable()}createEventSource(){return(t=()=>{if(!window.EventSource)return vt(()=>new Pt("EventSource is not supported in this environment"));this.lastEventSource&&this.lastEventSource.close(),this.lastEventSource=new EventSource(this.url,{withCredentials:this.options.withCredentials}),this.readyStateSubject$.next(this.lastEventSource.readyState);const t=xt(this.lastEventSource,"open").pipe(At(()=>(this.readyStateSubject$.next(1),function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return pt(t,J(t))}(this.lastEventSource)))),e=xt(this.lastEventSource,"error").pipe(At(()=>(this.readyStateSubject$.next(this.lastEventSource.readyState),this.lastEventSource.readyState===EventSource.CONNECTING?vt(()=>new Pt("Initial connection failed")):Z))),n=Et(this.options.connectionTimeout).pipe(At(()=>(this.readyStateSubject$.next(2),vt(()=>new Pt("Connection timeout")))));return Ot(t,e).pipe(It(n),function(t){var e;void 0===t&&(t=1/0);var n=(e=t&&"object"==typeof t?t:{count:t}).count,r=void 0===n?1/0:n,o=e.delay,i=e.resetOnSuccess,u=void 0!==i&&i;return r<=0?C:L(function(t,e){var n,i=0,s=function(){var c=!1;n=t.subscribe(k(e,function(t){u&&(i=0),e.next(t)},void 0,function(t){if(i++<r){var u=function(){n?(n.unsubscribe(),n=null,s()):c=!0};if(null!=o){var a="number"==typeof o?Et(o):ut(o(t,i)),l=k(e,function(){l.unsubscribe(),u()},function(){e.complete()});a.subscribe(l)}else u()}else e.error(t)})),c&&(n.unsubscribe(),n=null,s())};s()})}({count:this.options.maxRetries,delay:(t,e)=>{if(t instanceof Pt){if(e>=this.options.maxRetries)return this.readyStateSubject$.next(2),vt(()=>new Pt(t.message));const n=Math.min(this.options.initialDelay*Math.pow(2,e),this.options.maxDelay),r=Math.random()*n;return console.log(`Retrying connection (attempt ${e+1}) in ${r}ms`),this.readyStateSubject$.next(0),Et(r)}return vt(()=>new Pt("Unrecoverable EventSource error",e))}}),Ct(this.destroy$),(r=()=>{this.lastEventSource&&this.lastEventSource.close(),this.readyStateSubject$.next(2)},L(function(t,e){try{t.subscribe(e)}finally{e.add(r)}})));var r},new P(function(e){ut(t()).subscribe(e)})).pipe(function(t){void 0===t&&(t={});var e=t.connector,n=void 0===e?function(){return new M}:e,r=t.resetOnError,o=void 0===r||r,i=t.resetOnComplete,u=void 0===i||i,s=t.resetOnRefCountZero,c=void 0===s||s;return function(t){var e,r,i,s=0,a=!1,l=!1,f=function(){null==r||r.unsubscribe(),r=void 0},h=function(){f(),e=i=void 0,a=l=!1},p=function(){var t=e;h(),null==t||t.unsubscribe()};return L(function(t,v){s++,l||a||f();var d=i=null!=i?i:n();v.add(function(){0!==--s||l||a||(r=$t(p,c))}),d.subscribe(v),!e&&s>0&&(e=new j({next:function(t){return d.next(t)},error:function(t){l=!0,f(),r=$t(h,o,t),d.error(t)},complete:function(){a=!0,f(),r=$t(h,u),d.complete()}}),ut(t).subscribe(e))})(t)}}({connector:()=>new N(1),resetOnComplete:!1,resetOnError:!1,resetOnRefCountZero:!1}));var t}setupEventListener(t,e){const n=this.eventSource$.pipe(Ct(this.destroy$),At(n=>(this.readyStateSubject$.next(n.readyState),xt(n,t).pipe(jt(n=>(console.error(`Error in "${t}" event`,n),e.error(n),Z)))))).subscribe({next:t=>e.next(t),error:t=>e.error(t),complete:()=>e.complete()});this.subscriptions.set(t,n),this.eventListenerCleanup.set(t,()=>{const n=this.subscriptions.get(t);n&&(n.unsubscribe(),this.subscriptions.delete(t)),e.closed||e.complete()})}}});