Giter Site home page Giter Site logo

electron-mediator's Introduction

Why use this?

Simplicity

Interprocess communication in Electron is already quite simple, however, you'll often find that as an application grows, so does its complexity. This package aims to simplify the communication between renderer processes and the main process even more, by provided a simple promise-based API for sending messages.

Type-Safety

Improve the scalability of your code with TypeScript. All responses and requests require types to be defined, ensuring that you won't accidentally use an invalid or unexpected value. If you so wish, this package can be used with plain JavaScript too.

Security

This package is designed to be used with a number of secure browser settings, namely, ContextIsolation: true and NodeIntegration: false.

The IpcRenderer Electron module (used for interprocess communication) is NOT arbitrarily exposed since each channel is given its own send/receive functionality. This prevents the client from being able to send messages to any channel on the main process, but only the channels you define.

Usage

Please follow the example below to get started:

  1. Setup types for sending and receiving responses (shared between renderer and main for type-safety):
// this is the structure of a request
type DemoRequest = {
    name: string,
    id: number,
}

// this is the structure of a response
type DemoResponse = {
    result: number
}
  1. In the Main Process, setup the mediator and any channels for communication.
import { MainMediator, IpcChannel, IpcRequest } from "@dylan700/electron-mediator/main";

// this is a channel to handle "demo" requests
class DemoChannel implements IpcChannel<DemoRequest, DemoResponse> {
    public getName(): string {
        return "demo";
    }

    public handle(_event: Electron.IpcMainEvent, request: IpcRequest<DemoRequest>): void {
        // use params like this:
        console.log(request.params.name, request.params.id);
        // return results to the renderer like this
        return {result: 1};
    }
}

// register channels in the main process
MainMediator.registerChannel(new DemoChannel());
  1. In the Electron preload script, call the function below:
import { initPreload } from '@dylan700/electron-mediator/main';
// include the channels as a list of arguments or an array of strings using the spread operator; e.g. initPreload(...["demo", "anotherChannel"])
initPreload("demo"); // multiple channels as defined in main can be supplied here too, e.g. initPreload("demo", "anotherChannel");
  1. Use the mediator in the Renderer process:
import { RendererMediator } from "@dylan700/electron-mediator/renderer";
RendererMediator.send<DemoRequest, DemoResponse>("demo", {name: "Jason Bourne", id: 1}).then((res) => console.log(res.result));

or

import { RendererMediator } from "@dylan700/electron-mediator/renderer";
const result: DemoResponse = await RendererMediator.send<DemoRequest, DemoResponse>("demo", {name: "Jason Bourne", id: 1});

electron-mediator's People

Contributors

dylan700 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.