Skip to content

A library for constructing an extensible and easier to use API interface for handling IPC communications between an Electron main process and a forked utility process. The framework is modeled on REST architectural patterns with methods and functions familiar to and inspired by Express and Koa.js.

Notifications You must be signed in to change notification settings

mckchan13/Valence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Valence

Valence is a library for constructing an extensible and easy to use API interface for handling IPC communications between an Electron main process and a forked child/utility process. The framework is modeled on REST architectural patterns with methods and functions familiar to and inspired by Express and Koa.js.

Quick Start

  1. Installing Valence
  • Install via npm
npm install valence
  1. Then in your main and child process
// Main process
const path = require("path")
const utilityProcess = require("electron");

const childScriptPath = path.join(__dirname, "./src/child.js");
const child = utilityProcess.fork(childScriptPath);

child.on("message", (event) => {
  // will console log "Hello from the child!" when the child responds
  console.log(event.data)
})

child.postMessage({
  request: { route: "sayHelloWorld", method: "GET" },
});

// Child process
const Valence = require("electron-valence");
const valence = new Valence();

valence.use("sayHelloWorld", (ctx, next) => {
  ctx.respond("Hello World from the child!");
});

valence.listen(() => {
  console.log("Listening on parent port...");
});

How to use

Configuring Datasources

Initiate a Valence instance and configure it with a specified datasource.

const db = require("./src/db")
const Valence = require("electron-valence")

const valence = new Valence({ config : { db } })

valence.use("getAllPosts", async (ctx, next) => {
// db will be available in the the context object
const posts = await ctx.db.getAllPosts();
ctx.send(posts)
})

valence.listen();

Define Prehook and Route Handler Functions

The middleware handler function signature:

function middleware(ctx, next) {
  // handle business logic
  // call next to call the next function
  return next();
}

The first four parameters are the normal parameters for any resolver:

  • ctx: Context object
  • next: Calls the next middleware route handlers.

Prehooks run before any of the other route handlers.

valence.usePrehook((ctx, next) => {
  const { request } = ctx;
  // handle logic to throw error if request is not valid
});

Listening for message events from the parent port

Call the listen method on the valence instance. This will setup all the route handlers, and set the "message" event listener to route incoming messages to the route.

The listen method accepts an optional callback that can be used when the child "message" event listener is set. The port that the child process is listening on is exposed as the first argument of the callback.

valence.listen((port) => {
  const message = "Route handlers set, child is now listening."
  port.postMessage(message)
  console.log("Listening on the parent port")
});

About

A library for constructing an extensible and easier to use API interface for handling IPC communications between an Electron main process and a forked utility process. The framework is modeled on REST architectural patterns with methods and functions familiar to and inspired by Express and Koa.js.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published