aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrandon Dail <[email protected]>2017-02-27 20:18:26 -0600
committerJason Miller <[email protected]>2017-02-27 21:18:26 -0500
commit377d6f470289cfdbac3fd4edb91d8006d9cf8c8c (patch)
tree082f409f66a05b03775438def5f059fd6d17efa0 /src
parentf147246b7528904ec28702f43e11bd8ba946d0a6 (diff)
Add Flow type annotations (#23)
* Add flowtype annotations * Add flow to test script
Diffstat (limited to 'src')
-rw-r--r--src/index.js19
1 files changed, 15 insertions, 4 deletions
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<EventHandler>;
+// 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); });
}