Skip to content

Commit

Permalink
fix(Observable.toArray): Fix toArray with multiple subscriptions. (#3134
Browse files Browse the repository at this point in the history
)

Do not reuse the same array instance for Observable.toArray with
multiple subscriptions.
  • Loading branch information
josketres authored and benlesh committed Nov 28, 2017
1 parent 515a753 commit 3390926
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions spec/operators/toArray-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ describe('Observable.prototype.toArray', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should allow multiple subscriptions', () => {
const e1 = hot('-x-^--y--|');
const e1subs = '^ !';
const expected = '------(w|)';

const result = e1.toArray();
expectObservable(result).toBe(expected, { w: ['y'] });
expectObservable(result).toBe(expected, { w: ['y'] });
expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]);
});

it('should allow unsubscribing explicitly and early', () => {
const e1 = hot('--a--b----c-----d----e---|');
const unsub = ' ! ';
Expand Down
3 changes: 3 additions & 0 deletions src/operators/toArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { reduce } from './reduce';
import { OperatorFunction } from '../interfaces';

function toArrayReducer<T>(arr: T[], item: T, index: number) {
if (index === 0) {
return [item];
}
arr.push(item);
return arr;
}
Expand Down

0 comments on commit 3390926

Please sign in to comment.