Skip to content

Commit

Permalink
feat(throw): implement new static factory throw()
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Medeiros committed Apr 6, 2016
1 parent 23099ef commit 76879a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export class Stream<T> implements InternalListener<T> {
});
}

static ['throw'](err: any): Stream<void> {
return new Stream<void>({
_start(il: InternalListener<void>) { il._e(err); },
_stop: noop,
});
}

static from<T>(array: Array<T>): Stream<T> {
return new Stream<T>(new FromProducer(array));
}
Expand Down
18 changes: 18 additions & 0 deletions tests/factory/throw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import xs from '../../src/index';
import * as assert from 'assert';

describe('xs.throw()', function() {
it('should create a stream with just one error emission', (done) => {
const stream = xs.throw(new Error('not nice'));

stream.addListener({
next: () => done(new Error('This should not be called')),
error: (err: any) => {
assert.strictEqual(err instanceof Error, true);
assert.strictEqual(err.message, 'not nice');
done();
},
complete: done,
});
});
});
1 change: 1 addition & 0 deletions tests/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Stream', () => {
assert.equal(typeof xs.createWithMemory, 'function');
assert.equal(typeof xs.never, 'function');
assert.equal(typeof xs.empty, 'function');
assert.equal(typeof xs.throw, 'function');
assert.equal(typeof xs.from, 'function');
assert.equal(typeof xs.of, 'function');
assert.equal(typeof xs.interval, 'function');
Expand Down

0 comments on commit 76879a5

Please sign in to comment.