Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
705a66c938 | |||
45473bb2af | |||
24f38fdc72 | |||
bb5aa2561f | |||
816cd102a2 | |||
8ab5b6f4b7 | |||
dcff23e71d | |||
ab5c3fc8f2 | |||
61e463b66f | |||
487ab07f0e | |||
a1882ce241 | |||
75a59b1650 | |||
c3e4ae18c8 | |||
84a2476a0b | |||
f40c390507 | |||
c41a329087 | |||
007f25fcca |
9
assets/preload.js
Normal file
9
assets/preload.js
Normal file
@ -0,0 +1,9 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron');
|
||||
contextBridge.exposeInMainWorld('electronApi', {
|
||||
sendMessage: async (channelNameArg, payloadArg) => {
|
||||
ipcRenderer.send(channelNameArg, payloadArg);
|
||||
},
|
||||
receiveMessage: (channelNameArg, func) => {
|
||||
ipcRenderer.on(channelNameArg, (event, ...args) => func(...args));
|
||||
},
|
||||
});
|
@ -2,17 +2,29 @@
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"githost": "code.foss.global",
|
||||
"gitscope": "api.global",
|
||||
"gitrepo": "typedelectron",
|
||||
"description": "a package made for ipc communication in electron",
|
||||
"description": "A package designed for Electron IPC (Inter-Process Communication) with strong TypeScript support.",
|
||||
"npmPackagename": "@api.global/typedelectron",
|
||||
"license": "MIT",
|
||||
"projectDomain": "api.global"
|
||||
"projectDomain": "api.global",
|
||||
"keywords": [
|
||||
"Electron",
|
||||
"IPC",
|
||||
"TypeScript",
|
||||
"communication",
|
||||
"typed request",
|
||||
"backend",
|
||||
"preload script"
|
||||
]
|
||||
}
|
||||
},
|
||||
"npmci": {
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"tsdoc": {
|
||||
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||
}
|
||||
}
|
30
package.json
30
package.json
@ -1,16 +1,20 @@
|
||||
{
|
||||
"name": "@api.global/typedelectron",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.10",
|
||||
"private": false,
|
||||
"description": "a package made for ipc communication in electron",
|
||||
"description": "A package designed for Electron IPC (Inter-Process Communication) with strong TypeScript support.",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"exports": {
|
||||
"./ts": "./dist_ts/index.js",
|
||||
"./ts_web": "./dist_ts_web/index.js"
|
||||
},
|
||||
"type": "module",
|
||||
"author": "Task Venture Capital GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web --allowimplicitany)",
|
||||
"build": "(tsbuild --allowimplicitany && tsbuild element --allowimplicitany)",
|
||||
"buildDocs": "(tsdoc)"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -21,7 +25,14 @@
|
||||
"@push.rocks/tapbundle": "^5.0.15",
|
||||
"@types/node": "^20.8.7"
|
||||
},
|
||||
"dependencies": {},
|
||||
"dependencies": {
|
||||
"@api.global/typedrequest": "^3.0.2",
|
||||
"@api.global/typedrequest-interfaces": "^3.0.1",
|
||||
"@push.rocks/smartpath": "^5.0.11"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"electron": ">=28.0.0-beta.11"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://gitlab.com/api.global/typedelectron.git"
|
||||
@ -44,5 +55,14 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"keywords": [
|
||||
"Electron",
|
||||
"IPC",
|
||||
"TypeScript",
|
||||
"communication",
|
||||
"typed request",
|
||||
"backend",
|
||||
"preload script"
|
||||
]
|
||||
}
|
||||
}
|
389
pnpm-lock.yaml
generated
389
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
||||
|
130
readme.md
130
readme.md
@ -1,31 +1,115 @@
|
||||
# @api.global/typedelectron
|
||||
|
||||
a package made for ipc communication in electron
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@api.global/typedelectron)
|
||||
* [gitlab.com (source)](https://gitlab.com/api.global/typedelectron)
|
||||
* [github.com (source mirror)](https://github.com/api.global/typedelectron)
|
||||
* [docs (typedoc)](https://api.global.gitlab.io/typedelectron/)
|
||||
## Install
|
||||
|
||||
## Status for master
|
||||
To start using @api.global/typedelectron in your Electron project, first, you need to add it to your project dependencies using npm:
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
```bash
|
||||
npm install @api.global/typedelectron --save
|
||||
```
|
||||
|
||||
This command will install the package and add it to your `package.json` file.
|
||||
|
||||
## Usage
|
||||
Use TypeScript for best in class intellisense
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
## Legal
|
||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
@api.global/typedelectron is a TypeScript package designed to facilitate IPC (Inter-Process Communication) in Electron applications, leveraging the power of typed requests for enhanced developer experience and code reliability. Below, we provide an exhaustive guide to integrating and utilizing this package in your Electron project, ensuring that your main process and renderer processes communicate seamlessly and efficiently.
|
||||
|
||||
### Setting Up Your Project
|
||||
|
||||
Before diving into the usage of @api.global/typedelectron, ensure your Electron project is set up to support TypeScript and ESM (ECMAScript Modules) syntax. Your `tsconfig.json` should include settings that target ESNext modules, and your Electron version must meet the peer dependency requirement (>=28.0.0-beta.11).
|
||||
|
||||
### Initialization in the Main Process
|
||||
|
||||
To leverage @api.global/typedelectron for handling IPC in the main process of your Electron application, start by importing and initializing the `TypedElectronBackend` class. This class provides the functionalities needed to receive and respond to typed requests from renderer processes.
|
||||
|
||||
```typescript
|
||||
import { app, BrowserWindow } from 'electron';
|
||||
import { TypedElectronBackend } from '@api.global/typedelectron';
|
||||
|
||||
app.on('ready', async () => {
|
||||
const mainWindow = new BrowserWindow({
|
||||
// BrowserWindow options here
|
||||
webPreferences: {
|
||||
preload: YOUR_PRELOAD_SCRIPT,
|
||||
contextIsolation: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Initialize TypedElectronBackend
|
||||
const typedElectronBackend = await TypedElectronBackend.createTypedElectronBackend();
|
||||
|
||||
// Load your HTML file
|
||||
mainWindow.loadFile('path/to/your/index.html');
|
||||
});
|
||||
```
|
||||
|
||||
In the example above, replace `YOUR_PRELOAD_SCRIPT` with the path to your preload script, which should also import and use the `getPreloadScriptPath` provided by the package to ensure typed messages are properly set up.
|
||||
|
||||
### Handling Typed Requests
|
||||
|
||||
The core advantage of @api.global/typedelectron is its ability to process typed requests, making IPC more robust and easier to manage. Let's define a typed request interface:
|
||||
|
||||
```typescript
|
||||
// Define in a shared location accessible by both main and renderer processes
|
||||
export interface IMyTypedRequest {
|
||||
requestType: 'MY_REQUEST';
|
||||
payload: {
|
||||
someData: string;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
In the main process, use the `typedElectronBackend` instance to register handlers for specific request types:
|
||||
|
||||
```typescript
|
||||
typedElectronBackend.typedrouter.onTyped<IMyTypedRequest>('MY_REQUEST', async (request) => {
|
||||
console.log(request.payload.someData);
|
||||
return {
|
||||
response: 'Processed your request',
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
This setup allows the main process to listen for `MY_REQUEST` types and respond accordingly.
|
||||
|
||||
### Sending Requests from the Renderer Process
|
||||
|
||||
Now, let's send a typed request from the Renderer process. Ensure your preload script sets up the context bridge properly, and then use the provided `TypedRequest` class:
|
||||
|
||||
```typescript
|
||||
import { TypedRequest } from '@api.global/typedelectron';
|
||||
|
||||
const myTypedRequest = new TypedRequest<IMyTypedRequest>('MY_REQUEST');
|
||||
myTypedRequest.send({
|
||||
someData: 'Hello from Renderer',
|
||||
}).then((response) => {
|
||||
console.log(response); // Process response
|
||||
});
|
||||
```
|
||||
|
||||
In this example, the renderer process sends a request of type `MY_REQUEST` with some data, and then processes the response received from the main process.
|
||||
|
||||
### Conclusion
|
||||
|
||||
By following the steps and patterns outlined above, you can leverage @api.global/typedelectron to enhance the communication layer of your Electron applications. Typed requests help ensure that the data exchanged between your main and renderer processes is predictable and well-structured, ultimately leading to more reliable and maintainable code.
|
||||
|
||||
## License and Legal Information
|
||||
|
||||
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||
|
||||
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||
|
||||
### Trademarks
|
||||
|
||||
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||
|
||||
### Company Information
|
||||
|
||||
Task Venture Capital GmbH
|
||||
Registered at District court Bremen HRB 35230 HB, Germany
|
||||
|
||||
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||
|
||||
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@api.global/typedelectron',
|
||||
version: '1.0.2',
|
||||
version: '1.0.10',
|
||||
description: 'a package made for ipc communication in electron'
|
||||
}
|
||||
|
34
ts/classes.typedelectronbackend.ts
Normal file
34
ts/classes.typedelectronbackend.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export class TypedElectronBackend {
|
||||
// STATIC
|
||||
public static async createTypedElectronBackend() {
|
||||
return new TypedElectronBackend();
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
|
||||
constructor() {
|
||||
plugins.electron.ipcMain.on('typedrequest', async (eventArg, payloadArg) => {
|
||||
const updatedPayload = await this.typedrouter.routeAndAddResponse({ ...payloadArg }); // TODO: check how to pass on eventArg
|
||||
eventArg.sender.send('typedrequest', updatedPayload);
|
||||
});
|
||||
}
|
||||
|
||||
createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
methodName: T['method'],
|
||||
windowArg: plugins.electron.BrowserWindow
|
||||
) {
|
||||
const typedrequest = new plugins.typedrequest.TypedRequest<T>(
|
||||
new plugins.typedrequest.TypedTarget({
|
||||
typedRouterRef: this.typedrouter,
|
||||
postMethodWithTypedRouter: async (payloadArg: T) => {
|
||||
windowArg.webContents.send('typedrequest', payloadArg);
|
||||
},
|
||||
}),
|
||||
methodName
|
||||
);
|
||||
return typedrequest;
|
||||
}
|
||||
}
|
@ -1,3 +1,6 @@
|
||||
import * as plugins from './typedelectron.plugins.js';
|
||||
export * from './classes.typedelectronbackend.js';
|
||||
|
||||
export let demoExport = 'Hi there! :) This is an exported string';
|
||||
import * as paths from './paths.js';
|
||||
export const getPreloadScriptPath = () => {
|
||||
return paths.preloadScriptPath;
|
||||
}
|
||||
|
9
ts/paths.ts
Normal file
9
ts/paths.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export const packageDir = plugins.path.join(
|
||||
plugins.smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
|
||||
'../',
|
||||
);
|
||||
|
||||
export const assetsDir = plugins.path.join(packageDir, 'assets');
|
||||
export const preloadScriptPath = plugins.path.join(assetsDir, 'preload.js');
|
29
ts/plugins.ts
Normal file
29
ts/plugins.ts
Normal file
@ -0,0 +1,29 @@
|
||||
// node native
|
||||
import * as path from 'path';
|
||||
|
||||
export {
|
||||
path,
|
||||
}
|
||||
|
||||
// @push.rocks scope
|
||||
import * as smartpath from '@push.rocks/smartpath';
|
||||
|
||||
export {
|
||||
smartpath,
|
||||
}
|
||||
|
||||
// @api.global scope
|
||||
import * as typedrequest from '@api.global/typedrequest';
|
||||
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
||||
|
||||
export {
|
||||
typedrequest,
|
||||
typedrequestInterfaces,
|
||||
}
|
||||
|
||||
// third party
|
||||
import * as electron from 'electron';
|
||||
|
||||
export {
|
||||
electron,
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
const removeme = {};
|
||||
export {
|
||||
removeme
|
||||
}
|
8
ts_web/00_commitinfo_data.ts
Normal file
8
ts_web/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@api.global/typedelectron',
|
||||
version: '1.0.10',
|
||||
description: 'a package made for ipc communication in electron'
|
||||
}
|
29
ts_web/classes.typedelectronfrontend.ts
Normal file
29
ts_web/classes.typedelectronfrontend.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import * as plugins from './plugins.js';
|
||||
|
||||
export class TypedElectronFrontend {
|
||||
// STATIC
|
||||
public static async createTypedElectronFrontend() {
|
||||
return new TypedElectronFrontend();
|
||||
}
|
||||
|
||||
// INSTANCE
|
||||
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||
|
||||
constructor() {
|
||||
plugins.electronApi.receiveMessage('typedrequest', (payloadArg) => {
|
||||
this.typedrouter.routeAndAddResponse(payloadArg);
|
||||
});
|
||||
}
|
||||
|
||||
createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||
methodName: T['method'],
|
||||
) {
|
||||
const typedrequest = new plugins.typedrequest.TypedRequest<T>(new plugins.typedrequest.TypedTarget({
|
||||
typedRouterRef: this.typedrouter,
|
||||
postMethodWithTypedRouter: async (payloadArg: T) => {
|
||||
plugins.electronApi.sendMessage('typedrequest', payloadArg);
|
||||
}
|
||||
}), methodName);
|
||||
return typedrequest;
|
||||
}
|
||||
}
|
1
ts_web/index.ts
Normal file
1
ts_web/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './classes.typedelectronfrontend.js';
|
15
ts_web/plugins.ts
Normal file
15
ts_web/plugins.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// @api.global scope
|
||||
import * as typedrequest from '@api.global/typedrequest';
|
||||
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
||||
|
||||
export {
|
||||
typedrequest,
|
||||
typedrequestInterfaces,
|
||||
}
|
||||
|
||||
// electron through preload script
|
||||
const electronApi = (window as any).electronApi;
|
||||
|
||||
export {
|
||||
electronApi,
|
||||
}
|
Reference in New Issue
Block a user