orourkek / typesafe-ipc Goto Github PK
View Code? Open in Web Editor NEWA type-only library for adding strict typing to Electron's IPC modules
A type-only library for adding strict typing to Electron's IPC modules
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;
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;
// ...
}
@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 } });
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.