Issue
Could somebody tell me what is the right syntax for an rxjs pipe with a conditional operation? In this case i would like to do the mapping with the filter, if the enviroment name array lenght is not 1. How can i use an if statement without return? Is there any rxjs operator for this?
environmentName = ['env1', 'env2'];
sourceList$ = this.getSources().pipe(
tap((srcList) => console.log(srcList)), //[["stylesheet","env1-xyz"],["include","cable"],...]
// if(this.environmentName.length!==1){
map((sourceList) => sourceList.filter((scr) => scr[1].startsWith(this.environmentName[0]) || scr[0] === 'include')),
//}
repeatWhen(() => this.sourceListChanged$)
);
Solution
Mentioned iif
is not operator. Use e.g. mergeMap
to incorporate it into your code:
environmentName = ['env1', 'env2'];
const filterSourceListByEnv1OrInclude = srcList => srcList.filter((scr) => scr[1].startsWith(this.environmentName[0]) || scr[0] === 'include');
sourceList$ = this.getSources().pipe(
tap((srcList) => console.log(srcList)),
mergeMap(srcList => iff(
() => this.environmentName.length !== 1,
of(filterSourceListByEnv1OrInclude(srcList)),
of(srcList)
)),
repeatWhen(() => this.sourceListChanged$)
);
Or ternary operator could be used instead of iif
:
sourceList$ = this.getSources().pipe(
tap((srcList) => console.log(srcList)),
mergeMap(srcList => of(this.environmentName.length !== 1
? filterSourceListByEnv1OrInclude(srcList)
: srcList
)),
repeatWhen(() => this.sourceListChanged$)
);
Answered By - Lubomir Jurecka
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.