1
0

Fix deletion of open file on initial sync

This commit is contained in:
Zef Hemel 2023-07-28 18:06:49 +02:00
parent a758446ecd
commit fb67cba6ac
4 changed files with 40 additions and 17 deletions

View File

@ -23,7 +23,7 @@ export class FallbackSpacePrimitives implements SpacePrimitives {
return await this.primary.readFile(name);
} catch (e) {
console.info(
`Could not read file ${name} from primary, trying fallback`,
`Could not read file ${name} from primary, trying fallback, primary read error:`,
e.message,
);
try {
@ -40,7 +40,7 @@ export class FallbackSpacePrimitives implements SpacePrimitives {
return await this.primary.getFileMeta(name);
} catch (e) {
console.info(
`Could not fetch file ${name} metadata from primary, trying fallback`,
`Could not fetch file ${name} metadata from primary, trying fallback, primary read error`,
e.message,
);
try {

View File

@ -7,7 +7,7 @@ export type AppEvent =
| "minieditor:complete"
| "page:load"
| "editor:init"
| "editor:pageLoaded"
| "editor:pageLoaded" // args: pageName, previousPage, isSynced
| "editor:pageReloaded"
| "editor:pageSaved"
| "editor:modeswitch"

View File

@ -184,7 +184,12 @@ export class Client {
if (this.system.plugsUpdated) {
// To register new commands, update editor state based on new plugs
this.rebuildEditorState();
this.dispatchAppEvent("editor:pageLoaded", this.currentPage);
this.dispatchAppEvent(
"editor:pageLoaded",
this.currentPage,
undefined,
true,
);
if (operations) {
// Likely initial sync so let's show visually that we're synced now
// this.flashNotification(`Synced ${operations} files`, "info");
@ -299,13 +304,15 @@ export class Client {
this.system.indexSyscalls,
),
(meta) => fileFilterFn(meta.name),
// Run when a list of files has been retrieved
async () => {
// Run when a list of files has been retrieved
await this.loadSettings();
if (typeof this.settings?.spaceIgnore === "string") {
fileFilterFn = gitIgnoreCompiler(this.settings.spaceIgnore).accepts;
} else {
fileFilterFn = () => true;
if (await this.syncService?.hasInitialSyncCompleted()) {
await this.loadSettings();
if (typeof this.settings?.spaceIgnore === "string") {
fileFilterFn = gitIgnoreCompiler(this.settings.spaceIgnore).accepts;
} else {
fileFilterFn = () => true;
}
}
},
);

View File

@ -52,9 +52,14 @@ export class SyncService {
},
);
eventHook.addLocalListener("editor:pageLoaded", async (name) => {
await this.syncFile(`${name}.md`);
});
eventHook.addLocalListener(
"editor:pageLoaded",
async (name, _prevPage, isSynced) => {
if (!isSynced) {
await this.syncFile(`${name}.md`);
}
},
);
eventHook.addLocalListener("editor:pageSaved", async (name) => {
const path = `${name}.md`;
@ -158,19 +163,30 @@ export class SyncService {
snapshot,
(path) => this.isSyncCandidate(path),
);
await this.saveSnapshot(snapshot);
await this.registerSyncStop();
this.eventHook.dispatchEvent("sync:success", operations);
} catch (e: any) {
await this.saveSnapshot(snapshot);
await this.registerSyncStop();
this.eventHook.dispatchEvent("sync:error", e.message);
console.error("Sync error", e.message);
}
await this.saveSnapshot(snapshot);
await this.registerSyncStop();
return operations;
}
// Syncs a single file
async syncFile(name: string) {
// Reminder: main reason to do this is not accidentally sync files retrieved via fallthrough (remote) and treat them as locally deleted
if (!await this.hasInitialSyncCompleted()) {
console.info(
"Initial sync hasn't happened yet, skipping sync for individual file",
name,
);
return;
}
if (await this.isSyncing()) {
// console.log("Already syncing");
console.log("Already syncing, aborting individual file sync for", name);
return;
}
if (!this.isSyncCandidate(name)) {
@ -199,7 +215,7 @@ export class SyncService {
}
}
await this.spaceSync!.syncFile(snapshot, name, localHash, remoteHash);
await this.spaceSync.syncFile(snapshot, name, localHash, remoteHash);
this.eventHook.dispatchEvent("sync:success");
} catch (e: any) {
this.eventHook.dispatchEvent("sync:error", e.message);