Skip to content
Myr

index.js

This file

Path

src/index.js

59 lines · 1.9 KiB · JavaScript

Last changed by

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

History of this file

Take it

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

lines 1–11why, one step back
1// @flow
2// 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};11

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 12
12/** Mitt: Tiny (~200b) functional event emitter / pubsub.

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 13–14why, one step back
13 * @name mitt
14 * @returns {Mitt}

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 15
15 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

line 16why, one step back
16export 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 back
17 all = all || Object.create(null);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 18
18

Let's keep `all` as an argument for now to avoid a semver breaking changeJason Millerbefore adoption5363bf0

line 19why, one step back
19 return {

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 20–27why, one step back
20 /**
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 * @return {Object} the `mitt` instance for chaining26 * @memberOf mitt27 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

line 28why, one step back
28 on(type: string, handler: EventHandler) {

fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b

line 29why, one step back
29 (all[type] || (all[type] = [])).push(handler);

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 30why, one step back
30 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 31
31

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 32–39why, one step back
32 /**
33 * Remove an event handler for the given type.34 *35 * @param {String} type Type of event to unregister `handler` from, or `"*"`36 * @param {Function} handler Handler function to remove37 * @return {Object} the `mitt` instance for chaining38 * @memberOf mitt39 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

line 40why, one step back
40 off(type: string, handler: EventHandler) {

fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b

lines 41–42why, one step back
41 let e = all[type] || (all[type] = []);
42 e.splice(e.indexOf(handler) >>> 0, 1);

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 43why, one step back
43 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 44
44

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 45–49why, one step back
45 /**
46 * Invoke all handlers for the given type.47 * If present, `"*"` handlers are invoked prior to type-matched handlers.48 *49 * @param {String} type The event type to invoke

remove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4

line 50why, one step back
50 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 51–53why, one step back
51 * @return {Object} the `mitt` instance for chaining
52 * @memberof mitt53 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

line 54why, one step back
54 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 55–56why, one step back
55 (all[type] || []).map((handler) => { handler(evt); });
56 (all['*'] || []).map((handler) => { handler(type, evt); });

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 57–58why, one step back
57 }
58 };

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 59
59}