触发多个动作并等待它们解析 RxJS / Redux Observables
Fire multiple actions and wait for them to resolve RxJS / Redux Observables
我有一个动作想用来初始化我的应用程序,我想为这个动作创建一个史诗,然后在这个动作的背后触发多个其他动作,等待它们全部完成,然后再触发另一个行动。我查看了其他问题,它与此非常相似
我已经尝试过这种方法,对我来说,它不起作用,它会触发 APP_INIT
操作,但随后无法触发序列中的任何其他操作。有人能帮忙吗?
import { of } from 'rxjs';
import { mergeMap, zip, concat, mapTo } from 'rxjs/operators';
import { ofType } from 'redux-observable';
import { firstAction, secondAction } from 'actions';
export default function appInit (action$) {
return (
action$.pipe(
ofType('APP_INIT'),
mergeMap(() =>
concat(
of(firstAction()),
of(secondAction()),
zip(
action$.ofType('ACTION_ONE_COMPLETE'),
action$.ofType('ACTION_TWO_COMPLETE')
).mapTo(() => console.log('complete'))
)
)
)
);
}
combineLatest 是您想要的,它只会在所有可观察对象都发出时发出。
const { combineLatest, of } = rxjs;
const { delay } = rxjs.operators;
combineLatest(
of(1),
of(2).pipe(delay(2000)),
of(3).pipe(delay(1000))
).subscribe(([a,b,c]) => {
console.log(`${a} ${b} ${c}`); // Will take 2 seconds as that is when all have emitted
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.4.0/rxjs.umd.min.js"></script>
原来我的代码一开始就很好,主要原因是我从 rxjs/operators
导入 concat
而我应该直接从 rxjs
导入,我花了几个小时才意识到,但它现在有效了。
下面的完整代码可能对任何人都有帮助。
import { of, concat, zip } from 'rxjs';
import { mergeMap, map, take } from 'rxjs/operators';
import { ofType } from 'redux-observable';
import { appInitialisationComplete, APP_INITIALISATION } from 'client/actions/app/app';
import { actionOne, ACTION_ONE_COMPLETE } from 'client/actions/action-one/action-one';
import { actioTwo, ACTION_TWO_COMPLETE } from 'client/actions/action-two/action-two';
/**
* appInitialisationEpic
* @param {Object} action$
* @return {Object}
*/
export default function appInitialisationEpic (action$) {
return (
action$.pipe(
ofType(APP_INITIALISATION),
mergeMap(() =>
concat(
of(actionOne()),
of(actioTwo()),
zip(
action$.ofType(ACTION_ONE_COMPLETE).pipe(take(1)),
action$.ofType(ACTION_TWO_COMPLETE).pipe(take(1))
)
.pipe(map(() => appInitialisationComplete()))
)
)
)
);
}
我有一个动作想用来初始化我的应用程序,我想为这个动作创建一个史诗,然后在这个动作的背后触发多个其他动作,等待它们全部完成,然后再触发另一个行动。我查看了其他问题,它与此非常相似
我已经尝试过这种方法,对我来说,它不起作用,它会触发 APP_INIT
操作,但随后无法触发序列中的任何其他操作。有人能帮忙吗?
import { of } from 'rxjs';
import { mergeMap, zip, concat, mapTo } from 'rxjs/operators';
import { ofType } from 'redux-observable';
import { firstAction, secondAction } from 'actions';
export default function appInit (action$) {
return (
action$.pipe(
ofType('APP_INIT'),
mergeMap(() =>
concat(
of(firstAction()),
of(secondAction()),
zip(
action$.ofType('ACTION_ONE_COMPLETE'),
action$.ofType('ACTION_TWO_COMPLETE')
).mapTo(() => console.log('complete'))
)
)
)
);
}
combineLatest 是您想要的,它只会在所有可观察对象都发出时发出。
const { combineLatest, of } = rxjs;
const { delay } = rxjs.operators;
combineLatest(
of(1),
of(2).pipe(delay(2000)),
of(3).pipe(delay(1000))
).subscribe(([a,b,c]) => {
console.log(`${a} ${b} ${c}`); // Will take 2 seconds as that is when all have emitted
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.4.0/rxjs.umd.min.js"></script>
原来我的代码一开始就很好,主要原因是我从 rxjs/operators
导入 concat
而我应该直接从 rxjs
导入,我花了几个小时才意识到,但它现在有效了。
下面的完整代码可能对任何人都有帮助。
import { of, concat, zip } from 'rxjs';
import { mergeMap, map, take } from 'rxjs/operators';
import { ofType } from 'redux-observable';
import { appInitialisationComplete, APP_INITIALISATION } from 'client/actions/app/app';
import { actionOne, ACTION_ONE_COMPLETE } from 'client/actions/action-one/action-one';
import { actioTwo, ACTION_TWO_COMPLETE } from 'client/actions/action-two/action-two';
/**
* appInitialisationEpic
* @param {Object} action$
* @return {Object}
*/
export default function appInitialisationEpic (action$) {
return (
action$.pipe(
ofType(APP_INITIALISATION),
mergeMap(() =>
concat(
of(actionOne()),
of(actioTwo()),
zip(
action$.ofType(ACTION_ONE_COMPLETE).pipe(take(1)),
action$.ofType(ACTION_TWO_COMPLETE).pipe(take(1))
)
.pipe(map(() => appInitialisationComplete()))
)
)
)
);
}