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-hub

v1.0.3

Published

Promise backed IPC For Electron & Typescript type prompt

Downloads

9

Readme

english | 简体中文

Table of Contents

Features

  • Simple api
  • Size: less than 4kb gzipped(even smaller with tree-shaking), no external dependencies required
  • Promise support
  • Typescript support: this utility is written in typescript, has type definition inborn
  • hooks: Support the call of communication hook, which is convenient to view communication data and debugger

Install

use yarn

yarn add electron-ipc-hub

or npm

npm install electron-ipc-hub -S

Example:

use typescript

Define type

type RendererToMain = {
  ping: (num: number) => string;
};

type MainToRenderer = {
  "set-title": string;
};

After introducing types, you will get type constraints and prompts

renderer => main => renderer

// main
const mainHub = useMainHub<RendererToMain, MainToRenderer>();

// You will get type constraints and tips below
mainHub.on("get-title", async function (num) {
  return "pong" + num;
});

// renderer
const rendererHub = useRendererHub<RendererToMain, MainToRenderer>();

async function fn() {
  const re = await rendererHub.sendToMain("ping", 1);
  console.log(re); // Print 'pong1'
}
fn();

main => renderer

// main 
mainHub.sendToRenderers("set-title", "This is new title");

// renderer
rendererHub.on("set-title", function (title) {
  document.title = title;
});

use javascript

renderer => main => renderer

// in main
const { useMainHub } = require("electron-ipc-hub");
const mainHub = useMainHub();

mainHub.on("ping", async function (num) {
  return "pong" + num;
});

// in renderer
const { useRendererHub } = require("electron-ipc-hub");
const rendererHub = useRendererHub();

async function fn() {
  const re = await rendererHub.sendToMain("ping", 1);
  console.log(re); Print 'pong1'
}
fn();

main => renderer

// in renderer
rendererHub.on("change-title", (title) => {
  document.title = title;
});

// in main
mainHub.sendToRenderer(win, "change-title", "This is new title");

API

useMainHub

useMainHub([options])

  • options

    • onReceiveBeforeEach (param: ChannelData) => void (optional)
    • onReplyBeforeEach (param: ChannelData) => void (optional)
    • onSendBeforeEach (param: ChannelData) => void (optional)
  • mainHub.on(name, fn)

    • name: string (required) Name of listening event
    • fn: function (required) Execution function of listening event tips: Due to the need to respond to the request from the renderer, the same name can only be bound once, and the duplicate 'name' will overwrite the previous event with the same name
  • mainHub.off(name)

    • name: string (required) Name of listening event
  • mainHub.sendToRenderer(win, name, data)

    • win: BrowserWindow
    • name: string (required) Name of listening event
    • data: string | boolean | number | array | object ...等 (Your custom data type)
  • mainHub.sendToRenderers(name, data)

    • name: string (required)
    • data: string | boolean | number | array | object ...等 (Your custom data type)

useRendererHub

useRendererHub([options])

  • options
    • onReceiveBeforeEach (param: ChannelData) => void (optional)
    • onSendBackBeforeEach (param: ChannelData) => void (optional)
    • onSendBeforeEach (param: ChannelData) => void (optional)
  • rendererHub.on(name, fn)
    • name: string (required) Name of listening event
    • fn: function (required) Execution function of listening event
  • rendererHub.off(name, fn)
    • name: string (required) Name of listening event
    • fn: function (optional), Default empty, If it is empty, all the function events named name above will be removed
  • rendererHub.sendToMain(name, data)
    • name: string (required) Name of listening event
    • data: string | boolean | number | array | object ... (required) (Your custom data type)
    • @return Promise<response> response 为Your custom data type

hooks

The 'options' passed in usemainhub or userendererhub will be executed at a specific stage, for example:

// main
const mainHub = useMainHub({
  onReceiveBeforeEach(data) {
    console.log("<<< [receive]", data);
  },
  onReplyBeforeEach(data) {
    console.log(">>> [reply]", data);
  },
  onSendBeforeEach(data) {
    console.log(">>> [send]", data);
  },
});

// renderer
const rendererHub = useRendererHub({
  onSendBeforeEach: (data) => {
    console.log("<<< [send]", data);
  },
  onSendBackBeforeEach: (data) => {
    console.log("<<< [send back]", data);
  },
  onReceiveBeforeEach: (data) => {
    console.log("<<< [receive]", data);
  },
});


// channel: renderer => main => renderer
// Hooks are executed in the following order
[renderer]onSendBeforeEach -> [main]onReceiveBeforeEach -> [main]onReplyBeforeEach -> [renderer]onSendBackBeforeEach

// channel: main => renderer
// Hooks are executed in the following order
[main]onSendBeforeEach -> [renderer]onReceiveBeforeEach

renderer => main => renderer

// main
mainHub.on("ping", async function (num) {
  return "pong" + num;
});

// renderer
async function fn() {
  const re = await rendererHub.sendToMain("ping", 1);
}
fn();

// print results:
// <<< [send] {name: 'ping', id: '1_1650525606946', data: 1}
// <<< [receive] { name: 'ping', id: '1_1650525606946', data: 1 }
// <<< [reply] { name: 'ping', id: '1_1650525606946', err: null, data: 'pong1' }
// <<< [send back] {name: 'ping', id: '1_1650525606946', err: null, data: 'pong1'}

Debugger with hooks

const rendererHub = useRendererHub({
  onSendBeforeEach: (data) => {
    debugger;
  },
  onSendBackBeforeEach: (data) => {
    debugger;
  },
});