-
Notifications
You must be signed in to change notification settings - Fork 150
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
Events inheritance is not supported by EventHandlers #486
Comments
Would you like to create a PR for this issue? |
Sure |
@korzonkiee were you able to submit the PR for this? |
@kodeine not yet. IIRC I had some TypeScript issues when trying to implement this functionality. I might give it another try soon. |
I believe it's not a bug. To invoke its handle method when Dolphin or Monkey you should add this events in import { EventsHandler } from '@nestjs/cqrs';
import { IEventHandler } from '@nestjs/cqrs';
import { Dolphin, Mammal, Monkey } from './animal';
@EventsHandler(Mammal, Dolphin, Monkey)
export class MammalHandler implements IEventHandler<Mammal> {
handle(event: Mammal) {
console.log(`mammal`);
}
} |
I heard about an approach that every Event is extended per producer. Let say we have an event called TurnomentWonEvent which can be produced by FinishEnemyCommand and SurrenderCommand and we can create TurnomentWonBySurrenderEvent and so on. All of those should obviously trigger TurnomentWonEventHandler. |
I'm submitting a...
Current behavior
Given an abstract class (say
Mammal
) and its derived classes (sayDolphin
andMonkey
), the event handler defined as@EventHandler(Mammal)
does not invoke itshandle
method whenDolphin
orMonkey
event is published to the event bus.Expected behavior
Event handlers defined for abstract classes should be invoked when an instance of a derived class is published onto the event bus.
Minimal reproduction of the problem with instructions
mammal
should be logged into the console by thesrc/mammal.handler.ts
, but is not.What is the motivation / use case for changing the behavior?
In my system, I have multiple events that are related to
Task
, (TaskCreated
,TaskExecuted
, etc...). Each of those events extends an abstract classTaskEvent
. I would like to create an event handler that handles all of the events that extendTaskEvent
without manually listing them in the event handler's decorator constructor.Additional info
I looked through the code responsible for relaying events into a specific event handler and it turned out that upcoming events are compared with a list of events specified in the decorator by name:
https://github.com/nestjs/cqrs/blob/master/src/event-bus.ts#L104.
So, when we define
@EventsHandler(Mammal)
and publish an eventDolphin
, it compares"Mammal" === "Dolphin"
hence not dispatching the event into the event handler.I've changed the source code slightly so that it now compares the events using an
instanceof
operator and it works as expected, but I'm not sure if that is the desired solution.Environment
The text was updated successfully, but these errors were encountered: