From 76879a5f8f3f5b1d2af8697ce8f5bbfe3df06d02 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Wed, 6 Apr 2016 14:04:24 +0300 Subject: [PATCH] feat(throw): implement new static factory throw() --- src/Stream.ts | 7 +++++++ tests/factory/throw.ts | 18 ++++++++++++++++++ tests/stream.ts | 1 + 3 files changed, 26 insertions(+) create mode 100644 tests/factory/throw.ts diff --git a/src/Stream.ts b/src/Stream.ts index 2f10811..febf969 100644 --- a/src/Stream.ts +++ b/src/Stream.ts @@ -141,6 +141,13 @@ export class Stream implements InternalListener { }); } + static ['throw'](err: any): Stream { + return new Stream({ + _start(il: InternalListener) { il._e(err); }, + _stop: noop, + }); + } + static from(array: Array): Stream { return new Stream(new FromProducer(array)); } diff --git a/tests/factory/throw.ts b/tests/factory/throw.ts new file mode 100644 index 0000000..0733104 --- /dev/null +++ b/tests/factory/throw.ts @@ -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, + }); + }); +}); diff --git a/tests/stream.ts b/tests/stream.ts index d69a11e..de1d1b4 100644 --- a/tests/stream.ts +++ b/tests/stream.ts @@ -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');