我有我想用初始化我的应用程序的动作,我想创建这个动作史诗,然后关火这后面多其他操作,等待他们全部齐全,有消防另一个动作。我看了一下别人的质疑,这是非常类似于这样one
我试过这个方法对我来说,这是行不通的,它激发了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'))
)
)
)
);
}
原来我的代码是在一审非常好,主要的原因是,我是从concat
进口rxjs/operators
时,我应该已经从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()))
)
)
)
);
}
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>