Reactive-Extensions / RxJS

The Reactive Extensions for JavaScript
http://reactivex.io
Other
19.48k stars 2.1k forks source link

Rx.Observable.forkJoin with empty list as parameter not calling complete #1506

Open airiciuc opened 7 years ago

airiciuc commented 7 years ago

Assume the following scenario where you need a dynamic number of server calls (sometime is 0) and you need to execute some functionality when all of them are done.

let updatesObservable = getRequiredUpdates();                   

Rx.Observable.forkJoin(...updatesObservable)
    .subscribe(() => onUpdatesFinished());

function getRequiredUpdates() {
    //dinamically calculate the required updates and return a list of Observables for each ajax call
    return [].map(v => Rx.Observable.return(v).map(u => processUpdate(u)));
}

function processUpdate(u) {             
    //process each update
    console.log(u);
}

function onUpdatesFinished() {
    //do here some important stuff when all updates finished
    console.log("All updates done");
}

Expected behavior:

  1. processUpdate function is never called
  2. onUpdatesFinished is called once

Actual behavior:

  1. processUpdate function is never called
  2. onUpdatesFinished is nerver called

Note: Change return [].map(v => Rx.Observable.return(v).map(u => processUpdate(u))); to return [1, 2, 3].map(v => Rx.Observable.return(v).map(u => processUpdate(u))); and onUpdatesFinished is called once

airiciuc commented 6 years ago

Anyone watching this issue? I've hit this again and it's causing bugs so easily. Here is a simpler situation:

userSearchTemObserver
    .flatMap((term) => userService.searchUsers(term))
    .mergeMap((users) => Observable.forkJoin(users.map((user) => facebookService.getProfile(user))))
    .subscribe((users) => {
        if(!users.length) {
            //here i want to handle the special case when no user found mathing the search term
            //but the it will never get here because forkJoin doesn't emit if the users list is empty
            ....
        } else {
            ...
        }
    });
ManuelSch commented 6 years ago

I've got the same issue (using a map function returning a potentially empty array).