Skip to content
Myr

index.js

This file

Path

src/index.js

63 lines · 1.9 KiB · JavaScript

Last changed by

Add note about manually firing "*" handlersJason Millerbefore adoption32145f3

History of this file

Take it

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

lines 1–3why, one step back
1// @flow
2// An event handler can take an optional event argument3// and should not return a value

more precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958

lines 4–6why, one step back
4type EventHandler = (event?: any) => void;
5type WildCardEventHandler = (type: string, event?: any) => void6

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

lines 7–8why, one step back
7// An array of all currently registered event handlers for a type
8type EventHandlerList = Array<EventHandler>;

more precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958

line 9why, one step back
9type WildCardEventHandlerList = Array<WildCardEventHandler>;

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

lines 10–11why, one step back
10// 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 back
12 '*'?: WildCardEventHandlerList,

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

lines 13–15why, one step back
13 [type: string]: EventHandlerList,
14};15

Initial commit! 🚀Jason Millerbefore adoptione77a42c

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

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

lines 17–18why, one step back
17 * @name mitt
18 * @returns {Mitt}

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 19
19 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 22
22

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

line 23why, one step back
23 return {

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 24–26why, one step back
24 /**
25 * Register an event handler for the given type.26 *

Fix eslint complaintsJason Millerbefore adoption9eced2d

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 28–30why, one step back
28 * @param {Function} handler Function to call in response to given event
29 * @memberOf mitt30 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

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

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 33why, one step back
33 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 34
34

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 35–37why, one step back
35 /**
36 * Remove an event handler for the given type.37 *

Fix eslint complaintsJason Millerbefore adoption9eced2d

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 39–41why, one step back
39 * @param {Function} handler Handler function to remove
40 * @memberOf mitt41 */

Add Flow type annotations (#23)Brandon Dailbefore adoption377d6f4

line 42why, one step back
42 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 back
43 if (all[type]) {
44 all[type].splice(all[type].indexOf(handler) >>> 0, 1);45 }

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 46why, one step back
46 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 47
47

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

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

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 51why, one step back
51 *

Add note about manually firing "*" handlersJason Millerbefore adoption32145f3

lines 52–53why, one step back
52 * Note: Manually firing "*" handlers is not supported.
53 *

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

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

Fixing API documentation header (#62)Brian Wendtbefore adoptione911aac

line 56why, one step back
56 * @memberOf mitt

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

line 57why, one step back
57 */

more precise type annotation for WildCardEventHandler (#58)Tung Vubefore adoption4517958

line 58why, one step back
58 emit(type: string, evt: any) {

Fixes #65 - handler removal during handler invocation (`.off()` during `.emit()`).Jason Millerbefore adoptionf38922a

lines 59–60why, one step back
59 (all[type] || []).slice().map((handler) => { handler(evt); });
60 (all['*'] || []).slice().map((handler) => { handler(type, evt); });

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 61–62why, one step back
61 }
62 };

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 63
63}