index.js
This file
Path
src/index.js
59 lines · 1.8 KiB · JavaScript
Last changed by
refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960/** Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} */export default function mitt () { let ret = { all: Object.create(null),
/** * 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 * @return {Object} the `mitt` instance for chaining * @memberOf mitt */ on(type, handler) { list(type).add(handler); return ret; },
/** * 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 * @return {Object} the `mitt` instance for chaining * @memberOf mitt */ off(type, handler) { list(type).delete(handler); return ret; },
/** * 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} [arg1] A value (first argument), passed to each handler * @param {Any} [arg2] A value (second argument), passed to each handler * @param {Any} [arg3] A value (third argument), passed to each handler * @return {Object} the `mitt` instance for chaining * @memberof mitt */ emit(type, arg1, arg2, arg3) { list(type).forEach((handler) => handler(arg1, arg2, arg3)); list('*').forEach((handler) => handler(type, arg1, arg2, arg3)); return ret; } };
// Get or create a named handler list let list = (type) => { return ret.all[type = type.toLowerCase()] || (ret.all[type] = new Set()); };
return ret;}