4

I'm trying to solve an issue. Currently, I'm opening a modal but I needed the combination of the 3 route observables for this. So, my code logic is something like this:

combineLatest([obs1, obs2, obs3])
    .subscribe(([s1, s2, s3]: any) => { 
        openModal();
    });

Since it will get executed 3 times, my modal will also be opened three times but I don't want it to do that but I know is expected behavior.

So, for this, the bad solution I implemented was using a flag like this:

let shouldOpen = true;

combineLatest([obs1, obs2, obs3])
    .subscribe(([s1, s2, s3]: any) => { 
        if(shouldOpen) {
            shouldOpen = false;
            openModal();
        }
    });

But of course, that's not a good solution.

So, the question is, Is there a way for me to keep using combineLatest but getting it executed only once?

Here's a Stackblitz if you want to try with combineLatest.

Note: I cannot use forkJoin or zip, I already tried those.

Jonatan Lavado
  • 954
  • 2
  • 15
  • 26

1 Answers1

4

Since you can't use forkJoin (which is the right answer), skip the first two emissions from the combineLatest

combineLatest([obs1, obs2, obs3])
  .pipe(skip(2))
  .subscribe(([s1, s2, s3]: any) => { 
    openModal();
  });

You could also use skipWhile to skip until all three are defined:

      .pipe(skipWhile(([s1, s2, s3]) => s1===undefined || s2 === undefined || s3===undefined))
frosty
  • 21,036
  • 7
  • 52
  • 74
  • Since the complexity of my project is very high, I cannot use forkJoin because it'll carry out a huge refactor because if I use it, it breaks a lot of code, that's why I'm asking for a solution using only combineLatest. – Jonatan Lavado Oct 22 '21 at 03:38
  • See reply. My second answer is less elegant, but should work, and only open the modal the one time. – frosty Oct 22 '21 at 03:49
  • 1
    Wouldn't you also add a `take(1)`, just to be sure? – Pieterjan Oct 22 '21 at 05:54
  • You could also add a take(1) too. Good idea. – frosty Oct 22 '21 at 09:01