From 377d6f470289cfdbac3fd4edb91d8006d9cf8c8c Mon Sep 17 00:00:00 2001 From: Brandon Dail Date: Mon, 27 Feb 2017 20:18:26 -0600 Subject: Add Flow type annotations (#23) * Add flowtype annotations * Add flow to test script --- src/index.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/index.js b/src/index.js index ae6bd63..92b19f9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,19 @@ +// @flow +// An event handler can take an optional event argument +// and should not return a value +type EventHandler = (event?: any) => void; +// An array of all currently registered event handlers for a type +type EventHandlerList = Array; +// A map of event types and their corresponding event handlers. +type EventHandlerMap = { + [type: string]: EventHandlerList, +}; + /** Mitt: Tiny (~200b) functional event emitter / pubsub. * @name mitt * @returns {Mitt} */ -export default function mitt(all) { +export default function mitt(all: EventHandlerMap) { all = all || Object.create(null); return { @@ -14,7 +25,7 @@ export default function mitt(all) { * @return {Object} the `mitt` instance for chaining * @memberOf mitt */ - on(type, handler) { + on(type: string, handler: EventHandler) { (all[type] || (all[type] = [])).push(handler); }, @@ -26,7 +37,7 @@ export default function mitt(all) { * @return {Object} the `mitt` instance for chaining * @memberOf mitt */ - off(type, handler) { + off(type: string, handler: EventHandler) { let e = all[type] || (all[type] = []); e.splice(e.indexOf(handler) >>> 0, 1); }, @@ -40,7 +51,7 @@ export default function mitt(all) { * @return {Object} the `mitt` instance for chaining * @memberof mitt */ - emit(type, evt) { + emit(type: string, evt: EventHandler) { (all[type] || []).map((handler) => { handler(evt); }); (all['*'] || []).map((handler) => { handler(type, evt); }); } -- cgit v1.2.3