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

added generic for EventEmitter to allow Custom EventKeys like an enum… #134

Merged
merged 5 commits into from
Jul 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions EventEmitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ export = EventEmitter.EventEmitter;
export as namespace EventEmitter;

declare namespace EventEmitter {
type EventKey = string|RegExp;
type EventKey = string | RegExp;

interface EventMap {
[event: string]: EventKey;
}

type Events = EventKey|EventMap;
type Events = EventKey | EventMap;

interface Listener {
listener: Function;
Expand All @@ -19,30 +19,30 @@ declare namespace EventEmitter {
[event: string]: Listener[];
}

type Listeners = Listener[]|ListenerMap;
type Listeners = Listener[] | ListenerMap;

export class EventEmitter {
export class EventEmitter<E = EventKey> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So <E = EventKey> turns EventEmitter into a generic class with a type variable that defaults to EventKey if not provided? (I don't know TS, so I'm speculating! 😄)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is ture. If no generic type if given, EventKey is used. But is there is any given, E will turn to need the given value. Typescript genric works like any other generic languages.

I' ll test providing no value to the generic type resulting is need a type of KeyEventand providing eg. an enum type will result in requireing a typeof of enum given to the generic pattern.

You have to know that this default type KeyEvent will only allow a backwards compatiblity at typescript >= 2.3. but typescript ^2.4 is at current stable.

I hope this meight help

static noConflict(): typeof EventEmitter;

getListeners(event: EventKey): Listeners;
getListeners(event: E): Listeners;
flattenListeners(listeners: Listener[]): void;
getListenersAsObject(event: EventKey): ListenerMap;
addListener(event: EventKey, listener: Listener|Function): this;
on(event: EventKey, listener: Listener|Function): this;
addOnceListener(event: EventKey, listener: Function): this;
once(event: EventKey, listener: Function): this;
defineEvent(event: EventKey): this;
defineEvents(events: EventKey[]): this;
removeListener(event: EventKey, listener: Function): this;
off(event: EventKey, listener: Function): this;
getListenersAsObject(event: E): ListenerMap;
addListener(event: E, listener: Listener | Function): this;
on(event: E, listener: Listener | Function): this;
addOnceListener(event: E, listener: Function): this;
once(event: E, listener: Function): this;
defineEvent(event: E): this;
defineEvents(events: E[]): this;
removeListener(event: E, listener: Function): this;
off(event: E, listener: Function): this;
addListeners(event: Events, listeners: Function[]): this;
removeListeners(event: Events, listeners: Function[]): this;
manipulateListeners(remove: boolean, event: Events, listeners: Function[]): this;
removeEvent(event?: EventKey): this;
removeAllListeners(event?: EventKey): this;
emitEvent(event: EventKey, args?: any[]): this;
trigger(event: EventKey, args?: any[]): this;
emit(event: EventKey, ...args: any[]): this;
removeEvent(event?: E): this;
removeAllListeners(event?: E): this;
emitEvent(event: E, args?: any[]): this;
trigger(event: E, args?: any[]): this;
emit(event: E, ...args: any[]): this;
setOnceReturnValue(value: any): this;
}
}