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

v3.0.2

Published

Fluently and type-safely write IPC for Electron

Downloads

424

Readme

electron-ipc-flow

Fluently and type-safely write IPC for Electron.

Just focus on the IPC handlers and calls, without any trivial matters; everything will happen as you envision.

Features

  • Invoke and send just like using a local function.
  • Type declarations are in one place, so you can also use JSDoc.
  • All methods are type-safe.
  • Everything is module, with no window and global.d.ts.
  • By default, the serialize-error library is used to serialize error objects, so you don't have to worry about error handling. You can also customize the error handler.
  • Provide some optional security mechanisms to enhance security.

Install

Use npm: npm install electron-ipc-flow

Use yarn: yarn add electron-ipc-flow

Usage

Easy handle and invoke

// main.ts
import { ipcMain } from 'electron'
import { IpcServerController } from 'electron-ipc-flow'
import { server } from './hello.ts'

IpcServerController.IpcMain = ipcMain

// server.functions is proxy object
server.functions.say = (who) => `Hello ${who}!`
// server.handle('say', (who) => `Hello ${who}!`)

// hello.ts
import { IpcClientController, IpcServerController } from 'electron-ipc-flow'

type Functions = {
  say(who: string): string
}

export const client = new IpcClientController<Functions>('hello')
export const server = new IpcServerController<Functions>('hello')

// preload.ts
import { contextBridge, ipcRenderer } from 'electron/renderer'
import { preloadInit } from 'electron-ipc-flow'
import { client } from './hello.ts'

preloadInit(contextBridge, ipcRenderer, {
  autoRegisterIpcController: false, // Optional, default to true.
})
// If `autoRegister` is false, client controller needs to be register manually.
client.register()

// renderer.ts
import { client } from './hello.ts'

// client.functions is proxy object
console.log(await client.functions.say('World')) // Hello World!
// console.log(await client.invoke('say', 'World')) // Hello World!

Renderer send message to main

// main.ts
import { ipcMain } from 'electron'
import { IpcServerController } from 'electron-ipc-flow'
import { server } from './hello.ts'

IpcServerController.IpcMain = ipcMain

server.on('say', (e, who) => {
  console.log(`Hello ${who}!`) // Hello World!
})

// hello.ts
import { IpcClientController, IpcServerController } from 'electron-ipc-flow'

type ServerEvents = {
  say(who: string): void
}

export const client = new IpcClientController<any, any, ServerEvents>('hello')
export const server = new IpcServerController<any, any, ServerEvents>('hello')

// preload.ts
import { contextBridge, ipcRenderer } from 'electron/renderer'
import { preloadInit } from 'electron-ipc-flow'

preloadInit(contextBridge, ipcRenderer)

// renderer.ts
import { client } from './hello.ts'

client.send('say', 'World')

Main send (broadcasts) message to renderer

// main.ts
import { BrowserWindow } from 'electron'
import { IpcServerController } from 'electron-ipc-flow'
import { server } from './hello.ts'

// Define to send messages to those renderers. (global)
IpcServerController.WebContentsGetter = () => BrowserWindow.getAllWindows().map((win) => win.webContents)
 
server.send('say', 'World')

// hello.ts
import { IpcClientController, IpcServerController } from 'electron-ipc-flow'

type ClientEvents = {
    say(who: string): void
}

export const client = new IpcClientController<any, ClientEvents, any>('hello')
export const server = new IpcServerController<any, ClientEvents, any>('hello')

// preload.ts
import { contextBridge, ipcRenderer } from 'electron/renderer'
import { preloadInit } from 'electron-ipc-flow'

preloadInit(contextBridge, ipcRenderer)

// renderer.ts
import { client } from './hello.ts'

client.on('say', (e, who) => {
  console.log(`Hello ${who}!`) // Hello World!
})

Declaring types using JSDoc

If you don't want to use TypeScript, you can use JSDoc to get type support:

/**
 * @typedef Functions
 * @property {(who: string) => string} say
 */

/** @type {IpcClientController<Functions>} */
const controller = new IpcClientController('hello')

Some editors may not be able to handle it, VSCode will work.

Not using bundler

If you don't want to use bundler, or don't need to do anything else in the preload script, you can use this preload script: node_modules/electron-ipc-flow/dist/preload.js.

Then you can import it in the renderer like this:

import { IpcClientController } from './node_modules/electron-ipc-flow/dist/index.mjs'

API

You can check the definition file dist/index.d.ts for the API and comments.

Debug

The internal debug() method uses the callsites library to obtain the call stack, and callsites internally utilizes the V8 stack trace API.

You can set the environment variable ELECTRON_IPC_FLOW_DEBUG to true to enable debug mode. In debug mode, some data will be output to the console (verbose level).

enable: process.env.ELECTRON_IPC_FLOW_DEBUG = 'true' in main.js first line.

disable: process.env.ELECTRON_IPC_FLOW_DEBUG = 'false' in main.js first line.

You should always disable debug mode on production release ! ! !

Thanks

Thanks to JetBrains for providing the JetBrains IDEs open source license.