Skip to content

Commit

Permalink
fix(subscriber): makes sure default callbacks are provided
Browse files Browse the repository at this point in the history
default callbacks should be provided both - when given a subscriber object and also when given
separate callback functions. Default error callback should re-throw the error

BREAKING CHANGE: default callbacks are now provided both - when given a subscriber object and also
when given separate callback functions. Also default error callback now re-throws the error instead
of th rowing a new Error()
  • Loading branch information
pshev committed Mar 7, 2018
1 parent 004f2a1 commit c8de4b2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
61 changes: 61 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,39 @@ describe('subscribe', () => {

it("should work as a standalone function", (done) =>
of(1) |> subscribe({next: () => done()}))

it("should throw if next callback is not defined", (done) => {
try {
of(1).subscribe({})
} catch (ex) {
done()
}
})

it("should provide a default complete callback if it is not provided", (done) => {
const error = chai.spy()
try {
of(1).subscribe({
next: nx,
error: err
})
} catch (ex) {
error(ex)
}
expect(error).to.not.have.been.called()
done()
})

it("should provide a default error callback that re-throws an error if error callback is not provided", (done) => {
try {
error('bad').subscribe({next: nx})
} catch (ex) {
expect(ex).to.equal('bad')
done()
}
})
})

describe('called with callback functions', () => {
it("should call next", (done) =>
of(1).subscribe(() => done()))
Expand All @@ -64,7 +96,36 @@ describe('subscribe', () => {

it("should call complete", (done) =>
of(1).subscribe(nx, err, _ => done()))

it("should throw if next callback is not defined", (done) => {
try {
of(1).subscribe()
} catch (ex) {
done()
}
})

it("should provide a default complete callback if it is not provided", (done) => {
const error = chai.spy()
try {
of(1).subscribe(nx, err)
} catch (ex) {
error(ex)
}
expect(error).to.not.have.been.called()
done()
})

it("should provide a default error callback that re-throws an error if error callback is not provided", (done) => {
try {
error('bad').subscribe(nx)
} catch (ex) {
expect(ex).to.equal('bad')
done()
}
})
})

describe('unsubscribe', () => {
it("should return a subscription object", (done) => {
const subscription = of(1).subscribe(() => done())
Expand Down
21 changes: 13 additions & 8 deletions src/internal/subscriber.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
export const Subscriber = (nextOrSubscriber, error, complete) =>
typeof nextOrSubscriber === 'object'
? nextOrSubscriber
: {
next: nextOrSubscriber,
error: error || (() => {throw new Error()}),
complete: complete || (() => {})
}
export const Subscriber = (nextOrSubscriber, error, complete) => {
if (typeof nextOrSubscriber === 'object') {
nextOrSubscriber.error = nextOrSubscriber.error || (err => {throw err})
nextOrSubscriber.complete = nextOrSubscriber.complete || (() => {})
return nextOrSubscriber
}

return {
next: nextOrSubscriber,
error: error || (err => {throw err}),
complete: complete || (() => {})
}
}

0 comments on commit c8de4b2

Please sign in to comment.