UNPKG

@reix/rxjs-bits

Version:

Rxjs integration for the @reix/bits package.

2 lines (1 loc) 10.2 kB
import{__extends as r}from"tslib";function t(r){return"function"==typeof r}var e=!1,n={Promise:void 0,set useDeprecatedSynchronousErrorHandling(r){r&&(new Error).stack;e=r},get useDeprecatedSynchronousErrorHandling(){return e}};function o(r){setTimeout((function(){throw r}),0)}var s={closed:!0,next:function(r){},error:function(r){if(n.useDeprecatedSynchronousErrorHandling)throw r;o(r)},complete:function(){}},i=function(){return Array.isArray||function(r){return r&&"number"==typeof r.length}}();var c=function(){function r(r){return Error.call(this),this.message=r?r.length+" errors occurred during unsubscription:\n"+r.map((function(r,t){return t+1+") "+r.toString()})).join("\n "):"",this.name="UnsubscriptionError",this.errors=r,this}return r.prototype=Object.create(Error.prototype),r}(),u=function(){function r(r){this.closed=!1,this._parentOrParents=null,this._subscriptions=null,r&&(this._unsubscribe=r)}return r.prototype.unsubscribe=function(){var e;if(!this.closed){var n,o=this._parentOrParents,s=this._unsubscribe,u=this._subscriptions;if(this.closed=!0,this._parentOrParents=null,this._subscriptions=null,o instanceof r)o.remove(this);else if(null!==o)for(var a=0;a<o.length;++a){o[a].remove(this)}if(t(s))try{s.call(this)}catch(r){e=r instanceof c?h(r.errors):[r]}if(i(u)){a=-1;for(var p=u.length;++a<p;){var b=u[a];if(null!==(n=b)&&"object"==typeof n)try{b.unsubscribe()}catch(r){e=e||[],r instanceof c?e=e.concat(h(r.errors)):e.push(r)}}}if(e)throw new c(e)}},r.prototype.add=function(t){var e=t;if(!t)return r.EMPTY;switch(typeof t){case"function":e=new r(t);case"object":if(e===this||e.closed||"function"!=typeof e.unsubscribe)return e;if(this.closed)return e.unsubscribe(),e;if(!(e instanceof r)){var n=e;(e=new r)._subscriptions=[n]}break;default:throw new Error("unrecognized teardown "+t+" added to Subscription.")}var o=e._parentOrParents;if(null===o)e._parentOrParents=this;else if(o instanceof r){if(o===this)return e;e._parentOrParents=[o,this]}else{if(-1!==o.indexOf(this))return e;o.push(this)}var s=this._subscriptions;return null===s?this._subscriptions=[e]:s.push(e),e},r.prototype.remove=function(r){var t=this._subscriptions;if(t){var e=t.indexOf(r);-1!==e&&t.splice(e,1)}},r.EMPTY=function(r){return r.closed=!0,r}(new r),r}();function h(r){return r.reduce((function(r,t){return r.concat(t instanceof c?t.errors:t)}),[])}var a=function(){return"function"==typeof Symbol?Symbol("rxSubscriber"):"@@rxSubscriber_"+Math.random()}(),p=function(t){function e(r,n,o){var i=t.call(this)||this;switch(i.syncErrorValue=null,i.syncErrorThrown=!1,i.syncErrorThrowable=!1,i.isStopped=!1,arguments.length){case 0:i.destination=s;break;case 1:if(!r){i.destination=s;break}if("object"==typeof r){r instanceof e?(i.syncErrorThrowable=r.syncErrorThrowable,i.destination=r,r.add(i)):(i.syncErrorThrowable=!0,i.destination=new b(i,r));break}default:i.syncErrorThrowable=!0,i.destination=new b(i,r,n,o)}return i}return r(e,t),e.prototype[a]=function(){return this},e.create=function(r,t,n){var o=new e(r,t,n);return o.syncErrorThrowable=!1,o},e.prototype.next=function(r){this.isStopped||this._next(r)},e.prototype.error=function(r){this.isStopped||(this.isStopped=!0,this._error(r))},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))},e.prototype._next=function(r){this.destination.next(r)},e.prototype._error=function(r){this.destination.error(r),this.unsubscribe()},e.prototype._complete=function(){this.destination.complete(),this.unsubscribe()},e.prototype._unsubscribeAndRecycle=function(){var r=this._parentOrParents;return this._parentOrParents=null,this.unsubscribe(),this.closed=!1,this.isStopped=!1,this._parentOrParents=r,this},e}(u),b=function(e){function i(r,n,o,i){var c,u=e.call(this)||this;u._parentSubscriber=r;var h=u;return t(n)?c=n:n&&(c=n.next,o=n.error,i=n.complete,n!==s&&(t((h=Object.create(n)).unsubscribe)&&u.add(h.unsubscribe.bind(h)),h.unsubscribe=u.unsubscribe.bind(u))),u._context=h,u._next=c,u._error=o,u._complete=i,u}return r(i,e),i.prototype.next=function(r){if(!this.isStopped&&this._next){var t=this._parentSubscriber;n.useDeprecatedSynchronousErrorHandling&&t.syncErrorThrowable?this.__tryOrSetError(t,this._next,r)&&this.unsubscribe():this.__tryOrUnsub(this._next,r)}},i.prototype.error=function(r){if(!this.isStopped){var t=this._parentSubscriber,e=n.useDeprecatedSynchronousErrorHandling;if(this._error)e&&t.syncErrorThrowable?(this.__tryOrSetError(t,this._error,r),this.unsubscribe()):(this.__tryOrUnsub(this._error,r),this.unsubscribe());else if(t.syncErrorThrowable)e?(t.syncErrorValue=r,t.syncErrorThrown=!0):o(r),this.unsubscribe();else{if(this.unsubscribe(),e)throw r;o(r)}}},i.prototype.complete=function(){var r=this;if(!this.isStopped){var t=this._parentSubscriber;if(this._complete){var e=function(){return r._complete.call(r._context)};n.useDeprecatedSynchronousErrorHandling&&t.syncErrorThrowable?(this.__tryOrSetError(t,e),this.unsubscribe()):(this.__tryOrUnsub(e),this.unsubscribe())}else this.unsubscribe()}},i.prototype.__tryOrUnsub=function(r,t){try{r.call(this._context,t)}catch(r){if(this.unsubscribe(),n.useDeprecatedSynchronousErrorHandling)throw r;o(r)}},i.prototype.__tryOrSetError=function(r,t,e){if(!n.useDeprecatedSynchronousErrorHandling)throw new Error("bad call");try{t.call(this._context,e)}catch(t){return n.useDeprecatedSynchronousErrorHandling?(r.syncErrorValue=t,r.syncErrorThrown=!0,!0):(o(t),!0)}return!1},i.prototype._unsubscribe=function(){var r=this._parentSubscriber;this._context=null,this._parentSubscriber=null,r.unsubscribe()},i}(p);var l=function(){return"function"==typeof Symbol&&Symbol.observable||"@@observable"}();function f(){}function d(r){return r?1===r.length?r[0]:function(t){return r.reduce((function(r,t){return t(r)}),t)}:f}var y=function(){function r(r){this._isScalar=!1,r&&(this._subscribe=r)}return r.prototype.lift=function(t){var e=new r;return e.source=this,e.operator=t,e},r.prototype.subscribe=function(r,t,e){var o=this.operator,i=function(r,t,e){if(r){if(r instanceof p)return r;if(r[a])return r[a]()}return r||t||e?new p(r,t,e):new p(s)}(r,t,e);if(o?i.add(o.call(i,this.source)):i.add(this.source||n.useDeprecatedSynchronousErrorHandling&&!i.syncErrorThrowable?this._subscribe(i):this._trySubscribe(i)),n.useDeprecatedSynchronousErrorHandling&&i.syncErrorThrowable&&(i.syncErrorThrowable=!1,i.syncErrorThrown))throw i.syncErrorValue;return i},r.prototype._trySubscribe=function(r){try{return this._subscribe(r)}catch(t){n.useDeprecatedSynchronousErrorHandling&&(r.syncErrorThrown=!0,r.syncErrorValue=t),!function(r){for(;r;){var t=r,e=t.closed,n=t.destination,o=t.isStopped;if(e||o)return!1;r=n&&n instanceof p?n:null}return!0}(r)?console.warn(t):r.error(t)}},r.prototype.forEach=function(r,t){var e=this;return new(t=_(t))((function(t,n){var o;o=e.subscribe((function(t){try{r(t)}catch(r){n(r),o&&o.unsubscribe()}}),n,t)}))},r.prototype._subscribe=function(r){var t=this.source;return t&&t.subscribe(r)},r.prototype[l]=function(){return this},r.prototype.pipe=function(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];return 0===r.length?this:d(r)(this)},r.prototype.toPromise=function(r){var t=this;return new(r=_(r))((function(r,e){var n;t.subscribe((function(r){return n=r}),(function(r){return e(r)}),(function(){return r(n)}))}))},r.create=function(t){return new r(t)},r}();function _(r){if(r||(r=Promise),!r)throw new Error("no Promise impl found");return r}var v=function(){function r(){return Error.call(this),this.message="object unsubscribed",this.name="ObjectUnsubscribedError",this}return r.prototype=Object.create(Error.prototype),r}(),w=function(t){function e(r,e){var n=t.call(this)||this;return n.subject=r,n.subscriber=e,n.closed=!1,n}return r(e,t),e.prototype.unsubscribe=function(){if(!this.closed){this.closed=!0;var r=this.subject,t=r.observers;if(this.subject=null,t&&0!==t.length&&!r.isStopped&&!r.closed){var e=t.indexOf(this.subscriber);-1!==e&&t.splice(e,1)}}},e}(u),E=function(t){function e(r){var e=t.call(this,r)||this;return e.destination=r,e}return r(e,t),e}(p),S=function(t){function e(){var r=t.call(this)||this;return r.observers=[],r.closed=!1,r.isStopped=!1,r.hasError=!1,r.thrownError=null,r}return r(e,t),e.prototype[a]=function(){return new E(this)},e.prototype.lift=function(r){var t=new m(this,this);return t.operator=r,t},e.prototype.next=function(r){if(this.closed)throw new v;if(!this.isStopped)for(var t=this.observers,e=t.length,n=t.slice(),o=0;o<e;o++)n[o].next(r)},e.prototype.error=function(r){if(this.closed)throw new v;this.hasError=!0,this.thrownError=r,this.isStopped=!0;for(var t=this.observers,e=t.length,n=t.slice(),o=0;o<e;o++)n[o].error(r);this.observers.length=0},e.prototype.complete=function(){if(this.closed)throw new v;this.isStopped=!0;for(var r=this.observers,t=r.length,e=r.slice(),n=0;n<t;n++)e[n].complete();this.observers.length=0},e.prototype.unsubscribe=function(){this.isStopped=!0,this.closed=!0,this.observers=null},e.prototype._trySubscribe=function(r){if(this.closed)throw new v;return t.prototype._trySubscribe.call(this,r)},e.prototype._subscribe=function(r){if(this.closed)throw new v;return this.hasError?(r.error(this.thrownError),u.EMPTY):this.isStopped?(r.complete(),u.EMPTY):(this.observers.push(r),new w(this,r))},e.prototype.asObservable=function(){var r=new y;return r.source=this,r},e.create=function(r,t){return new m(r,t)},e}(y),m=function(t){function e(r,e){var n=t.call(this)||this;return n.destination=r,n.source=e,n}return r(e,t),e.prototype.next=function(r){var t=this.destination;t&&t.next&&t.next(r)},e.prototype.error=function(r){var t=this.destination;t&&t.error&&this.destination.error(r)},e.prototype.complete=function(){var r=this.destination;r&&r.complete&&this.destination.complete()},e.prototype._subscribe=function(r){return this.source?this.source.subscribe(r):u.EMPTY},e}(S);const g=(r,t)=>{const e=new S,n=new S,o=(r,t)=>e.next({value:r,code:t});r.on(t,o),n.subscribe(e=>{let{code:n,value:o,maxBitHint:s}=e;r.emit(void 0===n?t:n,o,s)});return{on$:e,emit$:n,dispose:()=>{r.remove(o),n.complete(),e.complete()}}};export{g as toSubjects};