@musubi/electron-link
v0.15.0
Published
<div style="text-align: center;width:100%"> <h1> Musubi πͺ’ </h1> <strong>End-to-end typesafe communication. π</strong> </div>
Downloads
14
Readme
@musubi/electron-link
Adapter for using Musubi in electron apps.
Documentation
Full documentation for musubi
can be found here.
Installation
# npm
npm install @musubi/electron-link
# Yarn
yarn add @musubi/electron-link
Basic setup
- Initialize your electron app
import { app } from "electron";
import { createMainLink } from "@musubi/electron-link/main";
import { cr } from "@musubi/electron-link/main";
import { schema } from "./schema";
app.on("ready", () => {
const win = new BrowserWindow({
webPreferences: {
// Replace this path with the path to your preload file (more in next step)
preload: "path/to/preload.js"
}
});
const electronLink = createMainLink({
windows: [win]
});
const { receiver, client } = createMusubi({
schema,
clientLinks: [electronLink.client],
receiverLinks: [electronLink.receiver]
});
receiver.handleCommand(...);
receiver.handleQuery(...);
// Send query to specific window, by passing it's ID as last parementer.
// It works with commands and events as well
client.query("testQuery", { test: true }, win.id);
});
Note: By omitting
channel
parameter, message will be sent to all windows, but you will always getvoid
as result
- Expose the IPC to the render process from the preload file:
import { exposeElectronLink } from '@musubi/electron-link/main';
exposeElectronLink();
- Create a client in the renderer process:
import { createRendererLink } from "@musubi/electron-link/renderer";
import { electronSchema } from "./schema";
import { createMusubi } from "@musubi/core";
const link = createRendererLink();
const { receiver, client } = createMusubi({
clientLinks: [link.client],
receiverLinks: [link.receiver],
schema: electronSchema
});
// Send query to main process
client.query(...);
// Send command to main process
client.command(...);
// Handle query from main process
receiver.handleQuery(...);
// Handle command from main process
receiver.handleCommand(...);
// Dispatch even to main process
receiver.dispatchEvent();
- Done! π