Skip to content

Commit

Permalink
fix(flatten): Fix flatten operator related to MemoryStream(s)
Browse files Browse the repository at this point in the history
When increasing strictness, in particular "strictFunctionTypes",
flatten was quite broken even when trying a simple cyclejs example
`sources.HTTP.select().flatten()`

This commit fixes the compile errors and the return types.

Resolve #262.
  • Loading branch information
Sinewyk authored and staltz committed Jan 14, 2019
1 parent f6f6190 commit c2e8655
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1764,8 +1764,8 @@ export class Stream<T> implements InternalListener<T> {
*
* @return {Stream}
*/
flatten<R>(this: Stream<Stream<R>>): T {
return new Stream<R>(new Flatten(this)) as T & Stream<R>;
flatten<R>(this: Stream<Stream<R>>): Stream<R> {
return new Stream<R>(new Flatten(this));
}

/**
Expand Down Expand Up @@ -1976,7 +1976,7 @@ export class Stream<T> implements InternalListener<T> {

export class MemoryStream<T> extends Stream<T> {
private _v?: T;
private _has: boolean = false;
private _has?: boolean = false;
constructor(producer: InternalProducer<T>) {
super(producer);
}
Expand Down
9 changes: 6 additions & 3 deletions tests/operator/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,11 @@ describe('Stream.prototype.flatten', () => {
});
});

it.skip('should allow to flatten with inner being a MemoryStream', function() {
// @TODO: actually make this next line compile
// xs.of(xs.of(1).remember()).flatten();
it('should allow to flatten while playing around with MemoryStream(s) and correct type signature', function() {
xs.of(xs.of(1)).flatten() // Stream<Stream<number>> => Stream<number>
xs.of(xs.of(1).remember()).flatten() // Stream<MemoryStream<number>> => Stream<number>
xs.of(xs.of(1)).remember().flatten() // MemoryStream<Stream<number>> => Stream<number>
// xs.of(xs.of(1).remember()).remember().flatten() // Doesn't compile
xs.of(xs.of(1).remember()).remember().flatten<number>() // MemoryStream<MemoryStream<number>> => Stream<number>
})
});

0 comments on commit c2e8655

Please sign in to comment.