如何从nestjs saga发出多个命令?

Tho*_*mas 7 nestjs

我创建了一个传奇来对给定事件做出反应。在这种情况下,需要发出多个命令。

我的传奇看起来像这样:

@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()));
            }),
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

调试时,我发现抛出了一个错误“CommandHandler 未找到异常!”,这有点令人困惑,因为万一我只返回SomeCommand正确调用命令处理程序的一个实例。

我是否错过了什么,或者传奇实现只是不支持发出多个命令?

Tho*_*mas 4

似乎我找到了答案 - 它与 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
        );
    }
}
Run Code Online (Sandbox Code Playgroud)