@ipsme/reflector-electron-ipc-main
v0.1.3
Published
main.js|electron.js
Downloads
20
Readme
main.js|electron.js
//------------------------------------------------------------------------------------------------- // reflector nsdnc <-> ipc
const Reflector_IPC_main = require('@ipsme/reflector-electron-ipc-main'); const { ipcMain } = require('electron');
const reflector_IPC_main_= new Reflector_IPC_main(ipcMain); reflector_IPC_main_.subscribe();
//-------------------------------------------------------------------------------------------------
function createWindow() { // Create the browser window. const win = new BrowserWindow({ // ... webPreferences: { // nodeIntegration: true, preload: __dirname + '/preload.js' }, });
// ...
reflector_IPC_main_.add_window(win);
win.on('closed', () => {
reflector_IPC_main_.remove_window(win);
// ...
});
}
//------------------------------------------------------------------------------------------------- // preload.js
// https://github.com/electron/electron/issues/9920#issuecomment-947170941
const { contextBridge, ipcRenderer } = require("electron");
// As an example, here we use the exposeInMainWorld API to expose the IPC renderer
// to the main window. They'll be accessible at "window.ipc_reflector".
process.once("loaded", () => {
contextBridge.exposeInMainWorld('ipc_reflector',
{
// https://github.com/electron/electron/issues/21437#issuecomment-573522360
send: (msg) => ipcRenderer.send('ipc-reflector-to-main', msg),
recv: (fn) => {
// Deliberately strip event as it includes sender
ipcRenderer.on('ipc-reflector-to-window', (event, ...args) => fn(...args));
}
}
);
});
// --OR // preload.ts
// https://github.com/electron/electron/issues/9920#issuecomment-947170941
import { IpcRendererEvent } from "electron";
const { contextBridge, ipcRenderer } = require("electron");
// As an example, here we use the exposeInMainWorld API to expose the IPC renderer
// to the main window. They'll be accessible at "window.ipc_reflector".
process.once("loaded", () => {
contextBridge.exposeInMainWorld('ipc_reflector',
{
// https://github.com/electron/electron/issues/21437#issuecomment-573522360
send: (msg : any) => ipcRenderer.send('ipc-reflector-to-main', msg),
recv: (fn : (...args : any[]) => void ) => {
// Deliberately strip event as it includes sender
ipcRenderer.on('ipc-reflector-to-window', (_event : IpcRendererEvent, ...args: any[]) => fn(...args));
}
}
);
});