gpt4 book ai didi

rxjs - 是否可以通过发出 Action 来启动/停止/恢复 redux-observable 史诗?

转载 作者:行者123 更新时间:2023-12-04 15:40:06 26 4
gpt4 key购买 nike

这个问题可能是关于redux-observablerxjs或两者。

我正在寻找一种通过特定 Action 开始、停止或恢复史诗的方法。例如,当操作 {type: 'START'} 时,史诗(已经是史诗中间件的一部分)将处于事件状态。已收到,但在操作 {type: 'END'} 时将处于非事件状态收到。这可能吗?

最佳答案

您可以使用 switchMap 的组合和 filter这样做(假设所有 Action 包括开始/结束 Action 来自同一个来源)

如果开始/结束 Action 来自单独的源,那就更容易了,那么您可以跳过分离源流的步骤。

运行下面的代码示例以查看它的运行情况。

// this would be your source
const actions$ = new Rx.Subject();

// in this example controllActions and dataActions are derived from the same stream,
// if you have the chance to use 2 seperate channels from the start, do that
const controllActions$ = actions$
.filter(action => action.type === "END" || action.type === "START");
const dataActions$ = actions$
.filter(action => action.type !== "END" && action.type !== "START");

const epic$ = controllActions$
.switchMap(action => {
if (action.type === "END") {
console.info("Pausing stream");
return Rx.Observable.never();
} else {
console.info("Starting/Resuming stream");
return dataActions$;
}
});
epic$.subscribe(console.log);

// simulating some action emissions, the code below is _not_ relevant for the actual implementation
Rx.Observable.from([
"Some data, that will not be emitted...",
{type: "START"},
"Some data, that _will_ be emitted...",
"Some more data, that _will_ be emitted...",
{type: "END"},
"Some data, that will not be emitted...",
"Some data, that will not be emitted...",
{type: "START"},
"Some data, that _will_ be emitted...",
"Some more data, that _will_ be emitted..."
])
.concatMap(d => Rx.Observable.of(d).delay(400))
.subscribe(actions$);
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>

关于rxjs - 是否可以通过发出 Action 来启动/停止/恢复 redux-observable 史诗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45528230/

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