如何创建一个在执行任何操作之前等待2个动作的redux-observable史诗

Dan*_*ott 1 javascript rxjs redux redux-observable

我想创建一个史诗,在工作之前监听一系列明确的动作.

这首史诗在第一次完成后也不需要存在.

我想象的是:

function doTheThing(action$) {
  return action$
     // The start of the sequence
    .ofType(FIRST_ACTION)

    // Do nothing until the second action occurs
    .waitForAnotherAction(SECOND_ACTION)

    // the correct actions have been dispatched, do the thing!
    .map(() => ({ type: DO_THE_THING_ACTION }))
    .destroyEpic();
}
Run Code Online (Sandbox Code Playgroud)

这样的事情可能redux-observable吗?

pau*_*els 6

正如@jayphelps在评论中指出的那样,根据您是否需要访问各种事件以及是否必须严格排序事件,有几种变体.所以以下都应该适合:

1)严格命令不关心事件:

action$
  .ofType(FIRST_ACTION)
  .take(1)
  .concat(action$.ofType(SECOND_ACTION).take(1))
  .mapTo({ type: DO_THE_THING_ACTION })
Run Code Online (Sandbox Code Playgroud)

2)严格命令关注事件

action$
  .ofType(FIRST_ACTION)
  .take(1)
  .concatMap(
    a1 => action$.ofType(SECOND_ACTION).take(1),
    (a1, a2) => ({type: DO_THE_THING_ACTION, a1, a2})
  )
Run Code Online (Sandbox Code Playgroud)

3)非严格命令(做或不做)关心事件

Observable.forkJoin(
  action$.ofType(FIRST_ACTION).take(1),
  action$.ofType(SECOND_ACTION).take(1),
  // Add this lambda if you *do* care
  (a1, a2) => ({type: DO_THE_THING_ACTION, a1, a2})
)
// Use mapTo if you *don't* care
.mapTo({type: DO_THE_THING_ACTION})
Run Code Online (Sandbox Code Playgroud)


Mat*_*own 5

Redux可观察对象的外观如下:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/zip';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/take';


function doTheThing(action$) {
  return Observable
     // waits for all actions listed to complete
    .zip(action$.ofType(FIRST_ACTION).take(1), 
         action$.ofType(SECOND_ACTION).take(1),
     )

    // do the thing
    .map(() => ({ type: DO_THE_THING_ACTION }));
}
Run Code Online (Sandbox Code Playgroud)