From 1fa737e40f0d9d3976c2d48e528679cf18a9250f Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Wed, 29 Nov 2023 00:58:25 +0100 Subject: [PATCH] Await reload of prs before loading is set to false --- packages/ui/src/lib/github/pullrequest.ts | 31 +++++++++++++++---- packages/ui/src/lib/stores/github.ts | 3 +- .../components/LocalCommits.svelte | 10 +++--- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/ui/src/lib/github/pullrequest.ts b/packages/ui/src/lib/github/pullrequest.ts index a4b761daa..1d6a915a1 100644 --- a/packages/ui/src/lib/github/pullrequest.ts +++ b/packages/ui/src/lib/github/pullrequest.ts @@ -1,6 +1,22 @@ import lscache from 'lscache'; -import { Observable, EMPTY, BehaviorSubject, of } from 'rxjs'; -import { catchError, combineLatestWith, map, shareReplay, switchMap, tap } from 'rxjs/operators'; +import { + Observable, + EMPTY, + BehaviorSubject, + of, + firstValueFrom, + lastValueFrom, + Subject +} from 'rxjs'; +import { + catchError, + combineLatestWith, + map, + shareReplay, + switchMap, + take, + tap +} from 'rxjs/operators'; import { type PullRequest, @@ -14,6 +30,7 @@ export class PrService { error$ = new BehaviorSubject(undefined); private reload$ = new BehaviorSubject<{ skipCache: boolean } | undefined>(undefined); private inject$ = new BehaviorSubject(undefined); + private fresh$ = new Subject(); constructor(ghContext$: Observable) { this.prs$ = ghContext$.pipe( @@ -21,7 +38,9 @@ export class PrService { tap(() => this.error$.next(undefined)), switchMap(([ctx, reload]) => { if (!ctx) return EMPTY; - return loadPrs(ctx, !!reload?.skipCache); + const prs = loadPrs(ctx, !!reload?.skipCache); + this.fresh$.next(); + return prs; }), combineLatestWith(this.inject$), map(([prs, inject]) => { @@ -30,17 +49,17 @@ export class PrService { }), shareReplay(1), catchError((err) => { - console.log(err); this.error$.next(err); return of([]); }) ); } - reload(): void { + reload(): Promise { this.reload$.next({ skipCache: true }); + return firstValueFrom(this.fresh$); } - add(pr: PullRequest) { + insert(pr: PullRequest) { this.inject$.next(pr); } get(branch: string | undefined): Observable | undefined { diff --git a/packages/ui/src/lib/stores/github.ts b/packages/ui/src/lib/stores/github.ts index 016ce2044..0f34887c8 100644 --- a/packages/ui/src/lib/stores/github.ts +++ b/packages/ui/src/lib/stores/github.ts @@ -1,7 +1,7 @@ import type { User } from '$lib/backend/cloud'; import type { GitHubIntegrationContext } from '$lib/github/types'; import type { BaseBranch } from '$lib/vbranches/types'; -import { combineLatest, switchMap, type Observable, of, shareReplay, distinct } from 'rxjs'; +import { combineLatest, switchMap, type Observable, of, shareReplay, distinct, tap } from 'rxjs'; export function getGithubContext( user$: Observable, @@ -18,6 +18,7 @@ export function getGithubContext( const [owner, repo] = remoteUrl.split('.git')[0].split(/\/|:/).slice(-2); return of({ authToken, owner, repo, username }); }), + distinct((val) => JSON.stringify(val)), shareReplay(1) ); } diff --git a/packages/ui/src/routes/[projectId]/components/LocalCommits.svelte b/packages/ui/src/routes/[projectId]/components/LocalCommits.svelte index b4066e4c1..4224526f3 100644 --- a/packages/ui/src/routes/[projectId]/components/LocalCommits.svelte +++ b/packages/ui/src/routes/[projectId]/components/LocalCommits.svelte @@ -68,7 +68,7 @@ isPushing = true; await branchController.pushBranch(branch.id, branch.requiresForce); if (opts?.createPr) { - await sleep(500); + await sleep(200); // Needed by GitHub await createPr(); } isPushing = false; @@ -82,7 +82,7 @@ return `${target.repoBaseUrl}/compare/${baseBranchName}...${branchName}`; } - async function createPr() { + async function createPr(): Promise { if (githubContext && base?.branchName && branchName) { const pr = await createPullRequest( githubContext, @@ -92,10 +92,10 @@ branch.notes ); if (pr) { - prService.add(pr); - prService.reload(); + prService.insert(pr); + await prService.reload(); } - return pr; + return; } }