Skip to content

Commit

Permalink
fix(FromObservableAsyncIterable): Fix handling of asynchronously emit…
Browse files Browse the repository at this point in the history
…ting Observables (#150)
  • Loading branch information
lizardruss authored and mattpodwysocki committed Nov 13, 2017
1 parent a4a4464 commit 2c7222c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
45 changes: 45 additions & 0 deletions spec/asynciterable/from-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Ix from '../Ix';
import * as test from 'tape-async';
const { from } = Ix.AsyncIterable;
import { hasNext, noNext } from '../asynciterablehelpers';
import { setInterval, clearInterval } from 'timers';

test('AsyncIterable#from from promise list', async t => {
const xs: Iterable<Promise<number>> = [
Expand Down Expand Up @@ -206,6 +207,50 @@ test('AsyncIterable#fromObservable with completion', async t => {
t.end();
});

test('AsyncIterable#fromObservable without completion', async t => {
const xs = new TestObservable<number>(obs => {
let count = 0;
const interval = setInterval(() => {
obs.next(count++);
if (count === 3) {
clearInterval(interval);
obs.complete();
}
}, 10);
return new EmptySubscription();
});
const ys = from(xs);

const it = ys[Symbol.asyncIterator]();
await hasNext(t, it, 0);
await hasNext(t, it, 1);
await hasNext(t, it, 2);
await noNext(t, it);
t.end();
});

test('AsyncIterable#fromObservable without completion', async t => {
const xs = new TestObservable<number>(obs => {
let count = 0;
const interval = setInterval(() => {
obs.next(count++);
if (count === 3) {
clearInterval(interval);
obs.complete();
}
}, 10);
return new EmptySubscription();
});
const ys = from(xs, (x, i) => x + i);

const it = ys[Symbol.asyncIterator]();
await hasNext(t, it, 0);
await hasNext(t, it, 2);
await hasNext(t, it, 4);
await noNext(t, it);
t.end();
});

test('AsyncIterable#fromObservable with error', async t => {
const err = new Error();
const xs = new TestObservable<number>(obs => {
Expand Down
66 changes: 18 additions & 48 deletions src/asynciterable/asynciterablex.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AsyncSink } from './../asyncsink';
import { OperatorAsyncFunction } from '../interfaces';
import { bindCallback } from '../internal/bindcallback';
import { identityAsync } from '../internal/identity';
Expand Down Expand Up @@ -131,42 +132,6 @@ class FromPromiseIterable<TSource, TResult = TSource> extends AsyncIterableX<TRe
}
}

class AsyncObserver<TSource> {
public values: TSource[];
public hasError: boolean;
public hasCompleted: boolean;
public errorValue: any;
public closed: boolean;

constructor() {
this.values = [];
this.hasCompleted = false;
this.hasError = false;
this.errorValue = null;
this.closed = false;
}

next(value: TSource) {
if (!this.closed) {
this.values.push(value);
}
}

error(err: any) {
if (!this.closed) {
this.closed = true;
this.hasError = true;
this.errorValue = err;
}
}

complete() {
if (!this.closed) {
this.closed = true;
}
}
}

class FromObservableAsyncIterable<TSource, TResult = TSource> extends AsyncIterableX<TResult> {
private _observable: Observable<TSource>;
private _selector: (value: TSource, index: number) => TResult | Promise<TResult>;
Expand All @@ -181,21 +146,26 @@ class FromObservableAsyncIterable<TSource, TResult = TSource> extends AsyncItera
}

async *[Symbol.asyncIterator]() {
const observer = new AsyncObserver<TSource>();
const subscription = this._observable.subscribe(observer);
const sink: AsyncSink<TSource> = new AsyncSink<TSource>();
const subscription = this._observable.subscribe({
next(value: TSource) {
sink.write(value);
},
error(err: any) {
sink.error(err);
},
complete() {
sink.end();
}
});

let i = 0;
while (1) {
if (observer.values.length > 0) {
yield await this._selector(observer.values.shift()!, i++);
} else if (observer.closed) {
subscription.unsubscribe();
if (observer.hasError) {
throw observer.errorValue;
} else {
break;
}
try {
for (let next; !(next = await sink.next()).done; ) {
yield await this._selector(next.value!, i++);
}
} finally {
subscription.unsubscribe();
}
}
}
Expand Down

0 comments on commit 2c7222c

Please sign in to comment.