Skip to content

Commit

Permalink
fix(subscribe): allow partial listeners (in TypeScript)
Browse files Browse the repository at this point in the history
we should allow subscribe to accept listener objects that are missing some fields, just like
addListener accepts
  • Loading branch information
staltz committed Jun 23, 2018
1 parent 7d10c70 commit 8bfaad7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ function internalizeProducer<T>(producer: Producer<T> & Partial<InternalProducer
}

class StreamSub<T> implements Subscription {
constructor(private _stream: Stream<T>, private _listener: Listener<T>) {}
constructor(private _stream: Stream<T>, private _listener: InternalListener<T>) {}

unsubscribe(): void {
this._stream.removeListener(this._listener);
this._stream._remove(this._listener);
}
}

Expand Down Expand Up @@ -1225,9 +1225,9 @@ export class Stream<T> implements InternalListener<T> {
* @param {Listener} listener
* @returns {Subscription}
*/
subscribe(listener: Listener<T>): Subscription {
subscribe(listener: Partial<Listener<T>>): Subscription {
this.addListener(listener);
return new StreamSub<T>(this, listener);
return new StreamSub<T>(this, listener as InternalListener<T>);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,5 +413,20 @@ describe('Stream', () => {

done();
});

it('should accept a partial listener', (done: any) => {
const stream = xs.empty();
const noop = (): void => void 0;
const listener = {
next: noop,
};

const subscription = stream.subscribe(listener);

assert.equal(typeof subscription, 'object');
assert.equal(typeof subscription.unsubscribe, 'function');

done();
});
});
});

0 comments on commit 8bfaad7

Please sign in to comment.