diff --git a/src-web/commands/commands.tsx b/src-web/commands/commands.tsx index e60c6e28..6802c39c 100644 --- a/src-web/commands/commands.tsx +++ b/src-web/commands/commands.tsx @@ -53,10 +53,10 @@ export const syncWorkspace = createFastMutation< mutationFn: async ({ workspaceId, syncDir }) => { const ops = (await calculateSync(workspaceId, syncDir)) ?? []; if (ops.length === 0) { - console.log('Nothing to sync', workspaceId, syncDir, ops); + console.log('Nothing to sync', workspaceId, syncDir); return; } - console.log('syncing workspace', workspaceId, syncDir, ops); + console.log('Syncing workspace', workspaceId, syncDir, ops); const dbOps = ops.filter((o) => o.type.startsWith('db')); diff --git a/src-web/init/sync.ts b/src-web/init/sync.ts index 343e2ede..2dba2f85 100644 --- a/src-web/init/sync.ts +++ b/src-web/init/sync.ts @@ -16,17 +16,11 @@ export function initSync() { }); } -// TODO: This list should be derived from something, because we might forget something here -const relevantModels: AnyModel['model'][] = [ - 'workspace', - 'folder', - 'environment', - 'http_request', - 'grpc_request', -]; - function initForWorkspace(workspaceId: string, syncDir: string | null) { - console.log('Initializing directory sync for', workspaceId, syncDir); + // Sync on sync dir changes + if (syncDir == null) { + return; + } const debouncedSync = debounce(() => { if (syncDir == null) return; @@ -34,22 +28,46 @@ function initForWorkspace(workspaceId: string, syncDir: string | null) { }); // Sync on model upsert - listenToTauriEvent('upserted_model', (p) => { - const isRelevant = relevantModels.includes(p.payload.model.model); - if (isRelevant) debouncedSync(); + const unsubUpsertedModels = listenToTauriEvent('upserted_model', (p) => { + if (isModelRelevant(workspaceId, p.payload.model)) { + debouncedSync(); + } }); // Sync on model deletion - listenToTauriEvent('deleted_model', (p) => { - const isRelevant = relevantModels.includes(p.payload.model.model); - if (isRelevant) debouncedSync(); + const unsubDeletedModels = listenToTauriEvent('deleted_model', (p) => { + if (isModelRelevant(workspaceId, p.payload.model)) { + debouncedSync(); + } }); - // Sync on sync dir changes - if (syncDir != null) { - return watchWorkspaceFiles(workspaceId, syncDir, debouncedSync); - } + // Sync on file changes in sync directory + const unsubFileWatch = watchWorkspaceFiles(workspaceId, syncDir, debouncedSync); + + console.log('Initializing directory sync for', workspaceId, syncDir); // Perform an initial sync operation debouncedSync(); + + return function unsub() { + unsubFileWatch(); + unsubDeletedModels(); + unsubUpsertedModels(); + }; +} + +function isModelRelevant(workspaceId: string, m: AnyModel) { + if ( + m.model !== 'workspace' && + m.model !== 'folder' && + m.model !== 'environment' && + m.model !== 'http_request' && + m.model !== 'grpc_request' + ) { + return false; + } else if (m.model === 'workspace') { + return m.id === workspaceId; + } else { + return m.workspaceId === workspaceId; + } }