Files
yaak/src-web/init/sync.ts
Gregory Schier b4a1c418bb Run oxfmt across repo, add format script and docs
Add .oxfmtignore to skip generated bindings and wasm-pack output.
Add npm format script, update DEVELOPMENT.md for Vite+ toolchain,
and format all non-generated files with oxfmt.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 10:15:49 -07:00

80 lines
2.5 KiB
TypeScript

import { debounce } from "@yaakapp-internal/lib";
import type { AnyModel, ModelPayload } from "@yaakapp-internal/models";
import { watchWorkspaceFiles } from "@yaakapp-internal/sync";
import { syncWorkspace } from "../commands/commands";
import { activeWorkspaceIdAtom, activeWorkspaceMetaAtom } from "../hooks/useActiveWorkspace";
import { listenToTauriEvent } from "../hooks/useListenToTauriEvent";
import { jotaiStore } from "../lib/jotai";
export function initSync() {
initModelListeners();
initFileChangeListeners();
sync().catch(console.error);
}
export async function sync({ force }: { force?: boolean } = {}) {
const workspaceMeta = jotaiStore.get(activeWorkspaceMetaAtom);
if (workspaceMeta == null || workspaceMeta.settingSyncDir == null) {
return;
}
await syncWorkspace.mutateAsync({
workspaceId: workspaceMeta.workspaceId,
syncDir: workspaceMeta.settingSyncDir,
force,
});
}
const debouncedSync = debounce(async () => {
await sync();
}, 1000);
/**
* Subscribe to model change events. Since we check the workspace ID on sync, we can
* simply add long-lived subscribers for the lifetime of the app.
*/
function initModelListeners() {
listenToTauriEvent<ModelPayload>("model_write", (p) => {
if (isModelRelevant(p.payload.model)) debouncedSync();
});
}
/**
* Subscribe to relevant files for a workspace. Since the workspace can change, this will
* keep track of the active workspace, as well as changes to the sync directory of the
* current workspace, and re-subscribe when necessary.
*/
function initFileChangeListeners() {
let unsub: null | ReturnType<typeof watchWorkspaceFiles> = null;
jotaiStore.sub(activeWorkspaceMetaAtom, async () => {
await unsub?.(); // Unsub to previous
const workspaceMeta = jotaiStore.get(activeWorkspaceMetaAtom);
if (workspaceMeta == null || workspaceMeta.settingSyncDir == null) return;
debouncedSync(); // Perform an initial sync when switching workspace
unsub = watchWorkspaceFiles(
workspaceMeta.workspaceId,
workspaceMeta.settingSyncDir,
debouncedSync,
);
});
}
function isModelRelevant(m: AnyModel) {
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
if (
m.model !== "workspace" &&
m.model !== "folder" &&
m.model !== "environment" &&
m.model !== "http_request" &&
m.model !== "grpc_request" &&
m.model !== "websocket_request"
) {
return false;
}
if (m.model === "workspace") {
return m.id === workspaceId;
}
return m.workspaceId === workspaceId;
}