Remove unnecessary ipc.ts file

This commit is contained in:
Mattias Granlund 2023-07-14 09:51:37 +02:00
parent 8dabbc29d4
commit 4643d4e796
4 changed files with 58 additions and 135 deletions

View File

@ -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<T>(command: string, params: Record<string, unknown> = {}): Promise<T> {
return invokeTauri<T>(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<T>(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<T>(event: EventName, handle: EventCallback<T>) {

View File

@ -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<Target>('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<void>('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<void>('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<void>('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<void>('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<void>('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<void>('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<void>('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<void>('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<void>('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<object>('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<string>('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<void>('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<void>('mark_resolved', { projectId, path });
await this.virtualBranchStore.refresh();
} catch (err) {
console.error(err);
toasts.error(`Failed to mark file resolved`);
}
}

View File

@ -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<void | object>;
@ -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<Branch[]> {
return plainToInstance(Branch, await invoke<any[]>('list_virtual_branches', params));
}
export async function getRemoteBranchesData(params: { projectId: string }): Promise<BranchData[]> {
return plainToInstance(BranchData, await invoke<any[]>('git_remote_branches_data', params));
}
export async function getTargetData(params: { projectId: string }): Promise<Target> {
return plainToInstance(Target, invoke<any>('get_target_data', params));
}

View File

@ -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<Branch[]> {
const result = await invoke<any[]>('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<void>('create_virtual_branch', params);
}
export async function commit(params: { projectId: string; branch: string; message: string }) {
return await invoke<void>('commit_virtual_branch', params);
}
export async function update(params: {
projectId: string;
branch: {
id: string;
order?: number;
ownership?: string;
name?: string;
};
}) {
return await invoke<void>('update_virtual_branch', params);
}
async function del(params: { projectId: string; branchId: string }) {
return await invoke<void>('delete_virtual_branch', params);
}
export { del as delete };
export async function push(params: { projectId: string; branchId: string }) {
return await invoke<void>('push_virtual_branch', params);
}
export async function apply(params: { projectId: string; branch: string }) {
return await invoke<void>('apply_branch', params);
}
export async function unapply(params: { projectId: string; branch: string }) {
return await invoke<void>('unapply_branch', params);
}
export async function getRemoteBranchesData(params: { projectId: string }) {
return invoke<Array<BranchData>>('git_remote_branches_data', params);
}
export async function getTargetData(params: { projectId: string }) {
return invoke<Target | null>('get_target_data', params);
}
export async function setTarget(params: { projectId: string; branch: string }) {
return await invoke<Target>('set_target_branch', params);
}
export async function updateBranchTarget(params: { projectId: string }) {
return invoke<object>('update_branch_target', params);
}
export async function createvBranchFromBranch(params: { projectId: string; branch: string }) {
return invoke<string>('create_virtual_branch_from_branch', params);
}
export async function fetchFromTarget(params: { projectId: string }) {
return invoke<void>('fetch_from_target', params);
}
function sortBranches(branches: Branch[]): Branch[] {
branches.sort((a, b) => a.order - b.order);
return branches;
}