UNPKG

ix

Version:

The Interactive Extensions for JavaScript

63 lines (61 loc) 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toObservable = void 0; const toobserver_js_1 = require("../util/toobserver.js"); const observer_js_1 = require("../observer.js"); class BooleanSubscription { constructor() { this.isUnsubscribed = false; } unsubscribe() { this.isUnsubscribed = true; } } class AsyncIterableObservable { constructor(source) { this._source = source; } /** @nocollapse */ [observer_js_1.observable]() { return this; } /** @nocollapse */ subscribe(next, error, complete) { const observer = (0, toobserver_js_1.toObserver)(next, error, complete); const subscription = new BooleanSubscription(); const it = this._source[Symbol.asyncIterator](); const f = () => { it.next() .then(({ value, done }) => { if (!subscription.isUnsubscribed) { if (done) { observer.complete(); } else { observer.next(value); f(); } } }) .catch((err) => { if (!subscription.isUnsubscribed) { observer.error(err); } }); }; f(); return subscription; } } /** * Converts the async-iterable sequence to an observable. * * @template TSource The type of the elements in the source sequence. * @param {AsyncIterable<TSource>} source The async-iterable to convert to an observable. * @returns {Observable<TSource>} The observable containing the elements from the async-iterable. */ function toObservable(source) { return new AsyncIterableObservable(source); } exports.toObservable = toObservable; //# sourceMappingURL=toobservable.js.map