Skip to content

Commit 5e26ad8

Browse files
committed
feat(sync): Simple check contents diff before syncing.
1 parent 14f36b2 commit 5e26ad8

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/main.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default class InvioPlugin extends Plugin {
105105
autoRunIntervalID?: number;
106106
i18n: I18n;
107107
vaultRandomID: string;
108+
recentSyncedFiles: any;
108109

109110
isUnderWatch(file: TAbstractFile) {
110111
const rootDir = this.settings.localWatchDir;
@@ -116,6 +117,29 @@ export default class InvioPlugin extends Plugin {
116117
return false;
117118
}
118119

120+
shouldAddToSyncFile(file: TFile): boolean {
121+
if (file.extension !== 'md') {
122+
return false;
123+
}
124+
return true;
125+
};
126+
127+
async addRecentSyncedFile(file: TFile): Promise<void> {
128+
log.info('file synced: ', file);
129+
if (!file || !this.shouldAddToSyncFile(file)) {
130+
return;
131+
}
132+
133+
!this.recentSyncedFiles && (this.recentSyncedFiles = {});
134+
const contents = await this.app.vault.read(file);
135+
136+
this.recentSyncedFiles[file.path] = {
137+
contents,
138+
...file.stat
139+
}
140+
log.info('file snapshot: ', this.recentSyncedFiles);
141+
}
142+
119143
// async checkDomain() {
120144
// // Domain check
121145
// return new Promise((resolve) => {
@@ -308,6 +332,7 @@ export default class InvioPlugin extends Plugin {
308332
const { plan, sortedKeys, deletions, sizesGoWrong, touchedFileMap } = await getSyncPlan(
309333
remoteStates,
310334
local,
335+
this.recentSyncedFiles,
311336
fileList?.length > 0,
312337
fileList,
313338
localConfigDirContents,
@@ -392,6 +417,7 @@ export default class InvioPlugin extends Plugin {
392417
fileList.forEach(filePath => {
393418
const file = allFiles.find(file => file.path === filePath);
394419
if (file) {
420+
395421
remoteChangingFiles.push({
396422
key: file.path,
397423
syncStatus: 'syncing',
@@ -463,6 +489,12 @@ export default class InvioPlugin extends Plugin {
463489
return;
464490
}
465491
log.info('sync done ', pathName, decision);
492+
if ((decision === 'uploadLocalToRemote') || (decision === 'downloadRemoteToLocal')) {
493+
const syncedFile = this.app.vault.getAbstractFileByPath(pathName);
494+
if (syncedFile instanceof TFile) {
495+
this.addRecentSyncedFile(syncedFile);
496+
}
497+
}
466498
// TODO: Get remote link, but need remote domain first
467499
view?.update(pathName, { syncStatus: 'sync-done', remoteLink: '' });
468500
view?.info(`${i}/${totalCount} - file ${pathName} sync done`);

src/sync.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,14 @@ const isSkipItem = (
369369
}
370370
}
371371
}
372+
if (watchDir && !key.startsWith(watchDir)) {
373+
return true;
374+
}
375+
372376
if (syncConfigDir && isInsideObsFolder(key, configDir)) {
373377
return false;
374378
}
375-
if (!key.startsWith(watchDir)) {
376-
return true;
377-
}
379+
378380
return (
379381
isHiddenPath(key, true, false) ||
380382
(!syncUnderscoreItems && isHiddenPath(key, false, true)) ||
@@ -386,6 +388,8 @@ const isSkipItem = (
386388
const ensembleMixedStates = async (
387389
remoteStates: FileOrFolderMixedState[],
388390
local: TAbstractFile[],
391+
vault: Vault,
392+
recentSyncedFiles: any,
389393
selectedMode: boolean,
390394
selectedLocalFilePaths: string[] | undefined,
391395
localConfigDirContents: ObsConfigDirFileType[] | undefined,
@@ -417,7 +421,21 @@ const ensembleMixedStates = async (
417421
// ignore
418422
continue;
419423
} else if (entry instanceof TFile) {
420-
const mtimeLocal = Math.max(entry.stat.mtime ?? 0, entry.stat.ctime ?? 0);
424+
let mtimeLocal = Math.max(entry.stat.mtime ?? 0, entry.stat.ctime ?? 0);
425+
426+
const snap = recentSyncedFiles && recentSyncedFiles[entry.path];
427+
if (snap) {
428+
log.info('snap found: ', snap);
429+
const curContents = await vault.read(entry);
430+
if (entry.stat.size === snap.size) {
431+
if (curContents === snap.contents) {
432+
log.info('current contents same with last sync, not changed');
433+
mtimeLocal = snap.mtime;
434+
}
435+
} else {
436+
log.info('current contents changed');
437+
}
438+
}
421439
r = {
422440
key: entry.path,
423441
existLocal: true,
@@ -1010,6 +1028,7 @@ const SIZES_GO_WRONG_DECISIONS: Set<DecisionType> = new Set([
10101028
export const getSyncPlan = async (
10111029
remoteStates: FileOrFolderMixedState[],
10121030
local: TAbstractFile[],
1031+
recentSyncedFiles: any,
10131032
selectedMode: boolean, // Selected mode only care about files in selectedLocalFilePaths
10141033
selectedLocalFilePaths: string[] | undefined, // Only care file in this list and ignore all other files, work when selectedMode is true
10151034
localConfigDirContents: ObsConfigDirFileType[] | undefined,
@@ -1028,6 +1047,8 @@ export const getSyncPlan = async (
10281047
const mixedStates = await ensembleMixedStates(
10291048
remoteStates,
10301049
local,
1050+
vault,
1051+
recentSyncedFiles,
10311052
selectedMode,
10321053
selectedLocalFilePaths,
10331054
localConfigDirContents,

0 commit comments

Comments
 (0)