Giter VIP home page Giter VIP logo

electron-ipc-tunnel's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

electron-ipc-tunnel's Issues

Take it to the next level

Would be awesome to have a generic-like API to take it to the next level. See what I mean.

api.ts:

export interface IpcAPI
{
    methodA(path: string): Promise<boolean>;
    methodB(path: string): Promise<void>;
}

client.ts:

import IpcClient from "electron-ipc-tunnel/client";
import {IpcAPI} from "./api";

class IpcProxy implements ProxyHandler<any>
{
    private readonly client: IpcClient;

    constructor()
    {
        this.client = new IpcClient();
    }

    get (target: any, p: PropertyKey, receiver: any): any
    {
        const zis = this;
        const methodName = p.toString();

        return function()
        {
            const args = [methodName];
            for (const arg of arguments)
            {
                args.push(arg);
            }
            
            return zis.client.send.apply(zis.client, args);
        };
    }
}

export const ipc: IpcAPI = new Proxy<any>({}, new IpcProxy()) as IpcAPI;

server.ts:


import register from "electron-ipc-tunnel/server";
import { IpcAPI } from "./api"

export class IpcServer implements IpcAPI
{
    public async methodA(path: string): Promise<boolean>
    {
        // implementation
    }

    public async methodB(path: string): Promise<void> 
    {
        // implementation
    }
}

const server: IpcServer = new IpcServer();

for (const methodName of Object.getOwnPropertyNames(Object.getPrototypeOf(server)))
{
    if (methodName == "constructor")
        continue;

    register(methodName, (receive: any, ...args: any[]) =>
    {
        return (server as any)[methodName].apply(server, args);
    });
}

In that way you can have IPC communication that acts pretty much undistinguishable from regular method all.

render:

result = await ipc.methodA(...);

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.