1
0

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

View File

@ -7,3 +7,7 @@ export function isSyncing(): Promise<boolean> {
export function hasInitialSyncCompleted(): Promise<boolean> { export function hasInitialSyncCompleted(): Promise<boolean> {
return syscall("sync.hasInitialSyncCompleted"); return syscall("sync.hasInitialSyncCompleted");
} }
export function scheduleFileSync(path: string): Promise<void> {
return syscall("sync.scheduleFileSync", path);
}

View File

@ -9,6 +9,7 @@ import {
index, index,
markdown, markdown,
space, space,
sync,
} from "$sb/silverbullet-syscall/mod.ts"; } from "$sb/silverbullet-syscall/mod.ts";
import { import {
@ -164,6 +165,7 @@ async function toggleTaskMarker(
taskMarkerNode.children![0].text = changeTo; taskMarkerNode.children![0].text = changeTo;
text = renderToText(referenceMdTree); text = renderToText(referenceMdTree);
await space.writePage(page, text); await space.writePage(page, text);
sync.scheduleFileSync(`${page}.md`);
} }
} }
} }

View File

@ -54,16 +54,16 @@ export class SyncService {
eventHook.addLocalListener( eventHook.addLocalListener(
"editor:pageLoaded", "editor:pageLoaded",
async (name, _prevPage, isSynced) => { (name, _prevPage, isSynced) => {
if (!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`; 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> { async noOngoingSync(): Promise<void> {
// Not completely safe, could have race condition on setting the syncStartTimeKey // Not completely safe, could have race condition on setting the syncStartTimeKey
while (await this.isSyncing()) { 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() { start() {
this.syncSpace().catch( this.syncSpace().catch(
console.error, console.error,
@ -177,11 +190,11 @@ export class SyncService {
// Syncs a single file // Syncs a single file
async syncFile(name: string) { async syncFile(name: string) {
if (await this.isSyncing()) { if (!this.isSyncCandidate(name)) {
console.log("Already syncing, aborting individual file sync for", name);
return; return;
} }
if (!this.isSyncCandidate(name)) { if (await this.isSyncing()) {
console.log("Already syncing, aborting individual file sync for", name);
return; return;
} }
await this.registerSyncStart(); await this.registerSyncStart();

View File

@ -9,5 +9,8 @@ export function syncSyscalls(editor: Client): SysCallMapping {
"sync.hasInitialSyncCompleted": (): Promise<boolean> => { "sync.hasInitialSyncCompleted": (): Promise<boolean> => {
return editor.syncService.hasInitialSyncCompleted(); return editor.syncService.hasInitialSyncCompleted();
}, },
"sync.scheduleFileSync": (_ctx, path: string): Promise<void> => {
return editor.syncService.scheduleFileSync(path);
},
}; };
} }