-
Notifications
You must be signed in to change notification settings - Fork 0
/
zen-observable-polyfill.js
1 lines (1 loc) · 4.99 KB
/
zen-observable-polyfill.js
1
!function(){"use strict";const e=()=>"function"==typeof Symbol,t=t=>e()&&Boolean(Symbol[t]),r=e=>t(e)?Symbol[e]:"@@"+e;function o(e,t){let r=e[t];if(null!=r){if("function"!=typeof r)throw new TypeError(r+" is not a function");return r}}function n(e){let t=e.constructor;return void 0!==t&&null===(t=t[r("species")])&&(t=void 0),void 0!==t?t:h}function i(e){i.log?i.log(e):setTimeout(()=>{throw e})}function s(e){Promise.resolve().then(()=>{try{e()}catch(e){i(e)}})}function c(e){let t=e._cleanup;if(void 0!==t&&(e._cleanup=void 0,t))try{if("function"==typeof t)t();else{let e=o(t,"unsubscribe");e&&e.call(t)}}catch(e){i(e)}}function u(e){e._observer=void 0,e._queue=void 0,e._state="closed"}function l(e,t,r){e._state="running";let n=e._observer;try{let s=o(n,t);switch(t){case"next":s&&s.call(n,r);break;case"error":if(u(e),!s)throw r;s.call(n,r);break;case"complete":u(e),s&&s.call(n)}}catch(e){i(e)}"closed"===e._state?c(e):"running"===e._state&&(e._state="ready")}function a(e,t,r){if("closed"!==e._state){if("buffering"!==e._state)return"ready"!==e._state?(e._state="buffering",e._queue=[{type:t,value:r}],void s(()=>(function(e){let t=e._queue;if(t){e._queue=void 0,e._state="ready";for(let r=0;r<t.length&&(l(e,t[r].type,t[r].value),"closed"!==e._state);++r);}})(e))):void l(e,t,r);e._queue.push({type:t,value:r})}}e()&&!t("observable")&&(Symbol.observable=Symbol("observable"));class f{constructor(e,t){this._cleanup=void 0,this._observer=e,this._queue=void 0,this._state="initializing";let r=new b(this);try{this._cleanup=t.call(void 0,r)}catch(e){r.error(e)}"initializing"===this._state&&(this._state="ready")}get closed(){return"closed"===this._state}unsubscribe(){"closed"!==this._state&&(u(this),c(this))}}class b{constructor(e){this._subscription=e}get closed(){return"closed"===this._subscription._state}next(e){a(this._subscription,"next",e)}error(e){a(this._subscription,"error",e)}complete(){a(this._subscription,"complete")}}class h{constructor(e){if(!(this instanceof h))throw new TypeError("Observable cannot be called as a function");if("function"!=typeof e)throw new TypeError("Observable initializer must be a function");this._subscriber=e}subscribe(e){return"object"==typeof e&&null!==e||(e={next:e,error:arguments[1],complete:arguments[2]}),new f(e,this._subscriber)}forEach(e){return new Promise((t,r)=>{if("function"!=typeof e)return void r(new TypeError(e+" is not a function"));function o(){n.unsubscribe(),t()}let n=this.subscribe({next(t){try{e(t,o)}catch(e){r(e),n.unsubscribe()}},error:r,complete:t})})}map(e){if("function"!=typeof e)throw new TypeError(e+" is not a function");return new(n(this))(t=>this.subscribe({next(r){try{r=e(r)}catch(e){return t.error(e)}t.next(r)},error(e){t.error(e)},complete(){t.complete()}}))}filter(e){if("function"!=typeof e)throw new TypeError(e+" is not a function");return new(n(this))(t=>this.subscribe({next(r){try{if(!e(r))return}catch(e){return t.error(e)}t.next(r)},error(e){t.error(e)},complete(){t.complete()}}))}reduce(e){if("function"!=typeof e)throw new TypeError(e+" is not a function");let t=n(this),r=arguments.length>1,o=!1,i=arguments[1];return new t(t=>this.subscribe({next(n){let s=!o;if(o=!0,!s||r)try{i=e(i,n)}catch(e){return t.error(e)}else i=n},error(e){t.error(e)},complete(){if(!o&&!r)return t.error(new TypeError("Cannot reduce an empty sequence"));t.next(i),t.complete()}}))}concat(...e){let t=n(this);return new t(r=>{let o;return function n(i){o=i.subscribe({next(e){r.next(e)},error(e){r.error(e)},complete(){0===e.length?(o=void 0,r.complete()):n(t.from(e.shift()))}})}(this),()=>{o&&(o.unsubscribe(),o=void 0)}})}flatMap(e){if("function"!=typeof e)throw new TypeError(e+" is not a function");let t=n(this);return new t(r=>{let o=[],n=this.subscribe({next(n){if(e)try{n=e(n)}catch(e){return r.error(e)}let s=t.from(n).subscribe({next(e){r.next(e)},error(e){r.error(e)},complete(){let e=o.indexOf(s);e>=0&&o.splice(e,1),i()}});o.push(s)},error(e){r.error(e)},complete(){i()}});function i(){n.closed&&0===o.length&&r.complete()}return()=>{o.forEach(e=>e.unsubscribe()),n.unsubscribe()}})}[r("observable")](){return this}static from(e){let n="function"==typeof this?this:h;if(null==e)throw new TypeError(e+" is not an object");let i=o(e,r("observable"));if(i){let t=i.call(e);if(Object(t)!==t)throw new TypeError(t+" is not an object");return function(e){return e instanceof h}(t)&&t.constructor===n?t:new n(e=>t.subscribe(e))}if(t("iterator")&&(i=o(e,r("iterator"))))return new n(t=>{s(()=>{if(!t.closed){for(let r of i.call(e))if(t.next(r),t.closed)return;t.complete()}})});if(Array.isArray(e))return new n(t=>{s(()=>{if(!t.closed){for(let r=0;r<e.length;++r)if(t.next(e[r]),t.closed)return;t.complete()}})});throw new TypeError(e+" is not observable")}static of(...e){return new("function"==typeof this?this:h)(t=>{s(()=>{if(!t.closed){for(let r=0;r<e.length;++r)if(t.next(e[r]),t.closed)return;t.complete()}})})}static get[r("species")](){return this}}e()&&Object.defineProperty(h,Symbol("extensions"),{value:{symbol:r("observable"),hostReportError:i},configurabe:!0}),window.Observable||(window.Observable=h)}();