I am using cqrs with nestjs
I have a saga, that basically its rxjs implementations
@Saga()
updateEvent = (events$: Observable<any>): Observable<ICommand> => {
return events$.pipe(
ofType(UpdatedEvent,),
map((event) => {
return new otherCommand(event);
})
);
};
}
the question is How can I merge Events that all extend the same class So I want only 1 saga to catch all events instead of saga per event
I tried
ofType(UpdatedEvent,CreateEvent,DeleteEvent),
but it does not work and typescript does not allow that , and also because of the generic type i got this :
right-hand side of 'instanceof' is not callable
my events is like:
class UpdateEvent extends Base<UpdateDto>{
constructor(){super()}
}
class CreateEvent extends Base<CreateDto>{
constructor(){super()}
}
there are differences between the events is the Dtos
Instead of using ofType
you could just use filter(x => x instanceof UpdatedEvent || x instanceof CreateEvent || x instanceof DeleteEvent)
If this is a pattern you use often you could easily write a utility function eg instanceOneOf
that takes a bunch of values in an array and does the instanceof
checks internally.
Then you could do something like filter(x => instanceOneOf([UpdatedEvent, CreateEvent, DeleteEvent], x))