diff --git a/src/lib/ipc.ts b/src/lib/ipc.ts index 070d0a102..2af36dda5 100644 --- a/src/lib/ipc.ts +++ b/src/lib/ipc.ts @@ -3,15 +3,17 @@ import { invoke as invokeTauri } from '@tauri-apps/api/tauri'; import { listen as listenTauri } from '@tauri-apps/api/event'; export async function invoke(command: string, params: Record = {}): Promise { - return invokeTauri(command, params) - .then((value) => { - console.log(`ipc->${command}(${JSON.stringify(params)})`, value); - return value; - }) - .catch((reason) => { - console.error(`ipc->${command}: ${params}`, reason); - throw reason; - }); + return ( + invokeTauri(command, params) + // .then((value) => { + // console.debug(`ipc->${command}(${JSON.stringify(params)})`, value); + // return value; + // }) + .catch((reason) => { + console.error(`ipc->${command}: ${params}`, reason); + throw reason; + }) + ); } export function listen(event: EventName, handle: EventCallback) { diff --git a/src/lib/vbranches/branchController.ts b/src/lib/vbranches/branchController.ts index f3ea74b53..7a63eb553 100644 --- a/src/lib/vbranches/branchController.ts +++ b/src/lib/vbranches/branchController.ts @@ -3,7 +3,6 @@ import type { Readable } from '@square/svelte-store'; import type { Loadable } from 'svelte-loadable-store'; import type { Branch, BranchData, Target } from './types'; import { toasts } from '$lib'; -import * as ipc from './ipc'; import { invoke } from '$lib/ipc'; export const BRANCH_CONTROLLER_KEY = Symbol(); @@ -18,145 +17,144 @@ export class BranchController { async setTarget(branch: string) { try { - await ipc.setTarget({ projectId: this.projectId, branch }); + await invoke('set_target_branch', { projectId: this.projectId, branch }); await Promise.all([ this.virtualBranchStore.refresh(), this.remoteBranchStore.refresh(), this.targetBranchStore.refresh() ]); } catch (err) { - console.log(err); toasts.error('Failed to set target'); } } async createBranch(branch: { name?: string; ownership?: string; order?: number }) { try { - await ipc.create({ projectId: this.projectId, branch }); + await invoke('create_virtual_branch', { projectId: this.projectId, branch }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to create branch'); } } async commitBranch(branch: string, message: string) { try { - await ipc.commit({ projectId: this.projectId, branch, message }); + await invoke('commit_virtual_branch', { projectId: this.projectId, branch, message }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to commit branch'); } } async updateBranchName(branchId: string, name: string) { try { - await ipc.update({ projectId: this.projectId, branch: { id: branchId, name } }); + await invoke('update_virtual_branch', { + projectId: this.projectId, + branch: { id: branchId, name } + }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to update branch name'); } } async updateBranchOrder(branchId: string, order: number) { try { - await ipc.update({ projectId: this.projectId, branch: { id: branchId, order } }); + await invoke('update_virtual_branch', { + projectId: this.projectId, + branch: { id: branchId, order } + }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to update branch order'); } } async applyBranch(branchId: string) { try { - await ipc.apply({ projectId: this.projectId, branch: branchId }); + await invoke('apply_branch', { projectId: this.projectId, branch: branchId }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to apply branch'); } } async unapplyBranch(branchId: string) { try { - await ipc.unapply({ projectId: this.projectId, branch: branchId }); + await invoke('unapply_branch', { projectId: this.projectId, branch: branchId }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to unapply branch'); } } async updateBranchOwnership(branchId: string, ownership: string) { try { - await ipc.update({ projectId: this.projectId, branch: { id: branchId, ownership } }); + await invoke('update_virtual_branch', { + projectId: this.projectId, + branch: { id: branchId, ownership } + }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to update branch ownership'); } } async pushBranch(branchId: string) { try { - await ipc.push({ projectId: this.projectId, branchId }); + await invoke('push_virtual_branch', { projectId: this.projectId, branchId }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to push branch'); } } async deleteBranch(branchId: string) { try { - await ipc.delete({ projectId: this.projectId, branchId }); + await invoke('delete_virtual_branch', { projectId: this.projectId, branchId }); await this.virtualBranchStore.refresh(); await this.remoteBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error('Failed to delete branch'); } } async updateBranchTarget() { try { - await ipc.updateBranchTarget({ projectId: this.projectId }); + await invoke('update_branch_target', { projectId: this.projectId }); await Promise.all([ this.remoteBranchStore.refresh(), this.virtualBranchStore.refresh(), this.targetBranchStore.refresh() ]); } catch (err) { - console.error(err); toasts.error('Failed to update target'); } } async createvBranchFromBranch(branch: string) { try { - await ipc.createvBranchFromBranch({ projectId: this.projectId, branch }); + await invoke('create_virtual_branch_from_branch', { + projectId: this.projectId, + branch + }); await Promise.all([ await this.remoteBranchStore.refresh(), await this.targetBranchStore.refresh() ]); } catch (err) { - console.error(err); toasts.error('Failed to create virtual branch from branch'); } } async fetchFromTarget() { try { - await ipc.fetchFromTarget({ projectId: this.projectId }); + await invoke('fetch_from_target', { projectId: this.projectId }); await Promise.all([ await this.remoteBranchStore.refresh(), await this.targetBranchStore.refresh() ]); } catch (err) { - console.error(err); toasts.error('Failed to fetch from target'); } } @@ -166,7 +164,6 @@ export class BranchController { await invoke('mark_resolved', { projectId, path }); await this.virtualBranchStore.refresh(); } catch (err) { - console.error(err); toasts.error(`Failed to mark file resolved`); } } diff --git a/src/lib/vbranches/branchStoresCache.ts b/src/lib/vbranches/branchStoresCache.ts index a581c5dc6..72a98d777 100644 --- a/src/lib/vbranches/branchStoresCache.ts +++ b/src/lib/vbranches/branchStoresCache.ts @@ -2,8 +2,9 @@ import { writable, type Loadable, Loaded } from 'svelte-loadable-store'; import type { Readable } from '@square/svelte-store'; import { git } from '$lib/api/ipc'; import { stores } from '$lib'; -import type { Target, Branch, BranchData } from './types'; -import * as ipc from './ipc'; +import { Target, Branch, BranchData } from './types'; +import { plainToInstance } from 'class-transformer'; +import { invoke } from '$lib/ipc'; export interface Refreshable { refresh(): Promise; @@ -20,21 +21,21 @@ export class BranchStoresCache { return cachedStore; } - const writableStore = writable(ipc.listVirtualBranches({ projectId }), (set) => { + const writableStore = writable(listVirtualBranches({ projectId }), (set) => { stores.sessions({ projectId }).subscribe((sessions) => { if (sessions.isLoading) return; if (Loaded.isError(sessions)) return; const lastSession = sessions.value.at(-1); if (!lastSession) return; return stores.deltas({ projectId, sessionId: lastSession.id }).subscribe(() => { - ipc.listVirtualBranches({ projectId }).then(set); + listVirtualBranches({ projectId }).then(set); }); }); }); const refreshableStore = { subscribe: writableStore.subscribe, refresh: async () => { - const newBranches = await ipc.listVirtualBranches({ projectId }); + const newBranches = await listVirtualBranches({ projectId }); return writableStore.set({ isLoading: false, value: newBranches }); } }; @@ -47,17 +48,15 @@ export class BranchStoresCache { if (cachedStore) { return cachedStore; } - const writableStore = writable(ipc.getRemoteBranchesData({ projectId }), (set) => { + const writableStore = writable(getRemoteBranchesData({ projectId }), (set) => { git.fetches.subscribe({ projectId }, () => { - ipc.getRemoteBranchesData({ projectId }).then((branches) => { - set(sortBranchData(branches)); - }); + getRemoteBranchesData({ projectId }).then(set); }); }); const refreshableStore = { subscribe: writableStore.subscribe, refresh: async () => { - const newRemoteBranches = await ipc.getRemoteBranchesData({ projectId }); + const newRemoteBranches = await getRemoteBranchesData({ projectId }); return writableStore.set({ isLoading: false, value: newRemoteBranches }); } }; @@ -70,17 +69,15 @@ export class BranchStoresCache { if (cachedStore) { return cachedStore; } - const writableStore = writable(ipc.getTargetData({ projectId }), (set) => { + const writableStore = writable(getTargetData({ projectId }), (set) => { git.fetches.subscribe({ projectId }, () => { - ipc.getTargetData({ projectId }).then((newTarget) => { - set(newTarget); - }); + getTargetData({ projectId }).then(set); }); }); const refreshableStore = { subscribe: writableStore.subscribe, refresh: async () => { - const newTarget = await ipc.getTargetData({ projectId }); + const newTarget = await getTargetData({ projectId }); return writableStore.set({ isLoading: false, value: newTarget }); } }; @@ -89,6 +86,14 @@ export class BranchStoresCache { } } -function sortBranchData(branchData: BranchData[]): BranchData[] { - return branchData.sort((a, b) => b.lastCommitTs - a.lastCommitTs); +export async function listVirtualBranches(params: { projectId: string }): Promise { + return plainToInstance(Branch, await invoke('list_virtual_branches', params)); +} + +export async function getRemoteBranchesData(params: { projectId: string }): Promise { + return plainToInstance(BranchData, await invoke('git_remote_branches_data', params)); +} + +export async function getTargetData(params: { projectId: string }): Promise { + return plainToInstance(Target, invoke('get_target_data', params)); } diff --git a/src/lib/vbranches/ipc.ts b/src/lib/vbranches/ipc.ts deleted file mode 100644 index 5575ffdc8..000000000 --- a/src/lib/vbranches/ipc.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { invoke } from '$lib/ipc'; -import { plainToInstance } from 'class-transformer'; -import { Branch, BranchData, Target } from './types'; - -export async function listVirtualBranches(params: { projectId: string }): Promise { - const result = await invoke('list_virtual_branches', params); - return sortBranches(plainToInstance(Branch, result)); -} - -export async function create(params: { - projectId: string; - branch: { - name?: string; - ownership?: string; - order?: number; - }; -}) { - return await invoke('create_virtual_branch', params); -} - -export async function commit(params: { projectId: string; branch: string; message: string }) { - return await invoke('commit_virtual_branch', params); -} - -export async function update(params: { - projectId: string; - branch: { - id: string; - order?: number; - ownership?: string; - name?: string; - }; -}) { - return await invoke('update_virtual_branch', params); -} - -async function del(params: { projectId: string; branchId: string }) { - return await invoke('delete_virtual_branch', params); -} -export { del as delete }; - -export async function push(params: { projectId: string; branchId: string }) { - return await invoke('push_virtual_branch', params); -} - -export async function apply(params: { projectId: string; branch: string }) { - return await invoke('apply_branch', params); -} - -export async function unapply(params: { projectId: string; branch: string }) { - return await invoke('unapply_branch', params); -} - -export async function getRemoteBranchesData(params: { projectId: string }) { - return invoke>('git_remote_branches_data', params); -} - -export async function getTargetData(params: { projectId: string }) { - return invoke('get_target_data', params); -} - -export async function setTarget(params: { projectId: string; branch: string }) { - return await invoke('set_target_branch', params); -} - -export async function updateBranchTarget(params: { projectId: string }) { - return invoke('update_branch_target', params); -} - -export async function createvBranchFromBranch(params: { projectId: string; branch: string }) { - return invoke('create_virtual_branch_from_branch', params); -} - -export async function fetchFromTarget(params: { projectId: string }) { - return invoke('fetch_from_target', params); -} - -function sortBranches(branches: Branch[]): Branch[] { - branches.sort((a, b) => a.order - b.order); - return branches; -}