emr-bridge 1.6.2 → 1.7.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.
Files changed (65) hide show
  1. package/cjs/errors.d.ts +28 -0
  2. package/cjs/errors.js +39 -0
  3. package/cjs/exp.d.ts +4 -4
  4. package/cjs/exp.js +16 -16
  5. package/cjs/index.d.ts +4 -4
  6. package/cjs/index.js +15 -15
  7. package/cjs/main.d.ts +4 -4
  8. package/cjs/main.js +15 -15
  9. package/cjs/preload.d.ts +3 -3
  10. package/cjs/preload.js +4 -4
  11. package/cjs/renderer.d.ts +2 -2
  12. package/cjs/renderer.js +2 -2
  13. package/cjs/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
  14. package/cjs/scripts/bridge.js +57 -0
  15. package/{esm/scripts/Main.d.ts → cjs/scripts/main.d.ts} +2 -2
  16. package/cjs/scripts/main.js +72 -0
  17. package/cjs/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
  18. package/cjs/scripts/{newDecorators.js → new-decorators.js} +14 -14
  19. package/cjs/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
  20. package/cjs/scripts/{oldDecorators.js → old-decorators.js} +10 -10
  21. package/cjs/scripts/preload-provider.js +96 -0
  22. package/cjs/scripts/provider.d.ts +25 -3
  23. package/cjs/scripts/provider.js +97 -93
  24. package/cjs/scripts/publish.d.ts +2 -2
  25. package/cjs/scripts/publish.js +36 -33
  26. package/cjs/scripts/renderer-tunnels.d.ts +47 -0
  27. package/cjs/scripts/renderer-tunnels.js +115 -0
  28. package/cjs/types.d.ts +32 -39
  29. package/esm/errors.d.ts +28 -0
  30. package/esm/errors.js +27 -0
  31. package/esm/exp.d.ts +4 -4
  32. package/esm/exp.js +3 -3
  33. package/esm/index.d.ts +4 -4
  34. package/esm/index.js +3 -3
  35. package/esm/main.d.ts +4 -4
  36. package/esm/main.js +3 -3
  37. package/esm/preload.d.ts +3 -3
  38. package/esm/preload.js +2 -2
  39. package/esm/renderer.d.ts +2 -2
  40. package/esm/renderer.js +1 -1
  41. package/esm/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
  42. package/esm/scripts/bridge.js +52 -0
  43. package/{cjs/scripts/Main.d.ts → esm/scripts/main.d.ts} +2 -2
  44. package/esm/scripts/main.js +67 -0
  45. package/esm/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
  46. package/esm/scripts/{newDecorators.js → new-decorators.js} +1 -1
  47. package/esm/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
  48. package/esm/scripts/{oldDecorators.js → old-decorators.js} +1 -1
  49. package/esm/scripts/preload-provider.js +91 -0
  50. package/esm/scripts/provider.d.ts +25 -3
  51. package/esm/scripts/provider.js +96 -93
  52. package/esm/scripts/publish.d.ts +2 -2
  53. package/esm/scripts/publish.js +19 -16
  54. package/esm/scripts/renderer-tunnels.d.ts +47 -0
  55. package/esm/scripts/renderer-tunnels.js +109 -0
  56. package/esm/types.d.ts +32 -39
  57. package/package.json +2 -1
  58. package/cjs/scripts/Bridge.js +0 -58
  59. package/cjs/scripts/Main.js +0 -85
  60. package/cjs/scripts/preloadProvider.js +0 -92
  61. package/esm/scripts/Bridge.js +0 -53
  62. package/esm/scripts/Main.js +0 -80
  63. package/esm/scripts/preloadProvider.js +0 -87
  64. /package/cjs/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
  65. /package/esm/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
