Instantly sync updated pages when ticking off a task in a directive

This commit is contained in:
Zef Hemel
2023-08-05 21:09:41 +02:00
parent 2b62b898ce
commit 4af7afa4aa
4 changed files with 30 additions and 8 deletions
+21 -8
View File
@@ -54,16 +54,16 @@ export class SyncService {
eventHook.addLocalListener(
"editor:pageLoaded",
async (name, _prevPage, isSynced) => {
(name, _prevPage, isSynced) => {
if (!isSynced) {
await this.syncFile(`${name}.md`);
this.scheduleFileSync(`${name}.md`).catch(console.error);
}
},
);
eventHook.addLocalListener("editor:pageSaved", async (name) => {
eventHook.addLocalListener("editor:pageSaved", (name) => {
const path = `${name}.md`;
await this.syncFile(path);
this.scheduleFileSync(path).catch(console.error);
});
}
@@ -126,10 +126,23 @@ export class SyncService {
async noOngoingSync(): Promise<void> {
// Not completely safe, could have race condition on setting the syncStartTimeKey
while (await this.isSyncing()) {
await sleep(100);
await sleep(250);
}
}
filesScheduledForSync = new Set<string>();
async scheduleFileSync(path: string): Promise<void> {
if (this.filesScheduledForSync.has(path)) {
// Already scheduled, no need to duplicate
console.info(`File ${path} already scheduled for sync`);
return;
}
this.filesScheduledForSync.add(path);
await this.noOngoingSync();
await this.syncFile(path);
this.filesScheduledForSync.delete(path);
}
start() {
this.syncSpace().catch(
console.error,
@@ -177,11 +190,11 @@ export class SyncService {
// Syncs a single file
async syncFile(name: string) {
if (await this.isSyncing()) {
console.log("Already syncing, aborting individual file sync for", name);
if (!this.isSyncCandidate(name)) {
return;
}
if (!this.isSyncCandidate(name)) {
if (await this.isSyncing()) {
console.log("Already syncing, aborting individual file sync for", name);
return;
}
await this.registerSyncStart();
+3
View File
@@ -9,5 +9,8 @@ export function syncSyscalls(editor: Client): SysCallMapping {
"sync.hasInitialSyncCompleted": (): Promise<boolean> => {
return editor.syncService.hasInitialSyncCompleted();
},
"sync.scheduleFileSync": (_ctx, path: string): Promise<void> => {
return editor.syncService.scheduleFileSync(path);
},
};
}