UNPKG

1.72 kBJavaScriptView Raw
1import { toObserver } from '../util/toobserver';
2import { observable as symbolObservable } from '../observer';
3class BooleanSubscription {
4 constructor() {
5 this.isUnsubscribed = false;
6 }
7 unsubscribe() {
8 this.isUnsubscribed = true;
9 }
10}
11class AsyncIterableObservable {
12 constructor(source) {
13 this._source = source;
14 }
15 [symbolObservable]() {
16 return this;
17 }
18 subscribe(next, error, complete) {
19 const observer = toObserver(next, error, complete);
20 const subscription = new BooleanSubscription();
21 const it = this._source[Symbol.asyncIterator]();
22 const f = () => {
23 it.next()
24 .then(({ value, done }) => {
25 if (!subscription.isUnsubscribed) {
26 if (done) {
27 observer.complete();
28 }
29 else {
30 observer.next(value);
31 f();
32 }
33 }
34 })
35 .catch((err) => {
36 if (!subscription.isUnsubscribed) {
37 observer.error(err);
38 }
39 });
40 };
41 f();
42 return subscription;
43 }
44}
45/**
46 * Converts the async-iterable sequence to an observable.
47 *
48 * @export
49 * @template TSource The type of the elements in the source sequence.
50 * @param {AsyncIterable<TSource>} source The async-iterable to convert to an observable.
51 * @returns {Observable<TSource>} The observable containing the elements from the async-iterable.
52 */
53export function toObservable(source) {
54 return new AsyncIterableObservable(source);
55}
56
57//# sourceMappingURL=toobservable.mjs.map