1

I'm trying to insert 2 pipe operators into pipe function, but I want to apply the first one by condition, else, only the second one will be applied.

that's the way it looks now without the condition:

getData(query).pipe(setLoding(this.store),tap(//some actions here...))

setLoading is an Akita pipe, and I would like it to be applied with some boolean condition.

I tried to use rxjs's iif() but I received an error since setLoding is not a type of SubscribableOrPromise.

Can anyone think of another way?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Iris
  • 21
  • 3

1 Answers1

1

Using the rxjs iif, you can conditionally write observables but it is not used for dealing with operators.

Since the setLoading is an operator in your case, it can not be used with iif. To use setLoading conditionally in pipe, you'll have to write something similar to -

getData(query)
.pipe(
  condition ? setLoading(this.store): tap(() => /* some other action */ )
)

EDIT:

In case you don't want to do anything in the else case and execute the tap always, you need to use the identity operator.

getData(query)
.pipe(
  condition ? setLoading(this.store): identity,
  tap(() => /* some other action */ )
)
Krantisinh
  • 1,579
  • 13
  • 16
  • this way forces me to add an "else" statement. I have an error of ": expected". Is there is any way to do nothing in the "else" case and still moving on to tap operator? – Iris May 08 '21 at 14:04
  • @Iris Yes, using the `identity` operator. Updated the answer. – Krantisinh May 08 '21 at 17:23