forked from trent_larson/crowd-funder-for-time-pwa
refactor: improve cache system typing in PlatformServiceMixin
- Replace WeakMap<any, Map<string, CacheEntry<any>>> with proper VueComponentWithMixin interface - Add VueComponentWithMixin interface with _platformService, $options, activeDid, and platformService() method - Use CacheEntry<unknown> instead of CacheEntry<any> 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
This commit is contained in:
@@ -44,11 +44,11 @@ import { Contact } from "@/db/tables/contacts";
|
|||||||
import { QueryExecResult, DatabaseExecResult } from "@/interfaces/database";
|
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<T> {
|
interface CacheEntry<T> {
|
||||||
data: T;
|
data: T;
|
||||||
@@ -56,11 +56,25 @@ interface CacheEntry<T> {
|
|||||||
ttl: number; // milliseconds
|
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
|
* Global cache store for mixin instances
|
||||||
* Uses WeakMap to avoid memory leaks when components are destroyed
|
* Uses WeakMap to avoid memory leaks when components are destroyed
|
||||||
*/
|
*/
|
||||||
const componentCaches = new WeakMap<any, Map<string, CacheEntry<any>>>();
|
const componentCaches = new WeakMap<
|
||||||
|
VueComponentWithMixin,
|
||||||
|
Map<string, CacheEntry<unknown>>
|
||||||
|
>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cache configuration constants
|
* Cache configuration constants
|
||||||
@@ -90,32 +104,41 @@ export const PlatformServiceMixin = {
|
|||||||
* Created once per component lifecycle
|
* Created once per component lifecycle
|
||||||
*/
|
*/
|
||||||
platformService(): PlatformService {
|
platformService(): PlatformService {
|
||||||
if (!(this as any)._platformService) {
|
if (!(this as unknown as VueComponentWithMixin)._platformService) {
|
||||||
(this as any)._platformService = PlatformServiceFactory.getInstance();
|
(this as unknown as VueComponentWithMixin)._platformService =
|
||||||
|
PlatformServiceFactory.getInstance();
|
||||||
}
|
}
|
||||||
return (this as any)._platformService;
|
return (this as unknown as VueComponentWithMixin)._platformService!;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Platform detection utilities
|
* Platform detection utilities
|
||||||
*/
|
*/
|
||||||
isCapacitor(): boolean {
|
isCapacitor(): boolean {
|
||||||
return (this as any).platformService().isCapacitor();
|
return (this as unknown as VueComponentWithMixin)
|
||||||
|
.platformService()
|
||||||
|
.isCapacitor();
|
||||||
},
|
},
|
||||||
|
|
||||||
isWeb(): boolean {
|
isWeb(): boolean {
|
||||||
return (this as any).platformService().isWeb();
|
return (this as unknown as VueComponentWithMixin)
|
||||||
|
.platformService()
|
||||||
|
.isWeb();
|
||||||
},
|
},
|
||||||
|
|
||||||
isElectron(): boolean {
|
isElectron(): boolean {
|
||||||
return (this as any).platformService().isElectron();
|
return (this as unknown as VueComponentWithMixin)
|
||||||
|
.platformService()
|
||||||
|
.isElectron();
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Platform capabilities
|
* Platform capabilities
|
||||||
*/
|
*/
|
||||||
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
|
* Get or initialize cache for this component instance
|
||||||
*/
|
*/
|
||||||
_getCache(): Map<string, CacheEntry<any>> {
|
_getCache(): Map<string, CacheEntry<unknown>> {
|
||||||
let cache = componentCaches.get(this);
|
let cache = componentCaches.get(this as unknown as VueComponentWithMixin);
|
||||||
if (!cache) {
|
if (!cache) {
|
||||||
cache = new Map();
|
cache = new Map();
|
||||||
componentCaches.set(this, cache);
|
componentCaches.set(this as unknown as VueComponentWithMixin, cache);
|
||||||
}
|
}
|
||||||
return cache;
|
return cache;
|
||||||
},
|
},
|
||||||
@@ -139,7 +162,7 @@ export const PlatformServiceMixin = {
|
|||||||
/**
|
/**
|
||||||
* Check if cache entry is valid (not expired)
|
* Check if cache entry is valid (not expired)
|
||||||
*/
|
*/
|
||||||
_isCacheValid(entry: CacheEntry<any>): boolean {
|
_isCacheValid(entry: CacheEntry<unknown>): boolean {
|
||||||
return Date.now() - entry.timestamp < entry.ttl;
|
return Date.now() - entry.timestamp < entry.ttl;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -150,7 +173,7 @@ export const PlatformServiceMixin = {
|
|||||||
const cache = this._getCache();
|
const cache = this._getCache();
|
||||||
const entry = cache.get(key);
|
const entry = cache.get(key);
|
||||||
if (entry && this._isCacheValid(entry)) {
|
if (entry && this._isCacheValid(entry)) {
|
||||||
return entry.data;
|
return entry.data as T;
|
||||||
}
|
}
|
||||||
cache.delete(key); // Clean up expired entries
|
cache.delete(key); // Clean up expired entries
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user