运行一个内部可观察对象,而不影响外部可观察对象



是否有一种方法可以运行内部可观察对象,如switchMap,concatMap等,但没有改变外部可观察对象的值?我这里所拥有的是switchMap中的内部可观察对象正在将string转换为boolean并沿着管道传递结果。我怎样才能让它把传入的东西传递出去呢?

fsExists = false;
projectExists = false;
private doChecks = new BehaviorSubject<string>('');
doChecks$ = this.doChecks.pipe(
debounceTime(100),
tap(i => (this.projectExists = this.projects.exists(i))),
// Converts the outer `i` to a boolean here:
switchMap(i => this.fs.exists(path.join(i, environment.manifest)).pipe(tap(i => (this.fsExists = i)))),
tap(i => /* `i` should be a string here. But it is a boolean. */)
);

只需添加一个map来返回原始参数,您可能希望以不同的方式命名变量,以减少混淆。为了更清楚,我将i的布尔实例重命名为exists

doChecks$ = this.doChecks.pipe(
debounceTime(100),
tap((i) => (this.projectExists = this.projects.exists(i))),
switchMap((i) =>
this.fs.exists(path.join(i, environment.manifest)).pipe(
tap((exists) => (this.fsExists = exists)),
map(() => i) // return original parameter of switchMap
)
),
tap((i) => console.log(i)) // i is string here
);

最新更新