Skip to content

Commit c52ff2e

Browse files
authoredAug 3, 2021
fix(forkJoin): now finalizes sources before emitting (#6546)
fixes #4914
1 parent 6f0e853 commit c52ff2e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
 

‎spec/observables/forkJoin-spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** @prettier */
22
import { expect } from 'chai';
3-
import { forkJoin, of } from 'rxjs';
3+
import { finalize, forkJoin, map, of, timer } from 'rxjs';
44
import { lowerCaseO } from '../helpers/test-helper';
55
import { TestScheduler } from 'rxjs/testing';
66
import { observableMatcher } from '../helpers/observableMatcher';
@@ -303,6 +303,22 @@ describe('forkJoin', () => {
303303
});
304304
});
305305

306+
it('should finalize in the proper order', () => {
307+
const results: any[] = [];
308+
const source = forkJoin(
309+
[1, 2, 3, 4].map((n) =>
310+
timer(100, rxTestScheduler).pipe(
311+
map(() => n),
312+
finalize(() => results.push(`finalized ${n}`))
313+
)
314+
)
315+
);
316+
317+
source.subscribe((value) => results.push(value));
318+
rxTestScheduler.flush();
319+
expect(results).to.deep.equal(['finalized 1', 'finalized 2', 'finalized 3', 'finalized 4', [1, 2, 3, 4]]);
320+
});
321+
306322
describe('forkJoin({ foo, bar, baz })', () => {
307323
it('should join the last values of the provided observables into an array', () => {
308324
rxTestScheduler.run(({ hot, expectObservable }) => {

‎src/internal/observable/forkJoin.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ export function forkJoin(...args: any[]): Observable<any> {
166166
}
167167
values[sourceIndex] = value;
168168
},
169+
() => remainingCompletions--,
170+
undefined,
169171
() => {
170-
if (!--remainingCompletions || !hasValue) {
172+
if (!remainingCompletions || !hasValue) {
171173
if (!remainingEmissions) {
172174
subscriber.next(keys ? createObject(keys, values) : values);
173175
}

0 commit comments

Comments
 (0)
Please sign in to comment.