@rg-dev/electron-helpers 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/TypedIpc.d.cjs +17 -0
- package/dist/TypedIpc.d.d.cts +103 -0
- package/dist/TypedIpc.d.d.ts +103 -0
- package/dist/TypedIpc.d.js +0 -0
- package/dist/backend.cjs +67 -0
- package/dist/backend.d.cts +9 -0
- package/dist/backend.d.ts +9 -0
- package/dist/backend.js +47 -0
- package/dist/client.cjs +84 -0
- package/dist/client.d.cts +46 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.js +62 -0
- package/dist/index.cjs +25 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/package.json +43 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
|
|
15
|
+
// src/TypedIpc.d.ts
|
|
16
|
+
var TypedIpc_d_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(TypedIpc_d_exports);
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { IpcMain, IpcMainEvent, IpcMainInvokeEvent, IpcRenderer, IpcRendererEvent, WebContents } from 'electron';
|
|
2
|
+
|
|
3
|
+
type OptionalPromise<T> = T | Promise<T>;
|
|
4
|
+
|
|
5
|
+
type InputMap = {
|
|
6
|
+
[key: string]: (...args: any) => any;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
interface TypedIpcMain<IpcEvents extends InputMap, IpcCommands extends InputMap
|
|
10
|
+
> extends IpcMain {
|
|
11
|
+
on<K extends keyof IpcEvents>(
|
|
12
|
+
channel: K,
|
|
13
|
+
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
|
|
14
|
+
): this;
|
|
15
|
+
once<K extends keyof IpcEvents>(
|
|
16
|
+
channel: K,
|
|
17
|
+
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
|
|
18
|
+
): this;
|
|
19
|
+
removeListener<K extends keyof IpcEvents>(
|
|
20
|
+
channel: K,
|
|
21
|
+
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
|
|
22
|
+
): this;
|
|
23
|
+
removeAllListeners<K extends keyof IpcEvents>(channel?: K): this;
|
|
24
|
+
handle<K extends keyof IpcCommands>(
|
|
25
|
+
channel: K,
|
|
26
|
+
listener: (
|
|
27
|
+
event: IpcMainInvokeEvent,
|
|
28
|
+
...args: Parameters<IpcCommands[K]>
|
|
29
|
+
) => OptionalPromise<ReturnType<IpcCommands[K]>>
|
|
30
|
+
): void;
|
|
31
|
+
handleOnce<K extends keyof IpcCommands>(
|
|
32
|
+
channel: K,
|
|
33
|
+
listener: (
|
|
34
|
+
event: IpcMainInvokeEvent,
|
|
35
|
+
...args: Parameters<IpcCommands[K]>
|
|
36
|
+
) => OptionalPromise<ReturnType<IpcCommands[K]>>
|
|
37
|
+
): void;
|
|
38
|
+
removeHandler<K extends keyof IpcCommands>(channel: K): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
interface TypedIpcRenderer<IpcEvents extends InputMap, IpcCommands extends InputMap
|
|
45
|
+
> extends IpcRenderer {
|
|
46
|
+
on<K extends keyof IpcEvents>(
|
|
47
|
+
channel: K,
|
|
48
|
+
listener: (
|
|
49
|
+
event: IpcRendererEvent,
|
|
50
|
+
...args: Parameters<IpcEvents[K]>
|
|
51
|
+
) => void
|
|
52
|
+
): this;
|
|
53
|
+
once<K extends keyof IpcEvents>(
|
|
54
|
+
channel: K,
|
|
55
|
+
listener: (
|
|
56
|
+
event: IpcRendererEvent,
|
|
57
|
+
...args: Parameters<IpcEvents[K]>
|
|
58
|
+
) => void
|
|
59
|
+
): this;
|
|
60
|
+
removeListener<K extends keyof IpcEvents>(
|
|
61
|
+
channel: K,
|
|
62
|
+
listener: (
|
|
63
|
+
event: IpcRendererEvent,
|
|
64
|
+
...args: Parameters<IpcEvents[K]>
|
|
65
|
+
) => void
|
|
66
|
+
): this;
|
|
67
|
+
removeAllListeners<K extends keyof IpcEvents>(channel: K): this;
|
|
68
|
+
off<K extends keyof IpcEvents>(channel: K): this;
|
|
69
|
+
send<K extends keyof IpcEvents>(
|
|
70
|
+
channel: K,
|
|
71
|
+
...args: Parameters<IpcEvents[K]>
|
|
72
|
+
): void;
|
|
73
|
+
sendSync<K extends keyof IpcEvents>(
|
|
74
|
+
channel: K,
|
|
75
|
+
...args: Parameters<IpcEvents[K]>
|
|
76
|
+
): ReturnType<IpcEvents[K]>;
|
|
77
|
+
sendTo<K extends keyof IpcEvents>(
|
|
78
|
+
webContentsId: number,
|
|
79
|
+
channel: K,
|
|
80
|
+
...args: Parameters<IpcEvents[K]>
|
|
81
|
+
): void;
|
|
82
|
+
sendToHost<K extends keyof IpcEvents>(
|
|
83
|
+
channel: K,
|
|
84
|
+
...args: Parameters<IpcEvents[K]>
|
|
85
|
+
): void;
|
|
86
|
+
invoke<K extends keyof IpcCommands>(
|
|
87
|
+
channel: K,
|
|
88
|
+
...args: Parameters<IpcCommands[K]>
|
|
89
|
+
): Promise<ReturnType<IpcCommands[K]>>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
interface TypedWebContents<IpcEvents extends InputMap> extends WebContents {
|
|
97
|
+
send<K extends keyof IpcEvents>(
|
|
98
|
+
channel: K,
|
|
99
|
+
...args: Parameters<IpcEvents[K]>
|
|
100
|
+
): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type { TypedIpcMain, TypedIpcRenderer, TypedWebContents };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { IpcMain, IpcMainEvent, IpcMainInvokeEvent, IpcRenderer, IpcRendererEvent, WebContents } from 'electron';
|
|
2
|
+
|
|
3
|
+
type OptionalPromise<T> = T | Promise<T>;
|
|
4
|
+
|
|
5
|
+
type InputMap = {
|
|
6
|
+
[key: string]: (...args: any) => any;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
interface TypedIpcMain<IpcEvents extends InputMap, IpcCommands extends InputMap
|
|
10
|
+
> extends IpcMain {
|
|
11
|
+
on<K extends keyof IpcEvents>(
|
|
12
|
+
channel: K,
|
|
13
|
+
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
|
|
14
|
+
): this;
|
|
15
|
+
once<K extends keyof IpcEvents>(
|
|
16
|
+
channel: K,
|
|
17
|
+
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
|
|
18
|
+
): this;
|
|
19
|
+
removeListener<K extends keyof IpcEvents>(
|
|
20
|
+
channel: K,
|
|
21
|
+
listener: (event: IpcMainEvent, ...args: Parameters<IpcEvents[K]>) => void
|
|
22
|
+
): this;
|
|
23
|
+
removeAllListeners<K extends keyof IpcEvents>(channel?: K): this;
|
|
24
|
+
handle<K extends keyof IpcCommands>(
|
|
25
|
+
channel: K,
|
|
26
|
+
listener: (
|
|
27
|
+
event: IpcMainInvokeEvent,
|
|
28
|
+
...args: Parameters<IpcCommands[K]>
|
|
29
|
+
) => OptionalPromise<ReturnType<IpcCommands[K]>>
|
|
30
|
+
): void;
|
|
31
|
+
handleOnce<K extends keyof IpcCommands>(
|
|
32
|
+
channel: K,
|
|
33
|
+
listener: (
|
|
34
|
+
event: IpcMainInvokeEvent,
|
|
35
|
+
...args: Parameters<IpcCommands[K]>
|
|
36
|
+
) => OptionalPromise<ReturnType<IpcCommands[K]>>
|
|
37
|
+
): void;
|
|
38
|
+
removeHandler<K extends keyof IpcCommands>(channel: K): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
interface TypedIpcRenderer<IpcEvents extends InputMap, IpcCommands extends InputMap
|
|
45
|
+
> extends IpcRenderer {
|
|
46
|
+
on<K extends keyof IpcEvents>(
|
|
47
|
+
channel: K,
|
|
48
|
+
listener: (
|
|
49
|
+
event: IpcRendererEvent,
|
|
50
|
+
...args: Parameters<IpcEvents[K]>
|
|
51
|
+
) => void
|
|
52
|
+
): this;
|
|
53
|
+
once<K extends keyof IpcEvents>(
|
|
54
|
+
channel: K,
|
|
55
|
+
listener: (
|
|
56
|
+
event: IpcRendererEvent,
|
|
57
|
+
...args: Parameters<IpcEvents[K]>
|
|
58
|
+
) => void
|
|
59
|
+
): this;
|
|
60
|
+
removeListener<K extends keyof IpcEvents>(
|
|
61
|
+
channel: K,
|
|
62
|
+
listener: (
|
|
63
|
+
event: IpcRendererEvent,
|
|
64
|
+
...args: Parameters<IpcEvents[K]>
|
|
65
|
+
) => void
|
|
66
|
+
): this;
|
|
67
|
+
removeAllListeners<K extends keyof IpcEvents>(channel: K): this;
|
|
68
|
+
off<K extends keyof IpcEvents>(channel: K): this;
|
|
69
|
+
send<K extends keyof IpcEvents>(
|
|
70
|
+
channel: K,
|
|
71
|
+
...args: Parameters<IpcEvents[K]>
|
|
72
|
+
): void;
|
|
73
|
+
sendSync<K extends keyof IpcEvents>(
|
|
74
|
+
channel: K,
|
|
75
|
+
...args: Parameters<IpcEvents[K]>
|
|
76
|
+
): ReturnType<IpcEvents[K]>;
|
|
77
|
+
sendTo<K extends keyof IpcEvents>(
|
|
78
|
+
webContentsId: number,
|
|
79
|
+
channel: K,
|
|
80
|
+
...args: Parameters<IpcEvents[K]>
|
|
81
|
+
): void;
|
|
82
|
+
sendToHost<K extends keyof IpcEvents>(
|
|
83
|
+
channel: K,
|
|
84
|
+
...args: Parameters<IpcEvents[K]>
|
|
85
|
+
): void;
|
|
86
|
+
invoke<K extends keyof IpcCommands>(
|
|
87
|
+
channel: K,
|
|
88
|
+
...args: Parameters<IpcCommands[K]>
|
|
89
|
+
): Promise<ReturnType<IpcCommands[K]>>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
interface TypedWebContents<IpcEvents extends InputMap> extends WebContents {
|
|
97
|
+
send<K extends keyof IpcEvents>(
|
|
98
|
+
channel: K,
|
|
99
|
+
...args: Parameters<IpcEvents[K]>
|
|
100
|
+
): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type { TypedIpcMain, TypedIpcRenderer, TypedWebContents };
|
|
File without changes
|
package/dist/backend.cjs
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/backend.ts
|
|
20
|
+
var backend_exports = {};
|
|
21
|
+
__export(backend_exports, {
|
|
22
|
+
__electron_helpers: () => __electron_helpers,
|
|
23
|
+
electronHelpers: () => electronHelpers
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(backend_exports);
|
|
26
|
+
var import_electron = require("electron");
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var __electron_helpers = "1";
|
|
30
|
+
|
|
31
|
+
// src/backend.ts
|
|
32
|
+
var electronHelpers = class {
|
|
33
|
+
static isRunningInElectron() {
|
|
34
|
+
return process.versions["electron"] != void 0;
|
|
35
|
+
}
|
|
36
|
+
static async electronSaveFileDialog(type) {
|
|
37
|
+
const result = await import_electron.dialog.showSaveDialog(
|
|
38
|
+
{
|
|
39
|
+
title: "Select a file path",
|
|
40
|
+
defaultPath: ".",
|
|
41
|
+
buttonLabel: "Save",
|
|
42
|
+
filters: [
|
|
43
|
+
{
|
|
44
|
+
name: `${type} File`,
|
|
45
|
+
extensions: [type]
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
if (!(result == null ? void 0 : result.canceled)) {
|
|
51
|
+
return result.filePath;
|
|
52
|
+
} else {
|
|
53
|
+
throw new Error("no path");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
static async electronFilePicker() {
|
|
57
|
+
if (!this.isRunningInElectron()) {
|
|
58
|
+
throw new Error("only work in electron");
|
|
59
|
+
}
|
|
60
|
+
const res = await import_electron.dialog.showOpenDialog({ properties: ["openFile"] });
|
|
61
|
+
if (!res.canceled) {
|
|
62
|
+
return res.filePaths[0];
|
|
63
|
+
} else {
|
|
64
|
+
throw new Error("no file selected");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { __electron_helpers } from './index.cjs';
|
|
2
|
+
|
|
3
|
+
declare class electronHelpers {
|
|
4
|
+
static isRunningInElectron(): boolean;
|
|
5
|
+
static electronSaveFileDialog(type: string): Promise<string>;
|
|
6
|
+
static electronFilePicker(): Promise<string>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { electronHelpers };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { __electron_helpers } from './index.js';
|
|
2
|
+
|
|
3
|
+
declare class electronHelpers {
|
|
4
|
+
static isRunningInElectron(): boolean;
|
|
5
|
+
static electronSaveFileDialog(type: string): Promise<string>;
|
|
6
|
+
static electronFilePicker(): Promise<string>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { electronHelpers };
|
package/dist/backend.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// src/backend.ts
|
|
2
|
+
import { dialog } from "electron";
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var __electron_helpers = "1";
|
|
6
|
+
|
|
7
|
+
// src/backend.ts
|
|
8
|
+
var electronHelpers = class {
|
|
9
|
+
static isRunningInElectron() {
|
|
10
|
+
return process.versions["electron"] != void 0;
|
|
11
|
+
}
|
|
12
|
+
static async electronSaveFileDialog(type) {
|
|
13
|
+
const result = await dialog.showSaveDialog(
|
|
14
|
+
{
|
|
15
|
+
title: "Select a file path",
|
|
16
|
+
defaultPath: ".",
|
|
17
|
+
buttonLabel: "Save",
|
|
18
|
+
filters: [
|
|
19
|
+
{
|
|
20
|
+
name: `${type} File`,
|
|
21
|
+
extensions: [type]
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
if (!(result == null ? void 0 : result.canceled)) {
|
|
27
|
+
return result.filePath;
|
|
28
|
+
} else {
|
|
29
|
+
throw new Error("no path");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
static async electronFilePicker() {
|
|
33
|
+
if (!this.isRunningInElectron()) {
|
|
34
|
+
throw new Error("only work in electron");
|
|
35
|
+
}
|
|
36
|
+
const res = await dialog.showOpenDialog({ properties: ["openFile"] });
|
|
37
|
+
if (!res.canceled) {
|
|
38
|
+
return res.filePaths[0];
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error("no file selected");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
__electron_helpers,
|
|
46
|
+
electronHelpers
|
|
47
|
+
};
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/client.ts
|
|
20
|
+
var client_exports = {};
|
|
21
|
+
__export(client_exports, {
|
|
22
|
+
IpcClient: () => IpcClient,
|
|
23
|
+
__electron_helpers: () => __electron_helpers,
|
|
24
|
+
defineIpcCommands: () => defineIpcCommands,
|
|
25
|
+
defineIpcEvents: () => defineIpcEvents
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(client_exports);
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var __electron_helpers = "1";
|
|
31
|
+
|
|
32
|
+
// src/client.ts
|
|
33
|
+
var defineIpcCommands = () => null;
|
|
34
|
+
var defineIpcEvents = () => null;
|
|
35
|
+
var IpcClient = class _IpcClient {
|
|
36
|
+
ipc;
|
|
37
|
+
/**
|
|
38
|
+
* const client = IpcClient.create(globalThis.ipc, {
|
|
39
|
+
* commands: defineIpcCommands<{ wow: () => string }>(),
|
|
40
|
+
* events: defineIpcEvents<{ message: (content: string) => void }>(),
|
|
41
|
+
* })
|
|
42
|
+
*/
|
|
43
|
+
static create(electronIpc, _shape) {
|
|
44
|
+
const client = new _IpcClient();
|
|
45
|
+
client.ipc = electronIpc;
|
|
46
|
+
return client;
|
|
47
|
+
}
|
|
48
|
+
sendIpc(route, ...args) {
|
|
49
|
+
return new Promise(
|
|
50
|
+
async (resolve, reject) => {
|
|
51
|
+
try {
|
|
52
|
+
const res = await this.ipc.invoke(route, ...args);
|
|
53
|
+
if (!res) {
|
|
54
|
+
resolve(void 0);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (res == null ? void 0 : res._error) {
|
|
58
|
+
const err = typeof res._error !== "string" ? safeJsonStringify(res._error) : res._error;
|
|
59
|
+
reject(err);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
resolve(res._content ?? res);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
reject((e == null ? void 0 : e.message) ?? String(e || "Unknown error"));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
on(channel, listener) {
|
|
70
|
+
this.ipc.on(channel, (_event, ...args) => listener(...args));
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
off(channel, listener) {
|
|
74
|
+
this.ipc.removeListener(channel, (_event, ...args) => listener(...args));
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
function safeJsonStringify(value) {
|
|
79
|
+
try {
|
|
80
|
+
return JSON.stringify(value);
|
|
81
|
+
} catch {
|
|
82
|
+
return String(value);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { TypedIpcRenderer } from './TypedIpc.d.cjs';
|
|
2
|
+
export { __electron_helpers } from './index.cjs';
|
|
3
|
+
import 'electron';
|
|
4
|
+
|
|
5
|
+
type IpcResponse<T> = {
|
|
6
|
+
_content: T;
|
|
7
|
+
_error?: undefined;
|
|
8
|
+
} | {
|
|
9
|
+
_content?: undefined;
|
|
10
|
+
_error: string | any;
|
|
11
|
+
} | undefined | void;
|
|
12
|
+
type EventMap = Record<string, (...args: any[]) => any>;
|
|
13
|
+
type CommandMap = Record<string, (...args: any[]) => IpcResponse<any>>;
|
|
14
|
+
type IpcResponseType<Commands extends CommandMap, cmd extends keyof Commands> = Promise<NonNullable<ReturnType<Commands[cmd]> extends infer R ? R extends {
|
|
15
|
+
_content: infer T;
|
|
16
|
+
} ? T : never : never>>;
|
|
17
|
+
/**
|
|
18
|
+
* Define commands with plain return types:
|
|
19
|
+
*
|
|
20
|
+
* defineIpcCommands<{ wow: () => string }>()
|
|
21
|
+
*/
|
|
22
|
+
declare const defineIpcCommands: <T extends Record<string, (...args: any[]) => any>>() => { [K in keyof T]: (...args: Parameters<T[K]>) => IpcResponse<ReturnType<T[K]>>; };
|
|
23
|
+
/**
|
|
24
|
+
* Define events (renderer <-> main, fire-and-forget):
|
|
25
|
+
*
|
|
26
|
+
* defineIpcEvents<{ message: (content: string) => void }>()
|
|
27
|
+
*/
|
|
28
|
+
declare const defineIpcEvents: <T extends EventMap>() => T;
|
|
29
|
+
declare class IpcClient<Commands extends CommandMap, Events extends EventMap = {}> {
|
|
30
|
+
private ipc;
|
|
31
|
+
/**
|
|
32
|
+
* const client = IpcClient.create(globalThis.ipc, {
|
|
33
|
+
* commands: defineIpcCommands<{ wow: () => string }>(),
|
|
34
|
+
* events: defineIpcEvents<{ message: (content: string) => void }>(),
|
|
35
|
+
* })
|
|
36
|
+
*/
|
|
37
|
+
static create<Commands extends CommandMap, Events extends EventMap = {}>(electronIpc: TypedIpcRenderer<Events, Commands>, _shape: {
|
|
38
|
+
commands: Commands;
|
|
39
|
+
events?: Events;
|
|
40
|
+
}): IpcClient<Commands, Events>;
|
|
41
|
+
sendIpc<cmd extends keyof Commands>(route: cmd, ...args: Parameters<Commands[cmd]>): IpcResponseType<Commands, cmd>;
|
|
42
|
+
on<K extends keyof Events>(channel: K, listener: (...args: Parameters<Events[K]>) => void): this;
|
|
43
|
+
off<K extends keyof Events>(channel: K, listener: (...args: Parameters<Events[K]>) => void): this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { IpcClient, type IpcResponse, defineIpcCommands, defineIpcEvents };
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { TypedIpcRenderer } from './TypedIpc.d.js';
|
|
2
|
+
export { __electron_helpers } from './index.js';
|
|
3
|
+
import 'electron';
|
|
4
|
+
|
|
5
|
+
type IpcResponse<T> = {
|
|
6
|
+
_content: T;
|
|
7
|
+
_error?: undefined;
|
|
8
|
+
} | {
|
|
9
|
+
_content?: undefined;
|
|
10
|
+
_error: string | any;
|
|
11
|
+
} | undefined | void;
|
|
12
|
+
type EventMap = Record<string, (...args: any[]) => any>;
|
|
13
|
+
type CommandMap = Record<string, (...args: any[]) => IpcResponse<any>>;
|
|
14
|
+
type IpcResponseType<Commands extends CommandMap, cmd extends keyof Commands> = Promise<NonNullable<ReturnType<Commands[cmd]> extends infer R ? R extends {
|
|
15
|
+
_content: infer T;
|
|
16
|
+
} ? T : never : never>>;
|
|
17
|
+
/**
|
|
18
|
+
* Define commands with plain return types:
|
|
19
|
+
*
|
|
20
|
+
* defineIpcCommands<{ wow: () => string }>()
|
|
21
|
+
*/
|
|
22
|
+
declare const defineIpcCommands: <T extends Record<string, (...args: any[]) => any>>() => { [K in keyof T]: (...args: Parameters<T[K]>) => IpcResponse<ReturnType<T[K]>>; };
|
|
23
|
+
/**
|
|
24
|
+
* Define events (renderer <-> main, fire-and-forget):
|
|
25
|
+
*
|
|
26
|
+
* defineIpcEvents<{ message: (content: string) => void }>()
|
|
27
|
+
*/
|
|
28
|
+
declare const defineIpcEvents: <T extends EventMap>() => T;
|
|
29
|
+
declare class IpcClient<Commands extends CommandMap, Events extends EventMap = {}> {
|
|
30
|
+
private ipc;
|
|
31
|
+
/**
|
|
32
|
+
* const client = IpcClient.create(globalThis.ipc, {
|
|
33
|
+
* commands: defineIpcCommands<{ wow: () => string }>(),
|
|
34
|
+
* events: defineIpcEvents<{ message: (content: string) => void }>(),
|
|
35
|
+
* })
|
|
36
|
+
*/
|
|
37
|
+
static create<Commands extends CommandMap, Events extends EventMap = {}>(electronIpc: TypedIpcRenderer<Events, Commands>, _shape: {
|
|
38
|
+
commands: Commands;
|
|
39
|
+
events?: Events;
|
|
40
|
+
}): IpcClient<Commands, Events>;
|
|
41
|
+
sendIpc<cmd extends keyof Commands>(route: cmd, ...args: Parameters<Commands[cmd]>): IpcResponseType<Commands, cmd>;
|
|
42
|
+
on<K extends keyof Events>(channel: K, listener: (...args: Parameters<Events[K]>) => void): this;
|
|
43
|
+
off<K extends keyof Events>(channel: K, listener: (...args: Parameters<Events[K]>) => void): this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { IpcClient, type IpcResponse, defineIpcCommands, defineIpcEvents };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var __electron_helpers = "1";
|
|
3
|
+
|
|
4
|
+
// src/client.ts
|
|
5
|
+
var defineIpcCommands = () => null;
|
|
6
|
+
var defineIpcEvents = () => null;
|
|
7
|
+
var IpcClient = class _IpcClient {
|
|
8
|
+
ipc;
|
|
9
|
+
/**
|
|
10
|
+
* const client = IpcClient.create(globalThis.ipc, {
|
|
11
|
+
* commands: defineIpcCommands<{ wow: () => string }>(),
|
|
12
|
+
* events: defineIpcEvents<{ message: (content: string) => void }>(),
|
|
13
|
+
* })
|
|
14
|
+
*/
|
|
15
|
+
static create(electronIpc, _shape) {
|
|
16
|
+
const client = new _IpcClient();
|
|
17
|
+
client.ipc = electronIpc;
|
|
18
|
+
return client;
|
|
19
|
+
}
|
|
20
|
+
sendIpc(route, ...args) {
|
|
21
|
+
return new Promise(
|
|
22
|
+
async (resolve, reject) => {
|
|
23
|
+
try {
|
|
24
|
+
const res = await this.ipc.invoke(route, ...args);
|
|
25
|
+
if (!res) {
|
|
26
|
+
resolve(void 0);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (res == null ? void 0 : res._error) {
|
|
30
|
+
const err = typeof res._error !== "string" ? safeJsonStringify(res._error) : res._error;
|
|
31
|
+
reject(err);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
resolve(res._content ?? res);
|
|
35
|
+
} catch (e) {
|
|
36
|
+
reject((e == null ? void 0 : e.message) ?? String(e || "Unknown error"));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
on(channel, listener) {
|
|
42
|
+
this.ipc.on(channel, (_event, ...args) => listener(...args));
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
off(channel, listener) {
|
|
46
|
+
this.ipc.removeListener(channel, (_event, ...args) => listener(...args));
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function safeJsonStringify(value) {
|
|
51
|
+
try {
|
|
52
|
+
return JSON.stringify(value);
|
|
53
|
+
} catch {
|
|
54
|
+
return String(value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
IpcClient,
|
|
59
|
+
__electron_helpers,
|
|
60
|
+
defineIpcCommands,
|
|
61
|
+
defineIpcEvents
|
|
62
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var src_exports = {};
|
|
21
|
+
__export(src_exports, {
|
|
22
|
+
__electron_helpers: () => __electron_helpers
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(src_exports);
|
|
25
|
+
var __electron_helpers = "1";
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rg-dev/electron-helpers",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "tsup && node dist/index.cjs",
|
|
7
|
+
"build": "tsup && node ./after-build.mjs"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
},
|
|
26
|
+
"./dist/client": {
|
|
27
|
+
"import": "./dist/client.js",
|
|
28
|
+
"require": "./dist/client.cjs"
|
|
29
|
+
},
|
|
30
|
+
"./dist/backend": {
|
|
31
|
+
"import": "./dist/backend.js",
|
|
32
|
+
"require": "./dist/backend.cjs"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^14.18.36",
|
|
37
|
+
"builtin-modules": "^3.3.0",
|
|
38
|
+
"electron": "^34.0.0",
|
|
39
|
+
"execa": "^8.0.1",
|
|
40
|
+
"tsup": "^8.0.2",
|
|
41
|
+
"typescript": "^5.4.5"
|
|
42
|
+
}
|
|
43
|
+
}
|