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

Publishing an event fires all event handlers #1889

Closed
2 of 4 tasks
StimulCross opened this issue Jan 14, 2025 · 1 comment
Closed
2 of 4 tasks

Publishing an event fires all event handlers #1889

StimulCross opened this issue Jan 14, 2025 · 1 comment
Labels

Comments

@StimulCross
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Publishing any event (either manually or committing from an aggregate) fires all event handlers, even those that are not related to the publishing events.

The reproduction repo contains 3 event handlers in different modules and 2 publishing events. Each event fires all existing handlers.

UserUpdatedHandler  ->  UserCreatedEvent { id: 1 }
UserDeletedHandler  ->  UserCreatedEvent { id: 1 }
UserCreatedHandler  ->  UserCreatedEvent { id: 1 }
UserUpdatedHandler  ->  UserDeletedEvent { id: 1 }
UserDeletedHandler  ->  UserDeletedEvent { id: 1 }
UserCreatedHandler  ->  UserDeletedEvent { id: 1 }

Minimum reproduction code

https://github.com/StimulCross/cqrs-events-problem

Steps to reproduce

git clone https://github.com/StimulCross/cqrs-events-problem
cd cqrs-events-problem
npm i
npm run start:dev

Expected behavior

Each event should fire only handlers that listen to this event.

UserCreatedHandler  ->  UserCreatedEvent { id: 1 }
UserDeletedHandler  ->  UserDeletedEvent { id: 1 }

Package version

11.0.0-next.1

NestJS version

10.4.15

Node.js version

22.12.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

If you go to a.module.ts, you will see the code publishing the events.

@Module({})
export class AModule implements OnApplicationBootstrap {
  constructor(
    private readonly _eventPublisher: EventPublisher,
    private readonly _eventBus: EventBus,
  ) {}

  onApplicationBootstrap() {
    setTimeout(() => {
      // ------- v11 ------- //

      // CASE 1 (commiting from the aggregate root)
      const user = new User(1);
      user.delete();
      user.commit();

      // CASE 2 (publishing manually)
      // const UserModel = this._eventPublisher.mergeClassContext(User);
      // const user = new UserModel(1);
      // this._eventBus.publish(new UserCreatedEvent(user.id));
      // this._eventBus.publish(new UserDeletedEvent(user.id));

      // ------- v10 ------- //

      // CASE 1 (commiting from the aggregate root)
      // const UserModel = this._eventPublisher.mergeClassContext(User);
      // const user = new UserModel(1);
      // user.delete();
      // user.commit();

      // CASE 2 (publishing manually)
      // const UserModel = this._eventPublisher.mergeClassContext(User);
      // const user = new UserModel(1);
      // this._eventBus.publish(new UserCreatedEvent(user.id));
      // this._eventBus.publish(new UserDeletedEvent(user.id));
    }, 1000);
  }
}

For v11, both cases lead to the output from the Current behavior section.

Downgrading to v10 npm i @nestjs/cqrs@latest fixes the problem. The output is as in the Expected behavior section for both cases.

@kamilmysliwiec
Copy link
Member

Fixed in 11.0.0-next.2

Thanks for reporting

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

No branches or pull requests

2 participants