mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-09-25 20:17:06 +00:00
Add sleep preferences for finite synchronisation
This commit is contained in:
@@ -11,17 +11,53 @@ import type { ObsidianServiceContext } from "@/modules/services/ObsidianServiceC
|
||||
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";
|
||||
import type { ReplicatorServiceDependencies } from "@vrtmrz/livesync-commonlib/compat/services/base/ReplicatorService";
|
||||
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
|
||||
|
||||
type ActivityOptions = {
|
||||
label?: string;
|
||||
};
|
||||
|
||||
type ObsidianReplicatorServiceDependencies = ReplicatorServiceDependencies & {
|
||||
isMobile: () => boolean;
|
||||
};
|
||||
|
||||
type SleepPreferenceSettings = Pick<
|
||||
ObsidianLiveSyncSettings,
|
||||
"allowSleepDuringSynchronisation" | "allowSleepDuringSynchronisationOnDesktop"
|
||||
>;
|
||||
|
||||
export function shouldAllowSleepDuringSynchronisation(settings: SleepPreferenceSettings, isMobile: boolean): boolean {
|
||||
return settings.allowSleepDuringSynchronisation || (!isMobile && settings.allowSleepDuringSynchronisationOnDesktop);
|
||||
}
|
||||
|
||||
function withSleepPreference(dependencies: ObsidianReplicatorServiceDependencies): ReplicatorServiceDependencies {
|
||||
const activityRunner = dependencies.activityRunner;
|
||||
if (!activityRunner) return dependencies;
|
||||
return {
|
||||
...dependencies,
|
||||
activityRunner: {
|
||||
async run<T>(task: () => T | PromiseLike<T>, options?: ActivityOptions): Promise<T> {
|
||||
const allowSleep = shouldAllowSleepDuringSynchronisation(
|
||||
dependencies.settingService.currentSettings(),
|
||||
dependencies.isMobile()
|
||||
);
|
||||
return allowSleep ? await task() : await activityRunner.run(task, options);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export class ObsidianDatabaseEventService extends InjectableDatabaseEventService<ObsidianServiceContext> {}
|
||||
|
||||
// InjectableReplicatorService
|
||||
export class ObsidianReplicatorService extends InjectableReplicatorService<ObsidianServiceContext> {
|
||||
readonly boundedLocalApplicationActivityCount = reactiveSource(0);
|
||||
|
||||
constructor(context: ObsidianServiceContext, dependencies: ObsidianReplicatorServiceDependencies) {
|
||||
super(context, withSleepPreference(dependencies));
|
||||
}
|
||||
|
||||
async runBoundedLocalApplicationActivity<T>(
|
||||
task: () => T | PromiseLike<T>,
|
||||
options?: ActivityOptions
|
||||
|
||||
Reference in New Issue
Block a user