RxJS:发出void值作为初始值的正确方法是什么?

问题描述 投票:3回答:1

the example中,您可以看到在为void类型的流需要初始值的情况下,它看起来不正确。

import { of, fromEvent, concat, Subject } from "rxjs";
import { map, switchMap, take, finalize } from "rxjs/operators";

let sequence = 1;
const trigger = new Subject<void>();
const source = concat(
  of(""), // is there any way to make it more pretty?
  trigger
).pipe(
  switchMap(() =>
    fromEvent(document, "click").pipe(
      take(3),
      finalize(() => {
        sequence++;
        trigger.next();
      })
    )
  ),
  map(
    ({ clientY }: MouseEvent) => `sequence: ${sequence} clientY: ${clientY}!`
  )
);

source.subscribe(x => console.log(x));
rxjs concat void
1个回答
1
投票

不确定是否有比这更漂亮的解决方案:

const source =
  trigger.pipe(
    startWith(undefined as void),
    switchMap(...),
    ...
  );
© www.soinside.com 2019 - 2024. All rights reserved.