Skip to content

Commit

Permalink
fix(forkJoin): now finalizes sources before emitting
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Aug 2, 2021
1 parent 6f0e853 commit 6454fcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 17 additions & 1 deletion spec/observables/forkJoin-spec.ts
@@ -1,6 +1,6 @@
/** @prettier */
import { expect } from 'chai';
import { forkJoin, of } from 'rxjs';
import { finalize, forkJoin, map, of, timer } from 'rxjs';
import { lowerCaseO } from '../helpers/test-helper';
import { TestScheduler } from 'rxjs/testing';
import { observableMatcher } from '../helpers/observableMatcher';
Expand Down Expand Up @@ -303,6 +303,22 @@ describe('forkJoin', () => {
});
});

it('should finalize in the proper order', () => {
const results: any[] = [];
const source = forkJoin(
[1, 2, 3, 4].map((n) =>
timer(100, rxTestScheduler).pipe(
map(() => n),
finalize(() => results.push(`finalized ${n}`))
)
)
);

source.subscribe((value) => results.push(value));
rxTestScheduler.flush();
expect(results).to.deep.equal(['finalized 1', 'finalized 2', 'finalized 3', 'finalized 4', [1, 2, 3, 4]]);
});

describe('forkJoin({ foo, bar, baz })', () => {
it('should join the last values of the provided observables into an array', () => {
rxTestScheduler.run(({ hot, expectObservable }) => {
Expand Down
4 changes: 3 additions & 1 deletion src/internal/observable/forkJoin.ts
Expand Up @@ -166,8 +166,10 @@ export function forkJoin(...args: any[]): Observable<any> {
}
values[sourceIndex] = value;
},
() => remainingCompletions--,
undefined,
() => {
if (!--remainingCompletions || !hasValue) {
if (!remainingCompletions || !hasValue) {
if (!remainingEmissions) {
subscriber.next(keys ? createObject(keys, values) : values);
}
Expand Down

0 comments on commit 6454fcf

Please sign in to comment.