From bb4302ea0a3ca768bd25847b945400faf92b7dff Mon Sep 17 00:00:00 2001 From: Francois Best Date: Sun, 10 May 2020 17:03:17 +0200 Subject: [PATCH] fix: Event listener callback type Also use strong key types everywhere --- src/index.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index b520923..415d26c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,21 +27,23 @@ const convertV0toV1 = (v0Entry: ExpirableKeyV0): ExpirableKeyV1 => ({ // -- -export interface KeyEvent { - name: string +export interface KeyEvent { + name: Keys } -export interface EventMap { - created: KeyEvent - read: KeyEvent - updated: KeyEvent - deleted: KeyEvent - expired: KeyEvent +export interface EventMap { + created: KeyEvent + read: KeyEvent + updated: KeyEvent + deleted: KeyEvent + expired: KeyEvent } -export type EventTypes = keyof EventMap -export type EventPayload = EventMap[T] -export type Callback = (value: T) => void +export type EventTypes = keyof EventMap +export type EventPayload> = EventMap[T] +export type Callback> = ( + value: EventPayload +) => void export interface ConstructorOptions { name?: string @@ -77,12 +79,12 @@ export default class SessionKeystore { // Event Emitter -- // Returns an unsubscribe callback - on(event: T, callback: Callback) { + on>(event: T, callback: Callback) { this.#emitter.on(event, callback) return () => this.#emitter.off(event, callback) } - off(event: T, callback: Callback) { + off>(event: T, callback: Callback) { this.#emitter.off(event, callback) }