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
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.ProviderIntoPreload = void 0;
|
|
7
|
-
exports.getBridge = getBridge;
|
|
8
|
-
const electron_1 = require("electron");
|
|
9
|
-
const enums_js_1 = require("../enums.js");
|
|
10
|
-
const errors_js_1 = require("../errors.js");
|
|
11
|
-
const index_js_1 = require("./helpers/index.js");
|
|
12
|
-
const provider_js_1 = __importDefault(require("./provider.js"));
|
|
13
|
-
const renderer_tunnels_js_1 = __importDefault(require("./renderer-tunnels.js"));
|
|
14
|
-
class ProviderIntoPreload extends provider_js_1.default {
|
|
15
|
-
entitiesInfo = electron_1.ipcRenderer.sendSync(enums_js_1.IpcChannel.getPublicInfo);
|
|
16
|
-
constructor() {
|
|
17
|
-
super();
|
|
18
|
-
if (!this.entitiesInfo) {
|
|
19
|
-
throw new errors_js_1.NotProvidedFromMain();
|
|
20
|
-
}
|
|
21
|
-
this.init();
|
|
22
|
-
}
|
|
23
|
-
callFunction(name, secret, args) {
|
|
24
|
-
return new renderer_tunnels_js_1.default.Function(name).call({ args, id: secret });
|
|
25
|
-
}
|
|
26
|
-
getVariable(name) {
|
|
27
|
-
return new renderer_tunnels_js_1.default.Property(name).get();
|
|
28
|
-
}
|
|
29
|
-
setVariable(name, value) {
|
|
30
|
-
return new renderer_tunnels_js_1.default.Property(name).set(value);
|
|
31
|
-
}
|
|
32
|
-
async waitPromise(channel, id) {
|
|
33
|
-
return await new renderer_tunnels_js_1.default.Promise(channel).wait(id);
|
|
34
|
-
}
|
|
35
|
-
handleEvent(name, type, listener) {
|
|
36
|
-
const tunnel = new renderer_tunnels_js_1.default.Event(name);
|
|
37
|
-
return type === 'on'
|
|
38
|
-
? tunnel.on(listener)
|
|
39
|
-
: tunnel.once(listener);
|
|
40
|
-
}
|
|
41
|
-
emitEvent(name, arg) {
|
|
42
|
-
const tunnel = new renderer_tunnels_js_1.default.Event(name);
|
|
43
|
-
if (arg instanceof Promise) {
|
|
44
|
-
arg
|
|
45
|
-
.then(value => tunnel.emit(value))
|
|
46
|
-
.catch(reason => tunnel.error(String(reason)));
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
tunnel.emit(arg);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
exports.ProviderIntoPreload = ProviderIntoPreload;
|
|
54
|
-
function getErrorThrower() {
|
|
55
|
-
if (!electron_1.ipcRenderer) {
|
|
56
|
-
return index_js_1.ErrorThrower.throws(errors_js_1.TryUseMainInMain);
|
|
57
|
-
}
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
function getBridge() {
|
|
61
|
-
const throwError = getErrorThrower();
|
|
62
|
-
if (throwError) {
|
|
63
|
-
return new Proxy({}, {
|
|
64
|
-
get: throwError,
|
|
65
|
-
set: throwError,
|
|
66
|
-
apply: throwError
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
return new ProviderIntoPreload();
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Bridge between `preload` and `main` processes.
|
|
73
|
-
*
|
|
74
|
-
* _only for preload process_
|
|
75
|
-
*/
|
|
76
|
-
exports.default = getBridge();
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const electron_1 = require("electron");
|
|
7
|
-
const enums_js_1 = require("../enums.js");
|
|
8
|
-
const errors_js_1 = require("../errors.js");
|
|
9
|
-
const index_js_1 = require("./helpers/index.js");
|
|
10
|
-
const renderer_tunnels_js_1 = __importDefault(require("./renderer-tunnels.js"));
|
|
11
|
-
/** Выбрасывает ошибку если используется не в том процессе */
|
|
12
|
-
let canThrowError = () => { };
|
|
13
|
-
if (!electron_1.ipcRenderer) {
|
|
14
|
-
canThrowError = index_js_1.ErrorThrower.throws(errors_js_1.TryProvideFromMainInMain);
|
|
15
|
-
}
|
|
16
|
-
else if (!electron_1.contextBridge) {
|
|
17
|
-
canThrowError = index_js_1.ErrorThrower.throws(errors_js_1.TryProvideFromMainInRenderer);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Sets access to main from renderer
|
|
21
|
-
*
|
|
22
|
-
* @param contextIsolation - _default_: `true`
|
|
23
|
-
*/
|
|
24
|
-
function provideFromMain(contextIsolation = true) {
|
|
25
|
-
canThrowError();
|
|
26
|
-
const entitiesInfo = electron_1.ipcRenderer.sendSync(enums_js_1.IpcChannel.getPublicInfo);
|
|
27
|
-
if (!entitiesInfo) {
|
|
28
|
-
throw new errors_js_1.NotProvidedFromMain();
|
|
29
|
-
}
|
|
30
|
-
const provider = {
|
|
31
|
-
getEntitiesInfo: () => entitiesInfo,
|
|
32
|
-
waitPromise(channel, resolve, reject, id) {
|
|
33
|
-
new renderer_tunnels_js_1.default.Promise(channel).wait(id)
|
|
34
|
-
.then(resolve)
|
|
35
|
-
.catch(reject);
|
|
36
|
-
},
|
|
37
|
-
entities: {
|
|
38
|
-
functions: getFunctions(),
|
|
39
|
-
variables: getProperties(),
|
|
40
|
-
events: {
|
|
41
|
-
emit(name, value) {
|
|
42
|
-
new renderer_tunnels_js_1.default.Event(name).emit(value);
|
|
43
|
-
},
|
|
44
|
-
on(name, listener) {
|
|
45
|
-
return new renderer_tunnels_js_1.default.Event(name).on(listener);
|
|
46
|
-
},
|
|
47
|
-
once(name, listener) {
|
|
48
|
-
return new renderer_tunnels_js_1.default.Event(name).once(listener);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
if (contextIsolation) {
|
|
54
|
-
electron_1.contextBridge.exposeInMainWorld('__provider__', provider);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
window.__provider__ = provider;
|
|
58
|
-
}
|
|
59
|
-
function getFunctions() {
|
|
60
|
-
const result = {};
|
|
61
|
-
for (const name of entitiesInfo.functions) {
|
|
62
|
-
result[name] = (id, args) => {
|
|
63
|
-
return new renderer_tunnels_js_1.default.Function(name).call({ args, id: id });
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
function getProperties() {
|
|
69
|
-
const result = {};
|
|
70
|
-
for (const name of entitiesInfo.variables) {
|
|
71
|
-
const tunnel = new renderer_tunnels_js_1.default.Property(name);
|
|
72
|
-
result[name] = {
|
|
73
|
-
get: () => tunnel.get(),
|
|
74
|
-
set: (value) => tunnel.set(value)
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
return result;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
exports.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
|
-
}
|
package/cjs/scripts/provider.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const errors_js_1 = require("../errors.js");
|
|
4
|
-
const index_js_1 = require("./helpers/index.js");
|
|
5
|
-
class Provider {
|
|
6
|
-
target = {
|
|
7
|
-
as: () => this.as(),
|
|
8
|
-
emit: (...args) => this.emit(...args),
|
|
9
|
-
on: (...args) => this.on(...args),
|
|
10
|
-
once: (...args) => this.once(...args),
|
|
11
|
-
call: (...args) => this.call(...args)
|
|
12
|
-
};
|
|
13
|
-
as() {
|
|
14
|
-
return this.target;
|
|
15
|
-
}
|
|
16
|
-
emit(name, arg) {
|
|
17
|
-
this.emitEvent(name, arg);
|
|
18
|
-
}
|
|
19
|
-
on(name, listener) {
|
|
20
|
-
return this.handleEvent(name, 'on', ({ value, error }) => {
|
|
21
|
-
this.handleError(`event: ${name}`, error);
|
|
22
|
-
listener(value);
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
once(name, listener) {
|
|
26
|
-
return this.handleEvent(name, 'once', ({ value, error }) => {
|
|
27
|
-
this.handleError(`event: ${name}`, error);
|
|
28
|
-
listener(value);
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
call(func, Class, args) {
|
|
32
|
-
const value = func(...args);
|
|
33
|
-
if (index_js_1.Transfer.isSnapshotValue(value)) {
|
|
34
|
-
return index_js_1.Transfer.getFromSnapshot(value, Class);
|
|
35
|
-
}
|
|
36
|
-
return value;
|
|
37
|
-
}
|
|
38
|
-
init() {
|
|
39
|
-
this.initFunctions();
|
|
40
|
-
this.initProperties();
|
|
41
|
-
}
|
|
42
|
-
handleError(channel, error) {
|
|
43
|
-
if (!error) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
throw new Error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
47
|
-
}
|
|
48
|
-
initFunctions() {
|
|
49
|
-
for (const name of this.entitiesInfo.functions) {
|
|
50
|
-
this.define(name, {
|
|
51
|
-
value: (...args) => {
|
|
52
|
-
const id = index_js_1.Id.generate();
|
|
53
|
-
const preparedArgs = args.map(arg => index_js_1.Transfer.toTransferable(arg));
|
|
54
|
-
const { error, promiseChannel, value } = this.callFunction(name, id, preparedArgs);
|
|
55
|
-
this.handleError(name, error);
|
|
56
|
-
return promiseChannel
|
|
57
|
-
? this.waitPromise(promiseChannel, id)
|
|
58
|
-
: value;
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
initProperties() {
|
|
64
|
-
for (const name of this.entitiesInfo.variables) {
|
|
65
|
-
this.define(name, {
|
|
66
|
-
get: () => {
|
|
67
|
-
const { error, value } = this.getVariable(name);
|
|
68
|
-
this.handleError(name, error);
|
|
69
|
-
return value;
|
|
70
|
-
},
|
|
71
|
-
set: (value) => {
|
|
72
|
-
const { error } = this.setVariable(name, value);
|
|
73
|
-
this.handleError(name, error);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
define(name, attrs) {
|
|
79
|
-
const hasValue = Boolean(attrs?.value);
|
|
80
|
-
const defaultDescriptor = {
|
|
81
|
-
get: () => attrs.value,
|
|
82
|
-
set: index_js_1.ErrorThrower.throws(errors_js_1.TrySetReadonlyError, name),
|
|
83
|
-
enumerable: true
|
|
84
|
-
};
|
|
85
|
-
const noValueDescriptor = {
|
|
86
|
-
get: attrs?.get || index_js_1.ErrorThrower.throws(errors_js_1.NoSetAccessError, name),
|
|
87
|
-
set: attrs?.set || index_js_1.ErrorThrower.throws(errors_js_1.TrySetReadonlyError, name),
|
|
88
|
-
enumerable: true
|
|
89
|
-
};
|
|
90
|
-
Object.defineProperty(this.target, name, hasValue
|
|
91
|
-
? defaultDescriptor
|
|
92
|
-
: noValueDescriptor);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
exports.default = Provider;
|
package/cjs/scripts/publish.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { EventListener, EventUnsubscribe, IHasSnapshot, IPublicProperty, PublicFunction } from '../types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Call the event from **main** process.
|
|
4
|
-
*
|
|
5
|
-
* @param name - event name
|
|
6
|
-
*/
|
|
7
|
-
export declare function emitEvent(name: string, value?: any): void;
|
|
8
|
-
/**
|
|
9
|
-
* Handle event from **main** process.
|
|
10
|
-
*
|
|
11
|
-
* @param name - event name
|
|
12
|
-
* @param listener - event handler
|
|
13
|
-
*/
|
|
14
|
-
export declare function on<TValue = any>(name: string, listener: EventListener<TValue>): EventUnsubscribe;
|
|
15
|
-
/**
|
|
16
|
-
* Handle event from **main** process.
|
|
17
|
-
*
|
|
18
|
-
* @param name - event name
|
|
19
|
-
* @param listener - event handler
|
|
20
|
-
*/
|
|
21
|
-
export declare function once<TValue = any>(name: string, listener: EventListener<TValue>): EventUnsubscribe;
|
|
22
|
-
/**
|
|
23
|
-
* Makes the function available for **renderer** and **preload** processes.
|
|
24
|
-
*
|
|
25
|
-
* _only for function_
|
|
26
|
-
*
|
|
27
|
-
* @param name - name by which the function will be accessed
|
|
28
|
-
* @param func - linked function
|
|
29
|
-
*/
|
|
30
|
-
export declare function publicFunction<TFunc extends PublicFunction>(name: string, func: TFunc, receives?: {
|
|
31
|
-
new (): IHasSnapshot;
|
|
32
|
-
}[]): void;
|
|
33
|
-
/**
|
|
34
|
-
* Makes the function available for **renderer** and **preload** processes.
|
|
35
|
-
*
|
|
36
|
-
* _only for variables_
|
|
37
|
-
*
|
|
38
|
-
* @param name - name by which the variable will be accessed
|
|
39
|
-
* @param value - variable getter/setter
|
|
40
|
-
*/
|
|
41
|
-
export declare function publicVariable<T>(name: string, value: IPublicProperty<T>): void;
|
package/cjs/scripts/publish.js
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.emitEvent = emitEvent;
|
|
4
|
-
exports.on = on;
|
|
5
|
-
exports.once = once;
|
|
6
|
-
exports.publicFunction = publicFunction;
|
|
7
|
-
exports.publicVariable = publicVariable;
|
|
8
|
-
const electron_1 = require("electron");
|
|
9
|
-
const enums_js_1 = require("../enums.js");
|
|
10
|
-
const errors_js_1 = require("../errors.js");
|
|
11
|
-
const index_js_1 = require("./helpers/index.js");
|
|
12
|
-
const entitiesInfo = {
|
|
13
|
-
variables: new Set(),
|
|
14
|
-
functions: new Set()
|
|
15
|
-
};
|
|
16
|
-
const mainEventHandlers = new index_js_1.WebContentsContainer();
|
|
17
|
-
/** Выбрасывает ошибку если используется не в том процессе */
|
|
18
|
-
const canThrowError = (index_js_1.Env.global.hasWindow || !electron_1.ipcMain)
|
|
19
|
-
? index_js_1.ErrorThrower.throws(errors_js_1.PublishMethodsUnavailable)
|
|
20
|
-
: () => { };
|
|
21
|
-
electron_1.ipcMain?.on(enums_js_1.IpcChannel.getPublicInfo, e => new index_js_1.MainEvent(e).return(entitiesInfo));
|
|
22
|
-
electron_1.ipcMain?.on(enums_js_1.IpcChannel.eventOn, (e, { value }) => mainEventHandlers.addOn(value, e.sender));
|
|
23
|
-
electron_1.ipcMain?.on(enums_js_1.IpcChannel.eventOnce, (e, { value }) => mainEventHandlers.addOn(value, e.sender));
|
|
24
|
-
/**
|
|
25
|
-
* Call the event from **main** process.
|
|
26
|
-
*
|
|
27
|
-
* @param name - event name
|
|
28
|
-
*/
|
|
29
|
-
function emitEvent(name, value) {
|
|
30
|
-
canThrowError();
|
|
31
|
-
const emitChannel = enums_js_1.IpcChannel.eventEmit + name;
|
|
32
|
-
try {
|
|
33
|
-
if (value instanceof Promise) {
|
|
34
|
-
value
|
|
35
|
-
.then(val => {
|
|
36
|
-
mainEventHandlers.getAll(name).forEach(sender => sender.send(emitChannel, index_js_1.IpcResult.withValue(val)));
|
|
37
|
-
mainEventHandlers.clearOnce(name);
|
|
38
|
-
})
|
|
39
|
-
.catch(reason => {
|
|
40
|
-
mainEventHandlers.getAll(name).forEach(sender => sender.send(emitChannel, index_js_1.IpcResult.withError(reason)));
|
|
41
|
-
mainEventHandlers.clearOnce(name);
|
|
42
|
-
});
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
mainEventHandlers.getAll(name).forEach(sender => sender.send(emitChannel, index_js_1.IpcResult.withValue(value)));
|
|
46
|
-
mainEventHandlers.clearOnce(name);
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
mainEventHandlers.getAll(name).forEach(sender => sender.send(emitChannel, index_js_1.IpcResult.withError(error)));
|
|
50
|
-
mainEventHandlers.clearOnce(name);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Handle event from **main** process.
|
|
55
|
-
*
|
|
56
|
-
* @param name - event name
|
|
57
|
-
* @param listener - event handler
|
|
58
|
-
*/
|
|
59
|
-
function on(name, listener) {
|
|
60
|
-
canThrowError();
|
|
61
|
-
const emitChannel = enums_js_1.IpcChannel.eventEmit + name;
|
|
62
|
-
const handler = (_, { value }) => listener(value);
|
|
63
|
-
electron_1.ipcMain.on(emitChannel, handler);
|
|
64
|
-
return () => electron_1.ipcMain.removeListener(emitChannel, handler);
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Handle event from **main** process.
|
|
68
|
-
*
|
|
69
|
-
* @param name - event name
|
|
70
|
-
* @param listener - event handler
|
|
71
|
-
*/
|
|
72
|
-
function once(name, listener) {
|
|
73
|
-
canThrowError();
|
|
74
|
-
const emitChannel = enums_js_1.IpcChannel.eventEmit + name;
|
|
75
|
-
const handler = (_, { value }) => listener(value);
|
|
76
|
-
electron_1.ipcMain.once(emitChannel, handler);
|
|
77
|
-
return () => electron_1.ipcMain.removeListener(emitChannel, handler);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Makes the function available for **renderer** and **preload** processes.
|
|
81
|
-
*
|
|
82
|
-
* _only for function_
|
|
83
|
-
*
|
|
84
|
-
* @param name - name by which the function will be accessed
|
|
85
|
-
* @param func - linked function
|
|
86
|
-
*/
|
|
87
|
-
function publicFunction(name, func, receives = []) {
|
|
88
|
-
canThrowError();
|
|
89
|
-
const channel = enums_js_1.IpcChannel.functionCall + name;
|
|
90
|
-
entitiesInfo.functions.add(name);
|
|
91
|
-
electron_1.ipcMain.removeAllListeners(channel);
|
|
92
|
-
electron_1.ipcMain.on(channel, (e, { args, id }) => {
|
|
93
|
-
const event = new index_js_1.MainEvent(e);
|
|
94
|
-
try {
|
|
95
|
-
const preparedArgs = args.map((arg, index) => (index_js_1.Transfer.isSnapshotValue(arg) && receives[index]
|
|
96
|
-
? index_js_1.Transfer.getFromSnapshot(arg, receives[index])
|
|
97
|
-
: arg));
|
|
98
|
-
const result = func(...preparedArgs);
|
|
99
|
-
if (result instanceof Promise) {
|
|
100
|
-
const promiseChannel = channel + enums_js_1.IpcChannel.promisePostfix;
|
|
101
|
-
result
|
|
102
|
-
.then(value => event.send(promiseChannel, index_js_1.IpcResult.withValue(value, id)))
|
|
103
|
-
.catch(reason => event.send(promiseChannel, index_js_1.IpcResult.withError(reason, id)));
|
|
104
|
-
event.return(index_js_1.IpcResult.withPromise(promiseChannel));
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
event.return(index_js_1.IpcResult.withValue(result));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
event.return(index_js_1.IpcResult.withError(error));
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Makes the function available for **renderer** and **preload** processes.
|
|
117
|
-
*
|
|
118
|
-
* _only for variables_
|
|
119
|
-
*
|
|
120
|
-
* @param name - name by which the variable will be accessed
|
|
121
|
-
* @param value - variable getter/setter
|
|
122
|
-
*/
|
|
123
|
-
function publicVariable(name, value) {
|
|
124
|
-
canThrowError();
|
|
125
|
-
const getChannel = enums_js_1.IpcChannel.propertyGet + name;
|
|
126
|
-
const setChannel = enums_js_1.IpcChannel.propertySet + name;
|
|
127
|
-
const { get: getter, set: setter } = value;
|
|
128
|
-
entitiesInfo.variables.add(name);
|
|
129
|
-
if (getter) {
|
|
130
|
-
electron_1.ipcMain.removeAllListeners(getChannel);
|
|
131
|
-
electron_1.ipcMain.on(getChannel, e => {
|
|
132
|
-
const event = new index_js_1.MainEvent(e);
|
|
133
|
-
try {
|
|
134
|
-
event.return(index_js_1.IpcResult.withValue(getter()));
|
|
135
|
-
}
|
|
136
|
-
catch (error) {
|
|
137
|
-
event.return(index_js_1.IpcResult.withError(error));
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
if (setter) {
|
|
142
|
-
electron_1.ipcMain.removeAllListeners(setChannel);
|
|
143
|
-
electron_1.ipcMain.on(setChannel, (e, { args }) => {
|
|
144
|
-
const event = new index_js_1.MainEvent(e);
|
|
145
|
-
try {
|
|
146
|
-
setter(args.at(0));
|
|
147
|
-
event.return(index_js_1.IpcResult.withValue(null));
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
event.return(index_js_1.IpcResult.withError(error));
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type { IpcRendererEvent } from 'electron';
|
|
2
|
-
import type { EventListener, EventUnsubscribe, IRequest, IResult } from '../types.js';
|
|
3
|
-
declare class FunctionTunnel {
|
|
4
|
-
private readonly tunnel;
|
|
5
|
-
constructor(name: string);
|
|
6
|
-
call(args?: IRequest): IResult;
|
|
7
|
-
}
|
|
8
|
-
declare class PropertyTunnel {
|
|
9
|
-
private readonly getTunnel;
|
|
10
|
-
private readonly setTunnel;
|
|
11
|
-
constructor(name: string);
|
|
12
|
-
get(): IResult;
|
|
13
|
-
set(value: any): IResult;
|
|
14
|
-
}
|
|
15
|
-
declare class PromiseTunnel {
|
|
16
|
-
private readonly tunnel;
|
|
17
|
-
constructor(channel: string);
|
|
18
|
-
wait(id?: string): Promise<any>;
|
|
19
|
-
}
|
|
20
|
-
declare class EventTunnel {
|
|
21
|
-
private readonly name;
|
|
22
|
-
private readonly emitTunnel;
|
|
23
|
-
private readonly onTunnel;
|
|
24
|
-
private readonly onceTunnel;
|
|
25
|
-
constructor(name: string);
|
|
26
|
-
on(listener: EventListener<IResult>): EventUnsubscribe;
|
|
27
|
-
once(listener: EventListener<IResult>): EventUnsubscribe;
|
|
28
|
-
emit(value: any): void;
|
|
29
|
-
error(error: string): void;
|
|
30
|
-
}
|
|
31
|
-
export default class IPCTunnel {
|
|
32
|
-
private channel;
|
|
33
|
-
static readonly Function: typeof FunctionTunnel;
|
|
34
|
-
static readonly Property: typeof PropertyTunnel;
|
|
35
|
-
static readonly Promise: typeof PromiseTunnel;
|
|
36
|
-
static readonly Event: typeof EventTunnel;
|
|
37
|
-
constructor(channel: string);
|
|
38
|
-
request(args?: IRequest): IResult;
|
|
39
|
-
response(args?: IResult): void;
|
|
40
|
-
on(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
|
|
41
|
-
once(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
|
|
42
|
-
removeListener(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
|
|
43
|
-
removeAllListeners(): void;
|
|
44
|
-
}
|
|
45
|
-
export {};
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const electron_1 = require("electron");
|
|
4
|
-
const enums_js_1 = require("../enums.js");
|
|
5
|
-
const index_js_1 = require("./helpers/index.js");
|
|
6
|
-
class FunctionTunnel {
|
|
7
|
-
tunnel;
|
|
8
|
-
constructor(name) {
|
|
9
|
-
this.tunnel = new IPCTunnel(enums_js_1.IpcChannel.functionCall + name);
|
|
10
|
-
}
|
|
11
|
-
call(args = { args: [] }) {
|
|
12
|
-
return this.tunnel.request(args);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
class PropertyTunnel {
|
|
16
|
-
getTunnel;
|
|
17
|
-
setTunnel;
|
|
18
|
-
constructor(name) {
|
|
19
|
-
this.getTunnel = new IPCTunnel(enums_js_1.IpcChannel.propertyGet + name);
|
|
20
|
-
this.setTunnel = new IPCTunnel(enums_js_1.IpcChannel.propertySet + name);
|
|
21
|
-
}
|
|
22
|
-
get() {
|
|
23
|
-
return this.getTunnel.request();
|
|
24
|
-
}
|
|
25
|
-
set(value) {
|
|
26
|
-
return this.setTunnel.request({ args: [value] });
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
class PromiseTunnel {
|
|
30
|
-
tunnel;
|
|
31
|
-
constructor(channel) {
|
|
32
|
-
this.tunnel = new IPCTunnel(channel);
|
|
33
|
-
}
|
|
34
|
-
wait(id) {
|
|
35
|
-
return new Promise((resolve, reject) => {
|
|
36
|
-
const handler = (_, { error, value, id: resSecret }) => {
|
|
37
|
-
if (id !== undefined && resSecret !== id) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (error) {
|
|
41
|
-
reject(error);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
resolve(value);
|
|
45
|
-
}
|
|
46
|
-
this.tunnel.removeListener(handler);
|
|
47
|
-
};
|
|
48
|
-
this.tunnel.on(handler);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
class EventTunnel {
|
|
53
|
-
name;
|
|
54
|
-
emitTunnel;
|
|
55
|
-
onTunnel;
|
|
56
|
-
onceTunnel;
|
|
57
|
-
constructor(name) {
|
|
58
|
-
this.name = name;
|
|
59
|
-
this.emitTunnel = new IPCTunnel(enums_js_1.IpcChannel.eventEmit + name);
|
|
60
|
-
this.onTunnel = new IPCTunnel(enums_js_1.IpcChannel.eventOn);
|
|
61
|
-
this.onceTunnel = new IPCTunnel(enums_js_1.IpcChannel.eventOnce);
|
|
62
|
-
}
|
|
63
|
-
on(listener) {
|
|
64
|
-
const handler = (_, result) => listener(result);
|
|
65
|
-
this.onTunnel.response(index_js_1.IpcResult.withValue(this.name));
|
|
66
|
-
this.emitTunnel.on(handler);
|
|
67
|
-
return () => this.emitTunnel.removeListener(handler);
|
|
68
|
-
}
|
|
69
|
-
once(listener) {
|
|
70
|
-
const handler = (_, result) => listener(result);
|
|
71
|
-
this.onceTunnel.response(index_js_1.IpcResult.withValue(this.name));
|
|
72
|
-
this.emitTunnel.once(handler);
|
|
73
|
-
return () => this.emitTunnel.removeListener(handler);
|
|
74
|
-
}
|
|
75
|
-
emit(value) {
|
|
76
|
-
this.emitTunnel.response({ value });
|
|
77
|
-
}
|
|
78
|
-
error(error) {
|
|
79
|
-
this.emitTunnel.response({ error });
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
class IPCTunnel {
|
|
83
|
-
channel;
|
|
84
|
-
static Function = FunctionTunnel;
|
|
85
|
-
static Property = PropertyTunnel;
|
|
86
|
-
static Promise = PromiseTunnel;
|
|
87
|
-
static Event = EventTunnel;
|
|
88
|
-
constructor(channel) {
|
|
89
|
-
this.channel = channel;
|
|
90
|
-
}
|
|
91
|
-
request(args = { args: [] }) {
|
|
92
|
-
return electron_1.ipcRenderer.sendSync(this.channel, args);
|
|
93
|
-
}
|
|
94
|
-
response(args = {}) {
|
|
95
|
-
electron_1.ipcRenderer.send(this.channel, args);
|
|
96
|
-
}
|
|
97
|
-
on(listener) {
|
|
98
|
-
electron_1.ipcRenderer.on(this.channel, listener);
|
|
99
|
-
}
|
|
100
|
-
once(listener) {
|
|
101
|
-
electron_1.ipcRenderer.once(this.channel, listener);
|
|
102
|
-
}
|
|
103
|
-
removeListener(listener) {
|
|
104
|
-
electron_1.ipcRenderer.removeListener(this.channel, listener);
|
|
105
|
-
}
|
|
106
|
-
removeAllListeners() {
|
|
107
|
-
electron_1.ipcRenderer.removeAllListeners(this.channel);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
exports.default = IPCTunnel;
|
package/esm/enums.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export declare enum IpcChannel {
|
|
2
|
-
getPublicInfo = "GET_PUBLIC_INFO",
|
|
3
|
-
functionCall = "FUNCTION_CALL_",
|
|
4
|
-
eventOn = "EVENT_ON",
|
|
5
|
-
eventOnce = "EVENT_ONCE",
|
|
6
|
-
eventEmit = "EVENT_EMIT_",
|
|
7
|
-
propertyGet = "PROPERTY_GET_",
|
|
8
|
-
propertySet = "PROPERTY_SET_",
|
|
9
|
-
promisePostfix = "_PROMISE"
|
|
10
|
-
}
|
package/esm/enums.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export var IpcChannel;
|
|
2
|
-
(function (IpcChannel) {
|
|
3
|
-
IpcChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
|
|
4
|
-
IpcChannel["functionCall"] = "FUNCTION_CALL_";
|
|
5
|
-
IpcChannel["eventOn"] = "EVENT_ON";
|
|
6
|
-
IpcChannel["eventOnce"] = "EVENT_ONCE";
|
|
7
|
-
IpcChannel["eventEmit"] = "EVENT_EMIT_";
|
|
8
|
-
IpcChannel["propertyGet"] = "PROPERTY_GET_";
|
|
9
|
-
IpcChannel["propertySet"] = "PROPERTY_SET_";
|
|
10
|
-
IpcChannel["promisePostfix"] = "_PROMISE";
|
|
11
|
-
})(IpcChannel || (IpcChannel = {}));
|