Files
plainleaf/web/syscalls/sync.ts
T
Zef HemelandGitHub dc2987a3f9 Sync engine rearchitecture (#1516)
* Don't require auth for .ping
* Finally introducing loggers
* Remove file list caching from disk_space_primitives
I don't think this is required anymore with the sync model
* Simplification of space primitives and more tests
* More reliable online status
* Add .zed settings
* Remove selfUpdate argument from `writeFile` no longer used
- Add `sync` config schema and document sync options
- Move sync config handling to client boot logic
- Move BootConfig type to ui_types.ts
- Remove sync env vars from server
- Update documentation for sync and architecture
* Improve sync logic and proxy routing for full sync state
* Add BoxProxy for late-bound client and improve Lua boot
- Add isMobile syscall for editor
* Add concurrency to sync process and log indexing duration
* Better service worker lifecycle management
2025-09-12 14:43:57 +02:00

78 lines
2.5 KiB
TypeScript

import type { SysCallMapping } from "../../lib/plugos/system.ts";
import type { Client } from "../client.ts";
// TODO: Reimplement this
export function syncSyscalls(client: Client): SysCallMapping {
return {
"sync.hasInitialSyncCompleted": (): boolean => {
return client.fullSyncCompleted;
},
"sync.performFileSync": (_ctx, path: string): Promise<void> => {
client.postServiceWorkerMessage({ type: "perform-file-sync", path });
return waitForServiceWorkerActivation(path);
},
"sync.performSpaceSync": (): Promise<number> => {
client.postServiceWorkerMessage({ type: "perform-space-sync" });
return waitForServiceWorkerActivation();
},
};
}
function waitForServiceWorkerActivation(path?: string): Promise<any> {
return new Promise<any>((resolve, reject) => {
client.eventHook.addLocalListener(
"service-worker:file-sync-complete",
eventHandler,
);
client.eventHook.addLocalListener(
"service-worker:space-sync-complete",
eventHandler,
);
client.eventHook.addLocalListener(
"service-worker:sync-error",
errorHandler,
);
function eventHandler(data: any) {
// If data.path is set, we are notified about a specific file sync -> all good, even for an individual file sync
// If data.path is not set, we are notified about a full space sync
// If we were waiting for a specific path, ignore other paths
if (data.path && path && data.path !== path) {
// Event for other file sync
return;
}
// If we were waiting for a specific path, ignore other paths
resolve(data);
// Unsubscribe from all these events
client.eventHook.removeLocalListener(
"service-worker:file-sync-complete",
eventHandler,
);
client.eventHook.removeLocalListener(
"service-worker:space-sync-complete",
eventHandler,
);
client.eventHook.removeLocalListener(
"service-worker:sync-error",
errorHandler,
);
}
function errorHandler(e: any) {
reject(e);
// Unsubscribe from all these events
client.eventHook.removeLocalListener(
"service-worker:file-sync-complete",
eventHandler,
);
client.eventHook.removeLocalListener(
"service-worker:space-sync-complete",
eventHandler,
);
client.eventHook.removeLocalListener(
"service-worker:sync-error",
errorHandler,
);
}
});
}