tidy up the codes of sync on save

This commit is contained in:
fyears
2024-01-06 14:40:32 +08:00
parent 30f7c0058c
commit a13cc8d5c1
9 changed files with 162 additions and 142 deletions
+72 -63
View File
@@ -831,7 +831,8 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
dropdown
.setValue(
`${this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable"
`${
this.plugin.settings.s3.bypassCorsLocally ? "enable" : "disable"
}`
)
.onChange(async (value) => {
@@ -1516,67 +1517,6 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
});
});
new Setting(basicDiv)
.setName(t("settings_saverun"))
.setDesc(t("settings_saverun_desc"))
.addDropdown((dropdown) => {
dropdown.addOption("-1", t("settings_saverun_notset"));
dropdown.addOption(`${1000 * 1}`, t("settings_saverun_1sec"));
dropdown.addOption(`${1000 * 5}`, t("settings_saverun_5sec"));
dropdown.addOption(`${1000 * 10}`, t("settings_saverun_10sec"));
dropdown.addOption(`${1000 * 60}`, t("settings_saverun_1min"));
let runScheduled = false
dropdown
.setValue(`${this.plugin.settings.syncOnSaveAfterMilliseconds}`)
.onChange(async (val: string) => {
const realVal = parseInt(val);
this.plugin.settings.syncOnSaveAfterMilliseconds = realVal;
await this.plugin.saveSettings();
if (
(realVal === undefined || realVal === null || realVal <= 0) &&
this.plugin.syncOnSaveIntervalID !== undefined
) {
// clear
window.clearInterval(this.plugin.syncOnSaveIntervalID);
this.plugin.syncOnSaveIntervalID = undefined;
} else if (
realVal !== undefined &&
realVal !== null &&
realVal > 0
) {
const intervalID = window.setInterval(() => {
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();
log.debug(
`Checking if file was modified within last ${this.plugin.settings.syncOnSaveAfterMilliseconds / 1000} seconds, last modified: ${(currentTime - lastModified) / 1000} seconds ago`
);
if (currentTime - lastModified < this.plugin.settings.syncOnSaveAfterMilliseconds) {
if (!runScheduled) {
const scheduleTimeFromNow = this.plugin.settings.syncOnSaveAfterMilliseconds - (currentTime - lastModified)
log.info(`schedule a run for ${scheduleTimeFromNow} milliseconds later`)
runScheduled = true
setTimeout(() => {
this.plugin.syncRun("auto")
runScheduled = false
},
scheduleTimeFromNow
)
}
}
}
}, realVal);
this.plugin.syncOnSaveIntervalID = intervalID;
this.plugin.registerInterval(intervalID);
}
});
});
new Setting(basicDiv)
.setName(t("settings_autorun"))
.setDesc(t("settings_autorun_desc"))
@@ -1607,7 +1547,6 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
) {
const intervalID = window.setInterval(() => {
log.info("auto run from settings.ts");
console.log("auto run from settings.ts");
this.plugin.syncRun("auto");
}, realVal);
this.plugin.autoRunIntervalID = intervalID;
@@ -1642,6 +1581,76 @@ export class RemotelySaveSettingTab extends PluginSettingTab {
});
});
new Setting(basicDiv)
.setName(t("settings_saverun"))
.setDesc(t("settings_saverun_desc"))
.addDropdown((dropdown) => {
dropdown.addOption("-1", t("settings_saverun_notset"));
dropdown.addOption(`${1000 * 1}`, t("settings_saverun_1sec"));
dropdown.addOption(`${1000 * 5}`, t("settings_saverun_5sec"));
dropdown.addOption(`${1000 * 10}`, t("settings_saverun_10sec"));
dropdown.addOption(`${1000 * 60}`, t("settings_saverun_1min"));
let runScheduled = false;
dropdown
.setValue(`${this.plugin.settings.syncOnSaveAfterMilliseconds}`)
.onChange(async (val: string) => {
const realVal = parseInt(val);
this.plugin.settings.syncOnSaveAfterMilliseconds = realVal;
await this.plugin.saveSettings();
if (
(realVal === undefined || realVal === null || realVal <= 0) &&
this.plugin.syncOnSaveIntervalID !== undefined
) {
// clear
window.clearInterval(this.plugin.syncOnSaveIntervalID);
this.plugin.syncOnSaveIntervalID = undefined;
} else if (
realVal !== undefined &&
realVal !== null &&
realVal > 0
) {
const intervalID = window.setInterval(() => {
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();
// log.debug(
// `Checking if file was modified within last ${
// this.plugin.settings.syncOnSaveAfterMilliseconds / 1000
// } seconds, last modified: ${
// (currentTime - lastModified) / 1000
// } seconds ago`
// );
if (
currentTime - lastModified <
this.plugin.settings.syncOnSaveAfterMilliseconds
) {
if (!runScheduled) {
const scheduleTimeFromNow =
this.plugin.settings.syncOnSaveAfterMilliseconds -
(currentTime - lastModified);
log.info(
`schedule a run for ${scheduleTimeFromNow} milliseconds later`
);
runScheduled = true;
setTimeout(() => {
this.plugin.syncRun("auto_sync_on_save");
runScheduled = false;
}, scheduleTimeFromNow);
}
}
}
}, realVal);
this.plugin.syncOnSaveIntervalID = intervalID;
this.plugin.registerInterval(intervalID);
}
});
});
new Setting(basicDiv)
.setName(t("settings_skiplargefiles"))
.setDesc(t("settings_skiplargefiles_desc"))