我已经创建了一个传奇来对特定事件做出反应。在这种情况下,需要发出多个命令。
我的传奇看起来像这样:
@Injectable()
export class SomeSagas {
public constructor() {}
onSomeEvent(events$: EventObservable<any>): Observable<ICommand> {
return events$.ofType(SomeEvent).pipe(
map((event: SomeEvent) => {
return of(new SomeCommand(uuid()), new SomeCommand(uuid()));
}),
);
}
}
在调试时我发现有一个错误抛出'CommandHandler not found exception!',这有点令人困惑,因为如果我只返回一个SomeCommand
实例,则命令处理程序被正确调用。
我是否会遗漏某些内容,或者是不支持发出多个命令的传奇实现?
好像我找到了它的答案 - 它与RxJS有关:
@Injectable()
export class SomeSagas {
public constructor() {}
onSomeEvent(events$: EventObservable<any>): Observable<ICommand> {
return events$.ofType(SomeEvent).pipe(
map((event: SomeEvent) => {
const commands: ICommand[] = [
new SomeCommand(uuid()),
new SomeCommand(uuid()),
new SomeCommand(uuid()),
];
return commands;
}),
flatMap(c => c), // piping to flatMap RxJS operator is solving the issue I had
);
}
}