New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

electron-call

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electron-call

The easiest main-renderer IPC communication

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

electron-call

Tests npm version Dependencies status

The easiest main-renderer IPC communication. Now calling a method/function in a renderer process looks the same as calling a local one. It supports both main → renderer and renderer → main calls.

Warning: API could be changed frequently until v0.2.0 release.

Key features

  • Very simple API
  • Typescript friendly
  • Lightweight and fast
  • No dependencies
  • Supports context isolation mode
// MainApi.ts
import { app } from 'electron';
import call from 'electron-call';

export class MainApi {
  async getAppName() {
    return app.getName();
  }
}

call.initialize();
call.provide('MainApi', new MainApi());
// renderer.ts
import call from 'electron-call';
import type { MainApi } from '../main/MainApi';

const mainApi = call.use<MainApi>('MainApi');
console.log(await mainApi.getAppName());

Installation

Install with npm:

npm install electron-call

Usage

Initialization

First of all, electron-call should be able to communicate between main and renderer processes:

call.initialize()

Under the hood call.initialize() attempts to inject a preload script via session.setPreloads(). By default, it only does this for the defaultSession.

Alternatively, you can import electron-call in your preload script.

Providing API

There are 3 ways of defining API:

Using a class

Preferred way, since it provides the best type support

export class FsApi {
  async selectDirectory(defaultPath: string) {
    return dialog.showOpenDialog({
      defaultPath,
      properties: ['openDirectory'],
    });
  }
}

call.provide('FsApi', new FsApi());
Using an object

It works the same as above, but there's a lack of types. That's fine if you don't use TypeScript or prefer a separated interface for ApiName

call.provide('FsApi', {
  async selectDirectory(defaultPath) {
    return dialog.showOpenDialog({
      defaultPath,
      properties: ['openDirectory'],
    });
  },
});
Using a function
call.provideFunction('selectDirectory', async (defaultPath) => {
  return dialog.showOpenDialog({
    defaultPath,
    properties: ['openDirectory'],
  });
});

Consuming API

Using a class/object

You can omit using FsApi generic if you don't need type support

const fsApi = call.use<FsApi>('FsApi');
console.log(await fsApi.selectDirectory(defaultPath));

Also, you can get a remote class constructor instead of an instance

const FsApiProxy = call.use('FsApi') console.log(await new FsApiProxy().selectDirectory(defaultPath));

Using a function
const selectDirectory = call.useFunction('selectDirectory');
console.log(await selectDirectory(defaultPath));

Keywords

FAQs

Package last updated on 11 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc