Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

EventKey should be also enumerable #133

Closed
wartoshika opened this issue Jul 5, 2017 · 4 comments
Closed

EventKey should be also enumerable #133

wartoshika opened this issue Jul 5, 2017 · 4 comments

Comments

@wartoshika
Copy link

wartoshika commented Jul 5, 2017

My current Issue is, that the declared type EventKey is from string | RegExp. I cannot declare an event enumeration in my project to have a better overview of my existing events.

My first Idea ist to update the EventEmitter as generic class to pass the possible type like this:

export class EventEmitter<EventType = EventKey> { ... on(event: EventType, listener: Listener|Function): this; ... }

What do you think about that?

@wartoshika
Copy link
Author

For anyone interested in a workarround: This is my temp solution.

import BaseEventEmitter = require('wolfy87-eventemitter');

import { EventName } from './EventName';

/**
 * a class that can emit events and can add listeners to events
 */
export abstract class EventEmitter extends BaseEventEmitter {

    /**
     * listen on the given event
     *
     * @param event the desired event
     * @param listener the listener function
     */
    public onEvent(event: EventName, listener: Function): this {

        return super.on(<any>event, listener);
    }

    /**
     * emit an event
     *
     * @param event the event name
     */
    public fireEvent(event: EventName): this {

        return super.emit(<any>event);
    }
}

Extend this class and you can fire your own event as enumeration defined by enum EventName

@Olical
Copy link
Owner

Olical commented Jul 5, 2017

I have never used TypeScript, so I'm really not sure about this. I let the .ts definitions into the repository since it was backwards compatible and could help some people.

If you can alter this to what you need in a backwards compatible way, I would consider a PR. If not, maybe the extension of the class is fine?

@wartoshika
Copy link
Author

Thanks for replying. I will go with the workarround until i have time create a pull request.
Issue can be closed.

@wartoshika
Copy link
Author

Created a PR #134

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

No branches or pull requests

2 participants