39
loading...
This website collects cookies to deliver better user experience
rpc
and receive
:contextBridge.exposeInMainWorld("ariNote", {
rpc: (op: {
type: "query" | "mutation" | "subscription";
input: unknown;
path: string;
}) => ipcRenderer.invoke("rpc", op),
receive: (channel: string, func: Function) => {
const validChannels = ["app"];
if (validChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.removeAllListeners(channel);
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
},
appPlatform: process.platform,
});
rpc
API to send those requests and get the response via ipcRenderer.invoke promise resolution. The Main process has a tRPC router that receives the request and resolves to the response. This all is described in more detail in Using React and tRPC with Electron.const workspace = *trpc*.useQuery(["workspace.byId", workspaceId]);
query("byId", {
input: zid,
async resolve({ctx, input: workspaceId}): Promise<Workspace> {
const workspace = await ctx.prisma.workspace.findUnique({ //...
//... omitted for brevity
return {
id: workspaceId,
boxes
}
}
})
useEffect
hook in a top-level component which listens to those events with the exposed ipcRenderer.on:useEffect(() => {
window.ariNote.receive("app", (event) => {
console.log("Received event from main ", event);
handleAction(event);
});
}, [handleAction])
{
label: i18nextMainBackend.t("About"),
click: async () => {
sendToRenderer(mainWindow.webContents, {
action: "about"
});
}
},
autoUpdater.on("download-progress", (progress: ProgressInfo) => {
if (win?.webContents) {
sendToRenderer(win.webContents, {
action: "updateDownloadProgress",
progress
})
}
});