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,
|
Platform,
|
||||||
requestUrl,
|
requestUrl,
|
||||||
requireApiVersion,
|
requireApiVersion,
|
||||||
|
Events,
|
||||||
} from "obsidian";
|
} from "obsidian";
|
||||||
import cloneDeep from "lodash/cloneDeep";
|
import cloneDeep from "lodash/cloneDeep";
|
||||||
import { createElement, RotateCcw, RefreshCcw, FileText } from "lucide";
|
import { createElement, RotateCcw, RefreshCcw, FileText } from "lucide";
|
||||||
@ -149,6 +150,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
i18n!: I18n;
|
i18n!: I18n;
|
||||||
vaultRandomID!: string;
|
vaultRandomID!: string;
|
||||||
debugServerTemp?: string;
|
debugServerTemp?: string;
|
||||||
|
syncEvent?: Events;
|
||||||
|
|
||||||
async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
|
async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
|
||||||
const t = (x: TransItemType, vars?: any) => {
|
const t = (x: TransItemType, vars?: any) => {
|
||||||
@ -416,6 +418,7 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
this.updateLastSuccessSyncMsg(lastSuccessSyncMillis);
|
this.updateLastSuccessSyncMsg(lastSuccessSyncMillis);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.syncEvent?.trigger("SYNC_DONE");
|
||||||
console.info(
|
console.info(
|
||||||
`${
|
`${
|
||||||
this.manifest.id
|
this.manifest.id
|
||||||
@ -465,6 +468,8 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
|
|
||||||
this.currSyncMsg = "";
|
this.currSyncMsg = "";
|
||||||
|
|
||||||
|
this.syncEvent = new Events();
|
||||||
|
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
|
|
||||||
// MUST after loadSettings and before prepareDB
|
// MUST after loadSettings and before prepareDB
|
||||||
@ -1126,44 +1131,53 @@ export default class RemotelySavePlugin extends Plugin {
|
|||||||
}, scheduleTimeFromNow);
|
}, scheduleTimeFromNow);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.app.workspace.onLayoutReady(() => {
|
const checkCurrFileModified = async (caller: "SYNC" | "FILE_CHANGES") => {
|
||||||
const intervalID = window.setInterval(() => {
|
const currentFile = this.app.workspace.getActiveFile();
|
||||||
const currentFile = this.app.workspace.getActiveFile();
|
|
||||||
|
|
||||||
if (currentFile) {
|
if (currentFile) {
|
||||||
// get the last modified time of the current file
|
// get the last modified time of the current file
|
||||||
// if it has been modified within the last syncOnSaveAfterMilliseconds
|
// if it has modified after lastSuccessSync
|
||||||
// then schedule a run for syncOnSaveAfterMilliseconds after it was modified
|
// then schedule a run for syncOnSaveAfterMilliseconds after it was modified
|
||||||
const lastModified = currentFile.stat.mtime;
|
const lastModified = currentFile.stat.mtime;
|
||||||
const currentTime = Date.now();
|
const lastSuccessSyncMillis = await getLastSuccessSyncByVault(
|
||||||
if (
|
this.db,
|
||||||
currentTime - lastModified <
|
this.vaultRandomID
|
||||||
this.settings!.syncOnSaveAfterMilliseconds!
|
);
|
||||||
) {
|
if (
|
||||||
if (
|
this.syncStatus === "idle" &&
|
||||||
!needToRunAgain &&
|
lastModified > lastSuccessSyncMillis &&
|
||||||
!runScheduled &&
|
!runScheduled
|
||||||
this.syncStatus === "idle"
|
) {
|
||||||
) {
|
scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!);
|
||||||
const scheduleTimeFromNow =
|
} else if (
|
||||||
this.settings!.syncOnSaveAfterMilliseconds! -
|
this.syncStatus === "idle" &&
|
||||||
(currentTime - lastModified);
|
needToRunAgain &&
|
||||||
scheduleSyncOnSave(scheduleTimeFromNow);
|
!runScheduled
|
||||||
} else if (
|
) {
|
||||||
needToRunAgain &&
|
scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!);
|
||||||
!runScheduled &&
|
needToRunAgain = false;
|
||||||
this.syncStatus === "idle"
|
} else {
|
||||||
) {
|
if (caller === "FILE_CHANGES") {
|
||||||
scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!);
|
needToRunAgain = true;
|
||||||
needToRunAgain = false;
|
|
||||||
} else {
|
|
||||||
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