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

View File

@ -183,4 +183,9 @@ export const DEFAULT_SYNC_PLANS_HISTORY_FILE_PREFIX =
"sync_plans_hist_exported_on_";
export const DEFAULT_LOG_HISTORY_FILE_PREFIX = "log_hist_exported_on_";
export type SyncTriggerSourceType = "manual" | "auto" | "dry" | "autoOnceInit";
export type SyncTriggerSourceType =
| "manual"
| "auto"
| "dry"
| "autoOnceInit"
| "auto_sync_on_save";

View File

@ -103,19 +103,19 @@
"settings_autorun_5min": "every 5 minutes",
"settings_autorun_10min": "every 10 minutes",
"settings_autorun_30min": "every 30 minutes",
"settings_saverun": "Sync on Save",
"settings_saverun_desc": "The plugin tries to sync on after each file save. Battery may be impacted. (May need to restart after changing)",
"settings_saverun_notset": "(not set)",
"settings_saverun_1sec": "every 1 second",
"settings_saverun_5sec": "every 5 seconds",
"settings_saverun_10sec": "every 10 seconds",
"settings_saverun_1min": "every 1 minute",
"settings_runoncestartup": "Run Once On Start Up Automatically",
"settings_runoncestartup_desc": "This settings allows setting running ONCE on start up automatically. This will take effect on NEXT start up after changing. This setting, is different from \"schedule for auto run\" which starts syncing after EVERY interval.",
"settings_runoncestartup_notset": "(not set)",
"settings_runoncestartup_1sec": "sync once after 1 second of start up",
"settings_runoncestartup_10sec": "sync once after 10 seconds of start up",
"settings_runoncestartup_30sec": "sync once after 30 seconds of start up",
"settings_saverun": "Sync On Save (experimental)",
"settings_saverun_desc": "A sync will be triggered if a file save action happened within a few seconds. Please pay attention that syncing is potentially a heavy action and battery may be impacted. (May need to reload the plugin or restart Obsidian after changing)",
"settings_saverun_notset": "(not set)",
"settings_saverun_1sec": "check every 1 second",
"settings_saverun_5sec": "check every 5 seconds",
"settings_saverun_10sec": "check every 10 seconds (recommended)",
"settings_saverun_1min": "check every 1 minute",
"settings_skiplargefiles": "Skip Large Files (experimental)",
"settings_skiplargefiles_desc": "Skip files with sizes larger than the threshold. Here 1 MB = 10^6 bytes.",
"settings_skiplargefiles_notset": "(not set)",
@ -272,4 +272,4 @@
"syncalgov2_texts": "Welcome to use Remotely Save!\nFrom version 0.3.0, a new algorithm has been developed, but it needs uploading extra meta data files _remotely-save-metadata-on-remote.{json,bin} to YOUR configured cloud destinations, besides your notes.\nSo that, for example, the second device can know that what files/folders have been deleted on the first device by reading those files.\nIf you agree, plase click the button \"Agree\", and enjoy the plugin! AND PLEASE REMEMBER TO BACKUP YOUR VAULT FIRSTLY!\nIf you do not agree, you should stop using the current and later versions of Remotely Save. You could consider manually install the old version 0.2.14 which uses old algorithm and does not upload any extra meta data files. By clicking the \"Do Not Agree\" button, the plugin will unload itself, and you need to manually disable it in Obsidian settings.",
"syncalgov2_button_agree": "Agree",
"syncalgov2_button_disagree": "Do Not Agree"
}
}

View File

@ -103,19 +103,19 @@
"settings_autorun_5min": "每 5 分钟",
"settings_autorun_10min": "每 10 分钟",
"settings_autorun_30min": "每 30 分钟",
"settings_saverun": "保存时同步",
"settings_saverun_desc": "插件在每次保存文件后尝试同步。电池可能会受到影响。",
"settings_saverun_notset": "(未设置)",
"settings_saverun_1sec": "每1秒",
"settings_saverun_5sec": "每5秒",
"settings_saverun_10sec": "每10秒",
"settings_saverun_1min": "每1分钟",
"settings_runoncestartup": "启动后自动运行一次",
"settings_runoncestartup_desc": "本设置使得插件在程序启动后自动触发一次同步。修改后,会在下一次启动生效。本设置和“自动运行”是不同的,那个设置是指“每隔一段时间”都自动同步一次。",
"settings_runoncestartup_notset": "(不设置)",
"settings_runoncestartup_1sec": "启动后第 1 秒运行一次",
"settings_runoncestartup_10sec": "启动后第 10 秒运行一次",
"settings_runoncestartup_30sec": "启动后第 30 秒运行一次",
"settings_saverun": "保存时同步(实验性质)",
"settings_saverun_desc": "插件如果检查到当前文件在最近一段时间有修改保存过,则尝试同步。请注意,同步是一个很重的操作,因此会影响到耗电量。(修改设置后可能需要重载插件或重启。)",
"settings_saverun_notset": "(不设置)",
"settings_saverun_1sec": "隔 1 秒检查一次",
"settings_saverun_5sec": "隔 5 秒检查一次",
"settings_saverun_10sec": "隔 10 秒检查一次(推荐)",
"settings_saverun_1min": "隔 1 分钟检查一次",
"settings_skiplargefiles": "跳过大文件(实验性质)",
"settings_skiplargefiles_desc": "跳过大于某一个阈值的文件。这里 1 MB = 10^6 bytes。",
"settings_skiplargefiles_notset": "(不设置)",
@ -272,4 +272,4 @@
"syncalgov2_texts": "欢迎使用 Remotely Save!\n从版本 0.3.0 开始,它带来了新的同步算法,但是,除了您的笔记之外,它还需要上传额外的带有元信息的文件 _remotely-save-metadata-on-remote.{json,bin} 到您的云服务目的地上。\n从而比如说通过读取这些信息另一台设备可以知道什么文件或文件夹在第一台设备上被删除了。\n如果您同意此策略请点击按钮 \"同意\"然后开始享用此插件且特别要注意使用插件之前请首先备份好您的库Vault\n如果您不同意此策略您应该停止使用此版本和之后版本的 Remotely Save。您可以考虑手动安装旧版 0.2.14,它使用旧的同步算法,并不上传额外元信息文件。点击 \"不同意\" 之后插件会自动停止运行unload然后您需要 Obsidian 设置里手动停用disable此插件。",
"syncalgov2_button_agree": "同意",
"syncalgov2_button_disagree": "不同意"
}
}

View File

@ -103,19 +103,19 @@
"settings_autorun_5min": "每 5 分鐘",
"settings_autorun_10min": "每 10 分鐘",
"settings_autorun_30min": "每 30 分鐘",
"settings_saverun": "保存時同步",
"settings_saverun_desc": "插件在每次保存文件後嘗試同步。電池可能會受到影響。",
"settings_saverun_notset": "(未設置)",
"settings_saverun_1sec": "每1秒",
"settings_saverun_5sec": "每5秒",
"settings_saverun_10sec": "每10秒",
"settings_saverun_1min": "每1分鐘",
"settings_runoncestartup": "啟動後自動執行一次",
"settings_runoncestartup_desc": "本設定使得外掛在程式啟動後自動觸發一次同步。修改後,會在下一次啟動生效。本設定和“自動執行”是不同的,那個設定是指“每隔一段時間”都自動同步一次。",
"settings_runoncestartup_notset": "(不設定)",
"settings_runoncestartup_1sec": "啟動後第 1 秒執行一次",
"settings_runoncestartup_10sec": "啟動後第 10 秒執行一次",
"settings_runoncestartup_30sec": "啟動後第 30 秒執行一次",
"settings_saverun": "儲存時同步(實驗性質)",
"settings_saverun_desc": "外掛如果檢查到當前檔案在最近一段時間有修改儲存過,則嘗試同步。請注意,同步是一個很重的操作,因此會影響到耗電量。(修改設定後可能需要過載外掛或重啟。)",
"settings_saverun_notset": "(不設定)",
"settings_saverun_1sec": "隔 1 秒檢查一次",
"settings_saverun_5sec": "隔 5 秒檢查一次",
"settings_saverun_10sec": "隔 10 秒檢查一次(推薦)",
"settings_saverun_1min": "隔 1 分鐘檢查一次",
"settings_skiplargefiles": "跳過大檔案(實驗性質)",
"settings_skiplargefiles_desc": "跳過大於某一個閾值的檔案。這裡 1 MB = 10^6 bytes。",
"settings_skiplargefiles_notset": "(不設定)",
@ -272,4 +272,4 @@
"syncalgov2_texts": "歡迎使用 Remotely Save!\n從版本 0.3.0 開始,它帶來了新的同步演算法,但是,除了您的筆記之外,它還需要上傳額外的帶有元資訊的檔案 _remotely-save-metadata-on-remote.{json,bin} 到您的雲服務目的地上。\n從而比如說透過讀取這些資訊另一臺裝置可以知道什麼檔案或資料夾在第一臺裝置上被刪除了。\n如果您同意此策略請點選按鈕 \"同意\"然後開始享用此外掛且特別要注意使用外掛之前請首先備份好您的儲存庫Vault\n如果您不同意此策略您應該停止使用此版本和之後版本的 Remotely Save。您可以考慮手動安裝舊版 0.2.14,它使用舊的同步演算法,並不上傳額外元資訊檔案。點選 \"不同意\" 之後外掛會自動停止執行unload然後您需要 Obsidian 設定裡手動停用disable此外掛。",
"syncalgov2_button_agree": "同意",
"syncalgov2_button_disagree": "不同意"
}
}

