opvault.js/packages/web/src/utils/ipc-adapter.ts

32 lines
878 B
TypeScript

import { Buffer } from "buffer"
import type { IAdapter } from "opvault.js/src/adapters"
import type { IPC } from "../electron/ipc-types"
import { memoize } from "./memoize"
declare const ipcRenderer: Electron.IpcRenderer
export async function openDirectory() {
return ipc.showDirectoryPicker()
}
export const electronAdapter: IAdapter = {
fs: {
exists: path => ipc.pathExists(path),
readBuffer: path => ipc.readBuffer(path).then(Buffer.from),
readFile: path => ipc.readFile(path),
readdir: path => ipc.readdir(path),
writeFile: (path, data) => ipc.writeFile(path, data),
isDirectory: path => ipc.isDirectory(path),
},
subtle: crypto.subtle,
}
const ipc = new Proxy<IPC>({} as any, {
get: memoize(
(_, channel: string) =>
(...args: any[]) =>
ipcRenderer.invoke(`service-${channel}`, ...args),
(_, name) => name
),
})