From 4f2dfca32223a7d8174fbe19a34cf4c08990cb13 Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Sat, 16 Mar 2024 15:46:12 +0100 Subject: [PATCH] Clean up variable assigment - just making the code more readable --- gitbutler-ui/src/lib/stores/remoteBranches.ts | 10 ++-- .../src/lib/vbranches/branchStoresCache.ts | 8 +-- .../src/routes/[projectId]/+layout.svelte | 56 +++++++++---------- .../src/routes/[projectId]/board/+page.svelte | 8 +-- .../[projectId]/remote/[sha]/+page.svelte | 10 ++-- .../src/routes/[projectId]/setup/+page.svelte | 4 +- .../stashed/[branchId]/+page.svelte | 15 ++--- 7 files changed, 49 insertions(+), 62 deletions(-) diff --git a/gitbutler-ui/src/lib/stores/remoteBranches.ts b/gitbutler-ui/src/lib/stores/remoteBranches.ts index 4e0bbfd01..02c933c34 100644 --- a/gitbutler-ui/src/lib/stores/remoteBranches.ts +++ b/gitbutler-ui/src/lib/stores/remoteBranches.ts @@ -1,4 +1,5 @@ import { invoke } from '$lib/backend/ipc'; +import { observableToStore } from '$lib/rxjs/store'; import * as toasts from '$lib/utils/toasts'; import { RemoteBranch, RemoteBranchData } from '$lib/vbranches/types'; import { plainToInstance } from 'class-transformer'; @@ -7,14 +8,15 @@ import { Observable, catchError, combineLatest, - of, shareReplay, switchMap } from 'rxjs'; +import type { Readable } from 'svelte/store'; export class RemoteBranchService { + branches: Readable; branches$: Observable; - branchesError$ = new BehaviorSubject(undefined); + error: Readable; private reload$ = new BehaviorSubject(undefined); constructor( @@ -28,11 +30,11 @@ export class RemoteBranchService { shareReplay(1), catchError((e) => { console.error(e); - this.branchesError$.next(e); toasts.error(`Failed load remote branches`); - return of([]); + throw e; }) ); + [this.branches, this.error] = observableToStore(this.branches$); } reload() { diff --git a/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts b/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts index a34080a8e..9c9dc34c7 100644 --- a/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts +++ b/gitbutler-ui/src/lib/vbranches/branchStoresCache.ts @@ -21,13 +21,13 @@ import { startWith, Subject } from 'rxjs'; -import type { Readable } from 'svelte/store'; +import { writable, type Readable } from 'svelte/store'; export class VirtualBranchService { branches$: Observable; stashedBranches$: Observable; activeBranches$: Observable; - branchesError$ = new BehaviorSubject(undefined); + branchesError = writable(); private reload$ = new BehaviorSubject(undefined); private fresh$ = new Subject(); @@ -62,7 +62,7 @@ export class VirtualBranchService { startWith(undefined), shareReplay(1), catchError((err) => { - this.branchesError$.next(err); + this.branchesError.set(err); return []; }) ); @@ -77,7 +77,7 @@ export class VirtualBranchService { } async reload() { - this.branchesError$.next(undefined); + this.branchesError.set(undefined); const fresh = firstValueFrom( this.fresh$.pipe( timeout(10000), diff --git a/gitbutler-ui/src/routes/[projectId]/+layout.svelte b/gitbutler-ui/src/routes/[projectId]/+layout.svelte index afbce8c14..d5165f1c8 100644 --- a/gitbutler-ui/src/routes/[projectId]/+layout.svelte +++ b/gitbutler-ui/src/routes/[projectId]/+layout.svelte @@ -18,18 +18,20 @@ export let data: LayoutData; - $: vbranchService = data.vbranchService; - $: branchesError$ = vbranchService.branchesError$; - $: project$ = data.project$; - $: projectId = data.projectId; - $: baseBranchService = data.baseBranchService; - $: baseBranch$ = baseBranchService.base$; - $: baseError$ = baseBranchService.error$; - $: gbBranchActive$ = data.gbBranchActive$; - $: user$ = data.user$; - $: branchService = data.branchService; - $: branchController = data.branchController; + $: ({ + vbranchService, + project$, + projectId, + baseBranchService, + gbBranchActive$, + user$, + branchService, + branchController + } = data); + + $: branchesError = vbranchService.branchesError; $: baseBranch = baseBranchService.base; + $: baseError = baseBranchService.error; $: setContext(BranchController, branchController); $: setContext(BranchService, branchService); @@ -45,44 +47,40 @@ function setupFetchInterval() { baseBranchService.fetchFromTarget(); clearFetchInterval(); - intervalId = setInterval(() => baseBranchService.fetchFromTarget(), 15 * 60 * 1000); + const intervalMs = 15 * 60 * 1000; // 15 minutes + intervalId = setInterval(() => baseBranchService.fetchFromTarget(), intervalMs); } function clearFetchInterval() { if (intervalId) clearInterval(intervalId); } - $: if ($baseBranch$ === null) { - goto(`/${projectId}/setup`, { replaceState: true }); - } + // TODO: Is this an ok way to redirect? + $: if ($baseBranch === null) goto(`/${projectId}/setup`, { replaceState: true }); onMount(() => { + // Once on load and every time the project id changes + handleMenuActions(data.projectId); return unsubscribe( menuSubscribe(data.projectId), hotkeys.on('Meta+Shift+S', () => syncToCloud(projectId)) ); }); - onDestroy(() => { - clearFetchInterval(); - }); - - $: if (data) { - setContext('hello', data.projectId); - } + onDestroy(() => clearFetchInterval()); {#if !$project$}

Project not found!

-{:else if $baseBranch$ === null} +{:else if $baseBranch === null} -{:else if $baseError$} - -{:else if $branchesError$} - -{:else if !$gbBranchActive$ && $baseBranch$} - +{:else if $baseError} + +{:else if $branchesError} + +{:else if !$gbBranchActive$ && $baseBranch} + {:else if $baseBranch}
diff --git a/gitbutler-ui/src/routes/[projectId]/board/+page.svelte b/gitbutler-ui/src/routes/[projectId]/board/+page.svelte index 58bc45226..5aa51b0cf 100644 --- a/gitbutler-ui/src/routes/[projectId]/board/+page.svelte +++ b/gitbutler-ui/src/routes/[projectId]/board/+page.svelte @@ -14,14 +14,10 @@ const baseBranchService = getContextByClass(BaseBranchService); const baseBranch = baseBranchService.base; - $: vbranchService = data.vbranchService; - $: cloud = data.cloud; - $: projectId = data.projectId; - $: user$ = data.user$; + $: ({ vbranchService, cloud, projectId, user$, project$ } = data); - $: project$ = data.project$; $: activeBranches$ = vbranchService.activeBranches$; - $: error$ = vbranchService.branchesError$; + $: error$ = vbranchService.branchesError; let viewport: HTMLDivElement; let contents: HTMLDivElement; diff --git a/gitbutler-ui/src/routes/[projectId]/remote/[sha]/+page.svelte b/gitbutler-ui/src/routes/[projectId]/remote/[sha]/+page.svelte index 943adbe29..af69ea786 100644 --- a/gitbutler-ui/src/routes/[projectId]/remote/[sha]/+page.svelte +++ b/gitbutler-ui/src/routes/[projectId]/remote/[sha]/+page.svelte @@ -11,17 +11,15 @@ const githubService = getContextByClass(GitHubService); $: project$ = data.project$; - $: remoteBranchService = data.remoteBranchService; - $: branches$ = remoteBranchService.branches$; - $: error$ = remoteBranchService.branchesError$; + $: ({ error, branches } = data.remoteBranchService); - $: branch = $branches$?.find((b) => b.sha == $page.params.sha); + $: branch = $branches?.find((b) => b.sha == $page.params.sha); $: pr = githubService.getPr(branch?.displayName); -{#if $error$} +{#if $error}

Error...

-{:else if !$branches$} +{:else if !$branches} {:else if branch} {#await getRemoteBranches(projectId)} diff --git a/gitbutler-ui/src/routes/[projectId]/stashed/[branchId]/+page.svelte b/gitbutler-ui/src/routes/[projectId]/stashed/[branchId]/+page.svelte index 8fb5c03f9..f5b3f6f4f 100644 --- a/gitbutler-ui/src/routes/[projectId]/stashed/[branchId]/+page.svelte +++ b/gitbutler-ui/src/routes/[projectId]/stashed/[branchId]/+page.svelte @@ -12,16 +12,11 @@ let applyConflictedModal: Modal; let deleteBranchModal: Modal; - $: projectId = data.projectId; - $: user$ = data.user$; - $: cloud = data.cloud; - $: project$ = data.project$; - - $: branchController = data.branchController; - $: vbranchService = data.vbranchService; - + $: ({ projectId, project$, cloud, userService, branchController, vbranchService } = data); $: branches$ = vbranchService.branches$; - $: error$ = vbranchService.branchesError$; + $: error$ = vbranchService.activeBranches$; + $: user = userService.user; + $: branch = $branches$?.find((b) => b.id == $page.params.branchId); @@ -35,7 +30,7 @@ {cloud} project={$project$} isUnapplied={!branch.active} - user={$user$} + user={$user} projectPath={$project$.path} /> {:else}