pencil.js
Version:
Nice modular interactive 2D drawing library.
80 lines (79 loc) • 2.69 kB
TypeScript
/**
* @module EventEmitter
*/
/**
* Able to listen and fire events
* @abstract
* @class
*/
export default class EventEmitter {
/**
* @typedef {Object} EventEmitterEvents
* @enum {String}
*/
/**
* @type {EventEmitterEvents}
*/
static get events(): any;
/**
* @typedef {Object} EventListener
* @prop {Function} callback - Function called when event is fired
* @prop {EventEmitter} element - Element on which to listen for the event
* @prop {Boolean} isTargeted - Will call the callback only when the event target is the element
* @prop {String} [modifier] - Additional data about the event
*/
/**
* Set of event listeners of this component
* @type {Object<string, Array<EventListener>>}
*/
eventListeners: {
[x: string]: Array<EventListener>;
};
/**
* Listen to an event or multiple events
* @param {String|Array<String>} eventName - Name of event (or a list of event names) to listen to.<br>
* The event name can be followed by a modifier (separated by a ".")
* @param {Function} callback - Function to call when event fire
* @param {Boolean} [isTargeted=false] - Should only listen to event targeting itself
* @return {EventEmitter} Itself
* @example component.on("event.modifier", () => console.log("Event fired with modifier"));
*/
on(eventName: string | Array<string>, callback: Function, isTargeted?: boolean): EventEmitter;
/**
* Trigger an event
* @param {BaseEvent} event - Event to trigger
* @return {EventEmitter} Itself
*/
fire(event: any): EventEmitter;
/**
* Remove an event or multiple events
* @param {String|Array<String>} eventName - Name of event (or a list of event names) to remove
* @param {Function} [callback] - Specify the callback to remove, if not defined remove all listeners
* @return {EventEmitter} Itself
*/
removeListener(eventName: string | Array<string>, callback?: Function): EventEmitter;
/**
* Remove all event listener
* @return {EventEmitter} Itself
*/
removeAllListener(): EventEmitter;
}
export type EventListener = {
/**
* - Function called when event is fired
*/
callback: Function;
/**
* - Element on which to listen for the event
*/
element: EventEmitter;
/**
* - Will call the callback only when the event target is the element
*/
isTargeted: boolean;
/**
* - Additional data about the event
*/
modifier?: string;
};
export type EventEmitterEvents = any;