mirror of
https://github.com/remotely-save/remotely-save.git
synced 2024-06-07 21:10:45 +00:00
Improvement of sync_on_save (#502)
* Improve sync_on_save * fix typo --------- Co-authored-by: fyears <1142836+fyears@users.noreply.github.com>
This commit is contained in:
parent
b674dd0f10
commit
98107ba4ea
84
src/main.ts
84
src/main.ts
@ -9,6 +9,7 @@ import {
|
||||
Platform,
|
||||
requestUrl,
|
||||
requireApiVersion,
|
||||
Events,
|
||||
} from "obsidian";
|
||||
import cloneDeep from "lodash/cloneDeep";
|
||||
import { createElement, RotateCcw, RefreshCcw, FileText } from "lucide";
|
||||
@ -149,6 +150,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
i18n!: I18n;
|
||||
vaultRandomID!: string;
|
||||
debugServerTemp?: string;
|
||||
syncEvent?: Events;
|
||||
|
||||
async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
|
||||
const t = (x: TransItemType, vars?: any) => {
|
||||
@ -416,6 +418,7 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
this.updateLastSuccessSyncMsg(lastSuccessSyncMillis);
|
||||
}
|
||||
|
||||
this.syncEvent?.trigger("SYNC_DONE");
|
||||
console.info(
|
||||
`${
|
||||
this.manifest.id
|
||||
@ -465,6 +468,8 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
|
||||
this.currSyncMsg = "";
|
||||
|
||||
this.syncEvent = new Events();
|
||||
|
||||
await this.loadSettings();
|
||||
|
||||
// MUST after loadSettings and before prepareDB
|
||||
@ -1126,44 +1131,53 @@ export default class RemotelySavePlugin extends Plugin {
|
||||
}, scheduleTimeFromNow);
|
||||
};
|
||||
|
||||
this.app.workspace.onLayoutReady(() => {
|
||||
const intervalID = window.setInterval(() => {
|
||||
const currentFile = this.app.workspace.getActiveFile();
|
||||
const checkCurrFileModified = async (caller: "SYNC" | "FILE_CHANGES") => {
|
||||
const currentFile = this.app.workspace.getActiveFile();
|
||||
|
||||
if (currentFile) {
|
||||
// get the last modified time of the current file
|
||||
// if it has been modified within the last syncOnSaveAfterMilliseconds
|
||||
// then schedule a run for syncOnSaveAfterMilliseconds after it was modified
|
||||
const lastModified = currentFile.stat.mtime;
|
||||
const currentTime = Date.now();
|
||||
if (
|
||||
currentTime - lastModified <
|
||||
this.settings!.syncOnSaveAfterMilliseconds!
|
||||
) {
|
||||
if (
|
||||
!needToRunAgain &&
|
||||
!runScheduled &&
|
||||
this.syncStatus === "idle"
|
||||
) {
|
||||
const scheduleTimeFromNow =
|
||||
this.settings!.syncOnSaveAfterMilliseconds! -
|
||||
(currentTime - lastModified);
|
||||
scheduleSyncOnSave(scheduleTimeFromNow);
|
||||
} else if (
|
||||
needToRunAgain &&
|
||||
!runScheduled &&
|
||||
this.syncStatus === "idle"
|
||||
) {
|
||||
scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!);
|
||||
needToRunAgain = false;
|
||||
} else {
|
||||
needToRunAgain = true;
|
||||
}
|
||||
if (currentFile) {
|
||||
// get the last modified time of the current file
|
||||
// if it has modified after lastSuccessSync
|
||||
// then schedule a run for syncOnSaveAfterMilliseconds after it was modified
|
||||
const lastModified = currentFile.stat.mtime;
|
||||
const lastSuccessSyncMillis = await getLastSuccessSyncByVault(
|
||||
this.db,
|
||||
this.vaultRandomID
|
||||
);
|
||||
if (
|
||||
this.syncStatus === "idle" &&
|
||||
lastModified > lastSuccessSyncMillis &&
|
||||
!runScheduled
|
||||
) {
|
||||
scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!);
|
||||
} else if (
|
||||
this.syncStatus === "idle" &&
|
||||
needToRunAgain &&
|
||||
!runScheduled
|
||||
) {
|
||||
scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!);
|
||||
needToRunAgain = false;
|
||||
} else {
|
||||
if (caller === "FILE_CHANGES") {
|
||||
needToRunAgain = true;
|
||||
}
|
||||
}
|
||||
}, this.settings.syncOnSaveAfterMilliseconds);
|
||||
this.syncOnSaveIntervalID = intervalID;
|
||||
this.registerInterval(intervalID);
|
||||
}
|
||||
};
|
||||
|
||||
this.app.workspace.onLayoutReady(() => {
|
||||
// listen to sync done
|
||||
this.registerEvent(
|
||||
this.syncEvent?.on("SYNC_DONE", () => {
|
||||
checkCurrFileModified("SYNC");
|
||||
})!
|
||||
);
|
||||
|
||||
// listen to current file save changes
|
||||
this.registerEvent(
|
||||
this.app.vault.on("modify", () => {
|
||||
checkCurrFileModified("FILE_CHANGES");
|
||||
})
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user