-
Notifications
You must be signed in to change notification settings - Fork 0
IPC API
Trevor Sears edited this page Feb 23, 2020
·
5 revisions
To register the various IPC handlers (the API, effectively) after each program start, just import registerHandlers
from main/api/ipc-handler
and call it:
import { registerHandlers } from "main/api/ipc-handler";
registerHandlers(window.webContents); // Handlers are now ready to go.
For each 'method' available over the IPC (inter-process communication), there is a 'function name', a parameter type, and a return type. Note that the return type is always promisified (the resolved value of a Promise). In effect, using a given 'function' on the main process's IPC API would look something like:
let argument: ParameterType = /* something */;
// Calling "function-name" on the main process.
let result: ReturnType = await ipcRenderer.invoke("function-name", argument);
Listening for an event from the main process in the renderer process would look something like:
ipcRenderer.on("event-name", (eventInfo: EventInfoType): any => { /* Do stuff */ });
// Not yet written
let argument: unknown = /* something */;
await ipcRenderer.invoke("sign-up", argument); // returns void
let result: boolean = await ipcRenderer.invoke("is-signed-in", undefined);
This must be done before calling any of the below commands.
let argument: { username: string, password: string } = /* something */;
await ipcRenderer.invoke("sign-in", argument); // returns void
let argument: { threadID: string, message: string } = /* something */;
let result: Message = await ipcRenderer.invoke("send-message", argument);
Typings for Message
here.
// Not yet written
let argument: unknown = /* something */;
let result: unknown = await ipcRenderer.invoke("new-conversation", argument);
let result: UserInfo = await ipcRenderer.invoke("get-user-info", undefined);
UserInfo
is an exported type in ipc-handler.ts
.
let argument: string = /* something */;
let result: string[] = await ipcRenderer.invoke("search-users", argument);
let argument: { filePath: string } = /* something */;
await ipcRenderer.invoke("set-user-avatar", argument); // returns void
let argument: { name?: string, tagline?: string } = /* something */;
await ipcRenderer.invoke("set-group-info", argument); // returns void
ipcRenderer.on("new-message", (message: Message): any => {
// handle new message
});
Typings for Message
here.
ipcRenderer.on("group-info-changed", (info: { threadID: string, name: string, tagline: string }): any => {
// handle group info changes
});