UNPKG

electron-ipc-typesafe

Version:

A type-safe IPC (Inter-Process Communication) library for Electron applications with flexible handler registration

2 lines (1 loc) 821 B
"use strict";var e=require("change-case"),r=require("./get-ipc-api.cjs");exports.createIpcEvents=function(n,s){const t=Object.entries(s),a={};t.forEach(([r])=>{a[e.camelCase(r)]=(e,...n)=>{e.webContents.send(r,...n)}});const c={};return t.forEach(([s])=>{const t=`on${e.camelCase(s).charAt(0).toUpperCase()+e.camelCase(s).slice(1)}`;c[t]=e=>{const s=r.getIpcApi(n);if(!s)throw new Error(`IPC with API key ${n} not available, make sure you are in an Electron renderer process, and exposeInPreload has been called in the preload script and '${n}' key exported`);return s[t](e)}}),{exposeInPreload:r=>{const n={};return t.forEach(([s])=>{const t=`on${e.camelCase(s).charAt(0).toUpperCase()+e.camelCase(s).slice(1)}`;n[t]=e=>{const n=(r,...n)=>e(...n);return r.on(s,n),()=>{r.removeListener(s,n)}}}),n},send:a,listeners:c}};