Skip to content

Commit

Permalink
fix(elementAt): will now unsubscribe from source when resulting obser…
Browse files Browse the repository at this point in the history
…vable completes or errors (#2672)

Force unsubscription logic in elementAt, when resulting Observable
completes or errors, so that all resources are disposed as fast
as possible, even if next operator in chain does not unsubscribe
reliably when given complete or error notification.

BREAKING CHANGE: unsubscription cadence has changed for `elementAt`,
this means side-effects related to unsubscription may occur at a
different time than in previous versions.
  • Loading branch information
mpodlasin authored and benlesh committed Jun 19, 2017
1 parent a75e04b commit 12bc7fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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();
}
}

0 comments on commit 12bc7fe

Please sign in to comment.