View File

@ -176,7 +176,8 @@ export default class RemotelySavePlugin extends Plugin {
try {
log.info(
`${this.manifest.id
`${
this.manifest.id
}-${Date.now()}: start sync, triggerSource=${triggerSource}`
);
@ -382,7 +383,8 @@ export default class RemotelySavePlugin extends Plugin {
}
log.info(
`${this.manifest.id
`${
this.manifest.id
}-${Date.now()}: finish sync, triggerSource=${triggerSource}`
);
} catch (error) {
@ -864,6 +866,9 @@ export default class RemotelySavePlugin extends Plugin {
if (this.settings.enableStatusBarInfo === undefined) {
this.settings.enableStatusBarInfo = true;
}
if (this.settings.syncOnSaveAfterMilliseconds === undefined) {
this.settings.syncOnSaveAfterMilliseconds = -1;
}
}
async checkIfPresetRulesFollowed() {
@ -991,47 +996,6 @@ export default class RemotelySavePlugin extends Plugin {
this.vaultRandomID = vaultRandomID;
}
enableSyncOnSaveIfSet() {
if (
this.settings.syncOnSaveAfterMilliseconds !== undefined &&
this.settings.syncOnSaveAfterMilliseconds !== null &&
this.settings.syncOnSaveAfterMilliseconds > 0
) {
let runScheduled = false;
this.app.workspace.onLayoutReady(() => {
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.settings.syncOnSaveAfterMilliseconds / 1000} seconds, last modified: ${(currentTime - lastModified) / 1000} seconds ago`
);
if (currentTime - lastModified < this.settings.syncOnSaveAfterMilliseconds) {
if (!runScheduled) {
const scheduleTimeFromNow = this.settings.syncOnSaveAfterMilliseconds - (currentTime - lastModified)
log.info(`schedule a run for ${scheduleTimeFromNow} milliseconds later`)
runScheduled = true
setTimeout(() => {
this.syncRun("auto")
runScheduled = false
},
scheduleTimeFromNow
)
}
}
}
}, 1_000);
this.syncOnSaveIntervalID = intervalID;
this.registerInterval(intervalID);
});
}
}
enableAutoSyncIfSet() {
if (
this.settings.autoRunEveryMilliseconds !== undefined &&
@ -1062,6 +1026,56 @@ export default class RemotelySavePlugin extends Plugin {
}
}
enableSyncOnSaveIfSet() {
if (
this.settings.syncOnSaveAfterMilliseconds !== undefined &&
this.settings.syncOnSaveAfterMilliseconds !== null &&
this.settings.syncOnSaveAfterMilliseconds > 0
) {
let runScheduled = false;
this.app.workspace.onLayoutReady(() => {
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.settings.syncOnSaveAfterMilliseconds / 1000
// } seconds, last modified: ${
// (currentTime - lastModified) / 1000
// } seconds ago`
// );
if (
currentTime - lastModified <
this.settings.syncOnSaveAfterMilliseconds
) {
if (!runScheduled) {
const scheduleTimeFromNow =
this.settings.syncOnSaveAfterMilliseconds -
(currentTime - lastModified);
log.info(
`schedule a run for ${scheduleTimeFromNow} milliseconds later`
);
runScheduled = true;
setTimeout(() => {
this.syncRun("auto_sync_on_save");
runScheduled = false;
}, scheduleTimeFromNow);
}
}
}
}, this.settings.syncOnSaveAfterMilliseconds);
this.syncOnSaveIntervalID = intervalID;
this.registerInterval(intervalID);
});
}
}
async saveAgreeToUseNewSyncAlgorithm() {
this.settings.agreeToUploadExtraMetadata = true;
await this.saveSettings();

View File

@ -21,7 +21,6 @@ export const applyLogWriterInplace = function (writer: (...msg: any[]) => any) {
logLevel: LogLevelNumbers,
loggerName: string | symbol
) {
// @ts-ignore
const rawMethod = originalFactory(methodName, logLevel, loggerName);
return function (...msg: any[]) {

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"))

View File

@ -308,7 +308,7 @@ const isSkipItem = (
if (syncConfigDir && isInsideObsFolder(key, configDir)) {
return false;
}
if (isFolderToSkip(key, [])) {
if (isFolderToSkip(key, [])) {
// some special dirs and files are always skipped
return true;
}

View File

@ -13,14 +13,7 @@
"esModuleInterop": true,
"importHelpers": true,
"isolatedModules": true,
"lib": [
"dom",
"es5",
"scripthost",
"es2015"
]
"lib": ["dom", "es5", "scripthost", "es2015"]
},
"include": [
"**/*.ts"
]
"include": ["**/*.ts"]
}