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

问题描述 投票:1回答:1

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

我的传奇看起来像这样:

@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实例,则命令处理程序被正确调用。

我是否会遗漏某些内容,或者是不支持发出多个命令的传奇实现?

nestjs
1个回答
0
投票

好像我找到了它的答案 - 它与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
        );
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.