emr-bridge 2.0.0 → 2.0.2
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/README.md +2 -2
- package/cjs/scripts/helpers/transfer.js +1 -1
- package/cjs/scripts/helpers/web-contents-events-container.d.ts +2 -7
- package/cjs/scripts/helpers/web-contents-events-container.js +14 -20
- package/cjs/scripts/main/publish.d.ts +2 -2
- package/cjs/scripts/main/publish.js +3 -5
- package/esm/scripts/helpers/transfer.js +1 -1
- package/esm/scripts/helpers/web-contents-events-container.d.ts +2 -7
- package/esm/scripts/helpers/web-contents-events-container.js +14 -20
- package/esm/scripts/main/publish.d.ts +2 -2
- package/esm/scripts/main/publish.js +3 -5
- package/package.json +2 -2
- package/cjs/enums.d.ts +0 -10
- package/cjs/enums.js +0 -14
- package/cjs/index.d.ts +0 -4
- package/cjs/index.js +0 -16
- package/cjs/main.d.ts +0 -2
- package/cjs/main.js +0 -9
- package/cjs/preload.d.ts +0 -3
- package/cjs/preload.js +0 -10
- package/cjs/renderer.d.ts +0 -2
- package/cjs/renderer.js +0 -8
- package/cjs/scripts/bridge.d.ts +0 -8
- package/cjs/scripts/bridge.js +0 -69
- package/cjs/scripts/helpers/error-thrower.d.ts +0 -3
- package/cjs/scripts/helpers/error-thrower.js +0 -8
- package/cjs/scripts/helpers/main-event.d.ts +0 -8
- package/cjs/scripts/helpers/main-event.js +0 -15
- package/cjs/scripts/helpers/web-contents-container.d.ts +0 -12
- package/cjs/scripts/helpers/web-contents-container.js +0 -34
- package/cjs/scripts/preload-bridge.d.ts +0 -20
- package/cjs/scripts/preload-bridge.js +0 -76
- package/cjs/scripts/preload-provider.d.ts +0 -7
- package/cjs/scripts/preload-provider.js +0 -80
- package/cjs/scripts/provider.d.ts +0 -27
- package/cjs/scripts/provider.js +0 -95
- package/cjs/scripts/publish.d.ts +0 -41
- package/cjs/scripts/publish.js +0 -154
- package/cjs/scripts/renderer-tunnels.d.ts +0 -45
- package/cjs/scripts/renderer-tunnels.js +0 -110
- package/esm/enums.d.ts +0 -10
- package/esm/enums.js +0 -11
- package/esm/helpers.d.ts +0 -41
- package/esm/helpers.js +0 -96
- package/esm/index.d.ts +0 -4
- package/esm/index.js +0 -3
- package/esm/main.d.ts +0 -2
- package/esm/main.js +0 -1
- package/esm/preload.d.ts +0 -3
- package/esm/preload.js +0 -2
- package/esm/renderer.d.ts +0 -2
- package/esm/renderer.js +0 -1
- package/esm/scripts/bridge.d.ts +0 -8
- package/esm/scripts/bridge.js +0 -64
- package/esm/scripts/helpers/electron-importer.d.ts +0 -4
- package/esm/scripts/helpers/electron-importer.js +0 -16
- package/esm/scripts/helpers/error-thrower.d.ts +0 -3
- package/esm/scripts/helpers/error-thrower.js +0 -5
- package/esm/scripts/helpers/main-event.d.ts +0 -8
- package/esm/scripts/helpers/main-event.js +0 -12
- package/esm/scripts/helpers/web-contents-container.d.ts +0 -12
- package/esm/scripts/helpers/web-contents-container.js +0 -31
- package/esm/scripts/main.d.ts +0 -8
- package/esm/scripts/main.js +0 -69
- package/esm/scripts/preload-bridge.d.ts +0 -20
- package/esm/scripts/preload-bridge.js +0 -68
- package/esm/scripts/preload-provider.d.ts +0 -7
- package/esm/scripts/preload-provider.js +0 -75
- package/esm/scripts/provider.d.ts +0 -27
- package/esm/scripts/provider.js +0 -92
- package/esm/scripts/publish.d.ts +0 -41
- package/esm/scripts/publish.js +0 -147
- package/esm/scripts/renderer-tunnels.d.ts +0 -45
- package/esm/scripts/renderer-tunnels.js +0 -107
package/esm/helpers.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { IpcMainEvent, WebContents } from 'electron';
|
|
2
|
-
import { IResult } from './types';
|
|
3
|
-
export declare class WebContentsContainer {
|
|
4
|
-
private container;
|
|
5
|
-
addOn(event: string, sender: WebContents): void;
|
|
6
|
-
addOnce(event: string, sender: WebContents): void;
|
|
7
|
-
getOn(event: string): WebContents[];
|
|
8
|
-
getOnce(event: string): WebContents[];
|
|
9
|
-
getAll(event: string): WebContents[];
|
|
10
|
-
clearOnce(event: string): void;
|
|
11
|
-
private addEvent;
|
|
12
|
-
private getEvent;
|
|
13
|
-
}
|
|
14
|
-
export declare class IpcResult {
|
|
15
|
-
static withPromise(promiseChannel: string): IResult;
|
|
16
|
-
static withValue(value: any, id?: string): IResult;
|
|
17
|
-
static withError(error?: Error | string, id?: string): IResult;
|
|
18
|
-
}
|
|
19
|
-
export declare class MainEvent {
|
|
20
|
-
private event;
|
|
21
|
-
constructor(event: IpcMainEvent);
|
|
22
|
-
send(channel: string, result: IResult): void;
|
|
23
|
-
return(value: any): void;
|
|
24
|
-
}
|
|
25
|
-
export declare class Id {
|
|
26
|
-
static generate(): string;
|
|
27
|
-
}
|
|
28
|
-
export declare class ErrorThrower {
|
|
29
|
-
static throws<TClass extends new (...args: TArgs) => InstanceType<TClass>, TArgs extends any[]>(errorClass: TClass, ...args: TArgs): () => never;
|
|
30
|
-
}
|
|
31
|
-
export declare class Env {
|
|
32
|
-
static readonly global: {
|
|
33
|
-
readonly hasWindow: boolean;
|
|
34
|
-
readonly hasProvider: boolean;
|
|
35
|
-
};
|
|
36
|
-
static readonly electron: {
|
|
37
|
-
readonly hasIpcRenderer: boolean;
|
|
38
|
-
readonly hasIpcMain: boolean;
|
|
39
|
-
readonly hasContextBridge: boolean;
|
|
40
|
-
};
|
|
41
|
-
}
|
package/esm/helpers.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import electron from 'electron';
|
|
2
|
-
export class WebContentsContainer {
|
|
3
|
-
container = {};
|
|
4
|
-
addOn(event, sender) {
|
|
5
|
-
this.addEvent(event);
|
|
6
|
-
this.container[event].on.push(sender);
|
|
7
|
-
}
|
|
8
|
-
addOnce(event, sender) {
|
|
9
|
-
this.addEvent(event);
|
|
10
|
-
this.container[event].once.push(sender);
|
|
11
|
-
}
|
|
12
|
-
getOn(event) {
|
|
13
|
-
return this.getEvent(event).on.filter(sender => !sender.isDestroyed());
|
|
14
|
-
}
|
|
15
|
-
getOnce(event) {
|
|
16
|
-
return this.getEvent(event).once.filter(sender => !sender.isDestroyed());
|
|
17
|
-
}
|
|
18
|
-
getAll(event) {
|
|
19
|
-
const senders = this.getEvent(event);
|
|
20
|
-
return [...senders.on, ...senders.once];
|
|
21
|
-
}
|
|
22
|
-
clearOnce(event) {
|
|
23
|
-
this.getEvent(event).once = [];
|
|
24
|
-
}
|
|
25
|
-
addEvent(name) {
|
|
26
|
-
this.container[name] ??= { on: [], once: [] };
|
|
27
|
-
}
|
|
28
|
-
getEvent(name) {
|
|
29
|
-
this.addEvent(name);
|
|
30
|
-
return this.container[name];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export class IpcResult {
|
|
34
|
-
static withPromise(promiseChannel) {
|
|
35
|
-
return { promiseChannel };
|
|
36
|
-
}
|
|
37
|
-
static withValue(value, id) {
|
|
38
|
-
return { value, id };
|
|
39
|
-
}
|
|
40
|
-
static withError(error, id) {
|
|
41
|
-
let resultError;
|
|
42
|
-
if (error === undefined) {
|
|
43
|
-
resultError = "Unknown error";
|
|
44
|
-
}
|
|
45
|
-
else if (typeof error === 'string') {
|
|
46
|
-
resultError = error;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
resultError = error.stack ?? error.message;
|
|
50
|
-
}
|
|
51
|
-
return { error: resultError, id };
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
export class MainEvent {
|
|
55
|
-
event;
|
|
56
|
-
constructor(event) {
|
|
57
|
-
this.event = event;
|
|
58
|
-
}
|
|
59
|
-
send(channel, result) {
|
|
60
|
-
this.event.sender.send(channel, result);
|
|
61
|
-
}
|
|
62
|
-
return(value) {
|
|
63
|
-
this.event.returnValue = value;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
export class Id {
|
|
67
|
-
static generate() {
|
|
68
|
-
return Math.random().toString();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
export class ErrorThrower {
|
|
72
|
-
static throws(errorClass, ...args) {
|
|
73
|
-
return () => { throw new errorClass(...args); };
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
export class Env {
|
|
77
|
-
static global = {
|
|
78
|
-
get hasWindow() {
|
|
79
|
-
return typeof window !== 'undefined';
|
|
80
|
-
},
|
|
81
|
-
get hasProvider() {
|
|
82
|
-
return Boolean(window.__provider__);
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
static electron = {
|
|
86
|
-
get hasIpcRenderer() {
|
|
87
|
-
return 'ipcRenderer' in electron;
|
|
88
|
-
},
|
|
89
|
-
get hasIpcMain() {
|
|
90
|
-
return 'ipcMain' in electron;
|
|
91
|
-
},
|
|
92
|
-
get hasContextBridge() {
|
|
93
|
-
return 'contextBridge' in electron;
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
package/esm/index.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { default as Bridge } from './scripts/bridge.js';
|
|
2
|
-
export { default as provideFromMain } from './scripts/preload-provider.js';
|
|
3
|
-
export { emitEvent, on, once, publicFunction, publicVariable } from './scripts/publish.js';
|
|
4
|
-
export type { EventListener, EventUnsubscribe, IPublicProperty, PublicFunction } from './types.js';
|
package/esm/index.js
DELETED
package/esm/main.d.ts
DELETED
package/esm/main.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { emitEvent, on, once, publicFunction, publicVariable } from './scripts/publish.js';
|
package/esm/preload.d.ts
DELETED
package/esm/preload.js
DELETED
package/esm/renderer.d.ts
DELETED
package/esm/renderer.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default as Bridge } from './scripts/bridge.js';
|
package/esm/scripts/bridge.d.ts
DELETED
package/esm/scripts/bridge.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { GlobalProviderNotFoundError, NotProvidedFromMain, TryUseBridgeInPreload } from '../errors.js';
|
|
2
|
-
import { Env, ErrorThrower } from './helpers/index.js';
|
|
3
|
-
import Provider from './provider.js';
|
|
4
|
-
class ProviderIntoRenderer extends Provider {
|
|
5
|
-
globalProvider;
|
|
6
|
-
entitiesInfo;
|
|
7
|
-
constructor() {
|
|
8
|
-
super();
|
|
9
|
-
if (!window.__provider__) {
|
|
10
|
-
throw new NotProvidedFromMain();
|
|
11
|
-
}
|
|
12
|
-
this.globalProvider = window.__provider__;
|
|
13
|
-
this.entitiesInfo = this.globalProvider.getEntitiesInfo();
|
|
14
|
-
this.init();
|
|
15
|
-
}
|
|
16
|
-
callFunction(name, id, args) {
|
|
17
|
-
return this.globalProvider.entities.functions[name](id, args);
|
|
18
|
-
}
|
|
19
|
-
getVariable(name) {
|
|
20
|
-
return this.globalProvider.entities.variables[name].get();
|
|
21
|
-
}
|
|
22
|
-
setVariable(name, value) {
|
|
23
|
-
return this.globalProvider.entities.variables[name].set(value);
|
|
24
|
-
}
|
|
25
|
-
waitPromise(channel, id) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
this.globalProvider.waitPromise(channel, resolve, reject, id);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
handleEvent(name, type, listener) {
|
|
31
|
-
return type === 'on'
|
|
32
|
-
? this.globalProvider.entities.events.on(name, listener)
|
|
33
|
-
: this.globalProvider.entities.events.once(name, listener);
|
|
34
|
-
}
|
|
35
|
-
emitEvent(name, value) {
|
|
36
|
-
this.globalProvider.entities.events.emit(name, value);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
function getErrorThrower() {
|
|
40
|
-
if (!Env.global.hasWindow) {
|
|
41
|
-
return ErrorThrower.throws(TryUseBridgeInPreload);
|
|
42
|
-
}
|
|
43
|
-
if (!Env.global.hasProvider) {
|
|
44
|
-
return ErrorThrower.throws(GlobalProviderNotFoundError);
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
function getBridge() {
|
|
49
|
-
const throwError = getErrorThrower();
|
|
50
|
-
if (throwError) {
|
|
51
|
-
return new Proxy({}, {
|
|
52
|
-
get: throwError,
|
|
53
|
-
set: throwError,
|
|
54
|
-
apply: throwError
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
return new ProviderIntoRenderer();
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Bridge between `renderer` and `main` processes.
|
|
61
|
-
*
|
|
62
|
-
* _only for renderer process_
|
|
63
|
-
*/
|
|
64
|
-
export default getBridge();
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export default class ElectronImporter {
|
|
2
|
-
static getMain() {
|
|
3
|
-
return new Promise(resolve => {
|
|
4
|
-
import('electron')
|
|
5
|
-
.then(electron => resolve(electron))
|
|
6
|
-
.catch(() => resolve({}));
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
static getRenderer() {
|
|
10
|
-
return new Promise(resolve => {
|
|
11
|
-
import('electron')
|
|
12
|
-
.then(electron => resolve(electron))
|
|
13
|
-
.catch(() => resolve({}));
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { WebContents } from 'electron';
|
|
2
|
-
export default class WebContentsContainer {
|
|
3
|
-
private container;
|
|
4
|
-
addOn(event: string, sender: WebContents): void;
|
|
5
|
-
addOnce(event: string, sender: WebContents): void;
|
|
6
|
-
getOn(event: string): WebContents[];
|
|
7
|
-
getOnce(event: string): WebContents[];
|
|
8
|
-
getAll(event: string): WebContents[];
|
|
9
|
-
clearOnce(event: string): void;
|
|
10
|
-
private addEvent;
|
|
11
|
-
private getEvent;
|
|
12
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
export default class WebContentsContainer {
|
|
2
|
-
container = {};
|
|
3
|
-
addOn(event, sender) {
|
|
4
|
-
this.addEvent(event);
|
|
5
|
-
this.container[event].on.push(sender);
|
|
6
|
-
}
|
|
7
|
-
addOnce(event, sender) {
|
|
8
|
-
this.addEvent(event);
|
|
9
|
-
this.container[event].once.push(sender);
|
|
10
|
-
}
|
|
11
|
-
getOn(event) {
|
|
12
|
-
return this.getEvent(event).on.filter(sender => !sender.isDestroyed());
|
|
13
|
-
}
|
|
14
|
-
getOnce(event) {
|
|
15
|
-
return this.getEvent(event).once.filter(sender => !sender.isDestroyed());
|
|
16
|
-
}
|
|
17
|
-
getAll(event) {
|
|
18
|
-
const senders = this.getEvent(event);
|
|
19
|
-
return [...senders.on, ...senders.once];
|
|
20
|
-
}
|
|
21
|
-
clearOnce(event) {
|
|
22
|
-
this.getEvent(event).once = [];
|
|
23
|
-
}
|
|
24
|
-
addEvent(name) {
|
|
25
|
-
this.container[name] ??= { on: [], once: [] };
|
|
26
|
-
}
|
|
27
|
-
getEvent(name) {
|
|
28
|
-
this.addEvent(name);
|
|
29
|
-
return this.container[name];
|
|
30
|
-
}
|
|
31
|
-
}
|
package/esm/scripts/main.d.ts
DELETED
package/esm/scripts/main.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import electron from 'electron';
|
|
2
|
-
import { IpcChannel } from '../enums.js';
|
|
3
|
-
import { NotProvidedFromMain, TryUseMainInMain } from '../errors.js';
|
|
4
|
-
import { Env, ErrorThrower } from '../helpers.js';
|
|
5
|
-
import Provider from './provider.js';
|
|
6
|
-
import IPCTunnel from './renderer-tunnels.js';
|
|
7
|
-
const ipcRenderer = electron.ipcRenderer;
|
|
8
|
-
class ProviderIntoPreload extends Provider {
|
|
9
|
-
entitiesInfo = ipcRenderer.sendSync(IpcChannel.getPublicInfo);
|
|
10
|
-
constructor() {
|
|
11
|
-
super();
|
|
12
|
-
if (!this.entitiesInfo) {
|
|
13
|
-
throw new NotProvidedFromMain();
|
|
14
|
-
}
|
|
15
|
-
this.init();
|
|
16
|
-
}
|
|
17
|
-
callFunction(name, secret, args) {
|
|
18
|
-
return new IPCTunnel.Function(name).call({ args, id: secret });
|
|
19
|
-
}
|
|
20
|
-
getVariable(name) {
|
|
21
|
-
return new IPCTunnel.Property(name).get();
|
|
22
|
-
}
|
|
23
|
-
setVariable(name, value) {
|
|
24
|
-
return new IPCTunnel.Property(name).set(value);
|
|
25
|
-
}
|
|
26
|
-
async waitPromise(channel, id) {
|
|
27
|
-
return await new IPCTunnel.Promise(channel).wait(id);
|
|
28
|
-
}
|
|
29
|
-
handleEvent(name, type, listener) {
|
|
30
|
-
const tunnel = new IPCTunnel.Event(name);
|
|
31
|
-
return type === 'on'
|
|
32
|
-
? tunnel.on(listener)
|
|
33
|
-
: tunnel.once(listener);
|
|
34
|
-
}
|
|
35
|
-
emitEvent(name, arg) {
|
|
36
|
-
const tunnel = new IPCTunnel.Event(name);
|
|
37
|
-
if (arg instanceof Promise) {
|
|
38
|
-
arg
|
|
39
|
-
.then(value => tunnel.emit(value))
|
|
40
|
-
.catch(reason => tunnel.error(String(reason)));
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
tunnel.emit(arg);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function getErrorThrower() {
|
|
48
|
-
if (!Env.electron.hasIpcRenderer) {
|
|
49
|
-
return ErrorThrower.throws(TryUseMainInMain);
|
|
50
|
-
}
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
function getBridge() {
|
|
54
|
-
const throwError = getErrorThrower();
|
|
55
|
-
if (throwError) {
|
|
56
|
-
return new Proxy({}, {
|
|
57
|
-
get: throwError,
|
|
58
|
-
set: throwError,
|
|
59
|
-
apply: throwError
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
return new ProviderIntoPreload();
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Bridge between `preload` and `main` processes.
|
|
66
|
-
*
|
|
67
|
-
* _only for preload process_
|
|
68
|
-
*/
|
|
69
|
-
export default getBridge();
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { EventListener, EventUnsubscribe, IBridge, IResult } from '../types.js';
|
|
2
|
-
import Provider from './provider.js';
|
|
3
|
-
export declare class ProviderIntoPreload extends Provider {
|
|
4
|
-
protected readonly entitiesInfo: any;
|
|
5
|
-
constructor();
|
|
6
|
-
protected callFunction(name: string, secret: string, args: any[]): IResult;
|
|
7
|
-
protected getVariable(name: string): IResult;
|
|
8
|
-
protected setVariable(name: string, value: any): IResult;
|
|
9
|
-
protected waitPromise(channel: string, id?: string): Promise<any>;
|
|
10
|
-
protected handleEvent(name: string, type: 'on' | 'once', listener: EventListener<IResult>): EventUnsubscribe;
|
|
11
|
-
protected emitEvent(name: string, arg: any): void;
|
|
12
|
-
}
|
|
13
|
-
export declare function getBridge(): IBridge;
|
|
14
|
-
/**
|
|
15
|
-
* Bridge between `preload` and `main` processes.
|
|
16
|
-
*
|
|
17
|
-
* _only for preload process_
|
|
18
|
-
*/
|
|
19
|
-
declare const _default: IBridge;
|
|
20
|
-
export default _default;
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { ipcRenderer } from 'electron';
|
|
2
|
-
import { IpcChannel } from '../enums.js';
|
|
3
|
-
import { NotProvidedFromMain, TryUseMainInMain } from '../errors.js';
|
|
4
|
-
import { ErrorThrower } from './helpers/index.js';
|
|
5
|
-
import Provider from './provider.js';
|
|
6
|
-
import IPCTunnel from './renderer-tunnels.js';
|
|
7
|
-
export class ProviderIntoPreload extends Provider {
|
|
8
|
-
entitiesInfo = ipcRenderer.sendSync(IpcChannel.getPublicInfo);
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
11
|
-
if (!this.entitiesInfo) {
|
|
12
|
-
throw new NotProvidedFromMain();
|
|
13
|
-
}
|
|
14
|
-
this.init();
|
|
15
|
-
}
|
|
16
|
-
callFunction(name, secret, args) {
|
|
17
|
-
return new IPCTunnel.Function(name).call({ args, id: secret });
|
|
18
|
-
}
|
|
19
|
-
getVariable(name) {
|
|
20
|
-
return new IPCTunnel.Property(name).get();
|
|
21
|
-
}
|
|
22
|
-
setVariable(name, value) {
|
|
23
|
-
return new IPCTunnel.Property(name).set(value);
|
|
24
|
-
}
|
|
25
|
-
async waitPromise(channel, id) {
|
|
26
|
-
return await new IPCTunnel.Promise(channel).wait(id);
|
|
27
|
-
}
|
|
28
|
-
handleEvent(name, type, listener) {
|
|
29
|
-
const tunnel = new IPCTunnel.Event(name);
|
|
30
|
-
return type === 'on'
|
|
31
|
-
? tunnel.on(listener)
|
|
32
|
-
: tunnel.once(listener);
|
|
33
|
-
}
|
|
34
|
-
emitEvent(name, arg) {
|
|
35
|
-
const tunnel = new IPCTunnel.Event(name);
|
|
36
|
-
if (arg instanceof Promise) {
|
|
37
|
-
arg
|
|
38
|
-
.then(value => tunnel.emit(value))
|
|
39
|
-
.catch(reason => tunnel.error(String(reason)));
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
tunnel.emit(arg);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function getErrorThrower() {
|
|
47
|
-
if (!ipcRenderer) {
|
|
48
|
-
return ErrorThrower.throws(TryUseMainInMain);
|
|
49
|
-
}
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
export function getBridge() {
|
|
53
|
-
const throwError = getErrorThrower();
|
|
54
|
-
if (throwError) {
|
|
55
|
-
return new Proxy({}, {
|
|
56
|
-
get: throwError,
|
|
57
|
-
set: throwError,
|
|
58
|
-
apply: throwError
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
return new ProviderIntoPreload();
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Bridge between `preload` and `main` processes.
|
|
65
|
-
*
|
|
66
|
-
* _only for preload process_
|
|
67
|
-
*/
|
|
68
|
-
export default getBridge();
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { contextBridge, ipcRenderer } from 'electron';
|
|
2
|
-
import { IpcChannel } from '../enums.js';
|
|
3
|
-
import { NotProvidedFromMain, TryProvideFromMainInMain, TryProvideFromMainInRenderer } from '../errors.js';
|
|
4
|
-
import { ErrorThrower } from './helpers/index.js';
|
|
5
|
-
import IPCTunnel from './renderer-tunnels.js';
|
|
6
|
-
/** Выбрасывает ошибку если используется не в том процессе */
|
|
7
|
-
let canThrowError = () => { };
|
|
8
|
-
if (!ipcRenderer) {
|
|
9
|
-
canThrowError = ErrorThrower.throws(TryProvideFromMainInMain);
|
|
10
|
-
}
|
|
11
|
-
else if (!contextBridge) {
|
|
12
|
-
canThrowError = ErrorThrower.throws(TryProvideFromMainInRenderer);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Sets access to main from renderer
|
|
16
|
-
*
|
|
17
|
-
* @param contextIsolation - _default_: `true`
|
|
18
|
-
*/
|
|
19
|
-
function provideFromMain(contextIsolation = true) {
|
|
20
|
-
canThrowError();
|
|
21
|
-
const entitiesInfo = ipcRenderer.sendSync(IpcChannel.getPublicInfo);
|
|
22
|
-
if (!entitiesInfo) {
|
|
23
|
-
throw new NotProvidedFromMain();
|
|
24
|
-
}
|
|
25
|
-
const provider = {
|
|
26
|
-
getEntitiesInfo: () => entitiesInfo,
|
|
27
|
-
waitPromise(channel, resolve, reject, id) {
|
|
28
|
-
new IPCTunnel.Promise(channel).wait(id)
|
|
29
|
-
.then(resolve)
|
|
30
|
-
.catch(reject);
|
|
31
|
-
},
|
|
32
|
-
entities: {
|
|
33
|
-
functions: getFunctions(),
|
|
34
|
-
variables: getProperties(),
|
|
35
|
-
events: {
|
|
36
|
-
emit(name, value) {
|
|
37
|
-
new IPCTunnel.Event(name).emit(value);
|
|
38
|
-
},
|
|
39
|
-
on(name, listener) {
|
|
40
|
-
return new IPCTunnel.Event(name).on(listener);
|
|
41
|
-
},
|
|
42
|
-
once(name, listener) {
|
|
43
|
-
return new IPCTunnel.Event(name).once(listener);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
if (contextIsolation) {
|
|
49
|
-
contextBridge.exposeInMainWorld('__provider__', provider);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
window.__provider__ = provider;
|
|
53
|
-
}
|
|
54
|
-
function getFunctions() {
|
|
55
|
-
const result = {};
|
|
56
|
-
for (const name of entitiesInfo.functions) {
|
|
57
|
-
result[name] = (id, args) => {
|
|
58
|
-
return new IPCTunnel.Function(name).call({ args, id: id });
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
function getProperties() {
|
|
64
|
-
const result = {};
|
|
65
|
-
for (const name of entitiesInfo.variables) {
|
|
66
|
-
const tunnel = new IPCTunnel.Property(name);
|
|
67
|
-
result[name] = {
|
|
68
|
-
get: () => tunnel.get(),
|
|
69
|
-
set: (value) => tunnel.set(value)
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
export default provideFromMain;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { EventListener, EventUnsubscribe, IBridge, IEntitiesInfo, IHasSnapshot, IResult } from '../types.js';
|
|
2
|
-
export default abstract class Provider implements IBridge {
|
|
3
|
-
protected abstract entitiesInfo: IEntitiesInfo;
|
|
4
|
-
protected readonly target: any;
|
|
5
|
-
as<TTarget>(): IBridge & TTarget;
|
|
6
|
-
emit(name: string, arg: any): void;
|
|
7
|
-
on<TValue = any>(name: string, listener: EventListener<TValue>): EventUnsubscribe;
|
|
8
|
-
once<TValue = any>(name: string, listener: EventListener<TValue>): EventUnsubscribe;
|
|
9
|
-
call<TFunc extends (...args: any[]) => InstanceType<TClass>, TClass extends {
|
|
10
|
-
new (): IHasSnapshot;
|
|
11
|
-
}>(func: TFunc, Class: TClass, args: Parameters<TFunc>): InstanceType<TClass>;
|
|
12
|
-
protected abstract getVariable<T = any>(name: string): IResult<T>;
|
|
13
|
-
protected abstract setVariable<T>(name: string, value: T): IResult<T>;
|
|
14
|
-
protected abstract callFunction<T = any>(name: string, id: string, args: any[]): IResult<T>;
|
|
15
|
-
protected abstract waitPromise<T = any>(channel: string, id?: string): Promise<T>;
|
|
16
|
-
protected abstract emitEvent(name: string, arg: any): void;
|
|
17
|
-
protected abstract handleEvent(name: string, type: 'on' | 'once', handler: EventListener<IResult>): EventUnsubscribe;
|
|
18
|
-
protected init(): void;
|
|
19
|
-
protected handleError(channel: string, error?: string): void | never;
|
|
20
|
-
protected initFunctions(): void;
|
|
21
|
-
protected initProperties(): void;
|
|
22
|
-
protected define(name: string, attrs?: {
|
|
23
|
-
get?: () => any;
|
|
24
|
-
set?: (v: any) => void;
|
|
25
|
-
value?: any;
|
|
26
|
-
}): void;
|
|
27
|
-
}
|