Skip to content

Commit 4462cfd

Browse files
committed
feat(stat): Collect publishing progress data for all touched files. WIP
1 parent 6a06d88 commit 4462cfd

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

src/baseTypes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ export type DecisionType =
140140
| DecisionTypeForFileSize
141141
| DecisionTypeForFolder;
142142

143+
export type TSyncStatus =
144+
| "pending"
145+
| "syncing"
146+
| "publishing"
147+
| "done"
148+
| "fail"
149+
143150
export interface FileOrFolderMixedState {
144151
key: string;
145152
existLocal?: boolean;
@@ -160,6 +167,9 @@ export interface FileOrFolderMixedState {
160167
decisionBranch?: number;
161168
syncDone?: "done";
162169
remoteEncryptedKey?: string;
170+
syncStatus?: TSyncStatus;
171+
syncError?: string;
172+
publishError?: string;
163173

164174
mtimeLocalFmt?: string;
165175
mtimeRemoteFmt?: string;

src/exporter.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ export const publishFiles = async (
8383
allFiles: TFile[],
8484
password: string = "",
8585
settings: any,
86-
triggerSource: string
86+
triggerSource: string,
87+
cb?: (key: string, status: 'START' | 'DONE' | 'FAIL') => any,
8788
) => {
8889
const htmlPath = AssetHandler.initHtmlPath();
8990

@@ -146,6 +147,10 @@ export const publishFiles = async (
146147
const resPromise = toUploads.map((upload, i) => {
147148
const htmlFileRelPath = Path.getRelativePathFromVault(new Path(upload.path), true).asString;
148149
log.info('rel path: ', htmlFileRelPath);
150+
if (cb) {
151+
const skip = cb(upload.key, 'START');
152+
if (skip) return;
153+
}
149154
return client.uploadToRemote(
150155
htmlFileRelPath,
151156
'',
@@ -159,7 +164,14 @@ export const publishFiles = async (
159164
upload.key
160165
).then((resp) => {
161166
RenderLog.progress(i++, toUploads.length, "Uploading Docs", "Upload success: " + upload.key, "var(--color-accent)");
167+
if (cb) {
168+
cb(upload.key, 'DONE');
169+
}
162170
return resp;
171+
}).catch(err => {
172+
if (cb) {
173+
cb(upload.key, 'FAIL');
174+
}
163175
})
164176
})
165177

@@ -212,9 +224,25 @@ const getFileFromRemoteKey = (vault: any, filePath: string) => {
212224
export const unpublishFile = async (
213225
client: RemoteClient,
214226
vault: any,
215-
path: string
227+
path: string,
228+
cb?: (key: string, status: 'START' | 'DONE' | 'FAIL') => any,
216229
) => {
217230
const remoteKey = getFileFromRemoteKey(vault, path);
218231
log.info('deleting.... ', remoteKey);
219-
return client.deleteFromRemote(remoteKey, '', '', '');
232+
if (cb) {
233+
const skip = cb(path, 'START');
234+
if (skip) return;
235+
}
236+
return client.deleteFromRemote(remoteKey, '', '', '')
237+
.then(resp => {
238+
if (cb) {
239+
cb(path, 'DONE');
240+
}
241+
return resp;
242+
})
243+
.catch(err => {
244+
if (cb) {
245+
cb(path, 'FAIL');
246+
}
247+
})
220248
}

src/main.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ export default class InvioPlugin extends Plugin {
316316
this.syncStatus = "syncing";
317317
// TODO: Delete all remote html files if triggerSource === force
318318
const pubPathList: string[] = [];
319+
const unPubList: string[] = [];
319320
await doActualSync(
320321
client,
321322
this.db,
@@ -341,14 +342,18 @@ export default class InvioPlugin extends Plugin {
341342
},
342343
async (i: number, totalCount: number, pathName: string, decision: string) => {
343344
self.setCurrSyncMsg(i, totalCount, pathName, decision);
344-
log.info('publishing ', pathName, decision);
345+
346+
log.info('syncing ', pathName, decision);
347+
if (touchedFileMap?.pathName) {
348+
touchedFileMap.pathName.syncStatus = 'syncing';
349+
}
345350
// TODO: Wrap in transation and do alert when publish failed
346351
if (decision === 'uploadLocalToRemote') {
347352
// upload
348353
pubPathList.push(pathName);
349354
} else if (decision === 'uploadLocalDelHistToRemote') {
350355
// delete
351-
await unpublishFile(client, this.app.vault, pathName);
356+
unPubList.push(pathName);
352357
} else {
353358
log.info('ignore decision ', decision, pathName);
354359
}
@@ -357,12 +362,44 @@ export default class InvioPlugin extends Plugin {
357362
log.warn('Remote files conflicts when syncing ... ', key);
358363
}
359364
);
365+
366+
log.info('sync done with touched file map: ', JSON.stringify(touchedFileMap));
367+
368+
for (const pathName of unPubList) {
369+
if (touchedFileMap?.pathName) {
370+
touchedFileMap.pathName.syncStatus = 'publishing';
371+
}
372+
await unpublishFile(client, this.app.vault, pathName, (pathName: string, status: string) => {
373+
log.info('publishing ', pathName, status);
374+
if (touchedFileMap?.pathName) {
375+
if (status === 'START') {
376+
touchedFileMap.pathName.syncStatus = 'publishing';
377+
} else if (status === 'DONE') {
378+
touchedFileMap.pathName.syncStatus = 'done';
379+
} else if (status === 'FAIL') {
380+
touchedFileMap.pathName.syncStatus = 'fail';
381+
}
382+
}
383+
});
384+
}
385+
360386
const basePath = new Path(this.settings.localWatchDir);
361387
// get files to export
362388
let allFiles = this.app.vault.getMarkdownFiles();
363389
// if we are at the root path export all files, otherwise only export files in the folder we are exporting
364390
allFiles = allFiles.filter((file: TFile) => new Path(file.path).directory.asString.startsWith(basePath.asString) && (file.extension === "md") && (!file.name.endsWith('.conflict.md')));
365-
await publishFiles(client, this.app.vault, pubPathList, allFiles, '', this.settings, triggerSource);
391+
await publishFiles(client, this.app.vault, pubPathList, allFiles, '', this.settings, triggerSource, (pathName: string, status: string) => {
392+
log.info('publishing ', pathName, status);
393+
if (touchedFileMap?.pathName) {
394+
if (status === 'START') {
395+
touchedFileMap.pathName.syncStatus = 'publishing';
396+
} else if (status === 'DONE') {
397+
touchedFileMap.pathName.syncStatus = 'done';
398+
} else if (status === 'FAIL') {
399+
touchedFileMap.pathName.syncStatus = 'fail';
400+
}
401+
}
402+
});
366403

367404
if (triggerSource === 'force') {
368405
const forceList: string[] = [];
@@ -405,6 +442,9 @@ export default class InvioPlugin extends Plugin {
405442
this.manifest.id
406443
}-${Date.now()}: finish sync, triggerSource=${triggerSource}`
407444
);
445+
446+
// TODO: Show stats model
447+
return touchedFileMap;
408448
} catch (error) {
409449
const msg = t("syncrun_abort", {
410450
manifestID: this.manifest.id,

src/sync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,16 +1084,16 @@ export const getSyncPlan = async (
10841084
}
10851085
}
10861086

1087-
const touchedFileMap: FileOrFolderMixedState[] = [];
1087+
const touchedFileMap: Record<string, FileOrFolderMixedState> = {};
10881088
for (let i = 0; i < sortedKeys.length; ++i) {
10891089
const key = sortedKeys[i];
10901090
const val = mixedStates[key];
10911091

10921092
if (RemoteFileTouchedDecisions.includes(val.decision)) {
1093-
touchedFileMap.push(val);
1093+
touchedFileMap[key] = val;
10941094
}
10951095
if (LocalFileTouchedDecisions.includes(val.decision)) {
1096-
touchedFileMap.push(val);
1096+
touchedFileMap[key] = val;
10971097
}
10981098
}
10991099

src/touchedPlanModel.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export class TouchedPlanModel extends Modal {
99
agree: boolean;
1010
readonly plugin: InvioPlugin;
1111
hook: (agree: boolean) => void;
12-
readonly files: FileOrFolderMixedState[];
13-
constructor(app: App, plugin: InvioPlugin, fileMap: FileOrFolderMixedState[], cb: (agree: boolean) => void) {
12+
readonly files: Record<string, FileOrFolderMixedState>;
13+
constructor(app: App, plugin: InvioPlugin, fileMap: Record<string, FileOrFolderMixedState>, cb: (agree: boolean) => void) {
1414
super(app);
1515
this.plugin = plugin;
1616
this.agree = false;
@@ -31,7 +31,8 @@ export class TouchedPlanModel extends Modal {
3131
const toLocalFiles: FileOrFolderMixedState[] = [];
3232
const conflictFiles: FileOrFolderMixedState[] = [];
3333

34-
this.files.forEach(f => {
34+
[ ...Object.keys(this.files) ].forEach(key => {
35+
const f = this.files[key];
3536
if ((f.decision === 'uploadLocalDelHistToRemote') && f.existRemote) {
3637
toRemoteFiles.push(f);
3738
}

0 commit comments

Comments
 (0)