Skip to content
Open
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
44 changes: 27 additions & 17 deletions types/minievents.d.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
/**
* Add support of events to class.
* Interface for objects with event handling capabilities
*/
export class Events {
interface Events {
/**
* Listen to events.
*
* @param type
* @param callback
* @param context
* @param type Event type
* @param func Callback function
* @param ctx Context for the callback
*/
on(type: string, callback: Function, context: any[])
on(type: string, func: Function, ctx?: any): this;

/**
* Stop listening to events or specific callback.
* Stop listening to event / specific callback.
*
* @param type
* @param callback
* @param type Event type (optional - if not provided, removes all events)
* @param func Specific callback to remove (optional)
*/
off(type?: string, callback?: Function)
off(type?: string, func?: Function): this;

/**
* Send event. Callbacks will be triggered.
* Send event, callbacks will be triggered.
*
* @param type
* @param type Event type
* @param args Additional arguments to pass to callbacks
*/
emit(type: string)
emit(type: string, ...args: any[]): this;
}

/**
* Add support of events to object.
*
* @param object
* Events can be used both as a function and as a constructor.
* When used as a function, it mixes event handling capabilities into the provided target.
* When used as a constructor, it creates a new event emitter.
*
* @param target Object to add event handling to (optional - defaults to this)
*/
export function Events<T>(object: T): T & Events;
interface EventsConstructor {
<T extends object = {}>(target?: T): T & Events;
new (): Events;
}

declare const Events: EventsConstructor;

export = Events;