diff --git a/src/main.ts b/src/main.ts index 0787453..5b73ecd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -582,6 +582,11 @@ export default class ObsidianLiveSyncPlugin $everyBeforeReplicate(showMessage: boolean): Promise { return InterceptiveEvery; } + + $$canReplicate(showMessage: boolean = false): Promise { + throwShouldBeOverridden(); + } + $$replicate(showMessage: boolean = false): Promise { throwShouldBeOverridden(); } diff --git a/src/modules/core/ModuleReplicator.ts b/src/modules/core/ModuleReplicator.ts index dc20b91..712a50e 100644 --- a/src/modules/core/ModuleReplicator.ts +++ b/src/modules/core/ModuleReplicator.ts @@ -173,17 +173,23 @@ Even if you choose to clean up, you will see this option again if you exit Obsid } }); } - async $$_replicate(showMessage: boolean = false): Promise { - //--? - if (!this.core.$$isReady()) return; + + async $$canReplicate(showMessage: boolean = false): Promise { + if (!this.core.$$isReady()) { + Logger(`Not ready`); + return false; + } + if (isLockAcquired("cleanup")) { Logger($msg("Replicator.Message.Cleaned"), LOG_LEVEL_NOTICE); - return; + return false; } + if (this.settings.versionUpFlash != "") { Logger($msg("Replicator.Message.VersionUpFlash"), LOG_LEVEL_NOTICE); - return; + return false; } + if (!(await this.core.$everyCommitPendingFileEvent())) { Logger($msg("Replicator.Message.Pending"), LOG_LEVEL_NOTICE); return false; @@ -197,6 +203,12 @@ Even if you choose to clean up, you will see this option again if you exit Obsid Logger($msg("Replicator.Message.SomeModuleFailed"), LOG_LEVEL_NOTICE); return false; } + return true; + } + + async $$_replicate(showMessage: boolean = false): Promise { + const checkBeforeReplicate = await this.$$canReplicate(showMessage); + if (!checkBeforeReplicate) return false; //<-- Here could be an module. const ret = await this.core.replicator.openReplication(this.settings, false, showMessage, false); diff --git a/src/modules/core/ModuleReplicatorCouchDB.ts b/src/modules/core/ModuleReplicatorCouchDB.ts index 417f28e..b5d836c 100644 --- a/src/modules/core/ModuleReplicatorCouchDB.ts +++ b/src/modules/core/ModuleReplicatorCouchDB.ts @@ -15,15 +15,22 @@ export class ModuleReplicatorCouchDB extends AbstractModule implements ICoreModu return Promise.resolve(new LiveSyncCouchDBReplicator(this.core)); } $everyAfterResumeProcess(): Promise { + if (!this.core.$$isSuspended) return Promise.resolve(true); + if (!this.core.$$isReady) return Promise.resolve(true); if (this.settings.remoteType != REMOTE_MINIO && this.settings.remoteType != REMOTE_P2P) { - // If LiveSync enabled, open replication - if (this.settings.liveSync) { - fireAndForget(() => this.core.$$replicate(false)); - } - // If sync on start enabled, open replication - if (!this.settings.liveSync && this.settings.syncOnStart) { - // Possibly ok as if only share the result - fireAndForget(() => this.core.$$replicate(false)); + const LiveSyncEnabled = this.settings.liveSync; + const continuous = LiveSyncEnabled; + const eventualOnStart = !LiveSyncEnabled && this.settings.syncOnStart; + + // If enabled LiveSync or on start, open replication + if (LiveSyncEnabled || eventualOnStart) { + // And note that we do not open the conflict detection dialogue directly during this process. + // This should be raised explicitly if needed. + fireAndForget(async () => { + const canReplicate = await this.core.$$canReplicate(false); + if (!canReplicate) return; + void this.core.replicator.openReplication(this.settings, continuous, false, false); + }); } }