Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(addListener): throw an error if next, error or complete functions are missing #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions tests/extra/fromEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('fromEvent (extra)', () => {
next(x: any) {
assert.strictEqual(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.strictEqual(expected.length, 0);
done();
Expand All @@ -89,7 +89,7 @@ describe('fromEvent (extra)', () => {

stream.take(1).addListener({
next(x) {},
error: done.fail,
error: done,
complete() {
setTimeout(() => {
assert.strictEqual('test', target.removedEvent);
Expand Down
4 changes: 2 additions & 2 deletions tests/factory/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('xs.combine', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand All @@ -28,7 +28,7 @@ describe('xs.combine', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand Down
4 changes: 2 additions & 2 deletions tests/factory/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('xs.merge', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand All @@ -29,7 +29,7 @@ describe('xs.merge', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/factory/of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('xs.of', () => {
next: (x: string) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand Down
4 changes: 2 additions & 2 deletions tests/operator/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Stream.prototype.combine', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand All @@ -28,7 +28,7 @@ describe('Stream.prototype.combine', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand Down
4 changes: 2 additions & 2 deletions tests/operator/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ describe('Stream.prototype.debug', () => {
done();
}
},
error: done.fail,
complete: done.fail,
error: done,
complete: () => done('complete should not be called'),
};
stream.addListener(listener);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/operator/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('Stream.prototype.drop', () => {
done();
}
},
error: done.fail,
complete: done.fail,
error: done,
complete: () => done('complete should not be called'),
};
stream.addListener(listener);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/operator/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('Stream.prototype.filter', () => {
done();
}
},
error: done.fail,
complete: done.fail,
error: done,
complete: () => done('complete should not be called'),
};
stream.addListener(listener);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/operator/fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Stream.prototype.fold', () => {
next: (x: number) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
stream.removeListener(listener);
Expand Down
2 changes: 1 addition & 1 deletion tests/operator/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Stream.prototype.last', () => {
next: (x: number) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
stream.removeListener(listener);
Expand Down
4 changes: 2 additions & 2 deletions tests/operator/mapTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('Stream.prototype.mapTo', () => {
done();
}
},
error: done.fail,
complete: done.fail,
error: done,
complete: () => done('complete should not be called'),
};
stream.addListener(listener);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/operator/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Stream.prototype.merge', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand All @@ -27,7 +27,7 @@ describe('Stream.prototype.merge', () => {
next: (x) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/operator/remember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Stream.prototype.remember', () => {
next(x) {
assert.strictEqual(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.strictEqual(expected.length, 0);
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/operator/startWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Stream.prototype.startWith', () => {
next(x: any) {
assert.strictEqual(x, 1);
},
error: done.fail,
error: done,
complete: done
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/operator/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Stream.prototype.take', () => {
next: (x: number) => {
assert.equal(x, expected.shift());
},
error: done.fail,
error: done,
complete: () => {
assert.equal(expected.length, 0);
stream.removeListener(listener);
Expand Down
57 changes: 57 additions & 0 deletions tests/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,61 @@ describe('Stream', () => {
assert.equal(expected2.length, 0);
done();
});

describe('addListener', () => {
it('throws a helpful error if you forget the next function', (done) => {
const stream = xs.empty();
const listener = <Listener<Number>> <any> {};

try {
stream.addListener(listener);
} catch (e) {
assert.equal(e.message, 'addListener requires a next function.')
done();
}
});

it('throws a helpful error if you forget the error function', (done) => {
const stream = xs.empty();
const listener = <Listener<Number>> <any> {
next: (x: any) => {}
};

try {
stream.addListener(listener);
} catch (e) {
assert.equal(e.message, 'addListener requires an error function.')
done();
}
});

it('throws a helpful error if you forget the complete function', (done) => {
const stream = xs.empty();
const listener = <Listener<Number>> <any> {
next: (x: any) => {},
error: (err: any) => {}
};

try {
stream.addListener(listener);
} catch (e) {
assert.equal(e.message, 'addListener requires a complete function.')
done();
}
});

it('throws a helpful error if you pass a non function value as the next function', (done) => {
const stream = xs.empty();
const listener = <Listener<Number>> <any> {
next: undefined
};

try {
stream.addListener(listener);
} catch (e) {
assert.equal(e.message, 'addListener requires a next function.')
done();
}
});
});
});