Skip to content
Myr

index.js

This file

Path

src/index.js

57 lines · 1.7 KiB · JavaScript

Last changed by

Avoid creating a new listener type array in `off()`, saves another 4 bytes (187b -> 183b)Jason Millerbefore adoptionaa9ae24

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–22why, one step back
20 /**
21 * Register an event handler for the given type.22 *

Fix eslint complaintsJason Millerbefore adoption9eced2d

line 23why, one step back
23 * @param {String} type Type of event to listen for, or `"*"` for all events

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 24–26why, one step back
24 * @param {Function} handler Function to call in response to given event
25 * @memberOf mitt26 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

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

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 29why, one step back
29 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 30
30

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 31–33why, one step back
31 /**
32 * Remove an event handler for the given type.33 *

Fix eslint complaintsJason Millerbefore adoption9eced2d

line 34why, one step back
34 * @param {String} type Type of event to unregister `handler` from, or `"*"`

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 35–37why, one step back
35 * @param {Function} handler Handler function to remove
36 * @memberOf mitt37 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

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

Avoid creating a new listener type array in `off()`, saves another 4 bytes (187b -> 183b)Jason Millerbefore adoptionaa9ae24

lines 39–41why, one step back
39 if (all[type]) {
40 all[type].splice(all[type].indexOf(handler) >>> 0, 1);41 }

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 42why, one step back
42 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 43
43

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 44–45why, one step back
44 /**
45 * Invoke all handlers for the given type.

Change description for emit (fix #49) (#50)Denis Sikulerbefore adoptioncf764a1

line 46why, one step back
46 * If present, `"*"` handlers are invoked after type-matched handlers.

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 47–48why, one step back
47 *
48 * @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 49why, one step back
49 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 50–51why, one step back
50 * @memberof mitt
51 */

Fix `emit` argument annotation (#45)Rūdolfs Vikmanisbefore adoptionc595dfb

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 55–56why, one step back
55 }
56 };

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 57
57}