refactor: register log cleanup through the host API

Delegate display, subscription, and collector cleanup through APIService
instead of registering directly on the Obsidian plug-in. The Obsidian adapter
forwards the same callbacks to native component ownership.

Preserve the separate log resource which remains alive throughout asynchronous
shutdown, and cover native delegation and renderer cleanup registration.
This commit is contained in:
vorotamoroz committed 2026-11-06 12:17:31 +00:00
1 parent cf527fbf86
commit a6b97ddb3b
4 files changed
+43 -5

No files matched your search

@@ -135,6 +135,11 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
return this.context.plugin.registerObsidianProtocolHandler(action, handler);
}
override registerCleanup(callback: () => void): void {
// Keep disposal owned by the native component, preserving its unload timing.
this.context.plugin.register(callback);
}
/**
* In Obsidian, we will use the native `requestUrl` function as the default fetch handler,
* to address unavoidable CORS issues.
@@ -248,3 +248,16 @@ describe("ObsidianAPIService.nativeFetch cache control", () => {
expect(sentHeaders().has("pragma")).toBe(false);
});
});
describe("ObsidianAPIService.registerCleanup", () => {
it("delegates the original callback to native component ownership without invoking it immediately", () => {
const register = vi.fn();
const service = new ObsidianAPIService({ app: {}, plugin: { register } } as unknown as ObsidianServiceContext);
const cleanup = vi.fn();
expect(service.registerCleanup(cleanup)).toBeUndefined();
expect(register).toHaveBeenCalledExactlyOnceWith(cleanup);
expect(cleanup).not.toHaveBeenCalled();
register.mock.calls[0][0]();
expect(cleanup).toHaveBeenCalledOnce();
});
});