fix: keep replicated document application active

Why:
- Large downloaded batches can outlive the finite remote operation and be suspended before every document reaches the Vault.
- The v1.0 activity contract requires local application to remain distinct from remote-operation reporting.

Changes:
- Add an Obsidian-owned local application activity boundary which reuses Wake Lock policy without incrementing remote activity.
- Retain one boundary through queue processing and the final recovery snapshot, releasing and reacquiring it around suspension.
- Log final snapshot failures, preserve processing results, and cover the lifecycle with focused tests and documentation.
This commit is contained in:
Ouyang Xingyuan
2026-07-31 11:06:18 +08:00
parent c385bd7ce7
commit 0df1f57f3b
8 changed files with 305 additions and 31 deletions
+22 -1
View File
@@ -10,11 +10,32 @@ import { ConfigServiceBrowserCompat } from "@vrtmrz/livesync-commonlib/compat/se
import type { ObsidianServiceContext } from "@/modules/services/ObsidianServiceContext";
import { KeyValueDBService } from "@vrtmrz/livesync-commonlib/compat/services/base/KeyValueDBService";
import { ControlService } from "@vrtmrz/livesync-commonlib/compat/services/base/ControlService";
import { reactiveSource } from "octagonal-wheels/dataobject/reactive";
type ActivityOptions = {
label?: string;
};
export class ObsidianDatabaseEventService extends InjectableDatabaseEventService<ObsidianServiceContext> {}
// InjectableReplicatorService
export class ObsidianReplicatorService extends InjectableReplicatorService<ObsidianServiceContext> {}
export class ObsidianReplicatorService extends InjectableReplicatorService<ObsidianServiceContext> {
readonly boundedLocalApplicationActivityCount = reactiveSource(0);
async runBoundedLocalApplicationActivity<T>(
task: () => T | PromiseLike<T>,
options?: ActivityOptions
): Promise<T> {
this.boundedLocalApplicationActivityCount.value++;
try {
return this.dependencies.activityRunner
? await this.dependencies.activityRunner.run(task, options)
: await task();
} finally {
this.boundedLocalApplicationActivityCount.value--;
}
}
}
// InjectableFileProcessingService
export class ObsidianFileProcessingService extends InjectableFileProcessingService<ObsidianServiceContext> {}
// InjectableReplicationService
@@ -0,0 +1,35 @@
import { promiseWithResolvers } from "octagonal-wheels/promises";
import { describe, expect, it, vi } from "vitest";
import { ObsidianReplicatorService } from "./ObsidianServices";
function handler() {
return { addHandler: vi.fn() };
}
describe("ObsidianReplicatorService", () => {
it("tracks local application activity without extending remote activity", async () => {
const activity = promiseWithResolvers<void>();
const service = new ObsidianReplicatorService({ events: {}, translate: String } as never, {
settingService: { onRealiseSetting: handler() },
appLifecycleService: { onSuspending: handler(), getUnresolvedMessages: handler() },
databaseEventService: {
onResetDatabase: handler(),
onDatabaseInitialisation: handler(),
onDatabaseInitialised: handler(),
onDatabaseHasReady: handler(),
},
activityRunner: { run: vi.fn(async (task: () => Promise<void>) => await task()) },
} as never);
const running = service.runBoundedLocalApplicationActivity(() => activity.promise);
expect(service.boundedLocalApplicationActivityCount.value).toBe(1);
expect(service.boundedRemoteActivityCount.value).toBe(0);
activity.resolve();
await running;
expect(service.boundedLocalApplicationActivityCount.value).toBe(0);
expect(service.boundedRemoteActivityCount.value).toBe(0);
});
});