Type-safe event handling
Everything is typed: callbacks, request arguments, response values, and errors. Define your event types, and everything in electron-reactive-event will follow.
Type-safe Electron IPC functions, including modern React hooks.
npm install electron-reactive-event --save
...args: any[] Once you create an event declaration type, the functions to send and receive your events will conform to the argument, response, and error types in your declaration.
Intellisense
function send(Channel: "GetBoxData", Request: Box): Promise<BoxData>;type Box = { X: number; Y: number; ... };const Result: BoxData = await send("GetBoxData", { X: ▌When you define your events as types, you are free to include or omit types for request data, response data, and error payload data.
Don't need to send data with your request? No Request argument needed.
Don't need to respond with data? No return statement needed.
Intellisense
function On(Channel: "GetPlayerIDs", Callback: ...): Promise<void>;type Callback = () => Promise<Array<number>>;On("GetPlayerIDs", async () =>
{
const PlayerIDs: Array<number> = // ...
if (PlayerIDs.length > 0)
{
return PlayerIDs▌
}
else
{
throw new ReactiveError("NoPlayersFound");
}
});The hooks provided by electron-reactive-event support modern features, including suspending and transitions.
Most hooks have Deferred counterparts, which are async and open up more options for interacting with main.
Intellisense
function useSend(Channel: "SetPlayerName", Request: string, Suspend: boolean = false): ...;const { Error, IsPending } = useSend("SetPlayerName", "f1ux", true▌For simple events, define your event's errors as string types to convey a simple message.
For events that need it, you can define your events as having an error payload type, to convey deeper information about erroneous state.
Intellisense
function On(..., Callback: Callback<"UploadSubmission">): Promise<void>;type Callback = (Request: Setting) => Promise<...>;On("Resubmit", async () =>
{
try
{
const Api = await GetSubmissionApi();
const Recent = Api.GetRecent();
await Api.Submit(Recent);
}
catch (SubmitError: unknown)
{
if (SubmitError instanceof SubmissionError)
{
throw new ReactiveError(
"SubmitApiFailed",
SubmitError.Reason▌
);
}
throw new ReactiveError("Unspecified");
}
});Still not convinced? Even the boilerplate is handled for you, with our (optional) CLI tool. Call it when you create new event types, or include it in your build step.
$ npx electron-reactive-event-cli register
> electron-reactive-event (version 1.1.0)
> register
✓ Found configuration file in default location!
✓ Found 49 event declarations across 30 modules!
✓ Generated declaration module from your events!
✓ Saved new module at ./src/registrars.types.ts!
✓ The CLI updated your event registrars successfully!
$ ▌