@@ -0,0 +1,91 @@
1
+ import electron from 'electron';
2
+ import { IPCChannel } from '../enums.js';
3
+ import IPCTunnel from './renderer-tunnels.js';
4
+ /** Выбрасывает ошибку если используется не в том процессе */
5
+ let mayThrowError = () => { };
6
+ if (typeof window === 'undefined') {
7
+ mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in main process.'); };
8
+ }
9
+ else if (!('contextBridge' in electron)) {
10
+ mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in renderer process.'); };
11
+ }
12
+ /**
13
+ * Sets access to main from renderer
14
+ *
15
+ * @param contextIsolation - _default_: `true`
16
+ */
17
+ function provideFromMain(contextIsolation = true) {
18
+ mayThrowError();
19
+ const { contextBridge, ipcRenderer } = electron;
20
+ const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
21
+ const provider = {
22
+ getInfo: () => info,
23
+ waitPromise(channel, resolve, reject, secret) {
24
+ new IPCTunnel.Promise(channel).wait(secret)
25
+ .then(resolve)
26
+ .catch(reject);
27
+ },
28
+ provided: {
29
+ functions: getFunctions(),
30
+ properties: getProperties(),
31
+ mainEvents: getMainEvents(),
32
+ rendererEvents: getRendererEvents(),
33
+ }
34
+ };
35
+ if (contextIsolation) {
36
+ contextBridge.exposeInMainWorld('__provider__', provider);
37
+ }
38
+ else {
39
+ window.__provider__ = provider;
40
+ }
41
+ function getFunctions() {
42
+ const result = {};
43
+ for (const name of info.functions) {
44
+ result[name] = (secret, args) => {
45
+ return new IPCTunnel.Function(name).call({ args, secret });
46
+ };
47
+ }
48
+ return result;
49
+ }
50
+ function getProperties() {
51
+ const result = {};
52
+ for (const name of info.properties) {
53
+ const tunnel = new IPCTunnel.Property(name);
54
+ result[name] = {
55
+ get: () => tunnel.get(),
56
+ set: (value) => tunnel.set(value)
57
+ };
58
+ }
59
+ return result;
60
+ }
61
+ function getMainEvents() {
62
+ const result = {};
63
+ for (const name of info.mainEvents) {
64
+ result[name] = (type, handler) => {
65
+ const tunnel = new IPCTunnel.MainEvent(name);
66
+ if (type === 'on')
67
+ return tunnel.on(handler);
68
+ else
69
+ return tunnel.once(handler);
70
+ };
71
+ }
72
+ return result;
73
+ }
74
+ function getRendererEvents() {
75
+ const result = {};
76
+ for (const name of info.rendererEvents) {
77
+ result[name] = (arg) => {
78
+ const tunnel = new IPCTunnel.RendererEvent(name);
79
+ if (arg instanceof Promise) {
80
+ arg.then(value => tunnel.emit(value))
81
+ .catch(reason => tunnel.error(String(reason)));
82
+ }
83
+ else {
84
+ tunnel.emit(arg);
85
+ }
86
+ };
87
+ }
88
+ return result;
89
+ }
90
+ }
91
+ export default provideFromMain;
@@ -1,3 +1,25 @@
1
- import type { ICreateProviderArgs, ITarget } from '../types';
2
- declare function createProvider(args: ICreateProviderArgs): ITarget;
3
- export default createProvider;
1
+ import { Scope } from '../enums.js';
2
+ import type { EventHandler, EventUnsubscriber, IPCResult, Info, Target } from '../types.js';
3
+ export default abstract class Provider implements Target {
4
+ protected abstract info: Info;
5
+ protected abstract scope: Scope;
6
+ protected abstract getVariable(name: string): IPCResult;
7
+ protected abstract setVariable(name: string, value: any): IPCResult;
8
+ protected abstract callFunction(name: string, secret: number, args: any[]): IPCResult;
9
+ protected abstract waitPromise(channel: string, secret?: number): Promise<any>;
10
+ protected abstract emitRendererEvent(name: string, arg: any): void;
11
+ protected abstract handleMainEvent(name: string, type: 'on' | 'once', handler: EventHandler<IPCResult>): EventUnsubscriber;
12
+ as<T>(): T;
13
+ protected readonly target: any;
14
+ protected init(): void;
15
+ protected errorHandler(error: string, channel: string): never;
16
+ protected initFunctions(): void;
17
+ protected initRendererEvents(): void;
18
+ protected initMainEvents(): void;
19
+ protected initProperties(): void;
20
+ protected define(name: string, attrs?: {
21
+ get?: () => any;
22
+ set?: (v: any) => void;
23
+ value?: any;
24
+ }): void;
25
+ }
@@ -1,110 +1,113 @@
1
- import { Access } from '../enums';
2
- function errorHandler(error, channel) {
3
- console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
4
- }
5
- function createProvider(args) {
6
- const { info, scope, getVariable, setVariable, callFunction, handleMainEvent, waitPromise, emitRendererEvent } = args;
7
- const target = {
8
- as: () => target
9
- };
10
- function define(name, attrs) {
1
+ import { Access } from '../enums.js';
2
+ import { NoGetAccessError, NoSetAccessError, NotProvidedIntoScopeError, TrySetReadonlyError } from '../errors.js';
3
+ export default class Provider {
4
+ as() { return this.target; }
5
+ target = {};
6
+ init() {
7
+ this.initFunctions();
8
+ this.initProperties();
9
+ this.initMainEvents();
10
+ this.initRendererEvents();
11
+ }
12
+ errorHandler(error, channel) {
13
+ throw new Error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
14
+ }
15
+ initFunctions() {
16
+ for (const name of this.info.functions) {
17
+ this.define(name, {
18
+ value: (...args) => {
19
+ const secret = Math.random();
20
+ const { error, promiseChannel, value } = this.callFunction(name, secret, args);
21
+ if (error) {
22
+ this.errorHandler(error, name);
23
+ }
24
+ if (promiseChannel)
25
+ return this.waitPromise(promiseChannel, secret);
26
+ return value;
27
+ }
28
+ });
29
+ }
30
+ }
31
+ initRendererEvents() {
32
+ for (const name of this.info.rendererEvents) {
33
+ this.define(name, {
34
+ value: (arg) => {
35
+ this.emitRendererEvent(name, arg);
36
+ }
37
+ });
38
+ }
39
+ }
40
+ initMainEvents() {
41
+ for (const name of this.info.mainEvents) {
42
+ const eventNameOn = `on${name[0].toUpperCase()}${name.slice(1)}`;
43
+ const eventNameOnce = `once${name[0].toUpperCase()}${name.slice(1)}`;
44
+ this.define(eventNameOn, {
45
+ value: (handler) => {
46
+ return this.handleMainEvent(name, 'on', ({ error, value }) => {
47
+ if (error) {
48
+ this.errorHandler(error, name);
49
+ }
50
+ handler(value);
51
+ });
52
+ }
53
+ });
54
+ this.define(eventNameOnce, {
55
+ value: (handler) => {
56
+ return this.handleMainEvent(name, 'once', ({ error, value }) => {
57
+ if (error) {
58
+ this.errorHandler(error, name);
59
+ }
60
+ handler(value);
61
+ });
62
+ }
63
+ });
64
+ }
65
+ }
66
+ initProperties() {
67
+ for (const name of this.info.properties) {
68
+ this.define(name, {
69
+ get: this.info.accesses.get(name).has(Access.get)
70
+ ? () => {
71
+ const { error, value } = this.getVariable(name);
72
+ if (error) {
73
+ this.errorHandler(error, name);
74
+ }
75
+ return value;
76
+ }
77
+ : () => { throw new NoGetAccessError(name); },
78
+ set: this.info.accesses.get(name).has(Access.set)
79
+ ? (value) => {
80
+ const { error } = this.setVariable(name, value);
81
+ if (error) {
82
+ this.errorHandler(error, name);
83
+ }
84
+ }
85
+ : () => { throw new NoSetAccessError(name); }
86
+ });
87
+ }
88
+ }
89
+ define(name, attrs) {
11
90
  let descriptor;
12
- if (!info.scopes.get(name).has(scope)) {
91
+ if (!this.info.scopes.get(name).has(this.scope)) {
13
92
  descriptor = {
14
- get() { throw new Error(`'${name}' is not provided into ${scope} scope`); },
15
- set() { throw new Error(`'${name}' is not provided into ${scope} scope`); }
93
+ get: () => { throw new NotProvidedIntoScopeError(name, this.scope); },
94
+ set: () => { throw new NotProvidedIntoScopeError(name, this.scope); }
16
95
  };
17
96
  }
18
97
  else if (attrs?.value) {
19
98
  descriptor = {
20
99
  get: () => attrs.value,
21
- set: () => { throw new Error(`'${name}' is readonly`); },
100
+ set: () => { throw new TrySetReadonlyError(name); },
22
101
  enumerable: true
23
102
  };
24
103
  }
25
104
  else {
26
105
  descriptor = {
27
- get: attrs?.get || (() => { throw new Error(`No access to getting '${name}'`); }),
28
- set: attrs?.set || (() => { throw new Error(`'${name}' is readonly`); }),
106
+ get: attrs?.get || (() => { throw new NoSetAccessError(name); }),
107
+ set: attrs?.set || (() => { throw new TrySetReadonlyError(name); }),
29
108
  enumerable: true
30
109
  };
31
110
  }
32
- Object.defineProperty(target, name, descriptor);
111
+ Object.defineProperty(this.target, name, descriptor);
33
112
  }
34
- info.functions.forEach(funcName => {
35
- define(funcName, {
36
- value(...args) {
37
- const result = callFunction(funcName, ...args);
38
- if (result.error) {
39
- errorHandler(result.error, funcName);
40
- return;
41
- }
42
- if (result.promiseChannel)
43
- return waitPromise(result.promiseChannel);
44
- return result.value;
45
- }
46
- });
47
- });
48
- info.rendererEvents.forEach(eventName => {
49
- define(eventName, {
50
- value(arg) {
51
- emitRendererEvent(eventName, arg);
52
- }
53
- });
54
- });
55
- info.mainEvents.forEach(mainEventName => {
56
- const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
57
- const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
58
- define(eventNameOn, {
59
- value(handler) {
60
- return handleMainEvent(mainEventName, 'on', (result) => {
61
- if (result.error) {
62
- errorHandler(result.error, mainEventName);
63
- return;
64
- }
65
- if (result.promiseChannel)
66
- waitPromise(result.promiseChannel).then(handler);
67
- handler(result.value);
68
- });
69
- }
70
- });
71
- define(eventNameOnce, {
72
- value(handler) {
73
- return handleMainEvent(mainEventName, 'once', (result) => {
74
- if (result.error) {
75
- errorHandler(result.error, mainEventName);
76
- return;
77
- }
78
- if (result.promiseChannel)
79
- waitPromise(result.promiseChannel).then(handler);
80
- handler(result.value);
81
- });
82
- }
83
- });
84
- });
85
- info.properties.forEach(propName => {
86
- define(propName, {
87
- get: info.accesses.get(propName).has(Access.get)
88
- ? () => {
89
- const result = getVariable(propName);
90
- if (result.error) {
91
- errorHandler(result.error, propName);
92
- return;
93
- }
94
- return result.value;
95
- }
96
- : () => { throw new Error(`No access to getting the '${propName}' property`); },
97
- set: info.accesses.get(propName).has(Access.set)
98
- ? (value) => {
99
- const result = setVariable(propName, value);
100
- if (result?.error) {
101
- errorHandler(result.error, propName);
102
- return;
103
- }
104
- }
105
- : () => { throw new Error(`No access to setting the '${propName}' property`); }
106
- });
107
- });
108
- return target;
109
113
  }
110
- export default createProvider;
@@ -1,5 +1,5 @@
1
- import { Access, Scope } from '../enums';
2
- import type { PublicFunction, PublicProperty, RendererEvent } from '../types';
1
+ import { Access, Scope } from '../enums.js';
2
+ import type { PublicFunction, PublicProperty, RendererEvent } from '../types.js';
3
3
  /**
4
4
  * Makes the event from **main** available for **renderer** and **preload** processes.
5
5
  *
@@ -1,5 +1,5 @@
1
1
  import electron from 'electron';
2
- import { Access, IPCChannel, Scope } from '../enums';
2
+ import { Access, IPCChannel, Scope } from '../enums.js';
3
3
  const publicInfo = {
4
4
  properties: new Set(),
5
5
  functions: new Set(),
@@ -95,13 +95,14 @@ export function publicRendererEvent(name, procFn = ((v) => v), scopes = [Scope.p
95
95
  const handlers = new Set();
96
96
  const handlersOnce = new Set();
97
97
  rendererEvents.add(name);
98
- if (!publicInfo.scopes.has(name))
98
+ if (!publicInfo.scopes.has(name)) {
99
99
  publicInfo.scopes.set(name, new Set(scopes));
100
+ }
100
101
  ipcMain.removeAllListeners(rendererEmitChannel);
101
- ipcMain.on(rendererEmitChannel, (_, result) => {
102
- if (result.error)
103
- throw new Error(result.error);
104
- const converted = procFn(result.value);
102
+ ipcMain.on(rendererEmitChannel, (_, { error, value }) => {
103
+ if (error)
104
+ throw new Error(error);
105
+ const converted = procFn(value);
105
106
  handlers.forEach(handler => handler(converted));
106
107
  handlersOnce.forEach(handler => handler(converted));
107
108
  handlersOnce.clear();
@@ -132,17 +133,18 @@ export function publicFunction(name, func, scopes = [Scope.preload, Scope.render
132
133
  const { functions } = publicInfo;
133
134
  const channel = IPCChannel.functionCall + name;
134
135
  functions.add(name);
135
- if (!publicInfo.scopes.has(name))
136
+ if (!publicInfo.scopes.has(name)) {
136
137
  publicInfo.scopes.set(name, new Set(scopes));
138
+ }
137
139
  ipcMain.removeAllListeners(channel);
138
- ipcMain.on(channel, (e, ...args) => {
140
+ ipcMain.on(channel, (e, { args, secret }) => {
139
141
  try {
140
142
  const result = func(...args);
141
143
  if (result instanceof Promise) {
142
144
  const promiseChannel = channel + IPCChannel.promisePostfix;
143
145
  result
144
- .then(value => e.sender.send(promiseChannel, { value }))
145
- .catch(reason => e.sender.send(promiseChannel, { error: String(reason?.stack || reason) }));
146
+ .then(value => e.sender.send(promiseChannel, { value, secret }))
147
+ .catch(reason => e.sender.send(promiseChannel, { error: String(reason?.stack || reason), secret }));
146
148
  e.returnValue = { promiseChannel };
147
149
  }
148
150
  else {
@@ -171,8 +173,9 @@ export function publicVariable(name, value, scopes = [Scope.renderer, Scope.prel
171
173
  const setChannel = IPCChannel.propertySet + name;
172
174
  const { get: getter, set: setter } = value;
173
175
  properties.add(name);
174
- if (!publicInfo.scopes.has(name))
176
+ if (!publicInfo.scopes.has(name)) {
175
177
  publicInfo.scopes.set(name, new Set(scopes));
178
+ }
176
179
  if (!publicInfo.accesses.has(name)) {
177
180
  const accesses = [];
178
181
  getter && accesses.push(Access.get);
@@ -186,25 +189,25 @@ export function publicVariable(name, value, scopes = [Scope.renderer, Scope.prel
186
189
  }
187
190
  if (getter) {
188
191
  ipcMain.removeAllListeners(getChannel);
189
- ipcMain.on(getChannel, event => {
192
+ ipcMain.on(getChannel, (event) => {
190
193
  try {
191
194
  const result = getter();
192
195
  event.returnValue = { value: result };
193
196
  }
194
197
  catch (error) {
195
- event.returnValue = { error };
198
+ event.returnValue = { error: String(error?.stack || error) };
196
199
  }
197
200
  });
198
201
  }
199
202
  if (setter) {
200
203
  ipcMain.removeAllListeners(setChannel);
201
- ipcMain.on(setChannel, (event, ...args) => {
204
+ ipcMain.on(setChannel, (event, { args }) => {
202
205
  try {
203
- setter(args[0]);
206
+ setter(args.at(0));
204
207
  event.returnValue = { value: null };
205
208
  }
206
209
  catch (error) {
207
- event.returnValue = { error };
210
+ event.returnValue = { error: String(error?.stack || error) };
208
211
  }
209
212
  });
210
213
  }
@@ -0,0 +1,47 @@
1
+ import type { IpcRendererEvent } from 'electron';
2
+ import type { EventHandler, EventUnsubscriber, IPCRequest, IPCResult } from '../types.js';
3
+ declare class FunctionTunnel {
4
+ #private;
5
+ constructor(name: string);
6
+ call(args?: IPCRequest): IPCResult;
7
+ }
8
+ declare class PropertyTunnel {
9
+ #private;
10
+ constructor(name: string);
11
+ get(): IPCResult;
12
+ set(value: any): IPCResult;
13
+ }
14
+ declare class PromiseTunnel {
15
+ #private;
16
+ constructor(channel: string);
17
+ wait(secret?: number): Promise<any>;
18
+ }
19
+ declare class MainEventTunnel {
20
+ #private;
21
+ constructor(name: string);
22
+ on(handler: EventHandler<IPCResult>): EventUnsubscriber;
23
+ once(handler: EventHandler<IPCResult>): EventUnsubscriber;
24
+ }
25
+ declare class RendererEventTunnel {
26
+ #private;
27
+ constructor(name: string);
28
+ emit(value: any): void;
29
+ error(error: string): void;
30
+ }
31
+ export default class IPCTunnel {
32
+ #private;
33
+ private channel;
34
+ static readonly Function: typeof FunctionTunnel;
35
+ static readonly Property: typeof PropertyTunnel;
36
+ static readonly Promise: typeof PromiseTunnel;
37
+ static readonly MainEvent: typeof MainEventTunnel;
38
+ static readonly RendererEvent: typeof RendererEventTunnel;
39
+ constructor(channel: string);
40
+ request(args?: IPCRequest): IPCResult;
41
+ response(args?: IPCResult): void;
42
+ on(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
43
+ once(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
44
+ removeListener(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
45
+ removeAllListeners(): void;
46
+ }
47
+ export {};
@@ -0,0 +1,109 @@
1
+ import electron from 'electron';
2
+ import { IPCChannel } from '../enums.js';
3
+ class FunctionTunnel {
4
+ #tunnel;
5
+ constructor(name) {
6
+ this.#tunnel = new IPCTunnel(IPCChannel.functionCall + name);
7
+ }
8
+ call(args = { args: [] }) {
9
+ return this.#tunnel.request(args);
10
+ }
11
+ }
12
+ class PropertyTunnel {
13
+ #getTunnel;
14
+ #setTunnel;
15
+ constructor(name) {
16
+ this.#getTunnel = new IPCTunnel(IPCChannel.propertyGet + name);
17
+ this.#setTunnel = new IPCTunnel(IPCChannel.propertySet + name);
18
+ }
19
+ get() {
20
+ return this.#getTunnel.request();
21
+ }
22
+ set(value) {
23
+ return this.#setTunnel.request({ args: [value] });
24
+ }
25
+ }
26
+ class PromiseTunnel {
27
+ #tunnel;
28
+ constructor(channel) {
29
+ this.#tunnel = new IPCTunnel(channel);
30
+ }
31
+ wait(secret) {
32
+ return new Promise((resolve, reject) => {
33
+ const handler = (_, { error, value, secret: resSecret }) => {
34
+ if (secret !== undefined && resSecret !== secret)
35
+ return;
36
+ if (error)
37
+ reject(error);
38
+ else
39
+ resolve(value);
40
+ this.#tunnel.removeListener(handler);
41
+ };
42
+ this.#tunnel.on(handler);
43
+ });
44
+ }
45
+ }
46
+ class MainEventTunnel {
47
+ #emitTunnel;
48
+ #onTunnel;
49
+ #onceTunnel;
50
+ constructor(name) {
51
+ this.#emitTunnel = new IPCTunnel(IPCChannel.mainEventEmit + name);
52
+ this.#onTunnel = new IPCTunnel(IPCChannel.mainEventOn + name);
53
+ this.#onceTunnel = new IPCTunnel(IPCChannel.mainEventOnce + name);
54
+ }
55
+ on(handler) {
56
+ const listener = (_, result) => handler(result);
57
+ this.#onTunnel.response();
58
+ this.#emitTunnel.on(listener);
59
+ return () => this.#emitTunnel.removeListener(listener);
60
+ }
61
+ once(handler) {
62
+ const listener = (_, result) => handler(result);
63
+ this.#onceTunnel.response();
64
+ this.#emitTunnel.once(listener);
65
+ return () => this.#emitTunnel.removeListener(listener);
66
+ }
67
+ }
68
+ class RendererEventTunnel {
69
+ #emitTunnel;
70
+ constructor(name) {
71
+ this.#emitTunnel = new IPCTunnel(IPCChannel.rendererEventEmit + name);
72
+ }
73
+ emit(value) {
74
+ this.#emitTunnel.response({ value });
75
+ }
76
+ error(error) {
77
+ this.#emitTunnel.response({ error });
78
+ }
79
+ }
80
+ export default class IPCTunnel {
81
+ channel;
82
+ #ipcRenderer = electron.ipcRenderer;
83
+ static Function = FunctionTunnel;
84
+ static Property = PropertyTunnel;
85
+ static Promise = PromiseTunnel;
86
+ static MainEvent = MainEventTunnel;
87
+ static RendererEvent = RendererEventTunnel;
88
+ constructor(channel) {
89
+ this.channel = channel;
90
+ }
91
+ request(args = { args: [] }) {
92
+ return this.#ipcRenderer.sendSync(this.channel, args);
93
+ }
94
+ response(args = {}) {
95
+ this.#ipcRenderer.send(this.channel, args);
96
+ }
97
+ on(listener) {
98
+ this.#ipcRenderer.on(this.channel, listener);
99
+ }
100
+ once(listener) {
101
+ this.#ipcRenderer.once(this.channel, listener);
102
+ }
103
+ removeListener(listener) {
104
+ this.#ipcRenderer.removeListener(this.channel, listener);
105
+ }
106
+ removeAllListeners() {
107
+ this.#ipcRenderer.removeAllListeners(this.channel);
108
+ }
109
+ }