Code Monkey home page Code Monkey logo

Comments (4)

kamilmysliwiec avatar kamilmysliwiec commented on May 8, 2024

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

image

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.

thunder-al avatar thunder-al commented on May 8, 2024

@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.

thalysonalexr avatar thalysonalexr commented on May 8, 2024

pls, any help to use this? @ThunderAl197

from event-emitter.

thunder-al avatar thunder-al commented on May 8, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.