index.js
This file
Path
src/index.js
54 lines · 1.6 KiB · JavaScript
Last changed by
fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
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 */refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
line 5why, one step back5export default function mitt () {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
line 6why, one step back6 let all = Object.create(null)add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 7why, one step back7 let ret = {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
line 8why, one step back8 all,9add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 10–18why, one step back10 /**11 * Register an event handler for the given type.12 *13 * @param {String} type Type of event to listen for, or `"*"` for all events14 * @param {Function} handler Function to call in response to given event15 * @return {Object} the `mitt` instance for chaining16 * @memberOf mitt17 */18 on(type, handler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
line 19why, one step back19 (all[type] || (all[type] = [])).push(handler);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 20–21why, one step back20 return ret;21 },22add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 23–31why, one step back23 /**24 * Remove an event handler for the given type.25 *26 * @param {String} type Type of event to unregister `handler` from, or `"*"`27 * @param {Function} handler Handler function to remove28 * @return {Object} the `mitt` instance for chaining29 * @memberOf mitt30 */31 off(type, handler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
lines 32–33why, one step back32 let e = all[type] || (all[type] = []);33 e.splice(e.indexOf(handler) >>> 0, 1);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 34–35why, one step back34 return ret;35 },36add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 37–46why, one step back37 /**38 * Invoke all handlers for the given type.39 * If present, `"*"` handlers are invoked prior to type-matched handlers.40 *41 * @param {String} type The event type to invoke42 * @param {Any} [arg1] A value (first argument), passed to each handler43 * @param {Any} [arg2] A value (second argument), passed to each handler44 * @return {Object} the `mitt` instance for chaining45 * @memberof mitt46 */fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
lines 47–49why, one step back47 emit(type, arg1, arg2) {48 (all[type] || []).map((handler) => { handler(arg1, arg2); });49 (all['*'] || []).map((handler) => { handler(type, arg1, arg2); });add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 50–53why, one step back50 return ret;51 }52 };53 return ret;54}