typescript 在不影响外部观察结果的情况下运行内部观察结果

u91tlkcl  于 2022-11-26  发布在  TypeScript
关注(0)|答案(1)|浏览(85)

有没有办法运行一个内部的可观测值,比如switchMapconcatMap,等等,但是不改变外部的可观测值?我在这里看到的是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. */)
  );
8cdiaqws

8cdiaqws1#

只需添加一个map来返回原始参数,您可能希望对变量进行不同的命名,以减少混淆。

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
  );

相关问题