diff options
| author | Jack Franklin <[email protected]> | 2020-07-15 15:31:43 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-07-15 10:31:43 -0400 |
| commit | 5116df46a020ae498eec6783cfe9147fc9add015 (patch) | |
| tree | 93be70d93df33d1d60c7c3e4cfed0ae408e272cb /src | |
| parent | 244cac2aa3d6831774129d6dc4942465dcef8099 (diff) | |
Add generic types and update tests (#107)
* Add generic types and update tests
* Add generic types to `on`, `off` and `emit` to enable some nicer TS
usage if you specify what type you're expecting from the `EventData`.
* Move the tests to be TypeScript source. This will help catch errors in
the tests if there are any type errors.
* Create a new test to test the generic types and make sure they pass
and error when expected.
* Upgrade to Mocha 8.
* Did some tidying up of the package.json scripts.
* Tweak TS setup to validate tests
* Fix d.ts generation and tests
Co-authored-by: Jason Miller <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.ts | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/index.ts b/src/index.ts index e640292..c681fd0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,8 @@ export type EventType = string | symbol; // An event handler can take an optional event argument // and should not return a value -export type Handler = (event?: any) => void; -export type WildcardHandler = (type: EventType, event?: any) => void +export type Handler<T = any> = (event?: T) => void; +export type WildcardHandler = (type: EventType, event?: any) => void; // An array of all currently registered event handlers for a type export type EventHandlerList = Array<Handler>; @@ -13,10 +13,10 @@ export type WildCardEventHandlerList = Array<WildcardHandler>; export type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>; export interface Emitter { - on(type: EventType, handler: Handler): void; + on<T = any>(type: EventType, handler: Handler<T>): void; on(type: '*', handler: WildcardHandler): void; - off(type: EventType, handler: Handler): void; + off<T = any>(type: EventType, handler: Handler<T>): void; off(type: '*', handler: WildcardHandler): void; emit<T = any>(type: EventType, event?: T): void; @@ -38,7 +38,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { * @param {Function} handler Function to call in response to given event * @memberOf mitt */ - on(type: EventType, handler: Handler) { + on<T = any>(type: EventType, handler: Handler<T>) { const handlers = all.get(type); const added = handlers && handlers.push(handler); if (!added) { @@ -53,7 +53,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { * @param {Function} handler Handler function to remove * @memberOf mitt */ - off(type: EventType, handler: Handler) { + off<T = any>(type: EventType, handler: Handler<T>) { const handlers = all.get(type); if (handlers) { handlers.splice(handlers.indexOf(handler) >>> 0, 1); @@ -70,7 +70,7 @@ export default function mitt(all?: EventHandlerMap): Emitter { * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler * @memberOf mitt */ - emit(type: EventType, evt: any) { + emit<T = any>(type: EventType, evt: T) { ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); }); ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); }); } |
