From b5ce3fff4f37eda9ef300a35ee802a513a3b0417 Mon Sep 17 00:00:00 2001 From: Josue Zarzosa Date: Tue, 28 Nov 2017 12:54:58 +0000 Subject: [PATCH] fix(Observable.toArray): Fix toArray with multiple subscriptions. Do not reuse the same array instance for Observable.toArray with multiple subscriptions. --- spec/operators/toArray-spec.ts | 11 +++++++++++ src/operators/toArray.ts | 3 +++ 2 files changed, 14 insertions(+) diff --git a/spec/operators/toArray-spec.ts b/spec/operators/toArray-spec.ts index 2588282653..0b91e79496 100644 --- a/spec/operators/toArray-spec.ts +++ b/spec/operators/toArray-spec.ts @@ -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 = ' ! '; diff --git a/src/operators/toArray.ts b/src/operators/toArray.ts index 3b11ffe27d..2148ad3e80 100644 --- a/src/operators/toArray.ts +++ b/src/operators/toArray.ts @@ -2,6 +2,9 @@ import { reduce } from './reduce'; import { OperatorFunction } from '../interfaces'; function toArrayReducer(arr: T[], item: T, index: number) { + if (index === 0) { + return [item]; + } arr.push(item); return arr; }