emr-bridge 1.5.0 → 1.6.0
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 +20 -20
- package/cjs/enums.js +3 -3
- package/cjs/index.d.ts +2 -0
- package/cjs/index.js +16 -0
- package/cjs/main.d.ts +4 -0
- package/cjs/main.js +18 -0
- package/cjs/scripts/Bridge.d.ts +2 -1
- package/cjs/scripts/Bridge.js +44 -27
- package/cjs/scripts/Main.d.ts +2 -1
- package/cjs/scripts/Main.js +71 -51
- package/cjs/scripts/newDecorators.d.ts +13 -6
- package/cjs/scripts/newDecorators.js +1 -1
- package/cjs/scripts/oldDecorators.d.ts +13 -37
- package/cjs/scripts/oldDecorators.js +1 -1
- package/cjs/scripts/preloadProvider.js +28 -15
- package/cjs/scripts/publish.d.ts +9 -5
- package/cjs/scripts/publish.js +40 -18
- package/cjs/types.d.ts +8 -8
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/main.d.ts +4 -0
- package/esm/main.js +3 -0
- package/esm/scripts/Bridge.d.ts +2 -1
- package/esm/scripts/Bridge.js +44 -27
- package/esm/scripts/Main.d.ts +2 -1
- package/esm/scripts/Main.js +71 -51
- package/esm/scripts/newDecorators.d.ts +13 -6
- package/esm/scripts/newDecorators.js +1 -1
- package/esm/scripts/oldDecorators.d.ts +13 -37
- package/esm/scripts/oldDecorators.js +1 -1
- package/esm/scripts/preloadProvider.js +12 -2
- package/esm/scripts/publish.d.ts +9 -5
- package/esm/scripts/publish.js +25 -6
- package/esm/types.d.ts +8 -8
- package/package.json +3 -35
|
@@ -3,7 +3,6 @@ import type { EventReceiver, IPublishMainEventArgs, IPublishMethodArgs, IPublish
|
|
|
3
3
|
* Makes the method available for **renderer** and **preload** processes.
|
|
4
4
|
*
|
|
5
5
|
* _only for static_
|
|
6
|
-
*
|
|
7
6
|
* _experimental decorators_
|
|
8
7
|
*/
|
|
9
8
|
export declare function publicStaticMethod(): MethodDecorator;
|
|
@@ -11,8 +10,8 @@ export declare function publicStaticMethod(): MethodDecorator;
|
|
|
11
10
|
* Makes the method available for **renderer** and **preload** processes.
|
|
12
11
|
*
|
|
13
12
|
* _only for static_
|
|
14
|
-
*
|
|
15
13
|
* _experimental decorators_
|
|
14
|
+
*
|
|
16
15
|
* @param name - name by which the method will be accessed
|
|
17
16
|
*/
|
|
18
17
|
export declare function publicStaticMethod(name: string): MethodDecorator;
|
|
@@ -20,8 +19,8 @@ export declare function publicStaticMethod(name: string): MethodDecorator;
|
|
|
20
19
|
* Makes the method available for **renderer** and **preload** processes.
|
|
21
20
|
*
|
|
22
21
|
* _only for static_
|
|
23
|
-
*
|
|
24
22
|
* _experimental decorators_
|
|
23
|
+
*
|
|
25
24
|
* @param args - publish args
|
|
26
25
|
*/
|
|
27
26
|
export declare function publicStaticMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
@@ -29,9 +28,7 @@ export declare function publicStaticMethod(args: IPublishMethodArgs): MethodDeco
|
|
|
29
28
|
* Makes the method available for **renderer** and **preload** processes.
|
|
30
29
|
*
|
|
31
30
|
* _only for non-static_
|
|
32
|
-
*
|
|
33
31
|
* _experimental decorators_
|
|
34
|
-
*
|
|
35
32
|
* _required `providePublic(classInstance)`_
|
|
36
33
|
*/
|
|
37
34
|
export declare function publicMethod(): MethodDecorator;
|
|
@@ -39,10 +36,9 @@ export declare function publicMethod(): MethodDecorator;
|
|
|
39
36
|
* Makes the method available for **renderer** and **preload** processes.
|
|
40
37
|
*
|
|
41
38
|
* _only for non-static_
|
|
42
|
-
*
|
|
43
39
|
* _experimental decorators_
|
|
44
|
-
*
|
|
45
40
|
* _required `providePublic(classInstance)`_
|
|
41
|
+
*
|
|
46
42
|
* @param name - name by which the method will be accessed
|
|
47
43
|
*/
|
|
48
44
|
export declare function publicMethod(name: string): MethodDecorator;
|
|
@@ -50,10 +46,9 @@ export declare function publicMethod(name: string): MethodDecorator;
|
|
|
50
46
|
* Makes the method available for **renderer** and **preload** processes.
|
|
51
47
|
*
|
|
52
48
|
* _only for non-static_
|
|
53
|
-
*
|
|
54
49
|
* _experimental decorators_
|
|
55
|
-
*
|
|
56
50
|
* _required `providePublic(classInstance)`_
|
|
51
|
+
*
|
|
57
52
|
* @param args - publish args
|
|
58
53
|
*/
|
|
59
54
|
export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
@@ -61,9 +56,7 @@ export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
|
61
56
|
* Makes the event available for **renderer** and **preload** processes.
|
|
62
57
|
*
|
|
63
58
|
* _only for non-static_
|
|
64
|
-
*
|
|
65
59
|
* _experimental decorators_
|
|
66
|
-
*
|
|
67
60
|
* _required `providePublic(classInstance)`_
|
|
68
61
|
*/
|
|
69
62
|
export declare function publicClassMainEvent(): MethodDecorator;
|
|
@@ -71,10 +64,9 @@ export declare function publicClassMainEvent(): MethodDecorator;
|
|
|
71
64
|
* Makes the event available for **renderer** and **preload** processes.
|
|
72
65
|
*
|
|
73
66
|
* _only for non-static_
|
|
74
|
-
*
|
|
75
67
|
* _experimental decorators_
|
|
76
|
-
*
|
|
77
68
|
* _required `providePublic(classInstance)`_
|
|
69
|
+
*
|
|
78
70
|
* @param name - name by which the event will be accessed
|
|
79
71
|
*/
|
|
80
72
|
export declare function publicClassMainEvent(name: string): MethodDecorator;
|
|
@@ -82,10 +74,9 @@ export declare function publicClassMainEvent(name: string): MethodDecorator;
|
|
|
82
74
|
* Makes the event available for **renderer** and **preload** processes.
|
|
83
75
|
*
|
|
84
76
|
* _only for non-static_
|
|
85
|
-
*
|
|
86
77
|
* _experimental decorators_
|
|
87
|
-
*
|
|
88
78
|
* _required `providePublic(classInstance)`_
|
|
79
|
+
*
|
|
89
80
|
* @param args - publish args
|
|
90
81
|
*/
|
|
91
82
|
export declare function publicClassMainEvent(args: IPublishMainEventArgs): MethodDecorator;
|
|
@@ -93,7 +84,6 @@ export declare function publicClassMainEvent(args: IPublishMainEventArgs): Metho
|
|
|
93
84
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
94
85
|
*
|
|
95
86
|
* _only for non-static_
|
|
96
|
-
*
|
|
97
87
|
* _experimental decorators_
|
|
98
88
|
*/
|
|
99
89
|
export declare function publicClassRendererEvent(): PropertyDecorator;
|
|
@@ -101,8 +91,8 @@ export declare function publicClassRendererEvent(): PropertyDecorator;
|
|
|
101
91
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
102
92
|
*
|
|
103
93
|
* _only for non-static_
|
|
104
|
-
*
|
|
105
94
|
* _experimental decorators_
|
|
95
|
+
*
|
|
106
96
|
* @param name - name by which the event will be accessed
|
|
107
97
|
*/
|
|
108
98
|
export declare function publicClassRendererEvent(name: string): PropertyDecorator;
|
|
@@ -110,8 +100,8 @@ export declare function publicClassRendererEvent(name: string): PropertyDecorato
|
|
|
110
100
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
111
101
|
*
|
|
112
102
|
* _only for non-static_
|
|
113
|
-
*
|
|
114
103
|
* _experimental decorators_
|
|
104
|
+
*
|
|
115
105
|
* @param receiver - linked receiver
|
|
116
106
|
*/
|
|
117
107
|
export declare function publicClassRendererEvent(receiver: EventReceiver): PropertyDecorator;
|
|
@@ -119,8 +109,8 @@ export declare function publicClassRendererEvent(receiver: EventReceiver): Prope
|
|
|
119
109
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
120
110
|
*
|
|
121
111
|
* _only for non-static_
|
|
122
|
-
*
|
|
123
112
|
* _experimental decorators_
|
|
113
|
+
*
|
|
124
114
|
* @param name - name by which the event will be accessed
|
|
125
115
|
* @param receiver - linked receiver
|
|
126
116
|
*/
|
|
@@ -129,17 +119,8 @@ export declare function publicClassRendererEvent(name: string, receiver: EventRe
|
|
|
129
119
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
130
120
|
*
|
|
131
121
|
* _only for non-static_
|
|
132
|
-
*
|
|
133
122
|
* _experimental decorators_
|
|
134
|
-
* @param args - publish args
|
|
135
|
-
*/
|
|
136
|
-
export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
|
|
137
|
-
/**
|
|
138
|
-
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
139
|
-
*
|
|
140
|
-
* _only for non-static_
|
|
141
123
|
*
|
|
142
|
-
* _experimental decorators_
|
|
143
124
|
* @param args - publish args
|
|
144
125
|
*/
|
|
145
126
|
export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
|
|
@@ -147,7 +128,6 @@ export declare function publicClassRendererEvent(args: IPublishRendererEventArgs
|
|
|
147
128
|
* Makes the property available for **renderer** and **preload** processes.
|
|
148
129
|
*
|
|
149
130
|
* _only for static_
|
|
150
|
-
*
|
|
151
131
|
* _experimental decorators_
|
|
152
132
|
*/
|
|
153
133
|
export declare function publicStaticProperty(): PropertyDecorator;
|
|
@@ -155,8 +135,8 @@ export declare function publicStaticProperty(): PropertyDecorator;
|
|
|
155
135
|
* Makes the property available for **renderer** and **preload** processes.
|
|
156
136
|
*
|
|
157
137
|
* _only for static_
|
|
158
|
-
*
|
|
159
138
|
* _experimental decorators_
|
|
139
|
+
*
|
|
160
140
|
* @param name - name by which the property will be accessed
|
|
161
141
|
*/
|
|
162
142
|
export declare function publicStaticProperty(name: string): PropertyDecorator;
|
|
@@ -164,8 +144,8 @@ export declare function publicStaticProperty(name: string): PropertyDecorator;
|
|
|
164
144
|
* Makes the property available for **renderer** and **preload** processes.
|
|
165
145
|
*
|
|
166
146
|
* _only for static_
|
|
167
|
-
*
|
|
168
147
|
* _experimental decorators_
|
|
148
|
+
*
|
|
169
149
|
* @param args - publish args
|
|
170
150
|
*/
|
|
171
151
|
export declare function publicStaticProperty(args: IPublishPropertyArgs): PropertyDecorator;
|
|
@@ -173,9 +153,7 @@ export declare function publicStaticProperty(args: IPublishPropertyArgs): Proper
|
|
|
173
153
|
* Makes the property available for **renderer** and **preload** processes.
|
|
174
154
|
*
|
|
175
155
|
* _only for non-static_
|
|
176
|
-
*
|
|
177
156
|
* _experimental decorators_
|
|
178
|
-
*
|
|
179
157
|
* _required `providePublic(classInstance)`_
|
|
180
158
|
*/
|
|
181
159
|
export declare function publicProperty(): PropertyDecorator;
|
|
@@ -183,10 +161,9 @@ export declare function publicProperty(): PropertyDecorator;
|
|
|
183
161
|
* Makes the property available for **renderer** and **preload** processes.
|
|
184
162
|
*
|
|
185
163
|
* _only for non-static_
|
|
186
|
-
*
|
|
187
164
|
* _experimental decorators_
|
|
188
|
-
*
|
|
189
165
|
* _required `providePublic(classInstance)`_
|
|
166
|
+
*
|
|
190
167
|
* @param name - name by which the property will be accessed
|
|
191
168
|
*/
|
|
192
169
|
export declare function publicProperty(name: string): PropertyDecorator;
|
|
@@ -194,10 +171,9 @@ export declare function publicProperty(name: string): PropertyDecorator;
|
|
|
194
171
|
* Makes the property available for **renderer** and **preload** processes.
|
|
195
172
|
*
|
|
196
173
|
* _only for non-static_
|
|
197
|
-
*
|
|
198
174
|
* _experimental decorators_
|
|
199
|
-
*
|
|
200
175
|
* _required `providePublic(classInstance)`_
|
|
176
|
+
*
|
|
201
177
|
* @param args - publish args
|
|
202
178
|
*/
|
|
203
179
|
export declare function publicProperty(args: IPublishPropertyArgs): PropertyDecorator;
|
|
@@ -66,7 +66,7 @@ function publicClassRendererEvent(arg) {
|
|
|
66
66
|
}
|
|
67
67
|
else if (typeof arg === 'object') {
|
|
68
68
|
name = arg.name ?? String(key);
|
|
69
|
-
receiver = arg.
|
|
69
|
+
receiver = arg.procFn ?? receiver;
|
|
70
70
|
if (arg.scope)
|
|
71
71
|
(0, publish_1.addScope)(name, arg.scope);
|
|
72
72
|
}
|
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const electron_1 = require("electron");
|
|
6
|
+
const electron_1 = __importDefault(require("electron"));
|
|
4
7
|
const enums_1 = require("../enums");
|
|
5
|
-
|
|
8
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
9
|
+
let mayThrowError = () => { };
|
|
10
|
+
if (typeof window === 'undefined') {
|
|
11
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in main process.'); };
|
|
12
|
+
}
|
|
13
|
+
else if (!electron_1.default.contextBridge) {
|
|
14
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in renderer process.'); };
|
|
15
|
+
}
|
|
6
16
|
/**
|
|
7
17
|
* Sets access to main from renderer
|
|
8
18
|
*
|
|
9
19
|
* @param contextIsolation - _default_: `true`
|
|
10
20
|
*/
|
|
11
21
|
function provideFromMain(contextIsolation = true) {
|
|
22
|
+
mayThrowError();
|
|
23
|
+
const { contextBridge, ipcRenderer } = electron_1.default;
|
|
24
|
+
const info = ipcRenderer.sendSync(enums_1.IPCChannel.getPublicInfo);
|
|
12
25
|
const provider = {
|
|
13
26
|
getInfo() {
|
|
14
27
|
return info;
|
|
15
28
|
},
|
|
16
29
|
waitPromise(channel, resolve, reject) {
|
|
17
|
-
|
|
30
|
+
ipcRenderer.once(channel, (_, result) => {
|
|
18
31
|
if (result.error)
|
|
19
32
|
reject?.(result.error);
|
|
20
33
|
else
|
|
@@ -30,7 +43,7 @@ function provideFromMain(contextIsolation = true) {
|
|
|
30
43
|
};
|
|
31
44
|
info.functions.forEach(funcName => {
|
|
32
45
|
provider.provided.functions[funcName] = (...args) => {
|
|
33
|
-
return
|
|
46
|
+
return ipcRenderer.sendSync(enums_1.IPCChannel.functionCall + funcName, ...args);
|
|
34
47
|
};
|
|
35
48
|
});
|
|
36
49
|
info.rendererEvents.forEach(eventName => {
|
|
@@ -38,11 +51,11 @@ function provideFromMain(contextIsolation = true) {
|
|
|
38
51
|
const emitChannel = enums_1.IPCChannel.rendererEventEmit + eventName;
|
|
39
52
|
if (arg instanceof Promise) {
|
|
40
53
|
arg
|
|
41
|
-
.then(value =>
|
|
42
|
-
.catch(reason =>
|
|
54
|
+
.then(value => ipcRenderer.send(emitChannel, { value }))
|
|
55
|
+
.catch(reason => ipcRenderer.send(emitChannel, { error: String(reason) }));
|
|
43
56
|
}
|
|
44
57
|
else {
|
|
45
|
-
|
|
58
|
+
ipcRenderer.send(emitChannel, { value: arg });
|
|
46
59
|
}
|
|
47
60
|
};
|
|
48
61
|
});
|
|
@@ -51,28 +64,28 @@ function provideFromMain(contextIsolation = true) {
|
|
|
51
64
|
const listener = (_, result) => handler(result);
|
|
52
65
|
const emitChannel = enums_1.IPCChannel.mainEventEmit + eventName;
|
|
53
66
|
if (type === 'on') {
|
|
54
|
-
|
|
55
|
-
|
|
67
|
+
ipcRenderer.on(emitChannel, listener);
|
|
68
|
+
ipcRenderer.send(enums_1.IPCChannel.mainEventOn + eventName);
|
|
56
69
|
}
|
|
57
70
|
else {
|
|
58
|
-
|
|
59
|
-
|
|
71
|
+
ipcRenderer.once(emitChannel, listener);
|
|
72
|
+
ipcRenderer.send(enums_1.IPCChannel.mainEventOnce + eventName);
|
|
60
73
|
}
|
|
61
|
-
return () =>
|
|
74
|
+
return () => ipcRenderer.removeListener(emitChannel, listener);
|
|
62
75
|
};
|
|
63
76
|
});
|
|
64
77
|
info.properties.forEach(propName => {
|
|
65
78
|
provider.provided.properties[propName] = {
|
|
66
79
|
get() {
|
|
67
|
-
return
|
|
80
|
+
return ipcRenderer.sendSync(enums_1.IPCChannel.propertyGet + propName);
|
|
68
81
|
},
|
|
69
82
|
set(value) {
|
|
70
|
-
|
|
83
|
+
ipcRenderer.sendSync(enums_1.IPCChannel.propertySet + propName, value);
|
|
71
84
|
}
|
|
72
85
|
};
|
|
73
86
|
});
|
|
74
87
|
if (contextIsolation)
|
|
75
|
-
|
|
88
|
+
contextBridge.exposeInMainWorld('__provider__', provider);
|
|
76
89
|
else
|
|
77
90
|
window.__provider__ = provider;
|
|
78
91
|
}
|
package/cjs/scripts/publish.d.ts
CHANGED
|
@@ -1,36 +1,40 @@
|
|
|
1
1
|
import { Access, Scope } from '../enums';
|
|
2
|
-
import type {
|
|
2
|
+
import type { PublicFunction, PublicProperty, RendererEvent } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
5
5
|
*
|
|
6
6
|
* @param name - name by which the event will be accessed
|
|
7
|
+
* @param procFn - function of processing before sending the event
|
|
7
8
|
* @param scopes - event scopes
|
|
8
9
|
*/
|
|
9
|
-
export declare function publicMainEvent<
|
|
10
|
+
export declare function publicMainEvent<In = void, Out = In extends Array<any> ? In[0] : In, ProcFn extends (...args: any[]) => any = In extends Array<any> ? (...args: In) => Out : (val: In) => Out>(name: string, procFn?: ProcFn, scopes?: Scope[]): (...args: Parameters<ProcFn>) => void;
|
|
10
11
|
/**
|
|
11
12
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
12
13
|
*
|
|
13
14
|
* @param name - name by which the event will be accessed
|
|
15
|
+
* @param procFn - processing function before calling handlers
|
|
14
16
|
* @param scopes - event scopes
|
|
15
17
|
*/
|
|
16
|
-
export declare function publicRendererEvent<
|
|
18
|
+
export declare function publicRendererEvent<In = void, Out = In, ProcFn extends (...args: any[]) => any = (v?: In) => Out>(name: string, procFn?: ProcFn, scopes?: Scope[]): RendererEvent<Out>;
|
|
17
19
|
/**
|
|
18
20
|
* Makes the function available for **renderer** and **preload** processes.
|
|
19
21
|
*
|
|
20
22
|
* _only for function_
|
|
23
|
+
*
|
|
21
24
|
* @param name - name by which the function will be accessed
|
|
22
25
|
* @param func - linked function
|
|
23
26
|
* @param scopes - function scopes
|
|
24
27
|
*/
|
|
25
|
-
export declare function publicFunction(name: string, func:
|
|
28
|
+
export declare function publicFunction<F extends PublicFunction>(name: string, func: F, scopes?: Scope[]): void;
|
|
26
29
|
/**
|
|
27
30
|
* Makes the function available for **renderer** and **preload** processes.
|
|
28
31
|
*
|
|
29
32
|
* _only for variables_
|
|
33
|
+
*
|
|
30
34
|
* @param name - name by which the variable will be accessed
|
|
31
35
|
* @param value - variable getter/setter
|
|
32
36
|
* @param scopes - variable scopes
|
|
33
37
|
*/
|
|
34
|
-
export declare function publicVariable(name: string, value: PublicProperty
|
|
38
|
+
export declare function publicVariable<T>(name: string, value: PublicProperty<T>, scopes?: Scope[]): void;
|
|
35
39
|
export declare function addScope(key: string, scope: Scope): void;
|
|
36
40
|
export declare function addAccess(key: string, access: Access): void;
|
package/cjs/scripts/publish.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = exports.publicRendererEvent = exports.publicMainEvent = void 0;
|
|
4
|
-
const electron_1 = require("electron");
|
|
7
|
+
const electron_1 = __importDefault(require("electron"));
|
|
5
8
|
const enums_1 = require("../enums");
|
|
6
9
|
const publicInfo = {
|
|
7
10
|
properties: new Set(),
|
|
@@ -11,14 +14,24 @@ const publicInfo = {
|
|
|
11
14
|
accesses: new Map(),
|
|
12
15
|
scopes: new Map()
|
|
13
16
|
};
|
|
14
|
-
|
|
17
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
18
|
+
let mayThrowError = () => { };
|
|
19
|
+
if (typeof window !== 'undefined' || !electron_1.default.ipcMain) {
|
|
20
|
+
mayThrowError = () => { throw new Error('Publish methods is available only in main process.'); };
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
electron_1.default.ipcMain.on(enums_1.IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
|
|
24
|
+
}
|
|
15
25
|
/**
|
|
16
26
|
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
17
27
|
*
|
|
18
28
|
* @param name - name by which the event will be accessed
|
|
29
|
+
* @param procFn - function of processing before sending the event
|
|
19
30
|
* @param scopes - event scopes
|
|
20
31
|
*/
|
|
21
|
-
function publicMainEvent(name,
|
|
32
|
+
function publicMainEvent(name, procFn = ((...args) => args[0]), scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
|
|
33
|
+
mayThrowError();
|
|
34
|
+
const { ipcMain } = electron_1.default;
|
|
22
35
|
const { mainEvents } = publicInfo;
|
|
23
36
|
const mainEmitChannel = enums_1.IPCChannel.mainEventEmit + name;
|
|
24
37
|
const mainHandleChannel = enums_1.IPCChannel.mainEventOn + name;
|
|
@@ -32,10 +45,10 @@ function publicMainEvent(name, receiver = ((v = undefined) => v), scopes = [enum
|
|
|
32
45
|
publicInfo.scopes.set(rendererNameOn, new Set(scopes));
|
|
33
46
|
publicInfo.scopes.set(rendererNameOnce, new Set(scopes));
|
|
34
47
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
48
|
+
ipcMain.removeAllListeners(mainHandleChannel);
|
|
49
|
+
ipcMain.removeAllListeners(mainHandleChannelOnce);
|
|
50
|
+
ipcMain.on(mainHandleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
|
|
51
|
+
ipcMain.on(mainHandleChannelOnce, ({ sender }) => !sendersOnce.includes(sender) && sendersOnce.push(sender));
|
|
39
52
|
return (...args) => {
|
|
40
53
|
function filterSenders() {
|
|
41
54
|
senders = senders.filter(sender => !sender.isDestroyed());
|
|
@@ -44,7 +57,7 @@ function publicMainEvent(name, receiver = ((v = undefined) => v), scopes = [enum
|
|
|
44
57
|
filterSenders();
|
|
45
58
|
const allSenders = [...senders, ...sendersOnce];
|
|
46
59
|
try {
|
|
47
|
-
const result =
|
|
60
|
+
const result = procFn(...args);
|
|
48
61
|
if (result instanceof Promise) {
|
|
49
62
|
result
|
|
50
63
|
.then(value => {
|
|
@@ -74,9 +87,12 @@ exports.publicMainEvent = publicMainEvent;
|
|
|
74
87
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
75
88
|
*
|
|
76
89
|
* @param name - name by which the event will be accessed
|
|
90
|
+
* @param procFn - processing function before calling handlers
|
|
77
91
|
* @param scopes - event scopes
|
|
78
92
|
*/
|
|
79
|
-
function publicRendererEvent(name,
|
|
93
|
+
function publicRendererEvent(name, procFn = ((v) => v), scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
|
|
94
|
+
mayThrowError();
|
|
95
|
+
const { ipcMain } = electron_1.default;
|
|
80
96
|
if (name.startsWith('on')) {
|
|
81
97
|
name = name.replace('on', '');
|
|
82
98
|
name = `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
@@ -88,11 +104,11 @@ function publicRendererEvent(name, receiver = ((v = undefined) => v), scopes = [
|
|
|
88
104
|
rendererEvents.add(name);
|
|
89
105
|
if (!publicInfo.scopes.has(name))
|
|
90
106
|
publicInfo.scopes.set(name, new Set(scopes));
|
|
91
|
-
|
|
92
|
-
|
|
107
|
+
ipcMain.removeAllListeners(rendererEmitChannel);
|
|
108
|
+
ipcMain.on(rendererEmitChannel, (_, result) => {
|
|
93
109
|
if (result.error)
|
|
94
110
|
throw new Error(result.error);
|
|
95
|
-
const converted =
|
|
111
|
+
const converted = procFn(result.value);
|
|
96
112
|
handlers.forEach(handler => handler(converted));
|
|
97
113
|
handlersOnce.forEach(handler => handler(converted));
|
|
98
114
|
handlersOnce.clear();
|
|
@@ -113,18 +129,21 @@ exports.publicRendererEvent = publicRendererEvent;
|
|
|
113
129
|
* Makes the function available for **renderer** and **preload** processes.
|
|
114
130
|
*
|
|
115
131
|
* _only for function_
|
|
132
|
+
*
|
|
116
133
|
* @param name - name by which the function will be accessed
|
|
117
134
|
* @param func - linked function
|
|
118
135
|
* @param scopes - function scopes
|
|
119
136
|
*/
|
|
120
137
|
function publicFunction(name, func, scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
|
|
138
|
+
mayThrowError();
|
|
139
|
+
const { ipcMain } = electron_1.default;
|
|
121
140
|
const { functions } = publicInfo;
|
|
122
141
|
const channel = enums_1.IPCChannel.functionCall + name;
|
|
123
142
|
functions.add(name);
|
|
124
143
|
if (!publicInfo.scopes.has(name))
|
|
125
144
|
publicInfo.scopes.set(name, new Set(scopes));
|
|
126
|
-
|
|
127
|
-
|
|
145
|
+
ipcMain.removeAllListeners(channel);
|
|
146
|
+
ipcMain.on(channel, (e, ...args) => {
|
|
128
147
|
try {
|
|
129
148
|
const result = func(...args);
|
|
130
149
|
if (result instanceof Promise) {
|
|
@@ -148,11 +167,14 @@ exports.publicFunction = publicFunction;
|
|
|
148
167
|
* Makes the function available for **renderer** and **preload** processes.
|
|
149
168
|
*
|
|
150
169
|
* _only for variables_
|
|
170
|
+
*
|
|
151
171
|
* @param name - name by which the variable will be accessed
|
|
152
172
|
* @param value - variable getter/setter
|
|
153
173
|
* @param scopes - variable scopes
|
|
154
174
|
*/
|
|
155
175
|
function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.Scope.preload]) {
|
|
176
|
+
mayThrowError();
|
|
177
|
+
const { ipcMain } = electron_1.default;
|
|
156
178
|
const { properties } = publicInfo;
|
|
157
179
|
const getChannel = enums_1.IPCChannel.propertyGet + name;
|
|
158
180
|
const setChannel = enums_1.IPCChannel.propertySet + name;
|
|
@@ -172,8 +194,8 @@ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.S
|
|
|
172
194
|
setter && accesses.add(enums_1.Access.set);
|
|
173
195
|
}
|
|
174
196
|
if (getter) {
|
|
175
|
-
|
|
176
|
-
|
|
197
|
+
ipcMain.removeAllListeners(getChannel);
|
|
198
|
+
ipcMain.on(getChannel, event => {
|
|
177
199
|
try {
|
|
178
200
|
const result = getter();
|
|
179
201
|
event.returnValue = { value: result };
|
|
@@ -184,8 +206,8 @@ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.S
|
|
|
184
206
|
});
|
|
185
207
|
}
|
|
186
208
|
if (setter) {
|
|
187
|
-
|
|
188
|
-
|
|
209
|
+
ipcMain.removeAllListeners(setChannel);
|
|
210
|
+
ipcMain.on(setChannel, (event, ...args) => {
|
|
189
211
|
try {
|
|
190
212
|
setter(args[0]);
|
|
191
213
|
event.returnValue = { value: null };
|
package/cjs/types.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export interface IPublishMainEventArgs {
|
|
|
25
25
|
export interface IPublishRendererEventArgs {
|
|
26
26
|
name?: string;
|
|
27
27
|
scope?: Scope;
|
|
28
|
-
|
|
28
|
+
procFn?: EventReceiver;
|
|
29
29
|
}
|
|
30
30
|
export interface IPublishPropertyArgs {
|
|
31
31
|
name?: string;
|
|
@@ -44,12 +44,12 @@ export interface ICreateProviderArgs {
|
|
|
44
44
|
}
|
|
45
45
|
export type Scopes = Map<string, Set<Scope>>;
|
|
46
46
|
export type Accesses = Map<string, Set<Access>>;
|
|
47
|
-
export type RendererEvent<T =
|
|
48
|
-
export type MainEvent<T =
|
|
47
|
+
export type RendererEvent<T = void> = (handler: EventHandler<T>, isOnce?: boolean) => EventUnsubscriber;
|
|
48
|
+
export type MainEvent<T = void> = (handler: EventHandler<T>) => EventUnsubscriber;
|
|
49
49
|
export type EventEmitter = (...args: any[]) => any;
|
|
50
|
-
export type EventReceiver<I =
|
|
50
|
+
export type EventReceiver<I = void, O = I> = (input: I) => O;
|
|
51
51
|
export type EventUnsubscriber = () => void;
|
|
52
|
-
export type EventHandler<T> = (result: T) => void;
|
|
52
|
+
export type EventHandler<T = void> = (result: T) => void;
|
|
53
53
|
export interface IRendererPublic {
|
|
54
54
|
__register__?(instance: any): void;
|
|
55
55
|
}
|
|
@@ -67,9 +67,9 @@ export interface IProvider {
|
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
export type PublicFunction = (...args: any[]) => any;
|
|
70
|
-
export type PublicProperty = {
|
|
71
|
-
get?():
|
|
72
|
-
set?(value:
|
|
70
|
+
export type PublicProperty<T> = {
|
|
71
|
+
get?(): T;
|
|
72
|
+
set?(value: T): void;
|
|
73
73
|
};
|
|
74
74
|
export interface IInfo {
|
|
75
75
|
properties: Set<string>;
|
package/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
+
export * from './preload';
|
|
3
|
+
export * from './renderer';
|
|
2
4
|
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
5
|
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
|
4
6
|
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs, MainEvent, PublicFunction, PublicProperty, RendererEvent } from './types';
|
package/esm/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
+
export * from './preload';
|
|
3
|
+
export * from './renderer';
|
|
2
4
|
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
5
|
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
package/esm/main.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Access, Scope } from './enums';
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
|
+
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
|
4
|
+
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs, MainEvent, PublicFunction, PublicProperty, RendererEvent } from './types';
|
package/esm/main.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { Access, Scope } from './enums';
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
|
+
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
package/esm/scripts/Bridge.d.ts
CHANGED
package/esm/scripts/Bridge.js
CHANGED
|
@@ -1,36 +1,53 @@
|
|
|
1
1
|
import { Scope } from '../enums';
|
|
2
2
|
import createProvider from './provider';
|
|
3
|
-
if (!window.__provider__)
|
|
4
|
-
throw new Error('Required methods not provided. Did you forget to call "provide" in preload?');
|
|
5
|
-
const provider = window.__provider__;
|
|
6
|
-
const info = provider.getInfo();
|
|
7
3
|
/**
|
|
8
4
|
* Bridge between `renderer` and `main` processes.
|
|
9
5
|
*
|
|
10
6
|
* _only for renderer process_
|
|
11
7
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
},
|
|
17
|
-
emitRendererEvent(name, arg) {
|
|
18
|
-
provider.provided.rendererEvents[name](arg);
|
|
19
|
-
},
|
|
20
|
-
handleMainEvent(name, type, handler) {
|
|
21
|
-
return provider.provided.mainEvents[name](type, handler);
|
|
22
|
-
},
|
|
23
|
-
getVariable(name) {
|
|
24
|
-
return provider.provided.properties[name].get();
|
|
25
|
-
},
|
|
26
|
-
setVariable(name, value) {
|
|
27
|
-
provider.provided.properties[name].set(value);
|
|
28
|
-
return undefined;
|
|
29
|
-
},
|
|
30
|
-
waitPromise(channel) {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
provider.waitPromise(channel, resolve, reject);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
8
|
+
let Bridge = new Proxy({ as() { throwError(); } }, {
|
|
9
|
+
get() { throwError(); },
|
|
10
|
+
set() { throwError(); },
|
|
11
|
+
apply() { throwError(); }
|
|
35
12
|
});
|
|
13
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
14
|
+
let throwError;
|
|
15
|
+
if (typeof window === 'undefined') {
|
|
16
|
+
throwError = () => { throw new Error('"Bridge" is unavailable in main process. In preload use "Main" instead.'); };
|
|
17
|
+
}
|
|
18
|
+
else if (!window.__provider__) {
|
|
19
|
+
throwError = () => { throw new Error('Required methods not provided. Call "provideFromMain" in preload process.'); };
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
initInWeb();
|
|
23
|
+
}
|
|
24
|
+
/** Выполняется только в web */
|
|
25
|
+
function initInWeb() {
|
|
26
|
+
const provider = window.__provider__;
|
|
27
|
+
const info = provider.getInfo();
|
|
28
|
+
Bridge = createProvider({
|
|
29
|
+
info, scope: Scope.renderer,
|
|
30
|
+
callFunction(name, ...args) {
|
|
31
|
+
return provider.provided.functions[name](...args);
|
|
32
|
+
},
|
|
33
|
+
emitRendererEvent(name, arg) {
|
|
34
|
+
provider.provided.rendererEvents[name](arg);
|
|
35
|
+
},
|
|
36
|
+
handleMainEvent(name, type, handler) {
|
|
37
|
+
return provider.provided.mainEvents[name](type, handler);
|
|
38
|
+
},
|
|
39
|
+
getVariable(name) {
|
|
40
|
+
return provider.provided.properties[name].get();
|
|
41
|
+
},
|
|
42
|
+
setVariable(name, value) {
|
|
43
|
+
provider.provided.properties[name].set(value);
|
|
44
|
+
return undefined;
|
|
45
|
+
},
|
|
46
|
+
waitPromise(channel) {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
|
+
provider.waitPromise(channel, resolve, reject);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
36
53
|
export default Bridge;
|