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-ipc-connector

v0.0.12

Published

Utilities for type-safe interprocess communication in electron.

Downloads

7

Readme

electron-ipc-connector

This is a module to reduce Electron boilerplate needed to expose functions from the main process to renderer processes.

Install

npm install electron-ipc-connector

Usage

main.js

import { register } from "electron-ipc-connector";
import fs from "fs/promises";
import path from "path";
import EventEmitter from "events";

export async function createMainWindow() {
  // Create the main window for your application
  const mainWindow = new BrowserWindow({
    minWidth: 1024,
    minHeight: 400,
    webPreferences: {
      // Enable context isolation and disable node integration for security
      contextIsolation: true,
      nodeIntegration: false,
      devTools: true,
      
      // 
      preload: path.resolve(path.join(__dirname, "preload.js")),
      webSecurity: true
    },
  });
  
  // Before loading the renderer code, 
  // register all callbacks that you want to expose from the main process.
  
  // Keep in mind name collisions.
  // It's not recommended to register and expos whole NodeJS modules for security reasons.
  register(fs);
  register(path);
  
  // You can register functions for specific namespaces to avoid name collisions.
  register("my-namespace", {
    helloWorld
  });
  
  // You can also pass EventEmitter instances instead of functions.
  // The renderer process can subscribe to these emitters using `on` and `once` methods.
  const events = new EventEmitter();
  register("my-events", events);
  
  // This will notify all `my-message` listeners in the renderer process.
  events.emit("my-message", "Hello, listener!");
  
  // Start loading the renderer process.
  // This will run the preload script.
  await mainWindow.loadURL(`file://${__dirname}/public/index.html`);
}

function helloWorld() {
  return "Hello, world!";
}

preload.js

import { expose } from "electron-ipc-connector";
import fs from "fs/promises";
import path from "path";
import { helloWorld } from "./main";

// You have to call `expose` once in the preload script to 
// attach registered functions and event emitters to the renderer process.
expose()

app.js

import { connect } from "electron-ipc-connector/browser";

// Get functions to communicate with my-namespace in the main process.
// This callback returns a promise with resolved values 
// from `helloWorld` function declared in `main.js`.
const { helloWorld } = connect("my-namespace");

document.addEventListener("DOMContentLoaded", async () => {
  const message = await helloWorld();
  
  // Prints "Hello, world!"
  console.log(message);
});


// Get event emitters to subscribe to main process events.
// This returns an object with `on` and `once` methods which results can
// be used to remove the attached event handler.
const events = connect("my-events");

let stopListening;

const startButton = document.querySelector("#start");
startButton.addEventListener("click", () => {
  // Attaches an event handler to `my-message` event:
  // When the `emit` method will be called in the main process,
  // this will notify all attached listeners in the renderer process.
  // Returns a callback which can be used to stop listening to events.
  stopListening = events.on("my-message", (message) => {
    console.log(event);
  });
});

const stopButton = document.querySelector("#stop");
stopButton.addEventListener("click", () => {
  stopListening();
});