index.js
This file
Path
src/index.js
61 lines · 1.9 KiB · JavaScript
Last changed by
Fixing API documentation header (#62)Brian Wendtbefore adoptione911aac
History of this fileTake it
Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
lines 1–3why, one step back1// @flow2// An event handler can take an optional event argument3// and should not return a valuemore precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958
lines 4–6why, one step back4type EventHandler = (event?: any) => void;5type WildCardEventHandler = (type: string, event?: any) => void6Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
lines 7–8why, one step back7// An array of all currently registered event handlers for a type8type EventHandlerList = Array<EventHandler>;more precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958
line 9why, one step back9type WildCardEventHandlerList = Array<WildCardEventHandler>;Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
lines 10–11why, one step back10// A map of event types and their corresponding event handlers.11type EventHandlerMap = {more precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958
line 12why, one step back12 '*'?: WildCardEventHandlerList,Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
lines 13–15why, one step back13 [type: string]: EventHandlerList,14};1516/** Mitt: Tiny (~200b) functional event emitter / pubsub.refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 17–18why, one step back17 * @name mitt18 * @returns {Mitt}19 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 20why, one step back20export default function mitt(all: EventHandlerMap) {Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
line 21why, one step back21 all = all || Object.create(null);22Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0
line 23why, one step back23 return {add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 24–26why, one step back24 /**25 * Register an event handler for the given type.26 *Fix eslint complaintsJason Millerbefore adoption9eced2d
line 27why, one step back27 * @param {String} type Type of event to listen for, or `"*"` for all eventsadd editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 28–30why, one step back28 * @param {Function} handler Function to call in response to given event29 * @memberOf mitt30 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 31why, one step back31 on(type: string, handler: EventHandler) {fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b
line 32why, one step back32 (all[type] || (all[type] = [])).push(handler);add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 33why, one step back33 },34add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 35–37why, one step back35 /**36 * Remove an event handler for the given type.37 *Fix eslint complaintsJason Millerbefore adoption9eced2d
line 38why, one step back38 * @param {String} type Type of event to unregister `handler` from, or `"*"`add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 39–41why, one step back39 * @param {Function} handler Handler function to remove40 * @memberOf mitt41 */Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4
line 42why, one step back42 off(type: string, handler: EventHandler) {Avoid creating a new listener type array in `off()`, saves another 4 bytes (187b -> 183b)Jason Millerbefore adoptionaa9ae24
lines 43–45why, one step back43 if (all[type]) {44 all[type].splice(all[type].indexOf(handler) >>> 0, 1);45 }add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 46why, one step back46 },47add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 48–49why, one step back48 /**49 * Invoke all handlers for the given type.Change description for emit (fix #49) (#50)Denis Sikulerbefore adoptioncf764a1
line 50why, one step back50 * If present, `"*"` handlers are invoked after type-matched handlers.add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 51–52why, one step back51 *52 * @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 53why, one step back53 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handlerFixing API documentation header (#62)Brian Wendtbefore adoptione911aac
line 54why, one step back54 * @memberOf mittadd editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
line 55why, one step back55 */more precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958
line 56why, one step back56 emit(type: string, evt: any) {remove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4
lines 57–58why, one step back57 (all[type] || []).map((handler) => { handler(evt); });58 (all['*'] || []).map((handler) => { handler(type, evt); });add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c
lines 59–60why, one step back59 }60 };61}