/** Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
export default function mitt(all: EventHandlerMap): Emitter {
export default function mitt(all?: EventHandlerMap): Emitter {
all = all || new Map();
return {
/**
* Register an event handler for the given type.
*
* @param {string|symbol} type Type of event to listen for, or `"*"` for all events
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
on(type: EventType, handler: Handler) {
const handlers = (all.get(type) || []);
handlers.push(handler);
all.set(type, handlers);
const handlers = all.get(type);
const added = handlers && handlers.push(handler);
if (!added) {
all.set(type, [handler]);
}
},
/**
* Remove an event handler for the given type.
* @param {Function} handler Handler function to remove
* @memberOf mitt
*/
off(type: EventType, handler: Handler) {
if (all.has(type)) {
all.get(type).splice(all.get(type).indexOf(handler) >>> 0, 1);
const handlers = all.get(type);
if (handlers) {
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
},
/**