index.js
This file
Path
src/index.js
51 lines · 1.5 KiB · JavaScript
Last changed by
Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
History of this fileTake it
1/** Mitt: Tiny (~200b) functional event emitter / pubsub.refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 2–3why, one step back2 * @name mitt3 * @returns {Mitt}4 */Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
lines 5–6why, one step back5export default function mitt(all) {6 all = all || Object.create(null);7Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
line 8why, one step back8 return {add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 9–17why, one step back9 /**10 * Register an event handler for the given type.11 *12 * @param {String} type Type of event to listen for, or `"*"` for all events13 * @param {Function} handler Function to call in response to given event14 * @return {Object} the `mitt` instance for chaining15 * @memberOf mitt16 */17 on(type, handler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
line 18why, one step back18 (all[type] || (all[type] = [])).push(handler);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 19–20why, one step back19 return ret;20 },21add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 22–30why, one step back22 /**23 * Remove an event handler for the given type.24 *25 * @param {String} type Type of event to unregister `handler` from, or `"*"`26 * @param {Function} handler Handler function to remove27 * @return {Object} the `mitt` instance for chaining28 * @memberOf mitt29 */30 off(type, handler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
lines 31–32why, one step back31 let e = all[type] || (all[type] = []);32 e.splice(e.indexOf(handler) >>> 0, 1);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 33–34why, one step back33 return ret;34 },35add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 36–40why, one step back36 /**37 * Invoke all handlers for the given type.38 * If present, `"*"` handlers are invoked prior to type-matched handlers.39 *40 * @param {String} type The event type to invokeremove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4
line 41why, one step back41 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handleradd editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 42–44why, one step back42 * @return {Object} the `mitt` instance for chaining43 * @memberof mitt44 */remove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4
lines 45–47why, one step back45 emit(type, evt) {46 (all[type] || []).map((handler) => { handler(evt); });47 (all['*'] || []).map((handler) => { handler(type, evt); });add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 48–50why, one step back48 return ret;49 }50 };51}