andy-stack_vaultkeeper-ai/Services/DependencyService.ts
Andrew Beal 167a2b13a5 refactor: standardize error handling with Exception helper and improve return types
Add Exception helper class for consistent error handling and logging. Replace throw statements and console.error calls with Exception methods. Update service methods to return Error | T instead of mixed success/failure objects. Improve type safety in Claude.extractContents with explicit return type.

Add WikiLinks helper to VaultCacheService for managing wiki link references.

Update unit tests.
2025-11-17 19:02:15 +00:00

30 lines
No EOL
786 B
TypeScript

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