Comments (4)
This isn't how arrays are supposed to work when passed into the @OnEvent
decorator.
As stated in the docs:
The first argument can be a string or symbol for a simple event emitter and a string | symbol | Array<string | symbol> in a case of a wildcard emitter
https://docs.nestjs.com/techniques/events#listening-to-events
See eventemitter2
docs https://www.npmjs.com/package/eventemitter2#static
Namespaces with Wildcards To use namespaces/wildcards, pass the wildcard option into the EventEmitter constructor. When namespaces/wildcards are enabled, events can either be strings (foo.bar) separated by a delimiter or arrays (['foo', 'bar']). The delimiter is also configurable as a constructor option.
An event name passed to any event emitter method can contain a wild card (the * character). If the event name is a string, a wildcard may appear as foo.*. If the event name is an array, the wildcard may appear as ['foo', '*'].
from event-emitter.
@kamilmysliwiec understood. I missed that feature.
But, how I can subscribe one class method to two or more different events?
If I place 2+ decorators, I recieve event only from first listed event.
Example:
class MyService {
@OnEvent('event.to.be.listened')
@OnEvent('event.which.not.be.listened')
private someMethod(data: SomeInterface) {
// ...
}
}
(yes, i can this.eventEmitter.on(...)
as many as i want in constructor etc, but it doesn't look right)
from event-emitter.
pls, any help to use this? @ThunderAl197
from event-emitter.
@thalysonalexr what did u mean?
i mean u can do something like this to subscribe more then one event to one method:
@Injectable()
export class SampleService implements OnModuleInit {
@Inject()
private readonly eventEmitter2!: EventEmitter2
public onModuleInit() {
this.eventEmitter2.on('some-event1', () => this.someMethod)
this.eventEmitter2.on('some-event2', () => this.someMethod)
}
protected someMethod() {
// do something
}
}
or u can subscribe to this events using decorators (remember, one @OnEvent
per one method):
@Injectable()
export class SampleService {
@OnEvent('some-event1')
protected onSomeEvent1() {
this.someMethod()
}
@OnEvent('some-event2')
protected onSomeEvent2() {
this.someMethod()
}
protected someMethod() {
// do something
}
}
(both examples works with this library without any modifications)
from event-emitter.
Related Issues (20)
- EventEmitterModule crash with TypeError from reflect-metadata stack when used with Proxy HOT 1
- Mitigating Event Loss Risk During NestJS Application Initialization HOT 2
- Events are not emitted when I use "onModuleInit" for testing purposes HOT 1
- Object null prototype error HOT 3
- Subscriber Trigger Multiple time on OnEvent Decorator HOT 1
- @OnEvent decorators is not working when applied multiple times on same method HOT 2
- Duplicate events when using useExisting providers HOT 3
- OnEvent decorator does not triggered if list of events provided HOT 4
- EventEmitterModuleOptions maxListeners setting isn't applied HOT 3
- Multiple event types not handled (impossible to) HOT 4
- Exceptions in async event handlers make the app crash HOT 1
- Custom event decorators break with the latest array metadata type requirement HOT 2
- Jest Unit tests only emit events when intializing full AppModule HOT 8
- Provider EventEmitter can't not resolve when using TerserPlugin webpack HOT 3
- Using event emitter in imported npm package cannot resolve emitter HOT 3
- async function constructor name not AsyncFunction HOT 1
- Upgrade to use event emitter 3, or add an option for it HOT 1
- Suppressing errors when updating from 2.0.0 to 2.0.1 HOT 1
- Option to throw error if an event fails to process HOT 3
- Add OnSafeEvent HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from event-emitter.