gpt4 book ai didi

javascript - RxJS 函数从一个 observable 发出最后一个值,然后另一个发出 true

转载 作者:搜寻专家 更新时间:2023-10-30 20:57:28 24 4
gpt4 key购买 nike

你好,我正在尝试创建 observable (OBS) 和 subject(SUB) 的函数来存储来自 OBS 的最后一项,而 SUB 具有 F 值,并在 SUN 变为 T 时发射它(并且仅发射它)

   OBS ---a----b----c----d----e----f----g----h-----
SUB ------F----------T------------F-------T-----
OUT -----------------c--------------------h-----

我试图解决这个问题

OBS.window(SUB)
.withLatestFrom(SUB)
.switchMap(([window, status]) => {

if(status === F) {
return window.combineLatest(SUB, (cmd, status) => {
if(status === T) {
return null;
};

return cmd;
}).last((e) => {
return !!e;
})
}

return Observable.empty<Command>();
}).filter((cmd) => {
return !!cmd;
})

但是没用

最佳答案

所以看起来你想要这样的东西:

SUB
// Only emit changes in the status
.distinctUntilChanged()
// Only forward true values down stream
.filter(t => t === T)
// Only emit the latest from OBS when you get a T from SUB
// Remap it so only cmd is forwarded
.withLatestFrom(OBS, (_, cmd) => cmd)

关于javascript - RxJS 函数从一个 observable 发出最后一个值,然后另一个发出 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45618451/

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