Skip to content

Commit

Permalink
Improve initial sync subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
gschier committed Jan 11, 2025
1 parent bcf5b3d commit 576340d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src-web/commands/commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
58 changes: 38 additions & 20 deletions src-web/init/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,58 @@ 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;
syncWorkspace.mutate({ workspaceId, syncDir });
});

// Sync on model upsert
listenToTauriEvent<ModelPayload>('upserted_model', (p) => {
const isRelevant = relevantModels.includes(p.payload.model.model);
if (isRelevant) debouncedSync();
const unsubUpsertedModels = listenToTauriEvent<ModelPayload>('upserted_model', (p) => {
if (isModelRelevant(workspaceId, p.payload.model)) {
debouncedSync();
}
});

// Sync on model deletion
listenToTauriEvent<ModelPayload>('deleted_model', (p) => {
const isRelevant = relevantModels.includes(p.payload.model.model);
if (isRelevant) debouncedSync();
const unsubDeletedModels = listenToTauriEvent<ModelPayload>('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;
}
}

0 comments on commit 576340d

Please sign in to comment.