remote branches store

This commit is contained in:
Kiril Videlov 2023-07-03 14:17:43 +03:00 committed by Kiril Videlov
parent a1960673ae
commit ed487ed916
3 changed files with 55 additions and 7 deletions

View File

@ -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
</script>

View File

@ -11,10 +11,6 @@ async function getTargetData(params: { projectId: string }) {
return invoke<Target>('get_target_data', params);
}
async function getRemoteBranchesData(params: { projectId: string }) {
return invoke<Array<BranchData>>('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) {

View File

@ -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<string, Readable<Loadable<BranchData[]>>> = new Map();
export interface RemoteBranchOperations {
setTarget(branch: string): Promise<object>;
}
export function getRemoteBranches(projectId: string): Readable<Loadable<BranchData[]>> {
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<Loadable<BranchData[]>>) {
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<Array<BranchData>>('git_remote_branches_data', { projectId });
}