https://github.com/react-gjs/gjs-multiprocess
https://github.com/react-gjs/gjs-multiprocess
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/react-gjs/gjs-multiprocess
- Owner: react-gjs
- License: mit
- Created: 2023-06-03T13:37:14.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-12-10T17:26:29.000Z (7 months ago)
- Last Synced: 2025-04-10T02:47:36.418Z (3 months ago)
- Language: TypeScript
- Size: 33.7 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gjs-multiprocess
A simple API for running child-processes and comunicating with them via DBus.
## Usage
```ts
// worker.jsexport function calculatePi(digits) {
// some calculations
return result;
}
``````ts
// main.jsconst server = await startServer("org.my.app");
const client = await server.createClient("./worker.js");
// note: path to the worker must be relative to the current working directory or absoluteconst pi = await client.invoke.calculatePi(1000);
console.log(pi);
client.terminate();
server.close();
```## Exposing methods to the client
Main process can expose methods to the client by passing an object to the `createClient` function.
```ts
// main.jsconst mainProcessApi = {
// Pass a log message to the Logger class in the main process
log: (message) => Logger.info(message),
};const client = await server.createClient("./worker.js", mainProcessApi);
``````ts
// worker.jsexport function doWork(message) {
try {
// ...
} catch (error) {
Subprocess.invoke.log(error.message);
}
}
```## TypeScript
If you are using TypeScript you will want to have the client and Subprocess fully typed. That cannot be done automatically, since it's impossible atm to infer type definitions from a filepath. But you can fairly easily provide those typings manually:
### Worker Typings
```ts
// worker.tsexport function calculatePi(digits: number): string {
// some calculations
return result;
}export function reverse(str: string): string {
return str.split("").reverse().join("");
}
``````ts
import type * as Worker from "./worker";const server = await startServer("org.my.app");
const client = await server.createClient("./worker.ts");
```### Main Process Typings
In case of the main process exported methods, those must be declared on a interface in the global scope called `MainProcessApi`.
```ts
// main.tsconst mainProcessApi = {
// Pass a log message to the Logger class in the main process
log: (message: string) => Logger.info(message),
};const client = await server.createClient("./worker.ts", mainProcessApi);
``````ts
// worker.tsdeclare global {
// Methods in this interface should match the methods provided by the main process
interface MainProcessApi {
log(message: string): void;
}
}Subprocess.invoke.log("Hello from the worker!");
```