@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
62 lines (60 loc) • 2.12 kB
JavaScript
import { toObserver } from '../util/toobserver.js';
import { observable as symbolObservable } from '../observer.js';
var BooleanSubscription = /** @class */ (function () {
function BooleanSubscription() {
this.isUnsubscribed = false;
}
BooleanSubscription.prototype.unsubscribe = function () {
this.isUnsubscribed = true;
};
return BooleanSubscription;
}());
var AsyncIterableObservable = /** @class */ (function () {
function AsyncIterableObservable(source) {
this._source = source;
}
/** @nocollapse */
AsyncIterableObservable.prototype[symbolObservable] = function () {
return this;
};
/** @nocollapse */
AsyncIterableObservable.prototype.subscribe = function (next, error, complete) {
var observer = toObserver(next, error, complete);
var subscription = new BooleanSubscription();
var it = this._source[Symbol.asyncIterator]();
var f = function () {
it.next()
.then(function (_a) {
var value = _a.value, done = _a.done;
if (!subscription.isUnsubscribed) {
if (done) {
observer.complete();
}
else {
observer.next(value);
f();
}
}
})
.catch(function (err) {
if (!subscription.isUnsubscribed) {
observer.error(err);
}
});
};
f();
return subscription;
};
return AsyncIterableObservable;
}());
/**
* 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.
*/
export function toObservable(source) {
return new AsyncIterableObservable(source);
}
//# sourceMappingURL=toobservable.js.map