electron-ipc-utils
v2.0.0
Published
A safe and convenient IPC utility for Electron applications
Maintainers
Readme
electron-ipc-utils
A safe and convenient IPC utility for Electron applications, providing a wrapper around Electron's ipcRenderer with automatic memory leak prevention.
Installation
npm install electron-ipc-utilsUsage
import ipc from 'electron-ipc-utils';
// Invoke a main process handler and wait for result
async function getAppVersion() {
try {
const version = await ipc.invoke<string>('get-app-version');
console.log('App version:', version);
return version;
} catch (error) {
console.error('Failed to get app version:', error);
}
}
// Send a one-way message to main process
function updateUserStatus(status: string) {
ipc.send('update-user-status', status);
}
// Listen for messages from main process
ipc.on<{ progress: number }>('download-progress', (data) => {
console.log(`Download progress: ${data.progress}%`);
});
// Remove listeners when no longer needed
function cleanup() {
ipc.off('download-progress');
// Or cleanup all listeners
// ipc.cleanup();
}API
invoke(channel: string, ...args: any[]): Promise
Call a main process handler and return a promise with the result.
send(channel: string, data?: T)
Send a one-way message to the main process.
on(channel: string, listener: (data: T) => void)
Listen for messages from the main process on a specific channel.
off(channel: string)
Remove all listeners for a specific channel.
cleanup()
Remove all registered listeners to prevent memory leaks.
