Skip to content

Commit 7d9b52b

Browse files
committed
feat(operator): Add do operator.
1 parent 3f743ab commit 7d9b52b

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

spec/operators/do-spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* globals describe, it, expect */
2+
var Rx = require('../../dist/cjs/Rx');
3+
var Observable = Rx.Observable;
4+
5+
describe('Observable.prototype.do()', function () {
6+
it('should do one value', function (done) {
7+
var act = false;
8+
Observable.value(42).do(function (x) {
9+
act = true;
10+
})
11+
.subscribe(function (x) {
12+
expect(x).toBe(42);
13+
expect(act).toBe(true);
14+
}, null, done);
15+
});
16+
});

src/Observable.ts

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export default class Observable<T> {
123123
zip: <R>(...observables: (Observable<any> | ((...values: Array<any>) => R)) []) => Observable<R>;
124124
zipAll: <R>(project?: (...values: Array<any>) => R) => Observable<R>;
125125

126+
do: <T>(next?: (x: T) => void, error?: (e: any) => void, complete?: () => void) => Observable<T>;
126127
map: <T, R>(project: (x: T, ix?: number) => R, thisArg?: any) => Observable<R>;
127128
mapTo: <R>(value: R) => Observable<R>;
128129
toArray: () => Observable<T[]>;

src/Rx.ts

+2
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ observableProto.switchLatest = switchLatest;
7575
observableProto.switchLatestTo = switchLatestTo;
7676
observableProto.expand = expand;
7777

78+
import _do from './operators/do';
7879
import map from './operators/map';
7980
import mapTo from './operators/mapTo';
8081
import toArray from './operators/toArray';
8182
import scan from './operators/scan';
8283
import reduce from './operators/reduce';
8384
import startWith from './operators/startWith';
8485

86+
observableProto.do = _do;
8587
observableProto.map = map;
8688
observableProto.mapTo = mapTo;
8789
observableProto.toArray = toArray;

src/operators/do.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import Operator from '../Operator';
2+
import Observer from '../Observer';
3+
import Subscriber from '../Subscriber';
4+
5+
import noop from '../util/noop';
6+
import tryCatch from '../util/tryCatch';
7+
import {errorObject} from '../util/errorObject';
8+
import bindCallback from '../util/bindCallback';
9+
10+
export default function _do<T>(next?: (x: T) => void, error?: (e: any) => void, complete?: () => void) {
11+
return this.lift(new DoOperator(next || noop, error || noop, complete || noop));
12+
}
13+
14+
export class DoOperator<T, R> extends Operator<T, R> {
15+
16+
next: (x: T) => void;
17+
error: (e: any) => void;
18+
complete: () => void;
19+
20+
constructor(next: (x: T) => void, error: (e: any) => void, complete: () => void) {
21+
super();
22+
this.next = next;
23+
this.error = error;
24+
this.complete = complete;
25+
}
26+
27+
call(observer: Observer<T>): Observer<T> {
28+
return new DoSubscriber(observer, this.next, this.error, this.complete);
29+
}
30+
}
31+
32+
export class DoSubscriber<T> extends Subscriber<T> {
33+
34+
__next: (x: T) => void;
35+
__error: (e: any) => void;
36+
__complete: () => void;
37+
38+
39+
constructor(destination: Observer<T>, next: (x: T) => void, error: (e: any) => void, complete: () => void) {
40+
super(destination);
41+
this.__next = next;
42+
this.__error = error;
43+
this.__complete = complete;
44+
}
45+
46+
_next(x) {
47+
const result = tryCatch(this.__next)(x);
48+
if (result === errorObject) {
49+
this.destination.error(errorObject.e);
50+
} else {
51+
this.destination.next(x);
52+
}
53+
}
54+
55+
_error(e) {
56+
const result = tryCatch(this.__error)(e);
57+
if (result === errorObject) {
58+
this.destination.error(errorObject.e);
59+
} else {
60+
this.destination.error(e);
61+
}
62+
}
63+
64+
_complete() {
65+
const result = tryCatch(this.__complete)();
66+
if (result === errorObject) {
67+
this.destination.error(errorObject.e);
68+
} else {
69+
this.destination.complete();
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)