Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 是否可以确定EPIC何时在可观察的Redux中完成?_Rxjs_Redux Observable - Fatal编程技术网

Rxjs 是否可以确定EPIC何时在可观察的Redux中完成?

Rxjs 是否可以确定EPIC何时在可观察的Redux中完成?,rxjs,redux-observable,Rxjs,Redux Observable,我是RxJS的新手,如果没有什么意义,我很抱歉 假设我想要一个可重用的epic,用于从应用程序加载epic获取将由操作调用的用户 过于简单的例子: const getUserEpic=action$=> 动作$.pipe( ofType(获取用户请求), switchMap(action=>from(service.fetchUser(action.userId).pipe( 映射到({type:GET_USER_SUCCESS}))) ), ); const appLoadEpic=操作$=>

我是RxJS的新手,如果没有什么意义,我很抱歉

假设我想要一个可重用的epic,用于从应用程序加载epic获取将由操作调用的用户

过于简单的例子:

const getUserEpic=action$=>
动作$.pipe(
ofType(获取用户请求),
switchMap(action=>from(service.fetchUser(action.userId).pipe(
映射到({type:GET_USER_SUCCESS})))
),
);
const appLoadEpic=操作$=>
动作$.pipe(
类型(加载应用请求),
映射(()=>of({type:GET\u USER\u REQUEST},{type:SOME\u OTHER\u REQUEST}))
);

如果我想在所有调用的epics(getUser等)完成后调用
LOAD_APP_SUCCESS
,该怎么办?如果可以在appLoadEpic中完成,那就太好了,但恐怕这是不可能的。

我建议这样做的方式是将各个史诗组合成一个“元”史诗。也就是说,您可以使用各个流来侦听它们各自的事件,并在所有合并流完成时传播它们

const getUserEpic = action$ => ... 
const someOtherEpic = action$ => ...
// Creates an epic that merges all the results from each provided epic
const initializationEpic = combineEpics(getUserEpic, someOtherEpic)

const appLoadEpic = (action$, state$) => {
  // Runs the new epic with the action and state values.
  const onLoad$ = initializationEpic(action$, state$).pipe(
    endWith({type: LOAD_APP_SUCCESS})
  )

  // Listen for the load app request before subscribing to the initialization
  action$.pipe(
    ofType(LOAD_APP_REQUEST),
    mergeMapTo(onLoad$),
  )
}
如果您觉得很新奇,不想通过导入来注入epics,您还可以动态注入epics细节,这是一种异步注入epic的方法,这意味着在启动过程中可以将epics作为动作体的一部分,而不是文件注入,这可能会使测试更容易一些

  const appLoadEpic = (action$, state$) => {
    // Listen for the load app request before subscribing to the initialization
    action$.pipe(
      ofType(LOAD_APP_REQUEST),
      // Now the epic is injected during the app loading, and you run it inline 
      // here. This makes it easy to mock it during testing
      mergeMap(({epic}) => epic(action$, state$).pipe(endWith({type: LOAD_APP_SUCCESS}))),
    )
  }

我建议这样做的方式是将单个史诗组合成一部“元”史诗。也就是说,您可以使用各个流来侦听它们各自的事件,并在所有合并流完成时传播它们

const getUserEpic = action$ => ... 
const someOtherEpic = action$ => ...
// Creates an epic that merges all the results from each provided epic
const initializationEpic = combineEpics(getUserEpic, someOtherEpic)

const appLoadEpic = (action$, state$) => {
  // Runs the new epic with the action and state values.
  const onLoad$ = initializationEpic(action$, state$).pipe(
    endWith({type: LOAD_APP_SUCCESS})
  )

  // Listen for the load app request before subscribing to the initialization
  action$.pipe(
    ofType(LOAD_APP_REQUEST),
    mergeMapTo(onLoad$),
  )
}
如果您觉得很新奇,不想通过导入来注入epics,您还可以动态注入epics细节,这是一种异步注入epic的方法,这意味着在启动过程中可以将epics作为动作体的一部分,而不是文件注入,这可能会使测试更容易一些

  const appLoadEpic = (action$, state$) => {
    // Listen for the load app request before subscribing to the initialization
    action$.pipe(
      ofType(LOAD_APP_REQUEST),
      // Now the epic is injected during the app loading, and you run it inline 
      // here. This makes it easy to mock it during testing
      mergeMap(({epic}) => epic(action$, state$).pipe(endWith({type: LOAD_APP_SUCCESS}))),
    )
  }

那看起来很聪明。谢谢!那看起来很聪明。谢谢!