123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.Observable = void 0;
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
- // === Symbol Support ===
- var hasSymbols = function () {
- return typeof Symbol === 'function';
- };
- var hasSymbol = function (name) {
- return hasSymbols() && Boolean(Symbol[name]);
- };
- var getSymbol = function (name) {
- return hasSymbol(name) ? Symbol[name] : '@@' + name;
- };
- if (hasSymbols() && !hasSymbol('observable')) {
- Symbol.observable = Symbol('observable');
- }
- var SymbolIterator = getSymbol('iterator');
- var SymbolObservable = getSymbol('observable');
- var SymbolSpecies = getSymbol('species'); // === Abstract Operations ===
- function getMethod(obj, key) {
- var value = obj[key];
- if (value == null) return undefined;
- if (typeof value !== 'function') throw new TypeError(value + ' is not a function');
- return value;
- }
- function getSpecies(obj) {
- var ctor = obj.constructor;
- if (ctor !== undefined) {
- ctor = ctor[SymbolSpecies];
- if (ctor === null) {
- ctor = undefined;
- }
- }
- return ctor !== undefined ? ctor : Observable;
- }
- function isObservable(x) {
- return x instanceof Observable; // SPEC: Brand check
- }
- function hostReportError(e) {
- if (hostReportError.log) {
- hostReportError.log(e);
- } else {
- setTimeout(function () {
- throw e;
- });
- }
- }
- function enqueue(fn) {
- Promise.resolve().then(function () {
- try {
- fn();
- } catch (e) {
- hostReportError(e);
- }
- });
- }
- function cleanupSubscription(subscription) {
- var cleanup = subscription._cleanup;
- if (cleanup === undefined) return;
- subscription._cleanup = undefined;
- if (!cleanup) {
- return;
- }
- try {
- if (typeof cleanup === 'function') {
- cleanup();
- } else {
- var unsubscribe = getMethod(cleanup, 'unsubscribe');
- if (unsubscribe) {
- unsubscribe.call(cleanup);
- }
- }
- } catch (e) {
- hostReportError(e);
- }
- }
- function closeSubscription(subscription) {
- subscription._observer = undefined;
- subscription._queue = undefined;
- subscription._state = 'closed';
- }
- function flushSubscription(subscription) {
- var queue = subscription._queue;
- if (!queue) {
- return;
- }
- subscription._queue = undefined;
- subscription._state = 'ready';
- for (var i = 0; i < queue.length; ++i) {
- notifySubscription(subscription, queue[i].type, queue[i].value);
- if (subscription._state === 'closed') break;
- }
- }
- function notifySubscription(subscription, type, value) {
- subscription._state = 'running';
- var observer = subscription._observer;
- try {
- var m = getMethod(observer, type);
- switch (type) {
- case 'next':
- if (m) m.call(observer, value);
- break;
- case 'error':
- closeSubscription(subscription);
- if (m) m.call(observer, value);else throw value;
- break;
- case 'complete':
- closeSubscription(subscription);
- if (m) m.call(observer);
- break;
- }
- } catch (e) {
- hostReportError(e);
- }
- if (subscription._state === 'closed') cleanupSubscription(subscription);else if (subscription._state === 'running') subscription._state = 'ready';
- }
- function onNotify(subscription, type, value) {
- if (subscription._state === 'closed') return;
- if (subscription._state === 'buffering') {
- subscription._queue.push({
- type: type,
- value: value
- });
- return;
- }
- if (subscription._state !== 'ready') {
- subscription._state = 'buffering';
- subscription._queue = [{
- type: type,
- value: value
- }];
- enqueue(function () {
- return flushSubscription(subscription);
- });
- return;
- }
- notifySubscription(subscription, type, value);
- }
- var Subscription =
- /*#__PURE__*/
- function () {
- function Subscription(observer, subscriber) {
- _classCallCheck(this, Subscription);
- // ASSERT: observer is an object
- // ASSERT: subscriber is callable
- this._cleanup = undefined;
- this._observer = observer;
- this._queue = undefined;
- this._state = 'initializing';
- var subscriptionObserver = new SubscriptionObserver(this);
- try {
- this._cleanup = subscriber.call(undefined, subscriptionObserver);
- } catch (e) {
- subscriptionObserver.error(e);
- }
- if (this._state === 'initializing') this._state = 'ready';
- }
- _createClass(Subscription, [{
- key: "unsubscribe",
- value: function unsubscribe() {
- if (this._state !== 'closed') {
- closeSubscription(this);
- cleanupSubscription(this);
- }
- }
- }, {
- key: "closed",
- get: function () {
- return this._state === 'closed';
- }
- }]);
- return Subscription;
- }();
- var SubscriptionObserver =
- /*#__PURE__*/
- function () {
- function SubscriptionObserver(subscription) {
- _classCallCheck(this, SubscriptionObserver);
- this._subscription = subscription;
- }
- _createClass(SubscriptionObserver, [{
- key: "next",
- value: function next(value) {
- onNotify(this._subscription, 'next', value);
- }
- }, {
- key: "error",
- value: function error(value) {
- onNotify(this._subscription, 'error', value);
- }
- }, {
- key: "complete",
- value: function complete() {
- onNotify(this._subscription, 'complete');
- }
- }, {
- key: "closed",
- get: function () {
- return this._subscription._state === 'closed';
- }
- }]);
- return SubscriptionObserver;
- }();
- var Observable =
- /*#__PURE__*/
- function () {
- function Observable(subscriber) {
- _classCallCheck(this, Observable);
- if (!(this instanceof Observable)) throw new TypeError('Observable cannot be called as a function');
- if (typeof subscriber !== 'function') throw new TypeError('Observable initializer must be a function');
- this._subscriber = subscriber;
- }
- _createClass(Observable, [{
- key: "subscribe",
- value: function subscribe(observer) {
- if (typeof observer !== 'object' || observer === null) {
- observer = {
- next: observer,
- error: arguments[1],
- complete: arguments[2]
- };
- }
- return new Subscription(observer, this._subscriber);
- }
- }, {
- key: "forEach",
- value: function forEach(fn) {
- var _this = this;
- return new Promise(function (resolve, reject) {
- if (typeof fn !== 'function') {
- reject(new TypeError(fn + ' is not a function'));
- return;
- }
- function done() {
- subscription.unsubscribe();
- resolve();
- }
- var subscription = _this.subscribe({
- next: function (value) {
- try {
- fn(value, done);
- } catch (e) {
- reject(e);
- subscription.unsubscribe();
- }
- },
- error: reject,
- complete: resolve
- });
- });
- }
- }, {
- key: "map",
- value: function map(fn) {
- var _this2 = this;
- if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
- var C = getSpecies(this);
- return new C(function (observer) {
- return _this2.subscribe({
- next: function (value) {
- try {
- value = fn(value);
- } catch (e) {
- return observer.error(e);
- }
- observer.next(value);
- },
- error: function (e) {
- observer.error(e);
- },
- complete: function () {
- observer.complete();
- }
- });
- });
- }
- }, {
- key: "filter",
- value: function filter(fn) {
- var _this3 = this;
- if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
- var C = getSpecies(this);
- return new C(function (observer) {
- return _this3.subscribe({
- next: function (value) {
- try {
- if (!fn(value)) return;
- } catch (e) {
- return observer.error(e);
- }
- observer.next(value);
- },
- error: function (e) {
- observer.error(e);
- },
- complete: function () {
- observer.complete();
- }
- });
- });
- }
- }, {
- key: "reduce",
- value: function reduce(fn) {
- var _this4 = this;
- if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
- var C = getSpecies(this);
- var hasSeed = arguments.length > 1;
- var hasValue = false;
- var seed = arguments[1];
- var acc = seed;
- return new C(function (observer) {
- return _this4.subscribe({
- next: function (value) {
- var first = !hasValue;
- hasValue = true;
- if (!first || hasSeed) {
- try {
- acc = fn(acc, value);
- } catch (e) {
- return observer.error(e);
- }
- } else {
- acc = value;
- }
- },
- error: function (e) {
- observer.error(e);
- },
- complete: function () {
- if (!hasValue && !hasSeed) return observer.error(new TypeError('Cannot reduce an empty sequence'));
- observer.next(acc);
- observer.complete();
- }
- });
- });
- }
- }, {
- key: "concat",
- value: function concat() {
- var _this5 = this;
- for (var _len = arguments.length, sources = new Array(_len), _key = 0; _key < _len; _key++) {
- sources[_key] = arguments[_key];
- }
- var C = getSpecies(this);
- return new C(function (observer) {
- var subscription;
- var index = 0;
- function startNext(next) {
- subscription = next.subscribe({
- next: function (v) {
- observer.next(v);
- },
- error: function (e) {
- observer.error(e);
- },
- complete: function () {
- if (index === sources.length) {
- subscription = undefined;
- observer.complete();
- } else {
- startNext(C.from(sources[index++]));
- }
- }
- });
- }
- startNext(_this5);
- return function () {
- if (subscription) {
- subscription.unsubscribe();
- subscription = undefined;
- }
- };
- });
- }
- }, {
- key: "flatMap",
- value: function flatMap(fn) {
- var _this6 = this;
- if (typeof fn !== 'function') throw new TypeError(fn + ' is not a function');
- var C = getSpecies(this);
- return new C(function (observer) {
- var subscriptions = [];
- var outer = _this6.subscribe({
- next: function (value) {
- if (fn) {
- try {
- value = fn(value);
- } catch (e) {
- return observer.error(e);
- }
- }
- var inner = C.from(value).subscribe({
- next: function (value) {
- observer.next(value);
- },
- error: function (e) {
- observer.error(e);
- },
- complete: function () {
- var i = subscriptions.indexOf(inner);
- if (i >= 0) subscriptions.splice(i, 1);
- completeIfDone();
- }
- });
- subscriptions.push(inner);
- },
- error: function (e) {
- observer.error(e);
- },
- complete: function () {
- completeIfDone();
- }
- });
- function completeIfDone() {
- if (outer.closed && subscriptions.length === 0) observer.complete();
- }
- return function () {
- subscriptions.forEach(function (s) {
- return s.unsubscribe();
- });
- outer.unsubscribe();
- };
- });
- }
- }, {
- key: SymbolObservable,
- value: function () {
- return this;
- }
- }], [{
- key: "from",
- value: function from(x) {
- var C = typeof this === 'function' ? this : Observable;
- if (x == null) throw new TypeError(x + ' is not an object');
- var method = getMethod(x, SymbolObservable);
- if (method) {
- var observable = method.call(x);
- if (Object(observable) !== observable) throw new TypeError(observable + ' is not an object');
- if (isObservable(observable) && observable.constructor === C) return observable;
- return new C(function (observer) {
- return observable.subscribe(observer);
- });
- }
- if (hasSymbol('iterator')) {
- method = getMethod(x, SymbolIterator);
- if (method) {
- return new C(function (observer) {
- enqueue(function () {
- if (observer.closed) return;
- var _iteratorNormalCompletion = true;
- var _didIteratorError = false;
- var _iteratorError = undefined;
- try {
- for (var _iterator = method.call(x)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
- var _item = _step.value;
- observer.next(_item);
- if (observer.closed) return;
- }
- } catch (err) {
- _didIteratorError = true;
- _iteratorError = err;
- } finally {
- try {
- if (!_iteratorNormalCompletion && _iterator.return != null) {
- _iterator.return();
- }
- } finally {
- if (_didIteratorError) {
- throw _iteratorError;
- }
- }
- }
- observer.complete();
- });
- });
- }
- }
- if (Array.isArray(x)) {
- return new C(function (observer) {
- enqueue(function () {
- if (observer.closed) return;
- for (var i = 0; i < x.length; ++i) {
- observer.next(x[i]);
- if (observer.closed) return;
- }
- observer.complete();
- });
- });
- }
- throw new TypeError(x + ' is not observable');
- }
- }, {
- key: "of",
- value: function of() {
- for (var _len2 = arguments.length, items = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
- items[_key2] = arguments[_key2];
- }
- var C = typeof this === 'function' ? this : Observable;
- return new C(function (observer) {
- enqueue(function () {
- if (observer.closed) return;
- for (var i = 0; i < items.length; ++i) {
- observer.next(items[i]);
- if (observer.closed) return;
- }
- observer.complete();
- });
- });
- }
- }, {
- key: SymbolSpecies,
- get: function () {
- return this;
- }
- }]);
- return Observable;
- }();
- exports.Observable = Observable;
- if (hasSymbols()) {
- Object.defineProperty(Observable, Symbol('extensions'), {
- value: {
- symbol: SymbolObservable,
- hostReportError: hostReportError
- },
- configurable: true
- });
- }
|