Refactor mode service to remove manual unsubscribe

This commit is contained in:
Caleb Owens 2024-08-14 11:22:47 +02:00
parent 83283d918c
commit 838f6e6226
2 changed files with 21 additions and 22 deletions

View File

@ -2,35 +2,35 @@ import { invoke, listen } from '$lib/backend/ipc';
import { derived, writable } from 'svelte/store';
type Mode = { type: 'OpenWorkspace' } | { type: 'OutsideWorksapce' } | { type: 'Edit' };
interface HeadAndMode {
head?: string;
operatingMode?: Mode;
}
export class ModeService {
readonly head = writable<string | undefined>(undefined);
readonly mode = writable<Mode | undefined>(undefined);
readonly gbBranchActive = derived(this.head, (head) => head === 'gitbutler/integration');
unsubscribe?: () => Promise<void>;
constructor(private projectId: string) {
this.unsubscribe = subscribeToHead(projectId, (head, mode) => {
this.head.set(head);
this.mode.set(mode);
});
private headAndMode = writable<HeadAndMode>({}, (set) => {
this.refresh();
}
const unsubscribe = subscribeToHead(this.projectId, (headAndMode) => {
set(headAndMode);
});
return unsubscribe;
});
readonly head = derived(this.headAndMode, ({ head }) => head);
readonly mode = derived(this.headAndMode, ({ operatingMode }) => operatingMode);
constructor(private projectId: string) {}
private async refresh() {
const head = await invoke<string>('git_head', { projectId: this.projectId });
this.head.set(head);
const operatingMode = await invoke<Mode>('operating_mode', { projectId: this.projectId });
const mode = await invoke<Mode>('operating_mode', { projectId: this.projectId });
this.mode.set(mode);
this.headAndMode.set({ head, operatingMode });
}
}
function subscribeToHead(projectId: string, callback: (head: string, mode: Mode) => void) {
return listen<{ head: string; operating_mode: Mode }>(
`project://${projectId}/git/head`,
(event) => callback(event.payload.head, event.payload.operating_mode)
);
function subscribeToHead(projectId: string, callback: (headAndMode: HeadAndMode) => void) {
return listen<HeadAndMode>(`project://${projectId}/git/head`, (event) => callback(event.payload));
}

View File

@ -158,7 +158,6 @@
onDestroy(() => {
clearFetchInterval();
modeService.unsubscribe?.();
});
</script>