Fix race condition in listing patch stacks

This commit is contained in:
Caleb Owens 2024-10-17 14:27:28 +02:00
parent 538c19671b
commit 73f458738b
3 changed files with 27 additions and 7 deletions

View File

@ -1,4 +1,5 @@
import { getUserErrorCode, invoke } from '$lib/backend/ipc';
import { ProjectService, type Project } from '$lib/backend/projects';
import { BaseBranchService } from '$lib/baseBranch/baseBranchService';
import { PatchStackCreationService } from '$lib/branch/patchStackCreationService';
import { BranchListingService } from '$lib/branches/branchListing';
@ -22,7 +23,6 @@ import {
} from '@gitbutler/shared/cloud/stacks/service';
import { error } from '@sveltejs/kit';
import { derived } from 'svelte/store';
import { ProjectService, type Project } from '$lib/backend/projects';
import type { LayoutLoad } from './$types';
export const prerender = false;

View File

@ -148,7 +148,13 @@ export interface PatchStackUpdateParams {
}
export class PatchStacksApiService {
constructor(private readonly httpClient: HttpClient) {}
readonly canGetPatchStacks: Readable<boolean>;
readonly canCreatePatchStack: Readable<boolean>;
constructor(private readonly httpClient: HttpClient) {
this.canGetPatchStacks = httpClient.authenticationAvailable;
this.canCreatePatchStack = httpClient.authenticationAvailable;
}
async getPatchStacks(
repositoryId: string,
@ -201,11 +207,16 @@ export class CloudPatchStacksService {
private readonly repositoryId: Readable<string | undefined>,
private readonly patchStacksApiService: PatchStacksApiService
) {
this.#apiPatchStacks = writableDerived<ApiPatchStack[], string | undefined>(
this.repositoryId,
const values = derived(
[this.patchStacksApiService.canGetPatchStacks, this.repositoryId],
(values) => values
);
this.#apiPatchStacks = writableDerived<ApiPatchStack[], [boolean, string | undefined]>(
values,
[],
(repositoryId, set) => {
if (!repositoryId) {
([canGetPatchStacks, repositoryId], set) => {
if (!repositoryId || !canGetPatchStacks) {
set([]);
return;
}

View File

@ -1,4 +1,4 @@
import { get, type Readable } from 'svelte/store';
import { derived, get, type Readable } from 'svelte/store';
export const DEFAULT_HEADERS = {
'Content-Type': 'application/json'
@ -14,12 +14,21 @@ type RequestOptions = {
export class HttpClient {
readonly apiUrl: URL;
/**
* Signals whether authentication is available.
*
* It should be noted that the authentication may be present but invalid.
*/
readonly authenticationAvailable: Readable<boolean>;
constructor(
public fetch = window.fetch,
publicApiBaseUrl: string,
private token: Readable<string | undefined>
) {
this.apiUrl = new URL('/api/', publicApiBaseUrl);
this.authenticationAvailable = derived(token, (token) => !!token);
}
private getApiUrl(path: string) {