File tree Expand file tree Collapse file tree 1 file changed +9
-7
lines changed
Expand file tree Collapse file tree 1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -27,21 +27,22 @@ export interface Emitter {
2727 * @name mitt
2828 * @returns {Mitt }
2929 */
30- export default function mitt ( all : EventHandlerMap ) : Emitter {
30+ export default function mitt ( all ? : EventHandlerMap ) : Emitter {
3131 all = all || new Map ( ) ;
3232
3333 return {
3434 /**
3535 * Register an event handler for the given type.
36- *
3736 * @param {string|symbol } type Type of event to listen for, or `"*"` for all events
3837 * @param {Function } handler Function to call in response to given event
3938 * @memberOf mitt
4039 */
4140 on ( type : EventType , handler : Handler ) {
42- const handlers = ( all . get ( type ) || [ ] ) ;
43- handlers . push ( handler ) ;
44- all . set ( type , handlers ) ;
41+ const handlers = all . get ( type ) ;
42+ const added = handlers && handlers . push ( handler ) ;
43+ if ( ! added ) {
44+ all . set ( type , [ handler ] ) ;
45+ }
4546 } ,
4647
4748 /**
@@ -52,8 +53,9 @@ export default function mitt(all: EventHandlerMap): Emitter {
5253 * @memberOf mitt
5354 */
5455 off ( type : EventType , handler : Handler ) {
55- if ( all . has ( type ) ) {
56- all . get ( type ) . splice ( all . get ( type ) . indexOf ( handler ) >>> 0 , 1 ) ;
56+ const handlers = all . get ( type ) ;
57+ if ( handlers ) {
58+ handlers . splice ( handlers . indexOf ( handler ) >>> 0 , 1 ) ;
5759 }
5860 } ,
5961
You can’t perform that action at this time.
0 commit comments