Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saga: Responding to event by issuing multiple commands #29

Closed
tschachten opened this issue Feb 12, 2019 · 4 comments
Closed

Saga: Responding to event by issuing multiple commands #29

tschachten opened this issue Feb 12, 2019 · 4 comments

Comments

@tschachten
Copy link

tschachten commented Feb 12, 2019

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Whenever I try to respond to an event within a saga by issuing multiple commands an error gets thrown and my application crashes.
Debugging showed me that the error seems to be 'CommandHandler not found exception!', but I don't know where this error arises from - when returning just 1 command instead of multiple ones then everything is working fine.

Expected behavior

A saga can return multiple commands in response to an event.

Minimal reproduction of the problem with instructions

My saga simplified looks like this:

@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()));
            }),
        );
    }
}

What is the motivation / use case for changing the behavior?

Having a saga that is able to issue multiple commands in response to an event.

Environment


Nest version: 5.4+
CQRS version: 5.1.1

 
For Tooling issues:
- Node version: 10.11.0
- Platform:  Mac

Others:

@tschachten
Copy link
Author

tschachten commented Feb 12, 2019

Seems like I was using it the wrong way - the following seems to be working for me:

@Injectable()
export class SomeSagas {
    public constructor() {}

    onSomeEvent(events$: EventObservable<any>): Observable<ICommand> {
        return events$.ofType(SomeEvent).pipe(
            map((event: SomeEvent) => {
                return [
                    new SomeCommand(uuid()),
                    new SomeCommand(uuid()),
                ];
            }),
            flatMap(c => c), // this is solving my issue
        );
    }
}

I'd like to suggest to add an example of that case to the example repository for guys like me that have not been working with RxJS before? IMO it's pretty common that you'll need to respond to an event by issuing multiple commands at some point.

@kamilmysliwiec
Copy link
Member

Hi @tschachten,
I'm glad that you found a solution :) Feel free to create a PR to the example repository, contributions are always welcome!

@codemile
Copy link

codemile commented Aug 1, 2020

Just use a mergeMap next time.

mergeMap((event: SomeEvent) => of(new SomeCommand(uuid()), new SomeCommand(uuid()))),

@LinboLen
Copy link

how to get a req/res like event stream ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants