Giter VIP home page Giter VIP logo

typesafe-ipc's People

Contributors

dependabot[bot] avatar orourkek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

typesafe-ipc's Issues

Suggestion: Different send/listen definitions for main and renderer

I really like this library concept, and I fully agree with the outlined issue described in the philosophy part. But one thing I think would be a great enhancement would be to be able to define different events which ipcMain and ipcRenderer can send.

I saw that another typings library had that feature, so I forked this and tried to tinker it myself but my understanding of how to work with these types isn't enough.

Basically, I think it'd be helpful to do:

interface EventsFromMain {
  'msg-A': void;
}

interface EventsFromRenderer {
  'msg-B': void;
}

/* First generic indicates what types can be used for send methods, 
second generic indicates what can be used in listener methods */
const ipcRenderer: StrictIpcRenderer<EventsFromRenderer, EventsFromMain > = electron.ipcRenderer;
const ipcMain: StrictIpcMain<EventsFromMain, EventsFromRenderer> = electron.ipcMain;

Incorrect signature for `RemoveAllListeners`

The signature for the removeAllListeners method is incorrectly defined here in typesafe-ipc:

type RemoveAllListenersSignatures<
  ChannelMap extends StrictChannelMap
> = IntersectMethodSignatures<{
  [C in keyof ChannelMap]: (
    channel: C,
    listener: ChannelMap[C] extends void ?
      (event: electron.Event) => void :
      (event: electron.Event, payload: ChannelMap[C]) => void
  ) => void
}>;

Here's the actual signature as defined by electron:

interface IpcMain extends EventEmitter {
  // ...
  removeAllListeners(channel: string): this;
  // ...
}

Use interface augmnentation

@orourkek Please have a look at this lib: https://github.com/zardoy/typed-ipc.

I think it's more easy to use since it uses module augmentation.

For instance, we could rewrite your example in this way:

// ipc schema
declare module "typed-ipc" {
 interface IpcMainEvents {
  'no-payload': void;
  'simple-payload': string;
  'complex-payload': {
    foo: string;
    bar: {
      baz: number;
    }
  };
 }
}
// there is no need to export functions

And use it somewhere in main/renderer process:

// everything is type safe and matches the schema, that we declared above
// main
import { typedIpcMain } from "typed-ipc";

typedIpcMain.addEventListener("complex-payload", async (_event, { foo, bar }) => {
	// ...
});

// renderer
import { typedIpcRenderer } from "typed-ipc";

typedIpcRenderer.send("complex-payload", { foo: "str", bar: { baz: 5 } });

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.