0

I have two streams coming from firestore and I need to do a computationally intensive operation only the first time these two streams produce values.

I've considered combineLatest2(), but as far as I can tell this will execute every time the streams produce output (not just the first time).

I've considered .firstWhere(), but I can't figure out how to combine the streams to use this.

Any suggestions would be appreciated.

Thanks

Simpler
  • 1,317
  • 2
  • 16
  • 31

2 Answers2

0

You can use forkJoin for this if you add take(1) to the streams coming in. Then the streams will complete after one input and the forkJoin will trigger.

forkJoin(
    stream1$.pipe(take(1)),
    stream2$.pipe(take(1))
).pipe(
    // Do your computationally intensive operation 
);
SnorreDan
  • 2,740
  • 1
  • 14
  • 17
0

Your responses helped me get to a useful solution.

combineLatest2(stream1.take(1), stream2.take(1), (s1, s2) {
// computationally intensive operation
}).listen((_) => {});
Simpler
  • 1,317
  • 2
  • 16
  • 31
  • I’d recommend forkJoin as it’s semantically more clear. ForkJoin only emits upon completion and has one emission, so if that’s the case you expect (a typical case), then it’s what you should use to make it more obvious at a glance what’s happening. CombineLatest implies a continuously emitting stream with multiple emissions. These semantics have important implications for managing your subscriptions and preventing memory leaks. Also, first() is a shorthand for / more semantic for take(1) though the difference is more trivial in that case. – bryan60 Mar 30 '19 at 17:06
  • @bryan60 Thanks for the clarification. I'm using rxdart and it doesn't have an implementation of forkJoin (yet). – Simpler Mar 30 '19 at 19:04