lig*_*303 3 javascript rxjs reactjs react-redux redux-observable
我在 redux-observable 中有以下场景。我有一个组件可以检测要使用的后端,并应该设置 api 客户端使用的后端 URL。客户端和 URL 都保存在全局状态对象中。
执行顺序应该是: 1. 检查后端 2. 出错时替换保持在状态中的后端 URL 3. 触发 3 个动作以使用新的后端状态 URL 加载资源
到目前为止,我在第 1 步中所做的是从我的史诗中访问 state$ 对象并修改支持的 URL。这似乎只工作了一半。状态由 3 中触发的操作更新。仍然看到旧状态并使用错误的后端。
如果您依赖于执行顺序,那么在操作之间更新状态的标准方法是什么?
我的 API-Epic 看起来像这样:
export const authenticate = (action$, state$) => action$.pipe(
ofType(actions.API_AUTHENTICATE),
mergeMap(action =>
from(state$.value.apiState.apiClient.authenticate(state$.value.apiState.bearer)).pipe(
map(bearer => apiActions.authenticatedSuccess(bearer))
)
)
)
export const authenticatedSuccess = (action$, state$) => action$.pipe(
ofType(actions.API_AUTHENTICATED_SUCCESS),
concatMap(action => concat(
of(resourceActions.doLoadAResource()),
of(resourceActions.doLoadOtherResource()),
of(resourceActions.doLoadSomethingElse()))
)
)
Run Code Online (Sandbox Code Playgroud)
我发现用户在 GitHub 和 StackOverflow 上讨论的一种常见方法是链接多个史诗,就像我相信您的示例试图演示的那样。第一个史诗在“完成”时发送一个动作。reducer 侦听此操作并更新商店的状态。第二个史诗(或许多其他史诗,如果您想要并发操作)侦听相同的操作并启动工作流程的下一个序列。次要史诗在减速器之后运行,从而看到更新的状态。从文档:
Epics 与正常的 Redux 调度通道一起运行,在减速器已经收到它们之后......
我发现链接方法可以很好地解耦更大工作流程的各个阶段。您可能出于设计原因(例如关注点分离)需要解耦,以重用较大工作流的较小部分,或者制作更小的单元以便于测试。当您的史诗在较大工作流的不同阶段之间调度操作时,这是一种易于实施的方法。
但是,请记住,这state$是一个可观察的。您可以使用它在任何时间点获取当前值——包括在单个史诗中调度不同操作之间。例如,考虑以下情况并假设我们的商店有一个简单的计数器:
export const workflow = (action$, state$) => action$.pipe(
ofType(constants.START),
withLatestFrom(state$),
mergeMap(([action, state]) => // "state" is the value when the START action was dispatched
concat(
of(actions.increment()),
state$.pipe(
first(),
map(state => // this new "state" is the _incremented_ value!
actions.decrement()),
),
defer(() => {
const state = state$.value // this new "state" is now the _decremented_ value!
return empty()
}),
),
),
)
Run Code Online (Sandbox Code Playgroud)
有很多方法可以从 observable 中获取当前状态!
关于您的示例中的以下代码行:
state$.value.apiState.apiClient.authenticate(state$.value.apiState.bearer)
Run Code Online (Sandbox Code Playgroud)
首先,使用状态传递 API 客户端不是常见/推荐的模式。您可能希望将 API 客户端注入作为您的史诗的依赖项(这使单元测试更容易!)。其次,不清楚 API 客户端如何从状态中获取当前后端 URL。API 客户端是否可能使用状态的缓存版本?如果是,您可能需要重构您的authenticate方法并传入当前后端 URL。
这是一个处理错误并包含上述内容的示例:
/**
* Let's assume the state looks like the following:
* state: {
* apiState: {
* backend: "URL",
* bearer: "token"
* }
*/
// Note how the API client is injected as a dependency
export const authenticate = (action$, state$, { apiClient }) => action$.pipe(
ofType(actions.API_AUTHENTICATE),
withLatestFrom(state$),
mergeMap(([action, state]) =>
// Try to authenticate against the current backend URL
from(apiClient.authenticate(state.apiState.backend, state.apiState.bearer)).pipe(
// On success, dispatch an action to kick off the chained epic(s)
map(bearer => apiActions.authenticatedSuccess(bearer)),
// On failure, dispatch two actions:
// 1) an action that replaces the backend URL in the state
// 2) an action that restarts _this_ epic using the new/replaced backend URL
catchError(error$ => of(apiActions.authenticatedFailed(), apiActions.authenticate()),
),
),
)
export const authenticatedSuccess = (action$, state$) => action$.pipe(
ofType(actions.API_AUTHENTICATED_SUCCESS),
...
)
Run Code Online (Sandbox Code Playgroud)
此外,请记住,在链接史诗时,类似的构造concat 不会等待链接的史诗“完成”。例如:
concat(
of(resourceActions.doLoadAResource()),
of(resourceActions.doLoadOtherResource()),
of(resourceActions.doLoadSomethingElse()))
)
Run Code Online (Sandbox Code Playgroud)
如果这些doLoadXXX操作中的每一个都“启动”了一个史诗,那么这三个操作可能会同时运行。每一个动作都会一个接一个地分派,每一个史诗都会“开始”一个接一个地运行,而不用等待前一个“完成”。这是因为史诗从未真正完整。它们是长寿的,永无止境的流。您将需要一些信号明确等待该标识时doLoadAResource完成,如果你想doLoadOtherResource运行后 doLoadAResource。
| 归档时间: |
|
| 查看次数: |
2135 次 |
| 最近记录: |