Skip to content

Commit aae91cc

Browse files
committed
Convert FetchService into singleton GitService
The $head signal should probably live in the same service.
1 parent 6d00ac1 commit aae91cc

File tree

5 files changed

+33
-40
lines changed

5 files changed

+33
-40
lines changed

apps/desktop/src/lib/fetchSignal/fetchSignal.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Tauri } from '$lib/backend/tauri';
2+
3+
export class GitService {
4+
constructor(private tauri: Tauri) {}
5+
6+
/**
7+
* Emits a new value when a fetch was detected by the back end.
8+
* @example
9+
* $effect(() => gitService.onFetch(data.projectId, () => {}));
10+
*/
11+
onFetch(projectId: string, callback: () => void) {
12+
return this.tauri.listen<any>(`project://${projectId}/git/fetch`, callback);
13+
}
14+
}

apps/desktop/src/routes/+layout.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import { GitHubClient } from '$lib/forge/github/githubClient';
3838
import { GitHubUserService } from '$lib/forge/github/githubUserService.svelte';
3939
import { GitLabClient } from '$lib/forge/gitlab/gitlabClient.svelte';
40+
import { GitService } from '$lib/git/gitService';
4041
import { OplogService } from '$lib/history/oplogService.svelte';
4142
import SnapshotDiffService from '$lib/history/snapshotDiffService.svelte';
4243
import { HooksService } from '$lib/hooks/hooksService';
@@ -240,6 +241,7 @@
240241
setContext(DragStateService, new DragStateService());
241242
setContext(ResizeSync, new ResizeSync());
242243
setContext(UncommitedFilesWatcher, new UncommitedFilesWatcher());
244+
setContext(GitService, new GitService(data.tauri));
243245
244246
const settingsService = data.settingsService;
245247
const settingsStore = settingsService.appSettings;

apps/desktop/src/routes/[projectId]/+layout.svelte

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import { GitLabClient } from '$lib/forge/gitlab/gitlabClient.svelte';
2727
import { GitLabState } from '$lib/forge/gitlab/gitlabState.svelte';
2828
import { TemplateService } from '$lib/forge/templateService';
29+
import { GitService } from '$lib/git/gitService';
2930
import { HistoryService } from '$lib/history/history';
3031
import { ModeService } from '$lib/mode/modeService';
3132
import { showError, showInfo } from '$lib/notifications/toasts';
@@ -44,7 +45,7 @@
4445
4546
const { data, children: pageChildren }: { data: LayoutData; children: Snippet } = $props();
4647
47-
const { projectId, userService, fetchSignal, posthog, projectMetrics, tauri } = $derived(data);
48+
const { projectId, userService, posthog, projectMetrics, tauri } = $derived(data);
4849
4950
const baseBranchService = getContext(BaseBranchService);
5051
const repoInfoResponse = $derived(baseBranchService.repo(projectId));
@@ -114,23 +115,27 @@
114115
const mode = $derived(modeService.mode);
115116
const head = $derived(modeService.head);
116117
117-
// TODO: can we eliminate the need to debounce?
118-
const fetch = $derived(fetchSignal.event);
119-
const debouncedBaseBranchRefresh = debounce(async () => {
120-
await baseBranchService.refreshBaseBranch(projectId);
121-
}, 500);
122-
$effect(() => {
123-
if ($fetch || $head) debouncedBaseBranchRefresh();
124-
});
118+
const debouncedBaseBranchRefresh = debounce(
119+
async () => await baseBranchService.refreshBaseBranch(projectId),
120+
500
121+
);
125122
126-
// TODO: can we eliminate the need to debounce?
127123
const debouncedRemoteBranchRefresh = debounce(
128124
async () => await branchService.refresh(projectId),
129125
500
130126
);
131127
128+
// TODO: Refactor `$head` into `.onHead()` as well.
129+
const gitService = getContext(GitService);
130+
$effect(() =>
131+
gitService.onFetch(data.projectId, () => {
132+
debouncedBaseBranchRefresh();
133+
debouncedRemoteBranchRefresh();
134+
})
135+
);
136+
132137
$effect(() => {
133-
if (baseBranch || $head || $fetch) debouncedRemoteBranchRefresh();
138+
if (baseBranch || $head) debouncedRemoteBranchRefresh();
134139
});
135140
136141
const gitlabConfigured = $derived(gitLabState.configured);
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { GitBranchService } from '$lib/branches/gitBranch';
2-
import { FetchSignal } from '$lib/fetchSignal/fetchSignal.js';
3-
// import { UncommitedFilesWatcher } from '$lib/files/watcher';
42
import { TemplateService } from '$lib/forge/templateService';
53
import { HistoryService } from '$lib/history/history';
64
import type { LayoutLoad } from './$types';
@@ -12,23 +10,15 @@ export const load: LayoutLoad = async ({ params, parent }) => {
1210
const { projectMetrics } = await parent();
1311

1412
const projectId = params.projectId;
15-
16-
const fetchSignal = new FetchSignal(projectId);
17-
1813
const historyService = new HistoryService(projectId);
1914
const templateService = new TemplateService(projectId);
20-
2115
const gitBranchService = new GitBranchService(projectId);
2216

2317
return {
2418
templateService,
2519
historyService,
2620
projectId,
2721
gitBranchService,
28-
projectMetrics,
29-
fetchSignal
30-
// uncommitedFileWatcher,
31-
// Cloud-related services
32-
// syncedSnapshotService,
22+
projectMetrics
3323
};
3424
};

0 commit comments

Comments
 (0)