UNPKG

rxjs-zone-less

Version:

A set of wrappers for RxJS to avoid unnecessary change detection and zone interference in Angular.

1 lines 11.5 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("rxjs"),require("tslib"),require("rxjs/operators"),require("rxjs/testing")):"function"==typeof define&&define.amd?define(["exports","rxjs","tslib","rxjs/operators","rxjs/testing"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["rxjs-zone-less"]={},e.rxjs,e.tslib,e.operators,e.testing)}(this,function(e,o,u,z,s){"use strict";var t="undefined"!=typeof globalThis&&globalThis,n="undefined"!=typeof window&&window,r="undefined"!=typeof self&&"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope&&self,i="undefined"!=typeof global&&global,C=t||i||n||r,G=h("Promise");function h(e,t){return"string"==typeof e&&(t=e,e=C),e["__zone_symbol__"+t]||e[t]}var l,a={setInterval:function(e,t){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];var i=a.delegate;return null!=i&&i.setInterval?i.setInterval.apply(i,u.__spreadArray([e,t],n,!1)):(i=e,e=t,h("setInterval")(i,e=void 0===e?0:e))},clearInterval:function(e){var t=a.delegate;return((null==t?void 0:t.clearInterval)||function(e){return h("clearInterval")(e)})(e)}};function c(e,t){return l.call(this)||this}l=o.Subscription,u.__extends(c,l),c.prototype.schedule=function(e,t){return this};d=c,u.__extends(f,d),f.prototype.schedule=function(e,t){if(void 0===t&&(t=0),this.closed)return this;this.state=e;var e=this.id,n=this.scheduler;return null!=e&&(this.id=this.recycleAsyncId(n,e,t)),this.pending=!0,this.delay=t,this.id=this.id||this.requestAsyncId(n,this.id,t),this},f.prototype.requestAsyncId=function(e,t,n){return void 0===n&&(n=0),a.setInterval(e.flush.bind(e,this),n)},f.prototype.recycleAsyncId=function(e,t,n){if(null!=(n=void 0===n?0:n)&&this.delay===n&&!1===this.pending)return t;a.clearInterval(t)},f.prototype.execute=function(e,t){if(this.closed)return new Error("executing a cancelled action");this.pending=!1;e=this._execute(e,t);if(e)return e;!1===this.pending&&null!=this.id&&(this.id=this.recycleAsyncId(this.scheduler,this.id,null))},f.prototype._execute=function(e,t){var n,r=!1;try{this.work(e)}catch(e){r=!0,n=!!e&&e||new Error(e)}if(r)return this.unsubscribe(),n},f.prototype.unsubscribe=function(){var e,t,n,r;this.closed||(e=this.id,n=(t=this.scheduler).actions,this.work=this.state=this.scheduler=null,this.pending=!1,n&&0<=(r=n.indexOf(this))&&n.splice(r,1),null!=e&&(this.id=this.recycleAsyncId(t,e,null)),this.delay=null,d.prototype.unsubscribe.call(this))};var d,t=f;function f(e,t){var n=d.call(this,e,t)||this;return n.scheduler=e,n.work=t,n.pending=!1,n}var v={now:function(){return(v.delegate||Date).now()}},N=(p.prototype.schedule=function(e,t,n){return void 0===t&&(t=0),new this.schedulerActionCtor(this,e).schedule(n,t)},p.now=v.now,p);function p(e,t){void 0===t&&(t=p.now),this.schedulerActionCtor=e,this.now=t}y=N,u.__extends(m,y),m.prototype.flush=function(e){var t,n=this.actions;if(this._active)n.push(e);else{this._active=!0;do{if(t=e.execute(e.state,e.delay))break}while(e=n.shift());if(this._active=!1,t){for(;e=n.shift();)e.unsubscribe();throw t}}};var y,i=m;function m(e,t){e=y.call(this,e,t=void 0===t?N.now:t)||this;return e.actions=[],e._active=!1,e._scheduled=void 0,e}var b=new i(t);function w(e){return!Array.isArray(e)&&0<=e-parseFloat(e)+1}function P(e){return e&&"function"==typeof e.schedule}function W(e){var t=e.subscriber,n=e.counter,e=e.period;t.next(n),this.schedule({subscriber:t,counter:n+1,period:e},e)}function R(e){var t=e.index,n=e.period,r=e.subscriber;if(r.next(t),!r.closed){if(-1===n)return r.complete();e.index=t+1,this.schedule(e,n)}}function B(e){return"function"==typeof e}var H=Array.isArray;function J(e){return e&&"function"==typeof e.addListener&&"function"==typeof e.removeListener}function K(e){return e&&"function"==typeof e.on&&"function"==typeof e.off}function Q(e){return e&&"function"==typeof e.addEventListener&&"function"==typeof e.removeEventListener}var U,V=1,_={};function X(e){return e in _&&(delete _[e],!0)}var g,x,Y=function(e){var t=V++;return _[t]=!0,(U=U||G.resolve()).then(function(){return X(t)&&e()}),t},Z=function(e){X(e)},A={setImmediate:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=A.delegate;return((null==n?void 0:n.setImmediate)||Y).apply(void 0,e)},clearImmediate:function(e){var t=A.delegate;return((null==t?void 0:t.clearImmediate)||Z)(e)}},n=(g=t,u.__extends(I,g),I.prototype.requestAsyncId=function(e,t,n){return null!==(n=void 0===n?0:n)&&0<n?g.prototype.requestAsyncId.call(this,e,t,n):(e.actions.push(this),e._scheduled||(e._scheduled=A.setImmediate(e.flush.bind(e,void 0))))},I.prototype.recycleAsyncId=function(e,t,n){if(null!==(n=void 0===n?0:n)&&0<n||null===n&&0<this.delay)return g.prototype.recycleAsyncId.call(this,e,t,n);e.actions.some(function(e){return e.id===t})||(A.clearImmediate(t),e._scheduled=void 0)},I);function I(e,t){var n=g.call(this,e,t)||this;return n.scheduler=e,n.work=t,n}function q(){return null!==x&&x.apply(this,arguments)||this}x=i,u.__extends(q,x),q.prototype.flush=function(e){this._active=!0;var t,n=this._scheduled,r=(this._scheduled=void 0,this.actions);e=e||r.shift();do{if(t=e.execute(e.state,e.delay))break}while((e=r[0])&&e.id===n&&r.shift());if(this._active=!1,t){for(;(e=r[0])&&e.id===n&&r.shift();)e.unsubscribe();throw t}};var E,F,r=new q(n),n=(E=t,u.__extends(k,E),k.prototype.schedule=function(e,t){return 0<(t=void 0===t?0:t)?E.prototype.schedule.call(this,e,t):(this.delay=t,this.state=e,this.scheduler.flush(this),this)},k.prototype.execute=function(e,t){return 0<t||this.closed?E.prototype.execute.call(this,e,t):this._execute(e,t)},k.prototype.requestAsyncId=function(e,t,n){return null!==(n=void 0===n?0:n)&&0<n||null===n&&0<this.delay?E.prototype.requestAsyncId.call(this,e,t,n):e.flush(this)},k);function k(e,t){var n=E.call(this,e,t)||this;return n.scheduler=e,n.work=t,n}function $(){return null!==F&&F.apply(this,arguments)||this}F=i,u.__extends($,F);var S,j,n=new $(n),ee=h("cancelAnimationFrame"),te=h("requestAnimationFrame"),T={schedule:function(t){var e=te,n=ee,r=T.delegate,i=(r&&(e=r.requestAnimationFrame,n=r.cancelAnimationFrame),e(function(e){n=void 0,t(e)}));return new o.Subscription(function(){return null==n?void 0:n(i)})},requestAnimationFrame:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=T.delegate;return((null==n?void 0:n.requestAnimationFrame)||te).apply(void 0,e)},cancelAnimationFrame:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=T.delegate;return((null==n?void 0:n.cancelAnimationFrame)||ee).apply(void 0,e)}},t=(S=t,u.__extends(L,S),L.prototype.requestAsyncId=function(e,t,n){return null!==(n=void 0===n?0:n)&&0<n?S.prototype.requestAsyncId.call(this,e,t,n):(e.actions.push(this),e._scheduled||(e._scheduled=T.requestAnimationFrame(function(){return e.flush(void 0)})))},L.prototype.recycleAsyncId=function(e,t,n){if(null!=(n=void 0===n?0:n)&&0<n||null==n&&0<this.delay)return S.prototype.recycleAsyncId.call(this,e,t,n);e.actions.some(function(e){return e.id===t})||(T.cancelAnimationFrame(t),e._scheduled=void 0)},L);function L(e,t){var n=S.call(this,e,t)||this;return n.scheduler=e,n.work=t,n}function M(){return null!==j&&j.apply(this,arguments)||this}j=i,u.__extends(M,j),M.prototype.flush=function(e){this._active=!0;var t,n=this._scheduled,r=(this._scheduled=void 0,this.actions);e=e||r.shift();do{if(t=e.execute(e.state,e.delay))break}while((e=r[0])&&e.id===n&&r.shift());if(this._active=!1,t){for(;(e=r[0])&&e.id===n&&r.shift();)e.unsubscribe();throw t}};var O,i=new M(t),t=(O=s.TestScheduler,u.__extends(D,O),D.prototype.run=function(i){var o=this;try{return O.prototype.run.call(this,function(e){var t=o._createAnimator(),n=o._createDelegates(),r=(T.delegate=t.delegate,a.delegate=n.interval,A.delegate=n.immediate,v.delegate=o,e.animate);return e.animate=function(e){t.animate(e),r(e)},i(e)})}finally{T.delegate=void 0,a.delegate=void 0,A.delegate=void 0,v.delegate=void 0}},D.prototype._createAnimator=function(){var i=this;if(!this.runMode)throw new Error("animate() must only be used in run mode");var o,n=0;return{animate:function(e){if(o)throw new Error("animate() must not be called more than once within run()");if(/[|#]/.test(e))throw new Error("animate() must not complete or error");o=new Map;for(var t=0,n=s.TestScheduler.parseMarbles(e,void 0,void 0,void 0,!0);t<n.length;t++){var r=n[t];i.schedule(function(){var e=i.now(),t=Array.from(o.values());o.clear();for(var n=0,r=t;n<r.length;n++)(0,r[n])(e)},r.frame)}},delegate:{requestAnimationFrame:function(e){if(!o)throw new Error("animate() was not called within run()");var t=++n;return o.set(t,e),t},cancelAnimationFrame:function(e){if(!o)throw new Error("animate() was not called within run()");o.delete(e)}}}},D.prototype._createDelegates=function(){var u=this,r=0,s=new Map,l=function(){var t=u.now(),e=Array.from(s.values()).filter(function(e){return e.due<=t}),n=e.filter(function(e){return"immediate"===e.type});if(0<n.length)return i=(n=n[0]).handle,o=n.handler,s.delete(i),void o();var n=e.filter(function(e){return"interval"===e.type});if(0<n.length)return r=(n=n[0]).duration,o=n.handler,n.due=t+r,n.subscription=u.schedule(l,r),void o();var r,i,o,n=e.filter(function(e){return"timeout"===e.type});if(0<n.length)return i=(r=n[0]).handle,o=r.handler,s.delete(i),void o();throw new Error("Expected a due immediate or interval")};return{immediate:{setImmediate:function(e){var t=++r;return s.set(t,{due:u.now(),duration:0,handle:t,handler:e,subscription:u.schedule(l,0),type:"immediate"}),t},clearImmediate:function(e){var t=s.get(e);t&&(t.subscription.unsubscribe(),s.delete(e))}},interval:{setInterval:function(e,t){void 0===t&&(t=0);var n=++r;return s.set(n,{due:u.now()+t,duration:t,handle:n,handler:e,subscription:u.schedule(l,t),type:"interval"}),n},clearInterval:function(e){var t=s.get(e);t&&(t.subscription.unsubscribe(),s.delete(e))}},timeout:{setTimeout:function(e,t){void 0===t&&(t=0);var n=++r;return s.set(n,{due:u.now()+t,duration:t,handle:n,handler:e,subscription:u.schedule(l,t),type:"timeout"}),n},clearTimeout:function(e){var t=s.get(e);t&&(t.subscription.unsubscribe(),s.delete(e))}}}},D);function D(){return null!==O&&O.apply(this,arguments)||this}e.RxTestScheduler=t,e.animationFrameScheduler=i,e.asapScheduler=r,e.asyncScheduler=b,e.fromEvent=function e(n,r,i,t){return B(i)&&(t=i,i=void 0),B(t)?e(n,r,i).pipe(z.map(function(e){return H(e)?t.apply(void 0,e):t(e)})):new o.Observable(function(t){!function e(t,n,r,i,o){var u;if(Q(t)){var s=t;h(t,"addEventListener").call(t,n,r,o),u=function(){return h(s,"removeEventListener").call(s,n,r,o)}}else if(K(t)){var l=t;t.on(n,r),u=function(){return l.off(n,r)}}else if(J(t)){var a=t;t.addListener(n,r),u=function(){return a.removeListener(n,r)}}else{if(!t||!t.length)throw new TypeError("Invalid event target");for(var c=0,d=t.length;c<d;c++)e(t[c],n,r,i,o)}i.add(u)}(n,r,function(e){1<arguments.length?t.next(Array.prototype.slice.call(arguments)):t.next(e)},t,i)})},e.interval=function(t,n){return void 0===n&&(n=b),(!w(t=void 0===t?0:t)||t<0)&&(t=0),n&&"function"==typeof n.schedule||(n=b),new o.Observable(function(e){return e.add(n.schedule(W,t,{subscriber:e,counter:0,period:t})),e})},e.queueScheduler=n,e.timer=function(n,e,r){void 0===n&&(n=0);var i=-1;return w(e)?i=Number(e)<1?1:Number(e):P(e)&&(r=e),P(r)||(r=b),new o.Observable(function(e){var t=w(n)?n:+n-r.now();return r.schedule(R,t,{index:0,period:i,subscriber:e})})},Object.defineProperty(e,"__esModule",{value:!0})});