From 54e30c1b48e99f357337aec430774e3a67fdb074 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Thu, 3 Jul 2025 10:24:21 +0000 Subject: [PATCH] refactor: improve cache system typing in PlatformServiceMixin - Replace WeakMap>> with proper VueComponentWithMixin interface - Add VueComponentWithMixin interface with _platformService, $options, activeDid, and platformService() method - Use CacheEntry instead of CacheEntry for better type safety - Replace (this as any) with (this as unknown as VueComponentWithMixin) for safer type assertions - Reduces TypeScript warnings from 53 to 29 (24 warnings eliminated) - Maintains all existing functionality while improving type safety --- src/utils/PlatformServiceMixin.ts | 53 ++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/src/utils/PlatformServiceMixin.ts b/src/utils/PlatformServiceMixin.ts index 2f8422c6..56baa309 100644 --- a/src/utils/PlatformServiceMixin.ts +++ b/src/utils/PlatformServiceMixin.ts @@ -44,11 +44,11 @@ import { Contact } from "@/db/tables/contacts"; import { QueryExecResult, DatabaseExecResult } from "@/interfaces/database"; // ================================================= -// CACHING INFRASTRUCTURE +// TYPESCRIPT INTERFACES // ================================================= /** - * Cache entry with TTL support + * Cache entry interface for storing data with TTL */ interface CacheEntry { data: T; @@ -56,11 +56,25 @@ interface CacheEntry { ttl: number; // milliseconds } +/** + * Vue component interface that uses the PlatformServiceMixin + * This provides proper typing for the cache system + */ +interface VueComponentWithMixin { + _platformService: PlatformService | null; + $options: { name?: string }; + activeDid?: string; + platformService(): PlatformService; +} + /** * Global cache store for mixin instances * Uses WeakMap to avoid memory leaks when components are destroyed */ -const componentCaches = new WeakMap>>(); +const componentCaches = new WeakMap< + VueComponentWithMixin, + Map> +>(); /** * Cache configuration constants @@ -90,32 +104,41 @@ export const PlatformServiceMixin = { * Created once per component lifecycle */ platformService(): PlatformService { - if (!(this as any)._platformService) { - (this as any)._platformService = PlatformServiceFactory.getInstance(); + if (!(this as unknown as VueComponentWithMixin)._platformService) { + (this as unknown as VueComponentWithMixin)._platformService = + PlatformServiceFactory.getInstance(); } - return (this as any)._platformService; + return (this as unknown as VueComponentWithMixin)._platformService!; }, /** * Platform detection utilities */ isCapacitor(): boolean { - return (this as any).platformService().isCapacitor(); + return (this as unknown as VueComponentWithMixin) + .platformService() + .isCapacitor(); }, isWeb(): boolean { - return (this as any).platformService().isWeb(); + return (this as unknown as VueComponentWithMixin) + .platformService() + .isWeb(); }, isElectron(): boolean { - return (this as any).platformService().isElectron(); + return (this as unknown as VueComponentWithMixin) + .platformService() + .isElectron(); }, /** * Platform capabilities */ capabilities() { - return (this as any).platformService().getCapabilities(); + return (this as unknown as VueComponentWithMixin) + .platformService() + .getCapabilities(); }, }, @@ -127,11 +150,11 @@ export const PlatformServiceMixin = { /** * Get or initialize cache for this component instance */ - _getCache(): Map> { - let cache = componentCaches.get(this); + _getCache(): Map> { + let cache = componentCaches.get(this as unknown as VueComponentWithMixin); if (!cache) { cache = new Map(); - componentCaches.set(this, cache); + componentCaches.set(this as unknown as VueComponentWithMixin, cache); } return cache; }, @@ -139,7 +162,7 @@ export const PlatformServiceMixin = { /** * Check if cache entry is valid (not expired) */ - _isCacheValid(entry: CacheEntry): boolean { + _isCacheValid(entry: CacheEntry): boolean { return Date.now() - entry.timestamp < entry.ttl; }, @@ -150,7 +173,7 @@ export const PlatformServiceMixin = { const cache = this._getCache(); const entry = cache.get(key); if (entry && this._isCacheValid(entry)) { - return entry.data; + return entry.data as T; } cache.delete(key); // Clean up expired entries return null;