Skip to content

Commit

Permalink
feat(operator): add finally
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed Aug 14, 2015
1 parent f9ba4da commit 526e4c9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
47 changes: 47 additions & 0 deletions spec/operators/finally-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* globals describe, it, expect */
var Rx = require('../../dist/cjs/Rx');
var Observable = Rx.Observable;

describe('Observable.prototype.finally()', function () {
it('should call finally after complete', function (done) {
var completed = false;
Observable.of(1, 2, 3)
.finally(function(x) {
expect(completed).toBe(true);
done();
})
.subscribe(null, null, function() {
completed = true;
});
});

it('should call finally after error', function (done) {
var thrown = false;
Observable.of(1, 2, 3)
.map(function(x) {
if(x === 3) {
throw x;
}
return x;
})
.finally(function(x) {
expect(thrown).toBe(true);
done();
})
.subscribe(null, function() {
thrown = true;
});
});

it('should call finally upon disposal', function (done) {
var disposed = false;
var subscription = Observable
.timer(100)
.finally(function(x) {
expect(disposed).toBe(true);
done();
}).subscribe();
disposed = true;
subscription.unsubscribe();
});
});
3 changes: 3 additions & 0 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,7 @@ export default class Observable<T> {

catch: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
retryWhen: (notifier: (errors: Observable<any>) => Observable<any>) => Observable<T>;

ensure: (ensure: () => void, thisArg?: any) => Observable<T>;
finally: (ensure: () => void, thisArg?: any) => Observable<T>;
}
5 changes: 5 additions & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ import retryWhen from './operators/retryWhen';
observableProto.catch = _catch;
observableProto.retryWhen = retryWhen;

import _finally from './operators/finally';

observableProto.ensure = _finally;
observableProto.finally = _finally;

export default {
Subject,
Scheduler,
Expand Down
35 changes: 35 additions & 0 deletions src/operators/finally.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Operator from '../Operator';
import Observer from '../Observer';
import Subscriber from '../Subscriber';
import Subscription from '../Subscription';

import tryCatch from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import bindCallback from '../util/bindCallback';

export default function _finally<T>(finallySelector: () => void, thisArg?: any) {
return this.lift(new FinallyOperator(thisArg ?
<() => void> bindCallback(finallySelector, thisArg, 2) :
finallySelector));
}

export class FinallyOperator<T, R> extends Operator<T, R> {

finallySelector: () => void;

constructor(finallySelector: () => void) {
super();
this.finallySelector = finallySelector;
}

call(observer: Observer<T>): Observer<T> {
return new FinallySubscriber(observer, this.finallySelector);
}
}

export class FinallySubscriber<T> extends Subscriber<T> {
constructor(destination: Observer<T>, finallySelector: () => void) {
super(destination);
this.add(new Subscription(finallySelector));
}
}

0 comments on commit 526e4c9

Please sign in to comment.