npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@electron-buddy/ipc

v0.0.8

Published

electron IPC helper

Downloads

584

Readme

Full-example

Each environment in Electron, namely main and renderer, operates in completely independent spaces. To enable these environments to share resources, Electron provides IPC (Inter-Process Communication). Additionally, Electron connects these environments through preload scripts.

While you can use ipcRenderer and ipcMain to facilitate this communication, managing types separately is necessary because the codebases for these environments must remain distinct. @electron-buddy/ipc was created to solve this problem efficiently.

This library allows you to declare channel, payload, and response globally, enabling seamless type-safe interaction between the main and renderer environments.

⚠️ This docs does not cover the basics of IPC in Electron. Please refer to the Electron documentation for more information.

Installation

npm i @electron-buddy/ipc
yarn add @electron-buddy/ipc
pnpm add @electron-buddy/ipc

Quick Start

electron-buddy.d.ts

Add the following code to your project's root directory or typeRoots in tsconfig.json.

export {};

declare module "@electron-buddy/ipc/main" {
  interface ElectronBuddyInvokeMap extends InvokeMap {}
  interface ElectronBuddyMessageMap extends MessageMap {}
}

declare module "@electron-buddy/ipc/renderer" {
  interface ElectronBuddyInvokeMap extends InvokeMap {}
  interface ElectronBuddyMessageMap extends MessageMap {}
}

/* 
  * Define channel, payload, and response types
  * 
  * channel: string
  * payload: any
  * response: any
  */
type InvokeMap = {
  ping:{
    payload:string,
    response: 'pong',
  }
}

type MessageMap = {
  tick:{
    response:number;
  }
}

preload/index.ts

import { registerIpc } from '@electron-buddy/ipc/preload';

registerIpc();

main/index.ts

import { mainIpc } from '@electron-buddy/ipc/main';

async function main() {
  log.initialize();

  await app.whenReady();

  // ...
  
  mainIpc.handle('ping', async () => { // channel type is 'ping' | ...
    return 'pong'; // response type is 'pong'
  });
  setInterval(() => {
    mainIpc.send(mainWin.webContents, 'tick', 1); // 🟢 channel type is 'tick' | ..., payload type is number
    mainIpc.send(mainWin.webContents, 'tick', '1'); // ❗️ Error: Argument of type 'string' is not assignable to parameter of type 'number'
  }, 1000);
  
}

renderer/App.tsx

Example code using React

import { rendererIpc } from '@electron-buddy/ipc/renderer';
import { useEffect } from 'react';

export default function App() {
  
  useEffect(() => {
    const off = rendererIpc.on('tick', (response) => {
      consoel.log(response); // current timestamp number
    });
    return () => {
      off(); // remove event listener
    };
  }, []);
  
  return (
    <div>
      <h1>App</h1>
      <button
        onClick={async () => {
          const r = await rendererIpc.invoke('ping', null); // ❗️payload must be set event if it is null | undefined
          console.log(r); // 'pong'
        }}
      >
        Ping
      </button>
    </div>
  );
}

Actual Working

mainIpc, rendererIpc is a wrapper of ipcMain, ipcRenderer respectively. It provides type-safe IPC communication between main and renderer processes.

rendererIpc.invoke

rendererIpc.invoke(channel:string, payload:Payload)

// Is equivalent to

ipcRenderer.invoke(channel, payload);

rendererIpc.on

rendererIpc.on(channel:string, listener:(response:Response) => void): ()=> void

// Is equivalent to

ipcRenderer.on(channel, (event, response) => {
  listener(response);
});

mainIpc.handle

mainIpc.handle(channel:string, handler:(payload:Payload) => Promise<any>)

// Is equivalent to 

ipcMain.handle(channel, async (event, payload) => {
  const response = null;
  return response;
});

mainIpc.send

mainIpc.send(webContents:WebContents, channel:string, payload:Payload)

// Is equivalent to

webContents.send(channel, payload);