question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injectable EventSubscribers

See original GitHub issue

Is your feature request related to a problem? Please describe. It would be awesome if EventSubscriber classes could support NestJs DI so we can inject dependencies in them.

Describe the solution you’d like The current @Subscriber() decorator cannot be used as it directly creates a new instance: https://github.com/mikro-orm/mikro-orm/blob/master/packages/core/src/decorators/Subscriber.ts#L8 And I didn’t find a clear solution to remove the attached subscriber before MikroORM init.

I think the easiest way is to have a new decorator (could be @InjectableSubscriber(), that also wraps Nestjs @Injectable() ?) that simply adds a metadata to the class prototype flagging it as a Subscriber, then the class has to be registered as a provider inside a Module (like any nestjs providers).

Then follow a common Nestjs pattern, “disover” this providers via the custom metadata and do stuff with them, in our case register them as subscriber (em.getEventManager().registerSubscriber()).

Additional context As a reference, @nestjs/bull does the same for registering queue processors https://github.com/nestjs/bull/blob/master/lib/bull.explorer.ts . It is a bit more complicated since it has to check every class methods instead of just the class, what we need are just the first lines of explore():

const providers: InstanceWrapper[] = this.discoveryService
      .getProviders()
      .filter((wrapper: InstanceWrapper) =>
        this.metadataAccessor.isQueueComponent(wrapper.metatype),
      );

Replace isQueueComponent with something like isSubscriberComponent and voilà, we have an array of all provided subscribers.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
B4nancommented, Jan 22, 2021

This is already possible:

@Injectable()
export class MySubscriber {

  constructor(em: EntityManager) {
    em.getEventManager().registerSubscriber(this);
  }

}
7reactions
EricMcRaycommented, Jun 21, 2021

This is already possible:

@Injectable()
export class MySubscriber {

  constructor(em: EntityManager) {
    em.getEventManager().registerSubscriber(this);
  }

}

Can you also put this to documents? I was looking for this solution for hours.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Event Subscribers and Dependency Injection Tags > Drupal 8
Here's the challenge: add a function that will be called on every request, right at the beginning, before any controller is called.
Read more >
Inject service to EventSubscriber - symfony - Stack Overflow
I have method deletePost in my PostService. I don't know what is wrong, i have service and deletePost method, service is inject to...
Read more >
EventSubscriber example | Simple FB Connect 8.x - Drupal
In this example we inject class SimpleFbConnectFbFactory to our new custom module. This is explained in detail later in this article. mymodule/ ...
Read more >
Drupal 8: Hooks, Events, and Event Subscribers
Event Subscribers – Drupal hooks are registered in the system by defining a ... services: # Subscriber to the config events, with dependencies...
Read more >
Events and Event Listeners (Symfony Docs)
To learn more about event subscribers, read The EventDispatcher Component. ... When configuring event listeners and subscribers via dependency injection, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found