andy-stack_vaultkeeper-ai/Services/DependencyService.ts
Andrew Beal f2d5c01970 test: add cleanup to prevent memory leaks in test suites
Add DeregisterAllServices() calls in afterEach hooks across all test files to clear singleton registry between tests. Export DeregisterAllServices from DependencyService. Fix StatusBarService test timing issues with animation frames.
2025-10-30 12:15:35 +00:00

28 lines
No EOL
739 B
TypeScript

const services = new Map<symbol, any>();
export function RegisterSingleton<T>(type: symbol, instance: T): void {
services.set(type, instance);
}
export function RegisterTransient<T>(type: symbol, factory: () => T): void {
services.set(type, factory);
}
export function Resolve<T>(type: symbol): T {
const service = services.get(type);
if (!service) {
throw new Error(`Service not found for type: ${type.description}`);
}
if (typeof service === 'function') {
// It's a transient factory, return a new instance
return service();
}
// It's a singleton, return the existing instance
return service as T;
}
export function DeregisterAllServices(): void {
services.clear();
}