gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:35:08 26 4
gpt4 key购买 nike

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

最佳答案

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

const source =
trigger.pipe(
startWith(undefined as void),
switchMap(...),
...
);

关于RxJS:将 void 值作为初始值发出的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60833287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com