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.
- package/cjs/errors.d.ts +28 -0
- package/cjs/errors.js +39 -0
- package/cjs/exp.d.ts +4 -4
- package/cjs/exp.js +16 -16
- package/cjs/index.d.ts +4 -4
- package/cjs/index.js +15 -15
- package/cjs/main.d.ts +4 -4
- package/cjs/main.js +15 -15
- package/cjs/preload.d.ts +3 -3
- package/cjs/preload.js +4 -4
- package/cjs/renderer.d.ts +2 -2
- package/cjs/renderer.js +2 -2
- package/cjs/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
- package/cjs/scripts/bridge.js +57 -0
- package/{esm/scripts/Main.d.ts → cjs/scripts/main.d.ts} +2 -2
- package/cjs/scripts/main.js +72 -0
- package/cjs/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
- package/cjs/scripts/{newDecorators.js → new-decorators.js} +14 -14
- package/cjs/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
- package/cjs/scripts/{oldDecorators.js → old-decorators.js} +10 -10
- package/cjs/scripts/preload-provider.js +96 -0
- package/cjs/scripts/provider.d.ts +25 -3
- package/cjs/scripts/provider.js +97 -93
- package/cjs/scripts/publish.d.ts +2 -2
- package/cjs/scripts/publish.js +36 -33
- package/cjs/scripts/renderer-tunnels.d.ts +47 -0
- package/cjs/scripts/renderer-tunnels.js +115 -0
- package/cjs/types.d.ts +32 -39
- package/esm/errors.d.ts +28 -0
- package/esm/errors.js +27 -0
- package/esm/exp.d.ts +4 -4
- package/esm/exp.js +3 -3
- package/esm/index.d.ts +4 -4
- package/esm/index.js +3 -3
- package/esm/main.d.ts +4 -4
- package/esm/main.js +3 -3
- package/esm/preload.d.ts +3 -3
- package/esm/preload.js +2 -2
- package/esm/renderer.d.ts +2 -2
- package/esm/renderer.js +1 -1
- package/esm/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
- package/esm/scripts/bridge.js +52 -0
- package/{cjs/scripts/Main.d.ts → esm/scripts/main.d.ts} +2 -2
- package/esm/scripts/main.js +67 -0
- package/esm/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
- package/esm/scripts/{newDecorators.js → new-decorators.js} +1 -1
- package/esm/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
- package/esm/scripts/{oldDecorators.js → old-decorators.js} +1 -1
- package/esm/scripts/preload-provider.js +91 -0
- package/esm/scripts/provider.d.ts +25 -3
- package/esm/scripts/provider.js +96 -93
- package/esm/scripts/publish.d.ts +2 -2
- package/esm/scripts/publish.js +19 -16
- package/esm/scripts/renderer-tunnels.d.ts +47 -0
- package/esm/scripts/renderer-tunnels.js +109 -0
- package/esm/types.d.ts +32 -39
- package/package.json +2 -1
- package/cjs/scripts/Bridge.js +0 -58
- package/cjs/scripts/Main.js +0 -85
- package/cjs/scripts/preloadProvider.js +0 -92
- package/esm/scripts/Bridge.js +0 -53
- package/esm/scripts/Main.js +0 -80
- package/esm/scripts/preloadProvider.js +0 -87
- /package/cjs/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
- /package/esm/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.providePublic = exports.publicProperty = exports.publicStaticProperty = exports.publicClassRendererEvent = exports.publicClassMainEvent = exports.publicMethod = exports.publicStaticMethod = void 0;
|
|
4
|
-
const
|
|
4
|
+
const publish_js_1 = require("./publish.js");
|
|
5
5
|
function publicStaticMethod(arg) {
|
|
6
6
|
return (target, key) => {
|
|
7
7
|
let name = String(key);
|
|
@@ -12,10 +12,10 @@ function publicStaticMethod(arg) {
|
|
|
12
12
|
else {
|
|
13
13
|
name = arg.name ?? String(key);
|
|
14
14
|
if (arg.scope)
|
|
15
|
-
(0,
|
|
15
|
+
(0, publish_js_1.addScope)(name, arg.scope);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
(0,
|
|
18
|
+
(0, publish_js_1.publicFunction)(name, (...args) => target[key](...args));
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
exports.publicStaticMethod = publicStaticMethod;
|
|
@@ -44,10 +44,10 @@ function publicClassMainEvent(arg) {
|
|
|
44
44
|
else {
|
|
45
45
|
name = arg.name ?? String(key);
|
|
46
46
|
if (arg.scope)
|
|
47
|
-
(0,
|
|
47
|
+
(0, publish_js_1.addScope)(name, arg.scope);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
instance[key] = (0,
|
|
50
|
+
instance[key] = (0, publish_js_1.publicMainEvent)(name, instance[key].bind(instance));
|
|
51
51
|
};
|
|
52
52
|
};
|
|
53
53
|
}
|
|
@@ -68,13 +68,13 @@ function publicClassRendererEvent(arg) {
|
|
|
68
68
|
name = arg.name ?? String(key);
|
|
69
69
|
receiver = arg.procFn ?? receiver;
|
|
70
70
|
if (arg.scope)
|
|
71
|
-
(0,
|
|
71
|
+
(0, publish_js_1.addScope)(name, arg.scope);
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
74
74
|
receiver = arg;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
instance[key] = (0,
|
|
77
|
+
instance[key] = (0, publish_js_1.publicRendererEvent)(name, receiver);
|
|
78
78
|
};
|
|
79
79
|
};
|
|
80
80
|
}
|
|
@@ -89,12 +89,12 @@ function publicStaticProperty(arg) {
|
|
|
89
89
|
else {
|
|
90
90
|
name = arg.name ?? String(key);
|
|
91
91
|
if (arg.scope)
|
|
92
|
-
(0,
|
|
92
|
+
(0, publish_js_1.addScope)(name, arg.scope);
|
|
93
93
|
if (arg.access)
|
|
94
|
-
(0,
|
|
94
|
+
(0, publish_js_1.addAccess)(name, arg.access);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
(0,
|
|
97
|
+
(0, publish_js_1.publicVariable)(name, {
|
|
98
98
|
get() {
|
|
99
99
|
return target[key];
|
|
100
100
|
},
|
|
@@ -0,0 +1,96 @@
|
|
|
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 = __importDefault(require("electron"));
|
|
7
|
+
const enums_js_1 = require("../enums.js");
|
|
8
|
+
const renderer_tunnels_js_1 = __importDefault(require("./renderer-tunnels.js"));
|
|
9
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
10
|
+
let mayThrowError = () => { };
|
|
11
|
+
if (typeof window === 'undefined') {
|
|
12
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in main process.'); };
|
|
13
|
+
}
|
|
14
|
+
else if (!('contextBridge' in electron_1.default)) {
|
|
15
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in renderer process.'); };
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Sets access to main from renderer
|
|
19
|
+
*
|
|
20
|
+
* @param contextIsolation - _default_: `true`
|
|
21
|
+
*/
|
|
22
|
+
function provideFromMain(contextIsolation = true) {
|
|
23
|
+
mayThrowError();
|
|
24
|
+
const { contextBridge, ipcRenderer } = electron_1.default;
|
|
25
|
+
const info = ipcRenderer.sendSync(enums_js_1.IPCChannel.getPublicInfo);
|
|
26
|
+
const provider = {
|
|
27
|
+
getInfo: () => info,
|
|
28
|
+
waitPromise(channel, resolve, reject, secret) {
|
|
29
|
+
new renderer_tunnels_js_1.default.Promise(channel).wait(secret)
|
|
30
|
+
.then(resolve)
|
|
31
|
+
.catch(reject);
|
|
32
|
+
},
|
|
33
|
+
provided: {
|
|
34
|
+
functions: getFunctions(),
|
|
35
|
+
properties: getProperties(),
|
|
36
|
+
mainEvents: getMainEvents(),
|
|
37
|
+
rendererEvents: getRendererEvents(),
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
if (contextIsolation) {
|
|
41
|
+
contextBridge.exposeInMainWorld('__provider__', provider);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
window.__provider__ = provider;
|
|
45
|
+
}
|
|
46
|
+
function getFunctions() {
|
|
47
|
+
const result = {};
|
|
48
|
+
for (const name of info.functions) {
|
|
49
|
+
result[name] = (secret, args) => {
|
|
50
|
+
return new renderer_tunnels_js_1.default.Function(name).call({ args, secret });
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
function getProperties() {
|
|
56
|
+
const result = {};
|
|
57
|
+
for (const name of info.properties) {
|
|
58
|
+
const tunnel = new renderer_tunnels_js_1.default.Property(name);
|
|
59
|
+
result[name] = {
|
|
60
|
+
get: () => tunnel.get(),
|
|
61
|
+
set: (value) => tunnel.set(value)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
function getMainEvents() {
|
|
67
|
+
const result = {};
|
|
68
|
+
for (const name of info.mainEvents) {
|
|
69
|
+
result[name] = (type, handler) => {
|
|
70
|
+
const tunnel = new renderer_tunnels_js_1.default.MainEvent(name);
|
|
71
|
+
if (type === 'on')
|
|
72
|
+
return tunnel.on(handler);
|
|
73
|
+
else
|
|
74
|
+
return tunnel.once(handler);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
function getRendererEvents() {
|
|
80
|
+
const result = {};
|
|
81
|
+
for (const name of info.rendererEvents) {
|
|
82
|
+
result[name] = (arg) => {
|
|
83
|
+
const tunnel = new renderer_tunnels_js_1.default.RendererEvent(name);
|
|
84
|
+
if (arg instanceof Promise) {
|
|
85
|
+
arg.then(value => tunnel.emit(value))
|
|
86
|
+
.catch(reason => tunnel.error(String(reason)));
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
tunnel.emit(arg);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.default = provideFromMain;
|
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export default
|
|
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
|
+
}
|
package/cjs/scripts/provider.js
CHANGED
|
@@ -1,112 +1,116 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
const enums_js_1 = require("../enums.js");
|
|
4
|
+
const errors_js_1 = require("../errors.js");
|
|
5
|
+
class Provider {
|
|
6
|
+
as() { return this.target; }
|
|
7
|
+
target = {};
|
|
8
|
+
init() {
|
|
9
|
+
this.initFunctions();
|
|
10
|
+
this.initProperties();
|
|
11
|
+
this.initMainEvents();
|
|
12
|
+
this.initRendererEvents();
|
|
13
|
+
}
|
|
14
|
+
errorHandler(error, channel) {
|
|
15
|
+
throw new Error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
16
|
+
}
|
|
17
|
+
initFunctions() {
|
|
18
|
+
for (const name of this.info.functions) {
|
|
19
|
+
this.define(name, {
|
|
20
|
+
value: (...args) => {
|
|
21
|
+
const secret = Math.random();
|
|
22
|
+
const { error, promiseChannel, value } = this.callFunction(name, secret, args);
|
|
23
|
+
if (error) {
|
|
24
|
+
this.errorHandler(error, name);
|
|
25
|
+
}
|
|
26
|
+
if (promiseChannel)
|
|
27
|
+
return this.waitPromise(promiseChannel, secret);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
initRendererEvents() {
|
|
34
|
+
for (const name of this.info.rendererEvents) {
|
|
35
|
+
this.define(name, {
|
|
36
|
+
value: (arg) => {
|
|
37
|
+
this.emitRendererEvent(name, arg);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
initMainEvents() {
|
|
43
|
+
for (const name of this.info.mainEvents) {
|
|
44
|
+
const eventNameOn = `on${name[0].toUpperCase()}${name.slice(1)}`;
|
|
45
|
+
const eventNameOnce = `once${name[0].toUpperCase()}${name.slice(1)}`;
|
|
46
|
+
this.define(eventNameOn, {
|
|
47
|
+
value: (handler) => {
|
|
48
|
+
return this.handleMainEvent(name, 'on', ({ error, value }) => {
|
|
49
|
+
if (error) {
|
|
50
|
+
this.errorHandler(error, name);
|
|
51
|
+
}
|
|
52
|
+
handler(value);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
this.define(eventNameOnce, {
|
|
57
|
+
value: (handler) => {
|
|
58
|
+
return this.handleMainEvent(name, 'once', ({ error, value }) => {
|
|
59
|
+
if (error) {
|
|
60
|
+
this.errorHandler(error, name);
|
|
61
|
+
}
|
|
62
|
+
handler(value);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
initProperties() {
|
|
69
|
+
for (const name of this.info.properties) {
|
|
70
|
+
this.define(name, {
|
|
71
|
+
get: this.info.accesses.get(name).has(enums_js_1.Access.get)
|
|
72
|
+
? () => {
|
|
73
|
+
const { error, value } = this.getVariable(name);
|
|
74
|
+
if (error) {
|
|
75
|
+
this.errorHandler(error, name);
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
: () => { throw new errors_js_1.NoGetAccessError(name); },
|
|
80
|
+
set: this.info.accesses.get(name).has(enums_js_1.Access.set)
|
|
81
|
+
? (value) => {
|
|
82
|
+
const { error } = this.setVariable(name, value);
|
|
83
|
+
if (error) {
|
|
84
|
+
this.errorHandler(error, name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
: () => { throw new errors_js_1.NoSetAccessError(name); }
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
define(name, attrs) {
|
|
13
92
|
let descriptor;
|
|
14
|
-
if (!info.scopes.get(name).has(scope)) {
|
|
93
|
+
if (!this.info.scopes.get(name).has(this.scope)) {
|
|
15
94
|
descriptor = {
|
|
16
|
-
get() { throw new
|
|
17
|
-
set() { throw new
|
|
95
|
+
get: () => { throw new errors_js_1.NotProvidedIntoScopeError(name, this.scope); },
|
|
96
|
+
set: () => { throw new errors_js_1.NotProvidedIntoScopeError(name, this.scope); }
|
|
18
97
|
};
|
|
19
98
|
}
|
|
20
99
|
else if (attrs?.value) {
|
|
21
100
|
descriptor = {
|
|
22
101
|
get: () => attrs.value,
|
|
23
|
-
set: () => { throw new
|
|
102
|
+
set: () => { throw new errors_js_1.TrySetReadonlyError(name); },
|
|
24
103
|
enumerable: true
|
|
25
104
|
};
|
|
26
105
|
}
|
|
27
106
|
else {
|
|
28
107
|
descriptor = {
|
|
29
|
-
get: attrs?.get || (() => { throw new
|
|
30
|
-
set: attrs?.set || (() => { throw new
|
|
108
|
+
get: attrs?.get || (() => { throw new errors_js_1.NoSetAccessError(name); }),
|
|
109
|
+
set: attrs?.set || (() => { throw new errors_js_1.TrySetReadonlyError(name); }),
|
|
31
110
|
enumerable: true
|
|
32
111
|
};
|
|
33
112
|
}
|
|
34
|
-
Object.defineProperty(target, name, descriptor);
|
|
113
|
+
Object.defineProperty(this.target, name, descriptor);
|
|
35
114
|
}
|
|
36
|
-
info.functions.forEach(funcName => {
|
|
37
|
-
define(funcName, {
|
|
38
|
-
value(...args) {
|
|
39
|
-
const result = callFunction(funcName, ...args);
|
|
40
|
-
if (result.error) {
|
|
41
|
-
errorHandler(result.error, funcName);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
if (result.promiseChannel)
|
|
45
|
-
return waitPromise(result.promiseChannel);
|
|
46
|
-
return result.value;
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
info.rendererEvents.forEach(eventName => {
|
|
51
|
-
define(eventName, {
|
|
52
|
-
value(arg) {
|
|
53
|
-
emitRendererEvent(eventName, arg);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
info.mainEvents.forEach(mainEventName => {
|
|
58
|
-
const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
59
|
-
const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
60
|
-
define(eventNameOn, {
|
|
61
|
-
value(handler) {
|
|
62
|
-
return handleMainEvent(mainEventName, 'on', (result) => {
|
|
63
|
-
if (result.error) {
|
|
64
|
-
errorHandler(result.error, mainEventName);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (result.promiseChannel)
|
|
68
|
-
waitPromise(result.promiseChannel).then(handler);
|
|
69
|
-
handler(result.value);
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
define(eventNameOnce, {
|
|
74
|
-
value(handler) {
|
|
75
|
-
return handleMainEvent(mainEventName, 'once', (result) => {
|
|
76
|
-
if (result.error) {
|
|
77
|
-
errorHandler(result.error, mainEventName);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (result.promiseChannel)
|
|
81
|
-
waitPromise(result.promiseChannel).then(handler);
|
|
82
|
-
handler(result.value);
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
info.properties.forEach(propName => {
|
|
88
|
-
define(propName, {
|
|
89
|
-
get: info.accesses.get(propName).has(enums_1.Access.get)
|
|
90
|
-
? () => {
|
|
91
|
-
const result = getVariable(propName);
|
|
92
|
-
if (result.error) {
|
|
93
|
-
errorHandler(result.error, propName);
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
return result.value;
|
|
97
|
-
}
|
|
98
|
-
: () => { throw new Error(`No access to getting the '${propName}' property`); },
|
|
99
|
-
set: info.accesses.get(propName).has(enums_1.Access.set)
|
|
100
|
-
? (value) => {
|
|
101
|
-
const result = setVariable(propName, value);
|
|
102
|
-
if (result?.error) {
|
|
103
|
-
errorHandler(result.error, propName);
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
: () => { throw new Error(`No access to setting the '${propName}' property`); }
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
return target;
|
|
111
115
|
}
|
|
112
|
-
exports.default =
|
|
116
|
+
exports.default = Provider;
|
package/cjs/scripts/publish.d.ts
CHANGED
|
@@ -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
|
*
|
package/cjs/scripts/publish.js
CHANGED
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = exports.publicRendererEvent = exports.publicMainEvent = void 0;
|
|
7
7
|
const electron_1 = __importDefault(require("electron"));
|
|
8
|
-
const
|
|
8
|
+
const enums_js_1 = require("../enums.js");
|
|
9
9
|
const publicInfo = {
|
|
10
10
|
properties: new Set(),
|
|
11
11
|
functions: new Set(),
|
|
@@ -20,7 +20,7 @@ if (typeof window !== 'undefined' || !('ipcMain' in electron_1.default)) {
|
|
|
20
20
|
mayThrowError = () => { throw new Error('Publish methods is available only in main process.'); };
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
|
-
electron_1.default.ipcMain.on(
|
|
23
|
+
electron_1.default.ipcMain.on(enums_js_1.IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
@@ -29,13 +29,13 @@ else {
|
|
|
29
29
|
* @param procFn - function of processing before sending the event
|
|
30
30
|
* @param scopes - event scopes
|
|
31
31
|
*/
|
|
32
|
-
function publicMainEvent(name, procFn = ((...args) => args[0]), scopes = [
|
|
32
|
+
function publicMainEvent(name, procFn = ((...args) => args[0]), scopes = [enums_js_1.Scope.preload, enums_js_1.Scope.renderer]) {
|
|
33
33
|
mayThrowError();
|
|
34
34
|
const { ipcMain } = electron_1.default;
|
|
35
35
|
const { mainEvents } = publicInfo;
|
|
36
|
-
const mainEmitChannel =
|
|
37
|
-
const mainHandleChannel =
|
|
38
|
-
const mainHandleChannelOnce =
|
|
36
|
+
const mainEmitChannel = enums_js_1.IPCChannel.mainEventEmit + name;
|
|
37
|
+
const mainHandleChannel = enums_js_1.IPCChannel.mainEventOn + name;
|
|
38
|
+
const mainHandleChannelOnce = enums_js_1.IPCChannel.mainEventOnce + name;
|
|
39
39
|
let senders = [];
|
|
40
40
|
let sendersOnce = [];
|
|
41
41
|
mainEvents.add(name);
|
|
@@ -90,7 +90,7 @@ exports.publicMainEvent = publicMainEvent;
|
|
|
90
90
|
* @param procFn - processing function before calling handlers
|
|
91
91
|
* @param scopes - event scopes
|
|
92
92
|
*/
|
|
93
|
-
function publicRendererEvent(name, procFn = ((v) => v), scopes = [
|
|
93
|
+
function publicRendererEvent(name, procFn = ((v) => v), scopes = [enums_js_1.Scope.preload, enums_js_1.Scope.renderer]) {
|
|
94
94
|
mayThrowError();
|
|
95
95
|
const { ipcMain } = electron_1.default;
|
|
96
96
|
if (name.startsWith('on')) {
|
|
@@ -98,17 +98,18 @@ function publicRendererEvent(name, procFn = ((v) => v), scopes = [enums_1.Scope.
|
|
|
98
98
|
name = `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
99
99
|
}
|
|
100
100
|
const { rendererEvents } = publicInfo;
|
|
101
|
-
const rendererEmitChannel =
|
|
101
|
+
const rendererEmitChannel = enums_js_1.IPCChannel.rendererEventEmit + name;
|
|
102
102
|
const handlers = new Set();
|
|
103
103
|
const handlersOnce = new Set();
|
|
104
104
|
rendererEvents.add(name);
|
|
105
|
-
if (!publicInfo.scopes.has(name))
|
|
105
|
+
if (!publicInfo.scopes.has(name)) {
|
|
106
106
|
publicInfo.scopes.set(name, new Set(scopes));
|
|
107
|
+
}
|
|
107
108
|
ipcMain.removeAllListeners(rendererEmitChannel);
|
|
108
|
-
ipcMain.on(rendererEmitChannel, (_,
|
|
109
|
-
if (
|
|
110
|
-
throw new Error(
|
|
111
|
-
const converted = procFn(
|
|
109
|
+
ipcMain.on(rendererEmitChannel, (_, { error, value }) => {
|
|
110
|
+
if (error)
|
|
111
|
+
throw new Error(error);
|
|
112
|
+
const converted = procFn(value);
|
|
112
113
|
handlers.forEach(handler => handler(converted));
|
|
113
114
|
handlersOnce.forEach(handler => handler(converted));
|
|
114
115
|
handlersOnce.clear();
|
|
@@ -134,23 +135,24 @@ exports.publicRendererEvent = publicRendererEvent;
|
|
|
134
135
|
* @param func - linked function
|
|
135
136
|
* @param scopes - function scopes
|
|
136
137
|
*/
|
|
137
|
-
function publicFunction(name, func, scopes = [
|
|
138
|
+
function publicFunction(name, func, scopes = [enums_js_1.Scope.preload, enums_js_1.Scope.renderer]) {
|
|
138
139
|
mayThrowError();
|
|
139
140
|
const { ipcMain } = electron_1.default;
|
|
140
141
|
const { functions } = publicInfo;
|
|
141
|
-
const channel =
|
|
142
|
+
const channel = enums_js_1.IPCChannel.functionCall + name;
|
|
142
143
|
functions.add(name);
|
|
143
|
-
if (!publicInfo.scopes.has(name))
|
|
144
|
+
if (!publicInfo.scopes.has(name)) {
|
|
144
145
|
publicInfo.scopes.set(name, new Set(scopes));
|
|
146
|
+
}
|
|
145
147
|
ipcMain.removeAllListeners(channel);
|
|
146
|
-
ipcMain.on(channel, (e,
|
|
148
|
+
ipcMain.on(channel, (e, { args, secret }) => {
|
|
147
149
|
try {
|
|
148
150
|
const result = func(...args);
|
|
149
151
|
if (result instanceof Promise) {
|
|
150
|
-
const promiseChannel = channel +
|
|
152
|
+
const promiseChannel = channel + enums_js_1.IPCChannel.promisePostfix;
|
|
151
153
|
result
|
|
152
|
-
.then(value => e.sender.send(promiseChannel, { value }))
|
|
153
|
-
.catch(reason => e.sender.send(promiseChannel, { error: String(reason?.stack || reason) }));
|
|
154
|
+
.then(value => e.sender.send(promiseChannel, { value, secret }))
|
|
155
|
+
.catch(reason => e.sender.send(promiseChannel, { error: String(reason?.stack || reason), secret }));
|
|
154
156
|
e.returnValue = { promiseChannel };
|
|
155
157
|
}
|
|
156
158
|
else {
|
|
@@ -172,48 +174,49 @@ exports.publicFunction = publicFunction;
|
|
|
172
174
|
* @param value - variable getter/setter
|
|
173
175
|
* @param scopes - variable scopes
|
|
174
176
|
*/
|
|
175
|
-
function publicVariable(name, value, scopes = [
|
|
177
|
+
function publicVariable(name, value, scopes = [enums_js_1.Scope.renderer, enums_js_1.Scope.preload]) {
|
|
176
178
|
mayThrowError();
|
|
177
179
|
const { ipcMain } = electron_1.default;
|
|
178
180
|
const { properties } = publicInfo;
|
|
179
|
-
const getChannel =
|
|
180
|
-
const setChannel =
|
|
181
|
+
const getChannel = enums_js_1.IPCChannel.propertyGet + name;
|
|
182
|
+
const setChannel = enums_js_1.IPCChannel.propertySet + name;
|
|
181
183
|
const { get: getter, set: setter } = value;
|
|
182
184
|
properties.add(name);
|
|
183
|
-
if (!publicInfo.scopes.has(name))
|
|
185
|
+
if (!publicInfo.scopes.has(name)) {
|
|
184
186
|
publicInfo.scopes.set(name, new Set(scopes));
|
|
187
|
+
}
|
|
185
188
|
if (!publicInfo.accesses.has(name)) {
|
|
186
189
|
const accesses = [];
|
|
187
|
-
getter && accesses.push(
|
|
188
|
-
setter && accesses.push(
|
|
190
|
+
getter && accesses.push(enums_js_1.Access.get);
|
|
191
|
+
setter && accesses.push(enums_js_1.Access.set);
|
|
189
192
|
publicInfo.accesses.set(name, new Set(accesses));
|
|
190
193
|
}
|
|
191
194
|
else {
|
|
192
195
|
const accesses = publicInfo.accesses.get(name);
|
|
193
|
-
getter && accesses.add(
|
|
194
|
-
setter && accesses.add(
|
|
196
|
+
getter && accesses.add(enums_js_1.Access.get);
|
|
197
|
+
setter && accesses.add(enums_js_1.Access.set);
|
|
195
198
|
}
|
|
196
199
|
if (getter) {
|
|
197
200
|
ipcMain.removeAllListeners(getChannel);
|
|
198
|
-
ipcMain.on(getChannel, event => {
|
|
201
|
+
ipcMain.on(getChannel, (event) => {
|
|
199
202
|
try {
|
|
200
203
|
const result = getter();
|
|
201
204
|
event.returnValue = { value: result };
|
|
202
205
|
}
|
|
203
206
|
catch (error) {
|
|
204
|
-
event.returnValue = { error };
|
|
207
|
+
event.returnValue = { error: String(error?.stack || error) };
|
|
205
208
|
}
|
|
206
209
|
});
|
|
207
210
|
}
|
|
208
211
|
if (setter) {
|
|
209
212
|
ipcMain.removeAllListeners(setChannel);
|
|
210
|
-
ipcMain.on(setChannel, (event,
|
|
213
|
+
ipcMain.on(setChannel, (event, { args }) => {
|
|
211
214
|
try {
|
|
212
|
-
setter(args
|
|
215
|
+
setter(args.at(0));
|
|
213
216
|
event.returnValue = { value: null };
|
|
214
217
|
}
|
|
215
218
|
catch (error) {
|
|
216
|
-
event.returnValue = { error };
|
|
219
|
+
event.returnValue = { error: String(error?.stack || error) };
|
|
217
220
|
}
|
|
218
221
|
});
|
|
219
222
|
}
|
|
@@ -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 {};
|