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

v2.2.4

Published

Run IPC calls with a promise API.

Downloads

1,323

Readme

electron-promise-ipc

Build Status Coverage Status npm version

Promise-y IPC calls in Electron.

Installation

npm install --save electron-promise-ipc

Usage

The most common use case: from the renderer, get data from the main process as a promise.

// in main process
import promiseIpc from 'electron-promise-ipc';
import fsp from 'fs-promise';

promiseIpc.on('writeSettingsFile', (newSettings, event) => {
  return fsp.writeFile('~/.settings', newSettings);
});

// in renderer
import promiseIpc from 'electron-promise-ipc';

promiseIpc
  .send('writeSettingsFile', '{ "name": "Jeff" }')
  .then(() => console.log('You wrote the settings!'))
  .catch((e) => console.error(e));

You can also send data from the main process to a renderer, if you pass in its WebContents object.

// in main process
import promiseIpc from 'electron-promise-ipc';

promiseIpc
  .send('getRendererData', webContentsForRenderer)
  .then((rendererData) => console.log(rendererData))
  .catch((e) => console.error(e));

// in renderer
import promiseIpc from 'electron-promise-ipc';

promiseIpc.on('getRendererData', (event) => {
  return getSomeSuperAwesomeRendererData();
});

Any arguments to send() will be passed directly to the event listener from on(), followed by the IPC event object. If there is an error thrown in the main process's listener, or if the listener returns a rejected promise (e.g., lack of permissions for a file read), then the send() promise is rejected with the same error.

Note that because this is IPC, only JSON-serializable values can be passed as arguments or data. Classes and functions will generally not survive a round of serialization/deserialization.

Preload

As of Electron 5.0, nodeIntegration is disabled by default. This means that you cannot import promiseIpc directly. Instead, you will need to use a preload script when opening a BrowserWindow. Preload scripts can access builtins such as require even if nodeIntegration is disabled.

For convenience, this library provides a preload script which you can require that sets window.promiseIpc.

// preload.js
require('electron-promise-ipc/preload');

Advanced usage

Timeouts

By default, the promise will wait forever for the other process to return it some data. If you want to set a timeout (after which the promise will be rejected automatically), you can create another instance of PromiseIpc like so:

// main process code remains the same
import promiseIpc from 'electron-promise-ipc';

promiseIpc.on('someRoute', () => {
  return someOperationThatNeverCompletesUhOh();
});

// in renderer - timeout is specified on the side that requests the data
import { PromiseIpc } from 'electron-promise-ipc';

const promiseIpc = new PromiseIpc({ maxTimeoutMs: 2000 });

promiseIpc
  .send('someRoute', '{ "name": "Jeff" }')
  .then(() => console.log('You wrote the settings!'))
  .catch((e) => console.error(e)); // will error out after 2 seconds

Removing Listeners

You can remove a listener with the off() method. It's aliased to removeListener() as well.

import promiseIpc from 'electron-promise-ipc';

promiseIpc.on('someRoute', () => {
  return something();
});

promiseIpc.off('someRoute'); // never mind

License

MIT