Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing empty array into combineLatest closes subscription prematurely #7110

Open
mkhodan opened this issue Nov 3, 2022 · 1 comment
Open
Labels
AGENDA ITEM Flagged for discussion at core team meetings

Comments

@mkhodan
Copy link

mkhodan commented Nov 3, 2022

Describe the bug

Since rxjs 7 passing empty array into combineLatest and filling it later doesn't work as it immediately closes subscription even though we haven't subscribed yet.

Expected behavior

combineLatest waits until subscription and only then decides whether array is actually empty.

Reproduction code

e.g. this code worked in 6.6.2 but doesn't work anymore in 7.5.7

import { combineLatest, of } from 'rxjs';

let observables = [];

let result = combineLatest(observables);

for (let i = 0; i < 20; i++) {
  observables.push(of(i));
}

result.subscribe((numbers) => console.log(numbers));

please note that if we create array with at least 1 observable already there this works as expected in rxjs 7:
let observables = [of(-1)];

Reproduction URL

https://stackblitz.com/edit/rxjs-6-daddo-playground-is6lgj?file=index.ts
https://stackblitz.com/edit/rxjs-7-playground-3urg4j?file=index.ts

Version

7.5.7

Environment

No response

Additional context

No response

@mkhodan mkhodan changed the title passing empty array into combineLatest immediately closes subscription prematurely passing empty array into combineLatest closes subscription prematurely Nov 4, 2022
@jakovljevic-mladen jakovljevic-mladen added the AGENDA ITEM Flagged for discussion at core team meetings label Jul 24, 2023
@benlesh
Copy link
Member

benlesh commented Mar 6, 2024

This looks like it's happening because we're examining the array given prior to subscription, we're not assuming that someone might mutate the array after that.

The workaround for now would be to create the array prior to passing it to the combineLatest. If that doesn't work, you can always wrap the combineLatest in a defer.

import { combineLatest, of, defer } from "rxjs";

let observables = [];

let result = defer(() => combineLatest(observables))

for (let i = 0; i < 20; i++) {
  observables.push(of(i));
}

result.subscribe((numbers) => console.log(numbers));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AGENDA ITEM Flagged for discussion at core team meetings
Projects
None yet
Development

No branches or pull requests

3 participants