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(find): force unsubscribe when it completes or errors #2550

Merged
merged 1 commit into from
May 16, 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
}
28 changes: 28 additions & 0 deletions spec/operators/find-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 @@ -162,6 +163,33 @@ describe('Observable.prototype.find', () => {
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should unsubscribe from source when complete, even if following operator does not unsubscribe', () => {
const values = {a: 3, b: 9, c: 15, d: 20};
const source = hot('---a--b--c--d---|', values);
const subs = '^ ! ';
const expected = '---------(c|) ';

const predicate = function (x) { return x % 5 === 0; };

expectObservable((<any>source).find(predicate).let(doNotUnsubscribe)).toBe(expected, values);
expectSubscriptions(source.subscriptions).toBe(subs);
});

it('should unsubscribe from source when predicate function errors,' +
' even if followring operator does not unsubscribe', () => {

const source = hot('--a--b--c--|');
const subs = '^ !';
const expected = '--#';

const predicate = function (value) {
throw 'error';
};

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

it('should support type guards without breaking previous behavior', () => {
// tslint:disable no-unused-variable

Expand Down
2 changes: 2 additions & 0 deletions src/operator/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export class FindValueSubscriber<T> extends Subscriber<T> {

destination.next(value);
destination.complete();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd... calling complete on the destination, should end up calling unsubscribe on the destination, and subsequently up the chain to here. Although maybe I've got my head in a knot thinking about it. cc @trxcllnt

this.unsubscribe();
}

protected _next(value: T): void {
Expand All @@ -97,6 +98,7 @@ export class FindValueSubscriber<T> extends Subscriber<T> {
}
} catch (err) {
this.destination.error(err);
this.unsubscribe();
}
}

Expand Down