From adbe65e659bbf17f6ab20a9b30fcca0e4d76af9a Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Thu, 15 Oct 2020 12:51:57 -0700 Subject: [PATCH] fix(withlatestfrom): allow synchronous source (#5828) --- spec/operators/withLatestFrom-spec.ts | 12 ++++++++++++ src/internal/operators/withLatestFrom.ts | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/spec/operators/withLatestFrom-spec.ts b/spec/operators/withLatestFrom-spec.ts index 208e40e768..f7f4f913ee 100644 --- a/spec/operators/withLatestFrom-spec.ts +++ b/spec/operators/withLatestFrom-spec.ts @@ -260,4 +260,16 @@ describe('withLatestFrom operator', () => { expect(x).to.deep.equal([1, 4, 6]); }); }); + + it('should work with synchronous observables', () => { + const result: Array> = []; + of(1, 2, 3).pipe(withLatestFrom(of(4, 5))).subscribe((x) => { + result.push(x); + }); + + expect(result.length).to.equal(3); + expect(result[0]).to.deep.equal([1,5]); + expect(result[1]).to.deep.equal([2,5]); + expect(result[2]).to.deep.equal([3,5]); + }) }); diff --git a/src/internal/operators/withLatestFrom.ts b/src/internal/operators/withLatestFrom.ts index 871d12cb58..24bb735eb3 100644 --- a/src/internal/operators/withLatestFrom.ts +++ b/src/internal/operators/withLatestFrom.ts @@ -159,17 +159,6 @@ export function withLatestFrom(...inputs: any[]): OperatorFunction { - if (ready) { - // We have at least one value from the other sources. Go ahead and emit. - const values = [value, ...otherValues]; - subscriber.next(project ? project(...values) : values); - } - }) - ); - // Other sources. Note that here we are not checking `subscriber.closed`, // this causes all inputs to be subscribed to, even if nothing can be emitted // from them. This is an important distinction because subscription constitutes @@ -197,5 +186,16 @@ export function withLatestFrom(...inputs: any[]): OperatorFunction { + if (ready) { + // We have at least one value from the other sources. Go ahead and emit. + const values = [value, ...otherValues]; + subscriber.next(project ? project(...values) : values); + } + }) + ); }); }