Skip to content

Commit a01b91b

Browse files
authored
Resove merge conflict (#1884)
Signed-off-by: Matouš Jan Fialka <mjf@mjf.cz>
1 parent 4a411b2 commit a01b91b

File tree

3 files changed

+28
-45
lines changed

3 files changed

+28
-45
lines changed

client/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,8 +954,9 @@ export class Client {
954954
public async postServiceWorkerMessage(message: ServiceWorkerTargetMessage) {
955955
const registration = await navigator.serviceWorker.getRegistration();
956956
if (!registration?.active) {
957-
throw new Error("No active service worker to post message to");
957+
console.warn("No active service worker, skipping message:", message.type);
958+
return;
958959
}
959-
registration?.active?.postMessage(message);
960+
registration.active.postMessage(message);
960961
}
961962
}

client/plugos/syscalls/sync.ts

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
import type { SysCallMapping } from "../system.ts";
22
import type { Client } from "../../client.ts";
33

4-
// TODO: Reimplement this
54
export function syncSyscalls(client: Client): SysCallMapping {
65
return {
76
"sync.hasInitialSyncCompleted": (): boolean => {
87
return client.fullSyncCompleted;
98
},
109
"sync.performFileSync": async (_ctx, path: string): Promise<void> => {
11-
try {
12-
await client.postServiceWorkerMessage({
13-
type: "perform-file-sync",
14-
path,
15-
});
16-
} catch (e: any) {
17-
console.warn(
18-
"No service worker available, so sync is inactive",
19-
e.message,
20-
);
21-
return;
10+
await client.postServiceWorkerMessage({
11+
type: "perform-file-sync",
12+
path,
13+
});
14+
// postServiceWorkerMessage returns silently if no SW, so only wait if SW is active
15+
const registration = await navigator.serviceWorker.getRegistration();
16+
if (registration?.active) {
17+
return waitForServiceWorkerActivation(client, path);
2218
}
23-
return waitForServiceWorkerActivation(path);
2419
},
2520
"sync.performSpaceSync": async (): Promise<number> => {
26-
try {
27-
await client.postServiceWorkerMessage({
28-
type: "perform-space-sync",
29-
});
30-
} catch (e: any) {
31-
console.warn("No service worker available, so sync is inactive", e);
32-
return 0;
21+
await client.postServiceWorkerMessage({ type: "perform-space-sync" });
22+
const registration = await navigator.serviceWorker.getRegistration();
23+
if (registration?.active) {
24+
return waitForServiceWorkerActivation(client);
3325
}
34-
return waitForServiceWorkerActivation();
26+
return 0;
3527
},
3628
};
3729
}
3830

39-
function waitForServiceWorkerActivation(path?: string): Promise<any> {
31+
function waitForServiceWorkerActivation(
32+
client: Client,
33+
path?: string,
34+
): Promise<any> {
4035
return new Promise<any>((resolve, reject) => {
4136
client.eventHook.addLocalListener(
4237
"service-worker:file-sync-complete",
@@ -51,33 +46,17 @@ function waitForServiceWorkerActivation(path?: string): Promise<any> {
5146
errorHandler,
5247
);
5348
function eventHandler(data: any) {
54-
// If data.path is set, we are notified about a specific file sync -> all good, even for an individual file sync
55-
// If data.path is not set, we are notified about a full space sync
56-
// If we were waiting for a specific path, ignore other paths
5749
if (data.path && path && data.path !== path) {
58-
// Event for other file sync
5950
return;
6051
}
61-
// If we were waiting for a specific path, ignore other paths
6252
resolve(data);
63-
64-
// Unsubscribe from all these events
65-
client.eventHook.removeLocalListener(
66-
"service-worker:file-sync-complete",
67-
eventHandler,
68-
);
69-
client.eventHook.removeLocalListener(
70-
"service-worker:space-sync-complete",
71-
eventHandler,
72-
);
73-
client.eventHook.removeLocalListener(
74-
"service-worker:sync-error",
75-
errorHandler,
76-
);
53+
cleanup();
7754
}
7855
function errorHandler(e: any) {
7956
reject(e);
80-
// Unsubscribe from all these events
57+
cleanup();
58+
}
59+
function cleanup() {
8160
client.eventHook.removeLocalListener(
8261
"service-worker:file-sync-complete",
8362
eventHandler,

plugs/index/task.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ export async function updateTaskState(
218218
taskStateNode.children![1].text = newState;
219219
text = renderToText(referenceMdTree);
220220
await space.writePage(pageName, text);
221-
void sync.performFileSync(`${pageName}.md`);
221+
// Best-effort sync; will catch up on next cycle if SW is unavailable
222+
sync.performFileSync(`${pageName}.md`).catch((e) => {
223+
console.warn("File sync after task update failed:", e.message);
224+
});
222225
}
223226
}
224227

0 commit comments

Comments
 (0)