diff options
| author | Tung Vu <[email protected]> | 2017-07-06 06:47:42 +0700 |
|---|---|---|
| committer | Jason Miller <[email protected]> | 2017-07-05 19:47:42 -0400 |
| commit | 4517958ad531ad919ccf531aa7287a785ff07e2d (patch) | |
| tree | 058dc0f5d556127b848f8430aa5d1dcea7047186 /src | |
| parent | 10711ebc0e9cb0b6f9b21b5f016da0966fed7d9b (diff) | |
more precise type annotation for WildCardEventHandler (#58)
* more precise type annotation for WildCardEventHandler
* fix: remove duplicated @flow
* fix: make * optional
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.js | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/index.js b/src/index.js index f568d90..62e8027 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,15 @@ // @flow // An event handler can take an optional event argument // and should not return a value -type EventHandler = (typeOrEvent?: string|any, event?: any) => void; +type EventHandler = (event?: any) => void; +type WildCardEventHandler = (type: string, event?: any) => void + // An array of all currently registered event handlers for a type type EventHandlerList = Array<EventHandler>; +type WildCardEventHandlerList = Array<WildCardEventHandler>; // A map of event types and their corresponding event handlers. type EventHandlerMap = { + '*'?: WildCardEventHandlerList, [type: string]: EventHandlerList, }; @@ -49,7 +53,7 @@ export default function mitt(all: EventHandlerMap) { * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberof mitt */ - emit(type: string, evt?: any) { + emit(type: string, evt: any) { (all[type] || []).map((handler) => { handler(evt); }); (all['*'] || []).map((handler) => { handler(type, evt); }); } |
