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
Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
lines 1–11why, one step back1// @flow2// An event handler can take an optional event argument3// and should not return a value4type EventHandler = (event?: any) => void;5// An array of all currently registered event handlers for a type6type EventHandlerList = Array<EventHandler>;7// A map of event types and their corresponding event handlers.8type EventHandlerMap = {9 [type: string]: EventHandlerList,10};1112/** Mitt: Tiny (~200b) functional event emitter / pubsub.refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 13–14why, one step back13 * @name mitt14 * @returns {Mitt}15 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 16why, one step back16export default function mitt(all: EventHandlerMap) {Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
line 17why, one step back17 all = all || Object.create(null);18Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
line 19why, one step back19 return {add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 20–26why, one step back20 /**21 * Register an event handler for the given type.22 *23 * @param {String} type Type of event to listen for, or `"*"` for all events24 * @param {Function} handler Function to call in response to given event25 * @memberOf mitt26 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 27why, one step back27 on(type: string, handler: EventHandler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
line 28why, one step back28 (all[type] || (all[type] = [])).push(handler);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 29why, one step back29 },30add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 31–37why, one step back31 /**32 * Remove an event handler for the given type.33 *34 * @param {String} type Type of event to unregister `handler` from, or `"*"`35 * @param {Function} handler Handler function to remove36 * @memberOf mitt37 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 38why, one step back38 off(type: string, handler: EventHandler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
lines 39–40why, one step back39 let e = all[type] || (all[type] = []);40 e.splice(e.indexOf(handler) >>> 0, 1);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 41why, one step back41 },42add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 43–47why, one step back43 /**44 * Invoke all handlers for the given type.45 * If present, `"*"` handlers are invoked prior to type-matched handlers.46 *47 * @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 48why, one step back48 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handleradd editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 49–50why, one step back49 * @memberof mitt50 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 51why, one step back51 emit(type: string, evt: EventHandler) {remove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4
lines 52–53why, one step back52 (all[type] || []).map((handler) => { handler(evt); });53 (all['*'] || []).map((handler) => { handler(type, evt); });add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 54–55why, one step back54 }55 };56}