emr-bridge 1.3.0 → 1.4.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 +131 -108
- 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 +5 -2
- 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 +23 -6
- 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 +14 -4
- 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 +5 -2
- 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 +23 -6
- 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 +14 -4
- package/package.json +1 -1
|
@@ -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,7 +24,8 @@ 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 => {
|
|
@@ -35,18 +36,34 @@ function provideFromMain(contextIsolation = true) {
|
|
|
35
36
|
enumerable: true
|
|
36
37
|
});
|
|
37
38
|
});
|
|
38
|
-
info.
|
|
39
|
-
Object.defineProperty(provider.provided.
|
|
39
|
+
info.rendererEvents.forEach(rendererEventName => {
|
|
40
|
+
Object.defineProperty(provider.provided.rendererEvents, rendererEventName, {
|
|
41
|
+
value(arg) {
|
|
42
|
+
const emitChannel = enums_1.IPCChannel.rendererEventEmit + rendererEventName;
|
|
43
|
+
if (arg instanceof Promise) {
|
|
44
|
+
arg
|
|
45
|
+
.then(value => electron_1.ipcRenderer.send(emitChannel, { value }))
|
|
46
|
+
.catch(reason => electron_1.ipcRenderer.send(emitChannel, { error: String(reason) }));
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
electron_1.ipcRenderer.send(emitChannel, { value: arg });
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
enumerable: true
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
info.mainEvents.forEach(mainEventName => {
|
|
56
|
+
Object.defineProperty(provider.provided.mainEvents, mainEventName, {
|
|
40
57
|
value(type, handler) {
|
|
41
58
|
const listener = (_, result) => handler(result);
|
|
42
|
-
const emitChannel = enums_1.IPCChannel.
|
|
59
|
+
const emitChannel = enums_1.IPCChannel.mainEventEmit + mainEventName;
|
|
43
60
|
if (type === 'on') {
|
|
44
|
-
electron_1.ipcRenderer.send(enums_1.IPCChannel.eventHandleOn + eventName);
|
|
45
61
|
electron_1.ipcRenderer.on(emitChannel, listener);
|
|
62
|
+
electron_1.ipcRenderer.send(enums_1.IPCChannel.mainEventOn + mainEventName);
|
|
46
63
|
}
|
|
47
64
|
else {
|
|
48
|
-
electron_1.ipcRenderer.send(enums_1.IPCChannel.eventHandleOnce + eventName);
|
|
49
65
|
electron_1.ipcRenderer.once(emitChannel, listener);
|
|
66
|
+
electron_1.ipcRenderer.send(enums_1.IPCChannel.mainEventOnce + mainEventName);
|
|
50
67
|
}
|
|
51
68
|
return () => electron_1.ipcRenderer.removeListener(emitChannel, listener);
|
|
52
69
|
},
|
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
|
*
|
package/cjs/types.d.ts
CHANGED
|
@@ -18,10 +18,15 @@ export interface IPublishMethodArgs {
|
|
|
18
18
|
name?: string;
|
|
19
19
|
scope?: Scope;
|
|
20
20
|
}
|
|
21
|
-
export interface
|
|
21
|
+
export interface IPublishMainEventArgs {
|
|
22
22
|
name?: string;
|
|
23
23
|
scope?: Scope;
|
|
24
24
|
}
|
|
25
|
+
export interface IPublishRendererEventArgs {
|
|
26
|
+
name?: string;
|
|
27
|
+
scope?: Scope;
|
|
28
|
+
receiver?: EventReceiver;
|
|
29
|
+
}
|
|
25
30
|
export interface IPublishPropertyArgs {
|
|
26
31
|
name?: string;
|
|
27
32
|
scope?: Scope;
|
|
@@ -30,7 +35,8 @@ export interface IPublishPropertyArgs {
|
|
|
30
35
|
export interface ICreateProviderArgs {
|
|
31
36
|
info: IInfo;
|
|
32
37
|
scope: Scope;
|
|
33
|
-
|
|
38
|
+
handleMainEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
|
|
39
|
+
emitRendererEvent(name: string, arg: any): void;
|
|
34
40
|
callFunction(name: string, ...args: any[]): IIPCResult;
|
|
35
41
|
getVariable(name: string): IIPCResult;
|
|
36
42
|
setVariable(name: string, value: any): IIPCResult | undefined;
|
|
@@ -38,8 +44,10 @@ export interface ICreateProviderArgs {
|
|
|
38
44
|
}
|
|
39
45
|
export type Scopes = Record<string, Set<Scope>>;
|
|
40
46
|
export type Accesses = Record<string, Set<Access>>;
|
|
47
|
+
export type RendererEvent<T = undefined> = (handler: EventHandler<T>, isOnce?: boolean) => EventUnsubscriber;
|
|
41
48
|
export type MainEvent<T = undefined> = (handler: EventHandler<T>) => EventUnsubscriber;
|
|
42
49
|
export type EventEmitter = (...args: any[]) => any;
|
|
50
|
+
export type EventReceiver<I = any, O = any> = (input: I) => O;
|
|
43
51
|
export type EventUnsubscriber = () => void;
|
|
44
52
|
export type EventHandler<T> = (result: T) => void;
|
|
45
53
|
export interface IRendererPublic {
|
|
@@ -50,7 +58,8 @@ export interface IProvider {
|
|
|
50
58
|
waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
|
|
51
59
|
provided: {
|
|
52
60
|
functions: Record<string, (...args: any[]) => IIPCResult>;
|
|
53
|
-
|
|
61
|
+
mainEvents: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
|
|
62
|
+
rendererEvents: Record<string, (arg: any) => void>;
|
|
54
63
|
properties: Record<string, IIPCResult>;
|
|
55
64
|
};
|
|
56
65
|
}
|
|
@@ -62,7 +71,8 @@ export type PublicProperty = {
|
|
|
62
71
|
export interface IInfo {
|
|
63
72
|
properties: Set<string>;
|
|
64
73
|
functions: Set<string>;
|
|
65
|
-
|
|
74
|
+
mainEvents: Set<string>;
|
|
75
|
+
rendererEvents: Set<string>;
|
|
66
76
|
scopes: Scopes;
|
|
67
77
|
accesses: Accesses;
|
|
68
78
|
}
|
package/esm/enums.d.ts
CHANGED
|
@@ -9,10 +9,11 @@ export declare enum Scope {
|
|
|
9
9
|
export declare enum IPCChannel {
|
|
10
10
|
getPublicInfo = "GET_PUBLIC_INFO",
|
|
11
11
|
functionCall = "FUNCTION_CALL_",
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
mainEventOn = "MAIN_EVENT_ON_",
|
|
13
|
+
mainEventOnce = "MAIN_EVENT_ONCE_",
|
|
14
|
+
mainEventEmit = "MAIN_EVENT_EMIT_",
|
|
15
15
|
propertyGet = "PROPERTY_GET_",
|
|
16
16
|
propertySet = "PROPERTY_SET_",
|
|
17
|
-
promisePostfix = "_PROMISE"
|
|
17
|
+
promisePostfix = "_PROMISE",
|
|
18
|
+
rendererEventEmit = "RENDERER_EMIT_"
|
|
18
19
|
}
|
package/esm/enums.js
CHANGED
|
@@ -12,10 +12,11 @@ export var IPCChannel;
|
|
|
12
12
|
(function (IPCChannel) {
|
|
13
13
|
IPCChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
|
|
14
14
|
IPCChannel["functionCall"] = "FUNCTION_CALL_";
|
|
15
|
-
IPCChannel["
|
|
16
|
-
IPCChannel["
|
|
17
|
-
IPCChannel["
|
|
15
|
+
IPCChannel["mainEventOn"] = "MAIN_EVENT_ON_";
|
|
16
|
+
IPCChannel["mainEventOnce"] = "MAIN_EVENT_ONCE_";
|
|
17
|
+
IPCChannel["mainEventEmit"] = "MAIN_EVENT_EMIT_";
|
|
18
18
|
IPCChannel["propertyGet"] = "PROPERTY_GET_";
|
|
19
19
|
IPCChannel["propertySet"] = "PROPERTY_SET_";
|
|
20
20
|
IPCChannel["promisePostfix"] = "_PROMISE";
|
|
21
|
+
IPCChannel["rendererEventEmit"] = "RENDERER_EMIT_";
|
|
21
22
|
})(IPCChannel || (IPCChannel = {}));
|
package/esm/exp.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export { providePublic,
|
|
3
|
-
export {
|
|
4
|
-
export type { EventEmitter, EventHandler, EventUnsubscriber,
|
|
2
|
+
export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMainEvent, publicStaticMethod, publicStaticProperty, publicStaticRendererEvent } from './scripts/oldDecorators';
|
|
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/exp.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export { providePublic,
|
|
3
|
-
export {
|
|
2
|
+
export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMainEvent, publicStaticMethod, publicStaticProperty, publicStaticRendererEvent } from './scripts/oldDecorators';
|
|
3
|
+
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
package/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export type { EventEmitter, EventHandler, EventUnsubscriber,
|
|
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/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
|
+
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
package/esm/scripts/Bridge.js
CHANGED
|
@@ -15,8 +15,11 @@ const Bridge = createProvider({
|
|
|
15
15
|
callFunction(name, ...args) {
|
|
16
16
|
return provider.provided.functions[name](...args);
|
|
17
17
|
},
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
emitRendererEvent(name, arg) {
|
|
19
|
+
provider.provided.rendererEvents[name](arg);
|
|
20
|
+
},
|
|
21
|
+
handleMainEvent(name, type, handler) {
|
|
22
|
+
return provider.provided.mainEvents[name](type, handler);
|
|
20
23
|
},
|
|
21
24
|
getVariable(name) {
|
|
22
25
|
return provider.provided.properties[name];
|