Skip to content

Commit

Permalink
feat(do): do will now handle an observer as an argument
Browse files Browse the repository at this point in the history
- also shored up tests around do a little more

closes #476
  • Loading branch information
benlesh committed Oct 7, 2015
1 parent e55d822 commit c1a4994
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 8 deletions.
97 changes: 90 additions & 7 deletions spec/operators/do-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,97 @@ var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.prototype.do()', function () {
it('should do one value', function (done) {
var act = false;
it('should next with a callback', function () {
var value = null;
Observable.of(42).do(function (x) {
act = true;
value = x;
})
.subscribe(function (x) {
expect(x).toBe(42);
expect(act).toBe(true);
}, null, done);
.subscribe();

expect(value).toBe(42);
});

it('should complete with a callback', function () {
var err = null;
Observable.throw('bad').do(null, function (x) {
err = x;
})
.subscribe(null, function (ex) {
expect(ex).toBe('bad');
});

expect(err).toBe('bad');
});

it('should handle everything with an observer', function () {
var expected = [1,2,3];
var results = [];
var completeCalled = false;
Observable.of(1,2,3)
.do({
next: function (x) {
results.push(x);
},
error: function (err) {
throw 'should not be called';
},
complete: function () {
completeCalled = true;
}
})
.subscribe();

expect(completeCalled).toBe(true);
expect(results).toEqual(expected);
});

it('should handle an error with a callback', function () {
var errored = false;
Observable.throw('bad').do(null, function (err) {
expect(err).toBe('bad');
})
.subscribe(null, function (err) {
errored = true;
expect(err).toBe('bad');
});

expect(errored).toBe(true);
});

it('should handle an error with observer', function () {
var errored = false;
Observable.throw('bad').do({ error: function (err) {
expect(err).toBe('bad');
} })
.subscribe(null, function (err) {
errored = true;
expect(err).toBe('bad');
});

expect(errored).toBe(true);
});

it('should handle complete with observer', function () {
var completed = false;

Observable.empty().do({
complete: function () {
completed = true;
}
}).subscribe();

expect(completed).toBe(true);
});

it('should handle next with observer', function () {
var value = null;

Observable.of('hi').do({
next: function (x) {
value = x;
}
}).subscribe();

expect(value).toBe('hi');
});
});
10 changes: 9 additions & 1 deletion src/operators/do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import tryCatch from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import bindCallback from '../util/bindCallback';

export default function _do<T>(next?: (x: T) => void, error?: (e: any) => void, complete?: () => void) {
export default function _do<T>(nextOrObserver?: Observer<T>|((x: T) => void), error?: (e: any) => void, complete?: () => void) {
let next;
if (nextOrObserver && typeof nextOrObserver === 'object') {
next = (<Observer<T>>nextOrObserver).next;
error = (<Observer<T>>nextOrObserver).error;
complete = (<Observer<T>>nextOrObserver).complete;
} else {
next = <(x: T) => void>nextOrObserver;
}
return this.lift(new DoOperator(next || noop, error || noop, complete || noop));
}

Expand Down

0 comments on commit c1a4994

Please sign in to comment.