Skip to content

Commit

Permalink
feat(addListener): throw an error if next, error or complete function…
Browse files Browse the repository at this point in the history
…s are missing

This class of error is a compiler error in TypeScript, but is a runtime
error in JS. Currently we get an error like "this._n is not a function"
which is quite cryptic to an end user.
  • Loading branch information
Widdershin authored and staltz committed Apr 27, 2016
1 parent dc47f7b commit b6e9df3
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,18 @@ export class Stream<T> implements InternalListener<T> {
* @param {Listener<T>} listener
*/
addListener(listener: Listener<T>): void {
if (typeof listener.next !== 'function') {
throw new Error('addListener requires a next function.');
}

if (typeof listener.error !== 'function') {
throw new Error('addListener requires an error function.');
}

if (typeof listener.complete !== 'function') {
throw new Error('addListener requires a complete function.');
}

(<InternalListener<T>> (<any> listener))._n = listener.next;
(<InternalListener<T>> (<any> listener))._e = listener.error;
(<InternalListener<T>> (<any> listener))._c = listener.complete;
Expand Down

0 comments on commit b6e9df3

Please sign in to comment.