From 5a9bc71f2e4e2aa49fa6b5b29b030d07e37f9af0 Mon Sep 17 00:00:00 2001 From: Felix Wotschofsky Date: Mon, 19 Dec 2022 17:27:55 +0100 Subject: [PATCH] =?UTF-8?q?Extend=20type-safety=20to=20event=20callbacks?= =?UTF-8?q?=20=F0=9F=92=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/ConsentManager.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/modules/ConsentManager.ts b/src/modules/ConsentManager.ts index 64e2cab..c748ce7 100644 --- a/src/modules/ConsentManager.ts +++ b/src/modules/ConsentManager.ts @@ -49,7 +49,7 @@ interface CookieData { type EventNames = 'update' | 'grant' | 'revoke'; -type UpdateEventCallback = (id: string) => void; +type UpdateEventCallback = (id: G) => void; export interface ConsentManagerConfig { version: string; @@ -75,7 +75,7 @@ export default class ConsentManager { public config: ConsentManagerConfig; public isCustomized = false; public grants: GrantsStatus; - private eventListeners: Record = {}; + private eventListeners: Record[]> = {}; constructor(config: ConsentManagerConfig) { this.config = merge(defaultConfig, config); @@ -127,12 +127,12 @@ export default class ConsentManager { }); } - public setGrant(id: string, status: boolean): void { + public setGrant(id: G, status: boolean): void { // Set all grants if (id === '*') { // Call recursively for all categories for (const key of Object.keys(this.grants)) { - this.setGrant(key, status); + this.setGrant(key as G, status); } return; } @@ -161,7 +161,7 @@ export default class ConsentManager { this.writeCookie(); } - public on(eventName: EventNames, callback: UpdateEventCallback): void { + public on(eventName: EventNames, callback: UpdateEventCallback): void { // Add empty array for event listeners if missing if (!(eventName in this.eventListeners)) { this.eventListeners[eventName] = []; @@ -170,7 +170,7 @@ export default class ConsentManager { this.eventListeners[eventName].push(callback); } - public off(eventName: EventNames, callback: UpdateEventCallback): void { + public off(eventName: EventNames, callback: UpdateEventCallback): void { // Ignore if no listeners are registered for event if ( !(eventName in this.eventListeners) || @@ -184,7 +184,7 @@ export default class ConsentManager { this.eventListeners[eventName].splice(callbackIndex, 1); } - private dispatch(eventName: EventNames, grant: string): void { + private dispatch(eventName: EventNames, grant: G): void { // Ignore if no listeners are registered for event if (!(eventName in this.eventListeners)) { return;