refresh sessions on vbranch updates

This commit is contained in:
Nikita Galaiko 2023-11-03 14:30:40 +01:00 committed by GitButler
parent 1a5cda3799
commit 5581a476e9
3 changed files with 12 additions and 6 deletions

View File

@ -1,12 +1,13 @@
import { Session, listSessions, subscribeToSessions } from '$lib/backend/sessions';
import { asyncWritable, get, type Loadable, type WritableLoadable } from '@square/svelte-store';
import type { CustomStore } from '$lib/vbranches/types';
import { asyncWritable, get } from '@square/svelte-store';
export function getSessionStore(projectId: string): Loadable<Session[]> {
export function getSessionStore(projectId: string): CustomStore<Session[]> {
const store = asyncWritable(
[],
async () => await listSessions(projectId),
async (data) => data,
{ trackState: true },
{ reloadable: true, trackState: true },
(set) => {
const unsubscribe = subscribeToSessions(projectId, (session) => {
const oldValue = get(store)?.filter((b) => b.id != session.id);
@ -21,6 +22,6 @@ export function getSessionStore(projectId: string): Loadable<Session[]> {
});
return () => unsubscribe();
}
) as WritableLoadable<Session[]>;
) as CustomStore<Session[]>;
return store;
}

View File

@ -8,20 +8,24 @@ import type {
} from './types';
import * as toasts from '$lib/utils/toasts';
import { invoke } from '$lib/backend/ipc';
import type { Session } from '$lib/backend/sessions';
export class BranchController {
constructor(
readonly projectId: string,
readonly virtualBranchStore: VirtualBranchStore<Branch>,
readonly remoteBranchStore: CustomStore<RemoteBranch[] | undefined>,
readonly targetBranchStore: CustomStore<BaseBranch | undefined>
readonly targetBranchStore: CustomStore<BaseBranch | undefined>,
readonly sessionsStore: CustomStore<Session[] | undefined>
) {}
async setTarget(branch: string) {
try {
await invoke<BaseBranch>('set_base_branch', { projectId: this.projectId, branch });
await this.sessionsStore.reload();
await this.targetBranchStore.reload();
} catch (err) {
console.error(err);
toasts.error('Failed to set base branch');
}
}

View File

@ -41,7 +41,8 @@ export const load: LayoutLoad = async ({ params }) => {
projectId,
vbranchStore,
remoteBranchStore,
baseBranchStore
baseBranchStore,
sessionsStore
);
const githubContextStore = getGitHubContextStore(userStore, baseBranchStore);