diff --git a/src/routes/repo/[projectId]/+page.svelte b/src/routes/repo/[projectId]/+page.svelte index 4304c1d22..2b805a731 100644 --- a/src/routes/repo/[projectId]/+page.svelte +++ b/src/routes/repo/[projectId]/+page.svelte @@ -3,6 +3,7 @@ import Tray from './Tray.svelte'; import type { PageData } from './$types'; import { getVirtualBranches } from './vbranches'; + import { getRemoteBranches } from './remoteBranches'; import { Value } from 'svelte-loadable-store'; import hljs from 'highlight.js/lib/core'; @@ -22,12 +23,17 @@ hljs.registerLanguage('python', python); export let data: PageData; - let { projectId, project, target, remoteBranches, remoteBranchesData } = data; + let { projectId, project, target, remoteBranches } = data; const virtualBranches = getVirtualBranches(projectId); $: branches = !$virtualBranches.isLoading && !Value.isError($virtualBranches.value) ? $virtualBranches.value : []; + const remoteBranchesStore = getRemoteBranches(projectId); + $: remoteBranchesData = + !$remoteBranchesStore.isLoading && !Value.isError($remoteBranchesStore.value) + ? $remoteBranchesStore.value + : []; let targetChoice = 'origin/master'; // prob should check if it exists diff --git a/src/routes/repo/[projectId]/+page.ts b/src/routes/repo/[projectId]/+page.ts index 122e66091..c3fc04f58 100644 --- a/src/routes/repo/[projectId]/+page.ts +++ b/src/routes/repo/[projectId]/+page.ts @@ -11,10 +11,6 @@ async function getTargetData(params: { projectId: string }) { return invoke('get_target_data', params); } -async function getRemoteBranchesData(params: { projectId: string }) { - return invoke>('git_remote_branches_data', params); -} - function sortBranchData(branchData: BranchData[]): BranchData[] { // sort remote_branches_data by date return branchData.sort((a, b) => b.lastCommitTs - a.lastCommitTs); @@ -24,9 +20,8 @@ export async function load(e: PageLoadEvent) { const projectId = e.params.projectId; const target = await getTargetData({ projectId }); const remoteBranches = await getRemoteBranches({ projectId }); - const remoteBranchesData = sortBranchData(await getRemoteBranchesData({ projectId })); const project = api.projects.get({ id: projectId }); - return { projectId, target, remoteBranches, remoteBranchesData, project }; + return { projectId, target, remoteBranches, project }; } if (import.meta.vitest) { diff --git a/src/routes/repo/[projectId]/remoteBranches.ts b/src/routes/repo/[projectId]/remoteBranches.ts new file mode 100644 index 000000000..c5a2ea93f --- /dev/null +++ b/src/routes/repo/[projectId]/remoteBranches.ts @@ -0,0 +1,47 @@ +import { invoke } from '$lib/ipc'; +import type { Target, BranchData } from './types'; +import { writable, type Loadable, Value } from 'svelte-loadable-store'; + +import type { Writable, Readable } from '@square/svelte-store'; + +const cache: Map>> = new Map(); + +export interface RemoteBranchOperations { + setTarget(branch: string): Promise; +} + +export function getRemoteBranches(projectId: string): Readable> { + const cachedStore = cache.get(projectId); + if (cachedStore) { + return cachedStore; + } + const writeable = createWriteable(projectId); + + cache.set(projectId, writeable); + return writeable; +} + +function createWriteable(projectId: string) { + return writable(getRemoteBranchesData(projectId), (set) => { + setInterval(() => { + getRemoteBranchesData(projectId).then((branches) => { + set(sortBranchData(branches)); + }); + }, 60000); // poll since we don't have a way to subscribe to changes + }); +} + +function refresh(projectId: string, store: Writable>) { + getRemoteBranchesData(projectId).then((newBranches) => + store.set({ isLoading: false, value: newBranches }) + ); +} + +function sortBranchData(branchData: BranchData[]): BranchData[] { + // sort remote_branches_data by date + return branchData.sort((a, b) => b.lastCommitTs - a.lastCommitTs); +} + +async function getRemoteBranchesData(projectId: string) { + return invoke>('git_remote_branches_data', { projectId }); +}