Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(elementAt): force unsubscribe when it completes or errors #2501

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions spec/helpers/doNotUnsubscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
///<reference path='../../typings/index.d.ts'/>
import * as Rx from '../../dist/cjs/Rx';

export function doNotUnsubscribe<T>(ob: Rx.Observable<T>): Rx.Observable<T> {
return ob.lift(new DoNotUnsubscribeOperator());
}

class DoNotUnsubscribeOperator<T, R> implements Rx.Operator<T, R> {
call(subscriber: Rx.Subscriber<R>, source: any): any {
return source.subscribe(new DoNotUnsubscribeSubscriber(subscriber));
}
}

class DoNotUnsubscribeSubscriber<T> extends Rx.Subscriber<T> {
unsubscribe() {} // tslint:disable-line no-empty
}
30 changes: 30 additions & 0 deletions spec/operators/elementAt-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { doNotUnsubscribe } from '../helpers/doNotUnsubscribe';

declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
Expand Down Expand Up @@ -126,4 +127,33 @@ describe('Observable.prototype.elementAt', () => {
expectObservable((<any>source).elementAt(3, defaultValue)).toBe(expected, { x: defaultValue });
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should unsubscribe from source Observable, even if destination does not unsubscribe', () => {
const source = hot('--a--b--c-d---|');
const subs = '^ ! ';
const expected = '--------(c|) ';

expectObservable((<any>source).elementAt(2).let(doNotUnsubscribe)).toBe(expected);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should unsubscribe from source if index is out of range, even if destination does not unsubscribe', () => {
const source = hot('--a--|');
const subs = '^ !';
const expected = '-----#';

expectObservable((<any>source).elementAt(3).let(doNotUnsubscribe))
.toBe(expected, null, new Rx.ArgumentOutOfRangeError());
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should unsubscribe when returning default value, even if destination does not unsubscribe', () => {
const source = hot('--a--|');
const subs = '^ !';
const expected = '-----(x|)';
const defaultValue = '42';

expectObservable((<any>source).elementAt(3, defaultValue).let(doNotUnsubscribe)).toBe(expected, { x: defaultValue });
expectSubscriptions(source.subscriptions).toBe(subs);
});
});
4 changes: 4 additions & 0 deletions src/operator/elementAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ElementAtSubscriber<T> extends Subscriber<T> {
if (this.index-- === 0) {
this.destination.next(x);
this.destination.complete();
this.unsubscribe();
}
}

Expand All @@ -88,8 +89,11 @@ class ElementAtSubscriber<T> extends Subscriber<T> {
destination.next(this.defaultValue);
} else {
destination.error(new ArgumentOutOfRangeError);
this.unsubscribe();
return;
}
}
destination.complete();
this.unsubscribe();
}
}