Skip to content

Commit 526e4c9

Browse files
committed
feat(operator): add finally
1 parent f9ba4da commit 526e4c9

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

Diff for: spec/operators/finally-spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* globals describe, it, expect */
2+
var Rx = require('../../dist/cjs/Rx');
3+
var Observable = Rx.Observable;
4+
5+
describe('Observable.prototype.finally()', function () {
6+
it('should call finally after complete', function (done) {
7+
var completed = false;
8+
Observable.of(1, 2, 3)
9+
.finally(function(x) {
10+
expect(completed).toBe(true);
11+
done();
12+
})
13+
.subscribe(null, null, function() {
14+
completed = true;
15+
});
16+
});
17+
18+
it('should call finally after error', function (done) {
19+
var thrown = false;
20+
Observable.of(1, 2, 3)
21+
.map(function(x) {
22+
if(x === 3) {
23+
throw x;
24+
}
25+
return x;
26+
})
27+
.finally(function(x) {
28+
expect(thrown).toBe(true);
29+
done();
30+
})
31+
.subscribe(null, function() {
32+
thrown = true;
33+
});
34+
});
35+
36+
it('should call finally upon disposal', function (done) {
37+
var disposed = false;
38+
var subscription = Observable
39+
.timer(100)
40+
.finally(function(x) {
41+
expect(disposed).toBe(true);
42+
done();
43+
}).subscribe();
44+
disposed = true;
45+
subscription.unsubscribe();
46+
});
47+
});

Diff for: src/Observable.ts

+3
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,7 @@ export default class Observable<T> {
149149

150150
catch: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
151151
retryWhen: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;
152+
153+
ensure: (ensure: () => void, thisArg?: any) => Observable<T>;
154+
finally: (ensure: () => void, thisArg?: any) => Observable<T>;
152155
}

Diff for: src/Rx.ts

+5
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ import retryWhen from './operators/retryWhen';
143143
observableProto.catch = _catch;
144144
observableProto.retryWhen = retryWhen;
145145

146+
import _finally from './operators/finally';
147+
148+
observableProto.ensure = _finally;
149+
observableProto.finally = _finally;
150+
146151
export default {
147152
Subject,
148153
Scheduler,

Diff for: src/operators/finally.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Operator from '../Operator';
2+
import Observer from '../Observer';
3+
import Subscriber from '../Subscriber';
4+
import Subscription from '../Subscription';
5+
6+
import tryCatch from '../util/tryCatch';
7+
import {errorObject} from '../util/errorObject';
8+
import bindCallback from '../util/bindCallback';
9+
10+
export default function _finally<T>(finallySelector: () => void, thisArg?: any) {
11+
return this.lift(new FinallyOperator(thisArg ?
12+
<() => void> bindCallback(finallySelector, thisArg, 2) :
13+
finallySelector));
14+
}
15+
16+
export class FinallyOperator<T, R> extends Operator<T, R> {
17+
18+
finallySelector: () => void;
19+
20+
constructor(finallySelector: () => void) {
21+
super();
22+
this.finallySelector = finallySelector;
23+
}
24+
25+
call(observer: Observer<T>): Observer<T> {
26+
return new FinallySubscriber(observer, this.finallySelector);
27+
}
28+
}
29+
30+
export class FinallySubscriber<T> extends Subscriber<T> {
31+
constructor(destination: Observer<T>, finallySelector: () => void) {
32+
super(destination);
33+
this.add(new Subscription(finallySelector));
34+
}
35+
}

0 commit comments

Comments
 (0)