emr-bridge 1.3.0 → 1.4.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/README.md +133 -110
- package/cjs/enums.d.ts +5 -4
- package/cjs/enums.js +4 -3
- package/cjs/exp.d.ts +3 -3
- package/cjs/exp.js +7 -4
- package/cjs/index.d.ts +3 -3
- package/cjs/index.js +5 -3
- package/cjs/scripts/Bridge.js +8 -5
- package/cjs/scripts/Main.js +17 -4
- package/cjs/scripts/newDecorators.d.ts +46 -4
- package/cjs/scripts/newDecorators.js +33 -8
- package/cjs/scripts/oldDecorators.d.ts +115 -7
- package/cjs/scripts/oldDecorators.js +40 -7
- package/cjs/scripts/preloadProvider.js +35 -28
- package/cjs/scripts/provider.js +22 -9
- package/cjs/scripts/publish.d.ts +10 -5
- package/cjs/scripts/publish.js +59 -23
- package/cjs/types.d.ts +18 -5
- package/esm/enums.d.ts +5 -4
- package/esm/enums.js +4 -3
- package/esm/exp.d.ts +3 -3
- package/esm/exp.js +2 -2
- package/esm/index.d.ts +3 -3
- package/esm/index.js +2 -2
- package/esm/scripts/Bridge.js +8 -5
- package/esm/scripts/Main.js +17 -4
- package/esm/scripts/newDecorators.d.ts +46 -4
- package/esm/scripts/newDecorators.js +31 -7
- package/esm/scripts/oldDecorators.d.ts +115 -7
- package/esm/scripts/oldDecorators.js +36 -5
- package/esm/scripts/preloadProvider.js +35 -28
- package/esm/scripts/provider.js +22 -9
- package/esm/scripts/publish.d.ts +10 -5
- package/esm/scripts/publish.js +56 -21
- package/esm/types.d.ts +18 -5
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.publicSetter = exports.publicGetter = exports.publicProperty = exports.
|
|
3
|
+
exports.publicSetter = exports.publicGetter = exports.publicProperty = exports.publicClassRendererEvent = exports.publicClassMainEvent = exports.publicMethod = void 0;
|
|
4
4
|
const publish_1 = require("./publish");
|
|
5
5
|
function publicMethod(arg) {
|
|
6
6
|
return (method, context) => {
|
|
@@ -21,7 +21,7 @@ function publicMethod(arg) {
|
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
exports.publicMethod = publicMethod;
|
|
24
|
-
function
|
|
24
|
+
function publicClassMainEvent(arg) {
|
|
25
25
|
return (method, context) => {
|
|
26
26
|
let name = String(context.name);
|
|
27
27
|
if (arg) {
|
|
@@ -36,15 +36,40 @@ function publicClassEvent(arg) {
|
|
|
36
36
|
}
|
|
37
37
|
context.addInitializer(function () {
|
|
38
38
|
// @ts-ignore
|
|
39
|
-
this[name] = (0, publish_1.
|
|
39
|
+
this[context.name] = (0, publish_1.publicMainEvent)(name, method.bind(this));
|
|
40
40
|
});
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
-
exports.
|
|
43
|
+
exports.publicClassMainEvent = publicClassMainEvent;
|
|
44
|
+
function publicClassRendererEvent(arg) {
|
|
45
|
+
return (_, context) => {
|
|
46
|
+
let name = String(context.name);
|
|
47
|
+
let receiver = (v) => v;
|
|
48
|
+
if (arg) {
|
|
49
|
+
if (typeof arg === 'string') {
|
|
50
|
+
name = arg;
|
|
51
|
+
}
|
|
52
|
+
else if (typeof arg === 'object') {
|
|
53
|
+
name = arg.name ?? String(context.name);
|
|
54
|
+
receiver = arg.receiver ?? receiver;
|
|
55
|
+
if (arg.scope)
|
|
56
|
+
(0, publish_1.addScope)(name, arg.scope);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
receiver = arg;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
context.addInitializer(function () {
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
this[context.name] = (0, publish_1.publicRendererEvent)(name, receiver);
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
exports.publicClassRendererEvent = publicClassRendererEvent;
|
|
44
69
|
function publicProperty(arg) {
|
|
45
70
|
return (_, context) => {
|
|
46
|
-
const
|
|
47
|
-
let name =
|
|
71
|
+
const key = String(context.name);
|
|
72
|
+
let name = key;
|
|
48
73
|
if (arg) {
|
|
49
74
|
if (typeof arg === 'string') {
|
|
50
75
|
name = arg;
|
|
@@ -61,11 +86,11 @@ function publicProperty(arg) {
|
|
|
61
86
|
(0, publish_1.publicVariable)(name, {
|
|
62
87
|
get: () => {
|
|
63
88
|
// @ts-ignore
|
|
64
|
-
return this[
|
|
89
|
+
return this[key];
|
|
65
90
|
},
|
|
66
91
|
set: (value) => {
|
|
67
92
|
// @ts-ignore
|
|
68
|
-
this[
|
|
93
|
+
this[key] = value;
|
|
69
94
|
}
|
|
70
95
|
});
|
|
71
96
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { EventReceiver, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Makes the method available for **renderer** and **preload** processes.
|
|
4
4
|
*
|
|
@@ -64,7 +64,7 @@ export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
|
64
64
|
*
|
|
65
65
|
* _experimental decorators_
|
|
66
66
|
*/
|
|
67
|
-
export declare function
|
|
67
|
+
export declare function publicStaticMainEvent(): MethodDecorator;
|
|
68
68
|
/**
|
|
69
69
|
* Makes the event available for **renderer** and **preload** processes.
|
|
70
70
|
*
|
|
@@ -73,7 +73,7 @@ export declare function publicStaticEvent(): MethodDecorator;
|
|
|
73
73
|
* _experimental decorators_
|
|
74
74
|
* @param name - name by which the event will be accessed
|
|
75
75
|
*/
|
|
76
|
-
export declare function
|
|
76
|
+
export declare function publicStaticMainEvent(name: string): MethodDecorator;
|
|
77
77
|
/**
|
|
78
78
|
* Makes the event available for **renderer** and **preload** processes.
|
|
79
79
|
*
|
|
@@ -82,7 +82,7 @@ export declare function publicStaticEvent(name: string): MethodDecorator;
|
|
|
82
82
|
* _experimental decorators_
|
|
83
83
|
* @param args - publish args
|
|
84
84
|
*/
|
|
85
|
-
export declare function
|
|
85
|
+
export declare function publicStaticMainEvent(args: IPublishMainEventArgs): MethodDecorator;
|
|
86
86
|
/**
|
|
87
87
|
* Makes the event available for **renderer** and **preload** processes.
|
|
88
88
|
*
|
|
@@ -92,7 +92,7 @@ export declare function publicStaticEvent(args: IPublishEventArgs): MethodDecora
|
|
|
92
92
|
*
|
|
93
93
|
* _required `providePublic(classInstance)`_
|
|
94
94
|
*/
|
|
95
|
-
export declare function
|
|
95
|
+
export declare function publicClassMainEvent(): MethodDecorator;
|
|
96
96
|
/**
|
|
97
97
|
* Makes the event available for **renderer** and **preload** processes.
|
|
98
98
|
*
|
|
@@ -103,7 +103,7 @@ export declare function publicClassEvent(): MethodDecorator;
|
|
|
103
103
|
* _required `providePublic(classInstance)`_
|
|
104
104
|
* @param name - name by which the event will be accessed
|
|
105
105
|
*/
|
|
106
|
-
export declare function
|
|
106
|
+
export declare function publicClassMainEvent(name: string): MethodDecorator;
|
|
107
107
|
/**
|
|
108
108
|
* Makes the event available for **renderer** and **preload** processes.
|
|
109
109
|
*
|
|
@@ -114,7 +114,115 @@ export declare function publicClassEvent(name: string): MethodDecorator;
|
|
|
114
114
|
* _required `providePublic(classInstance)`_
|
|
115
115
|
* @param args - publish args
|
|
116
116
|
*/
|
|
117
|
-
export declare function
|
|
117
|
+
export declare function publicClassMainEvent(args: IPublishMainEventArgs): MethodDecorator;
|
|
118
|
+
/**
|
|
119
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
120
|
+
*
|
|
121
|
+
* _only for static_
|
|
122
|
+
*
|
|
123
|
+
* _experimental decorators_
|
|
124
|
+
*/
|
|
125
|
+
export declare function publicStaticRendererEvent(): PropertyDecorator;
|
|
126
|
+
/**
|
|
127
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
128
|
+
*
|
|
129
|
+
* _only for static_
|
|
130
|
+
*
|
|
131
|
+
* _experimental decorators_
|
|
132
|
+
* @param name - name by which the event will be accessed
|
|
133
|
+
*/
|
|
134
|
+
export declare function publicStaticRendererEvent(name: string): PropertyDecorator;
|
|
135
|
+
/**
|
|
136
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
137
|
+
*
|
|
138
|
+
* _only for static_
|
|
139
|
+
*
|
|
140
|
+
* _experimental decorators_
|
|
141
|
+
* @param receiver - linked receiver
|
|
142
|
+
*/
|
|
143
|
+
export declare function publicStaticRendererEvent(receiver: EventReceiver): PropertyDecorator;
|
|
144
|
+
/**
|
|
145
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
146
|
+
*
|
|
147
|
+
* _only for static_
|
|
148
|
+
*
|
|
149
|
+
* _experimental decorators_
|
|
150
|
+
* @param name - name by which the event will be accessed
|
|
151
|
+
* @param receiver - linked receiver
|
|
152
|
+
*/
|
|
153
|
+
export declare function publicStaticRendererEvent(name: string, receiver: EventReceiver): PropertyDecorator;
|
|
154
|
+
/**
|
|
155
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
156
|
+
*
|
|
157
|
+
* _only for static_
|
|
158
|
+
*
|
|
159
|
+
* _experimental decorators_
|
|
160
|
+
* @param args - publish args
|
|
161
|
+
*/
|
|
162
|
+
export declare function publicStaticRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
|
|
163
|
+
/**
|
|
164
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
165
|
+
*
|
|
166
|
+
* _only for static_
|
|
167
|
+
*
|
|
168
|
+
* _experimental decorators_
|
|
169
|
+
* @param args - publish args
|
|
170
|
+
*/
|
|
171
|
+
export declare function publicStaticRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
|
|
172
|
+
/**
|
|
173
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
174
|
+
*
|
|
175
|
+
* _only for non-static_
|
|
176
|
+
*
|
|
177
|
+
* _experimental decorators_
|
|
178
|
+
*/
|
|
179
|
+
export declare function publicClassRendererEvent(): PropertyDecorator;
|
|
180
|
+
/**
|
|
181
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
182
|
+
*
|
|
183
|
+
* _only for non-static_
|
|
184
|
+
*
|
|
185
|
+
* _experimental decorators_
|
|
186
|
+
* @param name - name by which the event will be accessed
|
|
187
|
+
*/
|
|
188
|
+
export declare function publicClassRendererEvent(name: string): PropertyDecorator;
|
|
189
|
+
/**
|
|
190
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
191
|
+
*
|
|
192
|
+
* _only for non-static_
|
|
193
|
+
*
|
|
194
|
+
* _experimental decorators_
|
|
195
|
+
* @param receiver - linked receiver
|
|
196
|
+
*/
|
|
197
|
+
export declare function publicClassRendererEvent(receiver: EventReceiver): PropertyDecorator;
|
|
198
|
+
/**
|
|
199
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
200
|
+
*
|
|
201
|
+
* _only for non-static_
|
|
202
|
+
*
|
|
203
|
+
* _experimental decorators_
|
|
204
|
+
* @param name - name by which the event will be accessed
|
|
205
|
+
* @param receiver - linked receiver
|
|
206
|
+
*/
|
|
207
|
+
export declare function publicClassRendererEvent(name: string, receiver: EventReceiver): PropertyDecorator;
|
|
208
|
+
/**
|
|
209
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
210
|
+
*
|
|
211
|
+
* _only for non-static_
|
|
212
|
+
*
|
|
213
|
+
* _experimental decorators_
|
|
214
|
+
* @param args - publish args
|
|
215
|
+
*/
|
|
216
|
+
export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
|
|
217
|
+
/**
|
|
218
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
219
|
+
*
|
|
220
|
+
* _only for non-static_
|
|
221
|
+
*
|
|
222
|
+
* _experimental decorators_
|
|
223
|
+
* @param args - publish args
|
|
224
|
+
*/
|
|
225
|
+
export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
|
|
118
226
|
/**
|
|
119
227
|
* Makes the property available for **renderer** and **preload** processes.
|
|
120
228
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.providePublic = exports.publicProperty = exports.publicStaticProperty = exports.
|
|
3
|
+
exports.providePublic = exports.publicProperty = exports.publicStaticProperty = exports.publicClassRendererEvent = exports.publicStaticRendererEvent = exports.publicClassMainEvent = exports.publicStaticMainEvent = exports.publicMethod = exports.publicStaticMethod = void 0;
|
|
4
4
|
const publish_1 = require("./publish");
|
|
5
5
|
function publicStaticMethod(arg) {
|
|
6
6
|
return (target, key) => {
|
|
@@ -30,7 +30,7 @@ function publicMethod(arg) {
|
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
exports.publicMethod = publicMethod;
|
|
33
|
-
function
|
|
33
|
+
function publicStaticMainEvent(arg) {
|
|
34
34
|
return (target, key) => {
|
|
35
35
|
let name = String(key);
|
|
36
36
|
if (arg) {
|
|
@@ -43,21 +43,54 @@ function publicStaticEvent(arg) {
|
|
|
43
43
|
(0, publish_1.addScope)(name, arg.scope);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
target[key] = (0, publish_1.
|
|
46
|
+
target[key] = (0, publish_1.publicMainEvent)(name, target[key].bind(target));
|
|
47
47
|
};
|
|
48
48
|
}
|
|
49
|
-
exports.
|
|
50
|
-
function
|
|
49
|
+
exports.publicStaticMainEvent = publicStaticMainEvent;
|
|
50
|
+
function publicClassMainEvent(arg) {
|
|
51
51
|
return (tgt, key, _) => {
|
|
52
52
|
const target = tgt;
|
|
53
53
|
const prevRegister = target.__register__ ?? (() => { });
|
|
54
54
|
target.__register__ = instance => {
|
|
55
55
|
prevRegister(instance);
|
|
56
|
-
|
|
56
|
+
publicStaticMainEvent(arg)(instance, key, _);
|
|
57
57
|
};
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
-
exports.
|
|
60
|
+
exports.publicClassMainEvent = publicClassMainEvent;
|
|
61
|
+
function publicStaticRendererEvent(arg) {
|
|
62
|
+
return (target, key) => {
|
|
63
|
+
let name = String(key);
|
|
64
|
+
let receiver = (v) => v;
|
|
65
|
+
if (arg) {
|
|
66
|
+
if (typeof arg === 'string') {
|
|
67
|
+
name = arg;
|
|
68
|
+
}
|
|
69
|
+
else if (typeof arg === 'object') {
|
|
70
|
+
name = arg.name ?? String(key);
|
|
71
|
+
receiver = arg.receiver ?? receiver;
|
|
72
|
+
if (arg.scope)
|
|
73
|
+
(0, publish_1.addScope)(name, arg.scope);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
receiver = arg;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
target[key] = (0, publish_1.publicRendererEvent)(name, receiver);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
exports.publicStaticRendererEvent = publicStaticRendererEvent;
|
|
83
|
+
function publicClassRendererEvent(arg) {
|
|
84
|
+
return (tgt, key) => {
|
|
85
|
+
const target = tgt;
|
|
86
|
+
const prevRegister = target.__register__ ?? (() => { });
|
|
87
|
+
target.__register__ = instance => {
|
|
88
|
+
prevRegister(instance);
|
|
89
|
+
publicStaticRendererEvent(arg)(instance, key);
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
exports.publicClassRendererEvent = publicClassRendererEvent;
|
|
61
94
|
function publicStaticProperty(arg) {
|
|
62
95
|
return (target, key) => {
|
|
63
96
|
let name = String(key);
|
|
@@ -24,45 +24,52 @@ function provideFromMain(contextIsolation = true) {
|
|
|
24
24
|
provided: {
|
|
25
25
|
properties: {},
|
|
26
26
|
functions: {},
|
|
27
|
-
|
|
27
|
+
mainEvents: {},
|
|
28
|
+
rendererEvents: {}
|
|
28
29
|
}
|
|
29
30
|
};
|
|
30
31
|
info.functions.forEach(funcName => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
enumerable: true
|
|
36
|
-
});
|
|
32
|
+
provider.provided.functions[funcName] = (...args) => {
|
|
33
|
+
return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.functionCall + funcName, ...args);
|
|
34
|
+
};
|
|
37
35
|
});
|
|
38
|
-
info.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
electron_1.ipcRenderer.send(
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
36
|
+
info.rendererEvents.forEach(eventName => {
|
|
37
|
+
provider.provided.rendererEvents[eventName] = (arg) => {
|
|
38
|
+
const emitChannel = enums_1.IPCChannel.rendererEventEmit + eventName;
|
|
39
|
+
if (arg instanceof Promise) {
|
|
40
|
+
arg
|
|
41
|
+
.then(value => electron_1.ipcRenderer.send(emitChannel, { value }))
|
|
42
|
+
.catch(reason => electron_1.ipcRenderer.send(emitChannel, { error: String(reason) }));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
electron_1.ipcRenderer.send(emitChannel, { value: arg });
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
info.mainEvents.forEach(eventName => {
|
|
50
|
+
provider.provided.mainEvents[eventName] = (type, handler) => {
|
|
51
|
+
const listener = (_, result) => handler(result);
|
|
52
|
+
const emitChannel = enums_1.IPCChannel.mainEventEmit + eventName;
|
|
53
|
+
if (type === 'on') {
|
|
54
|
+
electron_1.ipcRenderer.on(emitChannel, listener);
|
|
55
|
+
electron_1.ipcRenderer.send(enums_1.IPCChannel.mainEventOn + eventName);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
electron_1.ipcRenderer.once(emitChannel, listener);
|
|
59
|
+
electron_1.ipcRenderer.send(enums_1.IPCChannel.mainEventOnce + eventName);
|
|
60
|
+
}
|
|
61
|
+
return () => electron_1.ipcRenderer.removeListener(emitChannel, listener);
|
|
62
|
+
};
|
|
55
63
|
});
|
|
56
64
|
info.properties.forEach(propName => {
|
|
57
|
-
|
|
65
|
+
provider.provided.properties[propName] = {
|
|
58
66
|
get() {
|
|
59
67
|
return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertyGet + propName);
|
|
60
68
|
},
|
|
61
69
|
set(value) {
|
|
62
70
|
electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertySet + propName, value);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
66
73
|
});
|
|
67
74
|
if (contextIsolation)
|
|
68
75
|
electron_1.contextBridge.exposeInMainWorld('__provider__', provider);
|
package/cjs/scripts/provider.js
CHANGED
|
@@ -5,7 +5,7 @@ function errorHandler(error, channel) {
|
|
|
5
5
|
console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
6
6
|
}
|
|
7
7
|
function createProvider(args) {
|
|
8
|
-
const { info, scope, getVariable, setVariable, callFunction,
|
|
8
|
+
const { info, scope, getVariable, setVariable, callFunction, handleMainEvent, waitPromise, emitRendererEvent } = args;
|
|
9
9
|
const target = {
|
|
10
10
|
as: () => proxy
|
|
11
11
|
};
|
|
@@ -31,17 +31,30 @@ function createProvider(args) {
|
|
|
31
31
|
enumerable: true
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
|
-
info.
|
|
35
|
-
if (!info.scopes[
|
|
34
|
+
info.rendererEvents.forEach(rendererEventName => {
|
|
35
|
+
if (!info.scopes[rendererEventName].has(scope))
|
|
36
36
|
return;
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
Object.defineProperty(target, rendererEventName, {
|
|
38
|
+
get() {
|
|
39
|
+
return (arg) => emitRendererEvent(rendererEventName, arg);
|
|
40
|
+
},
|
|
41
|
+
set() {
|
|
42
|
+
throw new Error(`Function '${rendererEventName}' is readonly`);
|
|
43
|
+
},
|
|
44
|
+
enumerable: true
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
info.mainEvents.forEach(mainEventName => {
|
|
48
|
+
if (!info.scopes[mainEventName].has(scope))
|
|
49
|
+
return;
|
|
50
|
+
const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
51
|
+
const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
39
52
|
Object.defineProperty(target, eventNameOn, {
|
|
40
53
|
get() {
|
|
41
54
|
return (handler) => {
|
|
42
|
-
return
|
|
55
|
+
return handleMainEvent(mainEventName, 'on', (result) => {
|
|
43
56
|
if (result.error) {
|
|
44
|
-
errorHandler(result.error,
|
|
57
|
+
errorHandler(result.error, mainEventName);
|
|
45
58
|
return;
|
|
46
59
|
}
|
|
47
60
|
if (result.promiseChannel)
|
|
@@ -58,9 +71,9 @@ function createProvider(args) {
|
|
|
58
71
|
Object.defineProperty(target, eventNameOnce, {
|
|
59
72
|
get() {
|
|
60
73
|
return (handler) => {
|
|
61
|
-
return
|
|
74
|
+
return handleMainEvent(mainEventName, 'once', (result) => {
|
|
62
75
|
if (result.error) {
|
|
63
|
-
errorHandler(result.error,
|
|
76
|
+
errorHandler(result.error, mainEventName);
|
|
64
77
|
return;
|
|
65
78
|
}
|
|
66
79
|
if (result.promiseChannel)
|
package/cjs/scripts/publish.d.ts
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { Access, Scope } from '../enums';
|
|
2
|
-
import type { EventEmitter, PublicFunction, PublicProperty } from '../types';
|
|
2
|
+
import type { EventEmitter, PublicFunction, PublicProperty, RendererEvent } from '../types';
|
|
3
3
|
/**
|
|
4
|
-
* Makes the event available for **renderer** and **preload** processes.
|
|
4
|
+
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
5
5
|
*
|
|
6
|
-
* _only for event_
|
|
7
6
|
* @param name - name by which the event will be accessed
|
|
8
|
-
* @param func - linked emitter
|
|
9
7
|
* @param scopes - event scopes
|
|
10
8
|
*/
|
|
11
|
-
export declare function
|
|
9
|
+
export declare function publicMainEvent<Emitter extends EventEmitter>(name: string, receiver?: Emitter, scopes?: Scope[]): (...args: Parameters<Emitter>) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
12
|
+
*
|
|
13
|
+
* @param name - name by which the event will be accessed
|
|
14
|
+
* @param scopes - event scopes
|
|
15
|
+
*/
|
|
16
|
+
export declare function publicRendererEvent<I = undefined, O = I>(name: string, receiver?: (v?: I) => O, scopes?: Scope[]): RendererEvent<O>;
|
|
12
17
|
/**
|
|
13
18
|
* Makes the function available for **renderer** and **preload** processes.
|
|
14
19
|
*
|
package/cjs/scripts/publish.js
CHANGED
|
@@ -1,38 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = exports.
|
|
3
|
+
exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = exports.publicRendererEvent = exports.publicMainEvent = void 0;
|
|
4
4
|
const electron_1 = require("electron");
|
|
5
5
|
const enums_1 = require("../enums");
|
|
6
6
|
const publicInfo = {
|
|
7
7
|
properties: new Set(),
|
|
8
8
|
functions: new Set(),
|
|
9
|
-
|
|
9
|
+
mainEvents: new Set(),
|
|
10
|
+
rendererEvents: new Set(),
|
|
10
11
|
accesses: {},
|
|
11
12
|
scopes: {}
|
|
12
13
|
};
|
|
13
14
|
electron_1.ipcMain.on(enums_1.IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
|
|
14
15
|
/**
|
|
15
|
-
* Makes the event available for **renderer** and **preload** processes.
|
|
16
|
+
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
16
17
|
*
|
|
17
|
-
* _only for event_
|
|
18
18
|
* @param name - name by which the event will be accessed
|
|
19
|
-
* @param func - linked emitter
|
|
20
19
|
* @param scopes - event scopes
|
|
21
20
|
*/
|
|
22
|
-
function
|
|
23
|
-
const {
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
21
|
+
function publicMainEvent(name, receiver = ((v = undefined) => v), scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
|
|
22
|
+
const { mainEvents } = publicInfo;
|
|
23
|
+
const mainEmitChannel = enums_1.IPCChannel.mainEventEmit + name;
|
|
24
|
+
const mainHandleChannel = enums_1.IPCChannel.mainEventOn + name;
|
|
25
|
+
const mainHandleChannelOnce = enums_1.IPCChannel.mainEventOnce + name;
|
|
27
26
|
let senders = [];
|
|
28
27
|
let sendersOnce = [];
|
|
29
|
-
|
|
28
|
+
mainEvents.add(name);
|
|
30
29
|
if (!publicInfo.scopes[name])
|
|
31
30
|
publicInfo.scopes[name] = new Set(scopes);
|
|
32
|
-
electron_1.ipcMain.removeAllListeners(
|
|
33
|
-
electron_1.ipcMain.removeAllListeners(
|
|
34
|
-
electron_1.ipcMain.on(
|
|
35
|
-
electron_1.ipcMain.on(
|
|
31
|
+
electron_1.ipcMain.removeAllListeners(mainHandleChannel);
|
|
32
|
+
electron_1.ipcMain.removeAllListeners(mainHandleChannelOnce);
|
|
33
|
+
electron_1.ipcMain.on(mainHandleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
|
|
34
|
+
electron_1.ipcMain.on(mainHandleChannelOnce, ({ sender }) => !sendersOnce.includes(sender) && sendersOnce.push(sender));
|
|
36
35
|
return (...args) => {
|
|
37
36
|
function filterSenders() {
|
|
38
37
|
senders = senders.filter(sender => !sender.isDestroyed());
|
|
@@ -41,34 +40,71 @@ function publicEvent(name, func = (() => { }), scopes = [enums_1.Scope.preload,
|
|
|
41
40
|
filterSenders();
|
|
42
41
|
const allSenders = [...senders, ...sendersOnce];
|
|
43
42
|
try {
|
|
44
|
-
const result =
|
|
43
|
+
const result = receiver(...args);
|
|
45
44
|
if (result instanceof Promise) {
|
|
46
|
-
const promiseChannel = emitChannel + enums_1.IPCChannel.promisePostfix;
|
|
47
45
|
result
|
|
48
46
|
.then(value => {
|
|
49
47
|
filterSenders();
|
|
50
|
-
[...senders, ...sendersOnce].forEach(sender => sender.send(
|
|
48
|
+
[...senders, ...sendersOnce].forEach(sender => sender.send(mainEmitChannel, { value }));
|
|
51
49
|
sendersOnce = [];
|
|
52
50
|
})
|
|
53
51
|
.catch(reason => {
|
|
54
52
|
filterSenders();
|
|
55
|
-
[...senders, ...sendersOnce].forEach(sender => sender.send(
|
|
53
|
+
[...senders, ...sendersOnce].forEach(sender => sender.send(mainEmitChannel, { error: String(reason) }));
|
|
56
54
|
sendersOnce = [];
|
|
57
55
|
});
|
|
58
|
-
allSenders.forEach(sender => sender.send(promiseChannel, { promiseChannel }));
|
|
59
56
|
}
|
|
60
57
|
else {
|
|
61
|
-
allSenders.forEach(sender => sender.send(
|
|
58
|
+
allSenders.forEach(sender => sender.send(mainEmitChannel, { value: result }));
|
|
62
59
|
sendersOnce = [];
|
|
63
60
|
}
|
|
64
61
|
}
|
|
65
62
|
catch (error) {
|
|
66
|
-
allSenders.forEach(sender => sender.send(
|
|
63
|
+
allSenders.forEach(sender => sender.send(mainEmitChannel, { error: String(error) }));
|
|
67
64
|
sendersOnce = [];
|
|
68
65
|
}
|
|
69
66
|
};
|
|
70
67
|
}
|
|
71
|
-
exports.
|
|
68
|
+
exports.publicMainEvent = publicMainEvent;
|
|
69
|
+
/**
|
|
70
|
+
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
71
|
+
*
|
|
72
|
+
* @param name - name by which the event will be accessed
|
|
73
|
+
* @param scopes - event scopes
|
|
74
|
+
*/
|
|
75
|
+
function publicRendererEvent(name, receiver = ((v = undefined) => v), scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
|
|
76
|
+
if (name.startsWith('on')) {
|
|
77
|
+
name = name.replace('on', '');
|
|
78
|
+
name = `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
79
|
+
}
|
|
80
|
+
const { rendererEvents } = publicInfo;
|
|
81
|
+
const rendererEmitChannel = enums_1.IPCChannel.rendererEventEmit + name;
|
|
82
|
+
const handlers = new Set();
|
|
83
|
+
const handlersOnce = new Set();
|
|
84
|
+
rendererEvents.add(name);
|
|
85
|
+
if (!publicInfo.scopes[name])
|
|
86
|
+
publicInfo.scopes[name] = new Set(scopes);
|
|
87
|
+
electron_1.ipcMain.removeAllListeners(rendererEmitChannel);
|
|
88
|
+
electron_1.ipcMain.on(rendererEmitChannel, (_, result) => {
|
|
89
|
+
if (result.error)
|
|
90
|
+
throw new Error(result.error);
|
|
91
|
+
const converted = receiver(result.value);
|
|
92
|
+
handlers.forEach(handler => handler(converted));
|
|
93
|
+
handlersOnce.forEach(handler => handler(converted));
|
|
94
|
+
handlersOnce.clear();
|
|
95
|
+
});
|
|
96
|
+
return (handler, isOnce = false) => {
|
|
97
|
+
if (isOnce) {
|
|
98
|
+
handlersOnce.add(handler);
|
|
99
|
+
return () => handlersOnce.delete(handler);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
handlers.add(handler);
|
|
103
|
+
return () => handlers.delete(handler);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
exports.publicRendererEvent = publicRendererEvent;
|
|
72
108
|
/**
|
|
73
109
|
* Makes the function available for **renderer** and **preload** processes.
|
|
74
110
|
*
|