index.js
This file
Path
src/index.js
48 lines · 1.4 KiB · JavaScript
Last changed by
Remove chaining return values so we don't make #1 impossible (yet?)Jason Millerbefore adoption53a44d8
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
line 19why, one step back19 },20add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 21–29why, one step back21 /**22 * Remove an event handler for the given type.23 *24 * @param {String} type Type of event to unregister `handler` from, or `"*"`25 * @param {Function} handler Handler function to remove26 * @return {Object} the `mitt` instance for chaining27 * @memberOf mitt28 */29 off(type, handler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
lines 30–31why, one step back30 let e = all[type] || (all[type] = []);31 e.splice(e.indexOf(handler) >>> 0, 1);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 32why, one step back32 },33add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 34–38why, one step back34 /**35 * Invoke all handlers for the given type.36 * If present, `"*"` handlers are invoked prior to type-matched handlers.37 *38 * @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 39why, one step back39 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handleradd editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 40–42why, one step back40 * @return {Object} the `mitt` instance for chaining41 * @memberof mitt42 */remove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4
lines 43–45why, one step back43 emit(type, evt) {44 (all[type] || []).map((handler) => { handler(evt); });45 (all['*'] || []).map((handler) => { handler(type, evt); });add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 46–47why, one step back46 }47 };48}