diff options
| -rw-r--r-- | src/index.ts | 20 | ||||
| -rw-r--r-- | test/index_test.ts | 16 |
2 files changed, 16 insertions, 20 deletions
diff --git a/src/index.ts b/src/index.ts index 7301520..17672aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,7 +10,9 @@ export type WildcardHandler<T = Record<string, unknown>> = ( // An array of all currently registered event handlers for a type export type EventHandlerList<T = unknown> = Array<Handler<T>>; -export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>; +export type WildCardEventHandlerList<T = Record<string, unknown>> = Array< + WildcardHandler<T> +>; // A map of event types and their corresponding event handlers. export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map< @@ -24,11 +26,16 @@ export interface Emitter<Events extends Record<EventType, unknown>> { on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void; on(type: '*', handler: WildcardHandler<Events>): void; - off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void; + off<Key extends keyof Events>( + type: Key, + handler?: Handler<Events[Key]> + ): void; off(type: '*', handler: WildcardHandler<Events>): void; emit<Key extends keyof Events>(type: Key, event: Events[Key]): void; - emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void; + emit<Key extends keyof Events>( + type: undefined extends Events[Key] ? Key : never + ): void; } /** @@ -45,7 +52,6 @@ export default function mitt<Events extends Record<EventType, unknown>>( all = all || new Map(); return { - /** * A Map of event names to registered handler functions. */ @@ -61,8 +67,7 @@ export default function mitt<Events extends Record<EventType, unknown>>( const handlers: Array<GenericEventHandler> | undefined = all!.get(type); if (handlers) { handlers.push(handler); - } - else { + } else { all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>); } }, @@ -79,8 +84,7 @@ export default function mitt<Events extends Record<EventType, unknown>>( if (handlers) { if (handler) { handlers.splice(handlers.indexOf(handler) >>> 0, 1); - } - else { + } else { all!.set(type, []); } } diff --git a/test/index_test.ts b/test/index_test.ts index 723dfa6..31c9495 100644 --- a/test/index_test.ts +++ b/test/index_test.ts @@ -44,17 +44,13 @@ describe('mitt#', () => { describe('properties', () => { it('should expose the event handler map', () => { - expect(inst) - .to.have.property('all') - .that.is.a('map'); + expect(inst).to.have.property('all').that.is.a('map'); }); }); describe('on()', () => { it('should be a function', () => { - expect(inst) - .to.have.property('on') - .that.is.a('function'); + expect(inst).to.have.property('on').that.is.a('function'); }); it('should register handler for new type', () => { @@ -111,9 +107,7 @@ describe('mitt#', () => { describe('off()', () => { it('should be a function', () => { - expect(inst) - .to.have.property('off') - .that.is.a('function'); + expect(inst).to.have.property('off').that.is.a('function'); }); it('should remove handler for type', () => { @@ -165,9 +159,7 @@ describe('mitt#', () => { describe('emit()', () => { it('should be a function', () => { - expect(inst) - .to.have.property('emit') - .that.is.a('function'); + expect(inst).to.have.property('emit').that.is.a('function'); }); it('should invoke handler for type', () => { |
