Files
yaak/src-tauri/yaak-sync/index.ts
Gregory Schier f678593903 OAuth 2 (#158)
2025-01-26 13:13:45 -08:00

47 lines
1.2 KiB
TypeScript

import { Channel, invoke } from '@tauri-apps/api/core';
import { emit } from '@tauri-apps/api/event';
import { SyncOp } from './bindings/gen_sync';
import { WatchEvent, WatchResult } from './bindings/gen_watch';
export async function calculateSync(workspaceId: string, syncDir: string) {
return invoke<SyncOp[]>('plugin:yaak-sync|calculate', {
workspaceId,
syncDir,
});
}
export async function calculateSyncFsOnly(dir: string) {
return invoke<SyncOp[]>('plugin:yaak-sync|calculate_fs', { dir });
}
export async function applySync(workspaceId: string, syncDir: string, syncOps: SyncOp[]) {
return invoke<void>('plugin:yaak-sync|apply', {
workspaceId,
syncDir,
syncOps: syncOps,
});
}
export function watchWorkspaceFiles(
workspaceId: string,
syncDir: string,
callback: (e: WatchEvent) => void,
) {
const channel = new Channel<WatchEvent>();
channel.onmessage = callback;
const promise = invoke<WatchResult>('plugin:yaak-sync|watch', {
workspaceId,
syncDir,
channel,
});
return () => {
promise
.then(({ unlistenEvent }) => {
console.log('Cancelling workspace watch', workspaceId, unlistenEvent);
return emit(unlistenEvent);
})
.catch(console.error);
};
}