简体   繁体   中英

How to add rxjs pipe operator dynamically with condition?

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?

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 */ )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM