index.js
This file
Path
src/index.js
56 lines · 1.7 KiB · JavaScript
Last changed by
Removed outdated (untrue) API docs - chaining was removed in 53a44d8 (#44)Mateusz Burzyńskibefore adoption09a9df0
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657// @flow// An event handler can take an optional event argument// and should not return a valuetype EventHandler = (event?: any) => void;// An array of all currently registered event handlers for a typetype EventHandlerList = Array<EventHandler>;// A map of event types and their corresponding event handlers.type EventHandlerMap = { [type: string]: EventHandlerList,};
/** Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} */export default function mitt(all: EventHandlerMap) { all = all || Object.create(null);
return { /** * Register an event handler for the given type. * * @param {String} 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: string, handler: EventHandler) { (all[type] || (all[type] = [])).push(handler); },
/** * Remove an event handler for the given type. * * @param {String} type Type of event to unregister `handler` from, or `"*"` * @param {Function} handler Handler function to remove * @memberOf mitt */ off(type: string, handler: EventHandler) { let e = all[type] || (all[type] = []); e.splice(e.indexOf(handler) >>> 0, 1); },
/** * Invoke all handlers for the given type. * If present, `"*"` handlers are invoked prior to type-matched handlers. * * @param {String} type The event type to invoke * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberof mitt */ emit(type: string, evt: EventHandler) { (all[type] || []).map((handler) => { handler(evt); }); (all['*'] || []).map((handler) => { handler(type, evt); }); } };}