Improve initial sync subscription

This commit is contained in:
Gregory Schier
2025-01-11 06:47:03 -08:00
parent bcf5b3db84
commit 576340db33
2 changed files with 40 additions and 22 deletions

View File

@@ -53,10 +53,10 @@ export const syncWorkspace = createFastMutation<
mutationFn: async ({ workspaceId, syncDir }) => { mutationFn: async ({ workspaceId, syncDir }) => {
const ops = (await calculateSync(workspaceId, syncDir)) ?? []; const ops = (await calculateSync(workspaceId, syncDir)) ?? [];
if (ops.length === 0) { if (ops.length === 0) {
console.log('Nothing to sync', workspaceId, syncDir, ops); console.log('Nothing to sync', workspaceId, syncDir);
return; return;
} }
console.log('syncing workspace', workspaceId, syncDir, ops); console.log('Syncing workspace', workspaceId, syncDir, ops);
const dbOps = ops.filter((o) => o.type.startsWith('db')); const dbOps = ops.filter((o) => o.type.startsWith('db'));

View File

@@ -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) { 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(() => { const debouncedSync = debounce(() => {
if (syncDir == null) return; if (syncDir == null) return;
@@ -34,22 +28,46 @@ function initForWorkspace(workspaceId: string, syncDir: string | null) {
}); });
// Sync on model upsert // Sync on model upsert
listenToTauriEvent<ModelPayload>('upserted_model', (p) => { const unsubUpsertedModels = listenToTauriEvent<ModelPayload>('upserted_model', (p) => {
const isRelevant = relevantModels.includes(p.payload.model.model); if (isModelRelevant(workspaceId, p.payload.model)) {
if (isRelevant) debouncedSync(); debouncedSync();
}
}); });
// Sync on model deletion // Sync on model deletion
listenToTauriEvent<ModelPayload>('deleted_model', (p) => { const unsubDeletedModels = listenToTauriEvent<ModelPayload>('deleted_model', (p) => {
const isRelevant = relevantModels.includes(p.payload.model.model); if (isModelRelevant(workspaceId, p.payload.model)) {
if (isRelevant) debouncedSync(); debouncedSync();
}
}); });
// Sync on sync dir changes // Sync on file changes in sync directory
if (syncDir != null) { const unsubFileWatch = watchWorkspaceFiles(workspaceId, syncDir, debouncedSync);
return watchWorkspaceFiles(workspaceId, syncDir, debouncedSync);
} console.log('Initializing directory sync for', workspaceId, syncDir);
// Perform an initial sync operation // Perform an initial sync operation
debouncedSync(); 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;
}
} }