mirror of
https://github.com/andy-stack/vaultkeeper-ai.git
synced 2026-07-22 16:30:27 +00:00
Replace object-based subscription tracking with token-based system to prevent memory leaks. Add dispose() method to BaseAIClass and ensure all components properly unsubscribe in onDestroy. Update SettingsService.subscribeToSettingsChanged to return subscription token instead of requiring subscriber object parameter.
49 lines
No EOL
1.3 KiB
TypeScript
49 lines
No EOL
1.3 KiB
TypeScript
import { Exception } from "Helpers/Exception";
|
|
import type { Component } from "obsidian";
|
|
|
|
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 TryResolve<T>(type: symbol): T | undefined {
|
|
if (services.has(type)) {
|
|
return Resolve<T>(type);
|
|
}
|
|
}
|
|
|
|
export function DeregisterAllServices() {
|
|
services.forEach((service) => {
|
|
if (!service || typeof service !== "object") {
|
|
return;
|
|
}
|
|
if ("dispose" in service && typeof service.dispose === "function") {
|
|
(service as { dispose: () => void }).dispose();
|
|
}
|
|
if ("unload" in service) {
|
|
(service as Component).unload();
|
|
}
|
|
});
|
|
services.clear();
|
|
} |