Skip to content
Myr

index.js

This file

Path

src/index.js

51 lines · 1.5 KiB · JavaScript

Last changed by

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

History of this file

Take it

Initial commit! 🚀Jason Millerbefore adoptione77a42c

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

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

lines 2–3why, one step back
2 * @name mitt
3 * @returns {Mitt}

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 4
4 */

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

lines 5–6why, one step back
5export default function mitt(all) {
6 all = all || Object.create(null);

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 7
7

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

line 8why, one step back
8 return {

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 9–17why, one step back
9 /**
10 * Register an event handler for the given type.11 *12 * @param {String} type Type of event to listen for, or `"*"` for all events13 * @param {Function} handler Function to call in response to given event14 * @return {Object} the `mitt` instance for chaining15 * @memberOf mitt16 */17 on(type, handler) {

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 19–20why, one step back
19 return ret;
20 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 21
21

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 22–30why, one step back
22 /**
23 * Remove an event handler for the given type.24 *25 * @param {String} type Type of event to unregister `handler` from, or `"*"`26 * @param {Function} handler Handler function to remove27 * @return {Object} the `mitt` instance for chaining28 * @memberOf mitt29 */30 off(type, handler) {

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

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

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 33–34why, one step back
33 return ret;
34 },

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 35
35

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 36–40why, one step back
36 /**
37 * Invoke all handlers for the given type.38 * If present, `"*"` handlers are invoked prior to type-matched handlers.39 *40 * @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 41why, one step back
41 * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 42–44why, one step back
42 * @return {Object} the `mitt` instance for chaining
43 * @memberof mitt44 */

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

lines 45–47why, one step back
45 emit(type, evt) {
46 (all[type] || []).map((handler) => { handler(evt); });47 (all['*'] || []).map((handler) => { handler(type, evt); });

add editorconfig, fix original identationtunnckoCorebefore adoptione7c222c

lines 48–50why, one step back
48 return ret;
49 }50 };

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 51
51}