gpt4 book ai didi

RxJS:带多个 Action 和不同过滤器的 takeUntil?

转载 作者:行者123 更新时间:2023-12-01 15:52:49 25 4
gpt4 key购买 nike

我有一个 Observable,我想继续执行,直到:

1) uploadActions.MARK_UPLOAD_AS_COMPLETE使用特定负载调用操作

或者

2) uploadActions.UPLOAD_FAILURE使用任何有效负载调用操作

这是我所能得到的(并且不起作用):

return Observable.interval(5000)
.takeUntil(
action$
.ofType(
uploadActions.UPLOAD_FAILURE,
uploadActions.MARK_UPLOAD_AS_COMPLETE
)
.filter(a => { // <---- this filter only applies to uploadActions.MARK_UPLOAD_AS_COMPLETE
const completedFileHandle = a.payload;
return handle === completedFileHandle;
})
)
.mergeMap(action =>
...
);

有没有一种干净的方法可以实现这一目标?

最佳答案

我将这两个条件拆分为单独的流,然后像这样合并它们:

const action$ = new Rx.Subject();
const uploadActions = {
UPLOAD_FAILURE: "UPLOAD_FAILURE",
MARK_UPLOAD_AS_COMPLETE: "MARK_UPLOAD_AS_COMPLETE"
};
const handle = 42;

window.setTimeout(() => action$.next({
type: uploadActions.MARK_UPLOAD_AS_COMPLETE,
payload: handle
}), 1200);

Rx.Observable.interval(500)
.takeUntil(
Rx.Observable.merge(
action$.filter(x => x.type === uploadActions.UPLOAD_FAILURE),
action$.filter(x => x.type === uploadActions.MARK_UPLOAD_AS_COMPLETE)
.filter(x => x.payload === handle)
)
).subscribe(
x => { console.log('Next: ', x); },
e => { console.log('Error: ', e); },
() => { console.log('Completed'); }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.min.js"></script>


例如,我必须使用 filter运算符而不是 ofTypeofType是一个 redux 的东西。

关于RxJS:带多个 Action 和不同过滤器的 takeUntil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709573/

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