emr-bridge 1.2.1 → 1.3.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/LICENCE +1 -1
- package/README.md +22 -22
- package/{lib → cjs}/enums.d.ts +3 -0
- package/cjs/enums.js +24 -0
- package/cjs/exp.d.ts +4 -0
- package/cjs/exp.js +18 -0
- package/cjs/index.d.ts +4 -0
- package/cjs/index.js +16 -0
- package/cjs/preload.js +10 -0
- package/cjs/renderer.js +8 -0
- package/cjs/scripts/Bridge.js +39 -0
- package/cjs/scripts/Main.js +53 -0
- package/{lib → cjs}/scripts/newDecorators.d.ts +21 -1
- package/cjs/scripts/newDecorators.js +116 -0
- package/{lib → cjs}/scripts/oldDecorators.d.ts +59 -1
- package/cjs/scripts/oldDecorators.js +111 -0
- package/cjs/scripts/preloadProvider.js +72 -0
- package/cjs/scripts/provider.js +146 -0
- package/{lib → cjs}/scripts/publish.d.ts +10 -1
- package/cjs/scripts/publish.js +167 -0
- package/{lib → cjs}/types.d.ts +11 -0
- package/cjs/types.js +2 -0
- package/esm/enums.d.ts +18 -0
- package/{lib → esm}/enums.js +3 -0
- package/esm/exp.d.ts +4 -0
- package/esm/exp.js +3 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +3 -0
- package/esm/preload.js +2 -0
- package/esm/renderer.js +1 -0
- package/esm/scripts/Bridge.d.ts +7 -0
- package/{lib → esm}/scripts/Bridge.js +3 -0
- package/esm/scripts/Main.d.ts +7 -0
- package/{lib → esm}/scripts/Main.js +13 -0
- package/esm/scripts/newDecorators.d.ts +101 -0
- package/{lib → esm}/scripts/newDecorators.js +20 -1
- package/esm/scripts/oldDecorators.d.ts +181 -0
- package/{lib → esm}/scripts/oldDecorators.js +27 -1
- package/esm/scripts/preloadProvider.d.ts +7 -0
- package/{lib → esm}/scripts/preloadProvider.js +20 -1
- package/esm/scripts/provider.d.ts +3 -0
- package/{lib → esm}/scripts/provider.js +45 -1
- package/esm/scripts/publish.d.ts +31 -0
- package/{lib → esm}/scripts/publish.js +58 -0
- package/esm/types.d.ts +68 -0
- package/package.json +48 -16
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -4
- /package/{lib → cjs}/preload.d.ts +0 -0
- /package/{lib → cjs}/renderer.d.ts +0 -0
- /package/{lib → cjs}/scripts/Bridge.d.ts +0 -0
- /package/{lib → cjs}/scripts/Main.d.ts +0 -0
- /package/{lib → cjs}/scripts/preloadProvider.d.ts +0 -0
- /package/{lib → cjs}/scripts/provider.d.ts +0 -0
- /package/{lib/preload.js → esm/preload.d.ts} +0 -0
- /package/{lib/renderer.js → esm/renderer.d.ts} +0 -0
- /package/{lib → esm}/types.js +0 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs, PublishGetterArgs, PublishSetterArgs } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
4
|
+
*
|
|
5
|
+
* _only new decorators_
|
|
6
|
+
*/
|
|
7
|
+
export declare function publicMethod(): ClassMethodDecorator;
|
|
8
|
+
/**
|
|
9
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
10
|
+
*
|
|
11
|
+
* _only new decorators_
|
|
12
|
+
* @param name - name by which the method will be accessed
|
|
13
|
+
*/
|
|
14
|
+
export declare function publicMethod(name: string): ClassMethodDecorator;
|
|
15
|
+
/**
|
|
16
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
17
|
+
*
|
|
18
|
+
* _only new decorators_
|
|
19
|
+
* @param args - publish args
|
|
20
|
+
*/
|
|
21
|
+
export declare function publicMethod(args: IPublishMethodArgs): ClassMethodDecorator;
|
|
22
|
+
/**
|
|
23
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
24
|
+
*
|
|
25
|
+
* _only new decorators_
|
|
26
|
+
*/
|
|
27
|
+
export declare function publicClassEvent(): ClassMethodDecorator;
|
|
28
|
+
/**
|
|
29
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
30
|
+
*
|
|
31
|
+
* _only new decorators_
|
|
32
|
+
* @param name - name by which the event will be accessed
|
|
33
|
+
*/
|
|
34
|
+
export declare function publicClassEvent(name: string): ClassMethodDecorator;
|
|
35
|
+
/**
|
|
36
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
37
|
+
*
|
|
38
|
+
* _only new decorators_
|
|
39
|
+
* @param args - publish args
|
|
40
|
+
*/
|
|
41
|
+
export declare function publicClassEvent(args: IPublishEventArgs): ClassMethodDecorator;
|
|
42
|
+
/**
|
|
43
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
44
|
+
*
|
|
45
|
+
* _only new decorators_
|
|
46
|
+
*/
|
|
47
|
+
export declare function publicProperty(): ClassPropertyDecorator;
|
|
48
|
+
/**
|
|
49
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
50
|
+
*
|
|
51
|
+
* _only new decorators_
|
|
52
|
+
* @param name - name by which the property will be accessed
|
|
53
|
+
*/
|
|
54
|
+
export declare function publicProperty(name: string): ClassPropertyDecorator;
|
|
55
|
+
/**
|
|
56
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
57
|
+
*
|
|
58
|
+
* _only new decorators_
|
|
59
|
+
* @param args - publish args
|
|
60
|
+
*/
|
|
61
|
+
export declare function publicProperty(args: IPublishPropertyArgs): ClassPropertyDecorator;
|
|
62
|
+
/**
|
|
63
|
+
* Makes the property getter available for **renderer** and **preload** processes.
|
|
64
|
+
*
|
|
65
|
+
* _only new decorators_
|
|
66
|
+
*/
|
|
67
|
+
export declare function publicGetter(): ClassGetterDecorator;
|
|
68
|
+
/**
|
|
69
|
+
* Makes the property getter available for **renderer** and **preload** processes.
|
|
70
|
+
*
|
|
71
|
+
* _only new decorators_
|
|
72
|
+
* @param name - name by which the property will be accessed
|
|
73
|
+
*/
|
|
74
|
+
export declare function publicGetter(name: string): ClassGetterDecorator;
|
|
75
|
+
/**
|
|
76
|
+
* Makes the property getter available for **renderer** and **preload** processes.
|
|
77
|
+
*
|
|
78
|
+
* _only new decorators_
|
|
79
|
+
* @param args - publish args
|
|
80
|
+
*/
|
|
81
|
+
export declare function publicGetter(args: PublishGetterArgs): ClassGetterDecorator;
|
|
82
|
+
/**
|
|
83
|
+
* Makes the property setter available for **renderer** and **preload** processes.
|
|
84
|
+
*
|
|
85
|
+
* _only new decorators_
|
|
86
|
+
*/
|
|
87
|
+
export declare function publicSetter(): ClassSetterDecorator;
|
|
88
|
+
/**
|
|
89
|
+
* Makes the property setter available for **renderer** and **preload** processes.
|
|
90
|
+
*
|
|
91
|
+
* _only new decorators_
|
|
92
|
+
* @param name - name by which the property will be accessed
|
|
93
|
+
*/
|
|
94
|
+
export declare function publicSetter(name: string): ClassSetterDecorator;
|
|
95
|
+
/**
|
|
96
|
+
* Makes the property setter available for **renderer** and **preload** processes.
|
|
97
|
+
*
|
|
98
|
+
* _only new decorators_
|
|
99
|
+
* @param args - publish args
|
|
100
|
+
*/
|
|
101
|
+
export declare function publicSetter(args: PublishSetterArgs): ClassSetterDecorator;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addAccess, addScope, publicFunction, publicVariable } from './publish';
|
|
1
|
+
import { addAccess, addScope, publicEvent, publicFunction, publicVariable } from './publish';
|
|
2
2
|
export function publicMethod(arg) {
|
|
3
3
|
return (method, context) => {
|
|
4
4
|
let name = String(context.name);
|
|
@@ -17,6 +17,25 @@ export function publicMethod(arg) {
|
|
|
17
17
|
});
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
+
export function publicClassEvent(arg) {
|
|
21
|
+
return (method, context) => {
|
|
22
|
+
let name = String(context.name);
|
|
23
|
+
if (arg) {
|
|
24
|
+
if (typeof arg === 'string') {
|
|
25
|
+
name = arg;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
name = arg.name ?? name;
|
|
29
|
+
if (arg.scope)
|
|
30
|
+
addScope(name, arg.scope);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
context.addInitializer(function () {
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
this[name] = publicEvent(name, method.bind(this));
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
20
39
|
export function publicProperty(arg) {
|
|
21
40
|
return (_, context) => {
|
|
22
41
|
const propName = String(context.name);
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
4
|
+
*
|
|
5
|
+
* _only for static_
|
|
6
|
+
*
|
|
7
|
+
* _experimental decorators_
|
|
8
|
+
*/
|
|
9
|
+
export declare function publicStaticMethod(): MethodDecorator;
|
|
10
|
+
/**
|
|
11
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
12
|
+
*
|
|
13
|
+
* _only for static_
|
|
14
|
+
*
|
|
15
|
+
* _experimental decorators_
|
|
16
|
+
* @param name - name by which the method will be accessed
|
|
17
|
+
*/
|
|
18
|
+
export declare function publicStaticMethod(name: string): MethodDecorator;
|
|
19
|
+
/**
|
|
20
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
21
|
+
*
|
|
22
|
+
* _only for static_
|
|
23
|
+
*
|
|
24
|
+
* _experimental decorators_
|
|
25
|
+
* @param args - publish args
|
|
26
|
+
*/
|
|
27
|
+
export declare function publicStaticMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
28
|
+
/**
|
|
29
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
30
|
+
*
|
|
31
|
+
* _only for non-static_
|
|
32
|
+
*
|
|
33
|
+
* _experimental decorators_
|
|
34
|
+
*
|
|
35
|
+
* _required `providePublic(classInstance)`_
|
|
36
|
+
*/
|
|
37
|
+
export declare function publicMethod(): MethodDecorator;
|
|
38
|
+
/**
|
|
39
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
40
|
+
*
|
|
41
|
+
* _only for non-static_
|
|
42
|
+
*
|
|
43
|
+
* _experimental decorators_
|
|
44
|
+
*
|
|
45
|
+
* _required `providePublic(classInstance)`_
|
|
46
|
+
* @param name - name by which the method will be accessed
|
|
47
|
+
*/
|
|
48
|
+
export declare function publicMethod(name: string): MethodDecorator;
|
|
49
|
+
/**
|
|
50
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
51
|
+
*
|
|
52
|
+
* _only for non-static_
|
|
53
|
+
*
|
|
54
|
+
* _experimental decorators_
|
|
55
|
+
*
|
|
56
|
+
* _required `providePublic(classInstance)`_
|
|
57
|
+
* @param args - publish args
|
|
58
|
+
*/
|
|
59
|
+
export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
60
|
+
/**
|
|
61
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
62
|
+
*
|
|
63
|
+
* _only for static_
|
|
64
|
+
*
|
|
65
|
+
* _experimental decorators_
|
|
66
|
+
*/
|
|
67
|
+
export declare function publicStaticEvent(): MethodDecorator;
|
|
68
|
+
/**
|
|
69
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
70
|
+
*
|
|
71
|
+
* _only for static_
|
|
72
|
+
*
|
|
73
|
+
* _experimental decorators_
|
|
74
|
+
* @param name - name by which the event will be accessed
|
|
75
|
+
*/
|
|
76
|
+
export declare function publicStaticEvent(name: string): MethodDecorator;
|
|
77
|
+
/**
|
|
78
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
79
|
+
*
|
|
80
|
+
* _only for static_
|
|
81
|
+
*
|
|
82
|
+
* _experimental decorators_
|
|
83
|
+
* @param args - publish args
|
|
84
|
+
*/
|
|
85
|
+
export declare function publicStaticEvent(args: IPublishEventArgs): MethodDecorator;
|
|
86
|
+
/**
|
|
87
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
88
|
+
*
|
|
89
|
+
* _only for non-static_
|
|
90
|
+
*
|
|
91
|
+
* _experimental decorators_
|
|
92
|
+
*
|
|
93
|
+
* _required `providePublic(classInstance)`_
|
|
94
|
+
*/
|
|
95
|
+
export declare function publicClassEvent(): MethodDecorator;
|
|
96
|
+
/**
|
|
97
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
98
|
+
*
|
|
99
|
+
* _only for non-static_
|
|
100
|
+
*
|
|
101
|
+
* _experimental decorators_
|
|
102
|
+
*
|
|
103
|
+
* _required `providePublic(classInstance)`_
|
|
104
|
+
* @param name - name by which the event will be accessed
|
|
105
|
+
*/
|
|
106
|
+
export declare function publicClassEvent(name: string): MethodDecorator;
|
|
107
|
+
/**
|
|
108
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
109
|
+
*
|
|
110
|
+
* _only for non-static_
|
|
111
|
+
*
|
|
112
|
+
* _experimental decorators_
|
|
113
|
+
*
|
|
114
|
+
* _required `providePublic(classInstance)`_
|
|
115
|
+
* @param args - publish args
|
|
116
|
+
*/
|
|
117
|
+
export declare function publicClassEvent(args: IPublishEventArgs): MethodDecorator;
|
|
118
|
+
/**
|
|
119
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
120
|
+
*
|
|
121
|
+
* _only for static_
|
|
122
|
+
*
|
|
123
|
+
* _experimental decorators_
|
|
124
|
+
*/
|
|
125
|
+
export declare function publicStaticProperty(): PropertyDecorator;
|
|
126
|
+
/**
|
|
127
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
128
|
+
*
|
|
129
|
+
* _only for static_
|
|
130
|
+
*
|
|
131
|
+
* _experimental decorators_
|
|
132
|
+
* @param name - name by which the property will be accessed
|
|
133
|
+
*/
|
|
134
|
+
export declare function publicStaticProperty(name: string): PropertyDecorator;
|
|
135
|
+
/**
|
|
136
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
137
|
+
*
|
|
138
|
+
* _only for static_
|
|
139
|
+
*
|
|
140
|
+
* _experimental decorators_
|
|
141
|
+
* @param args - publish args
|
|
142
|
+
*/
|
|
143
|
+
export declare function publicStaticProperty(args: IPublishPropertyArgs): PropertyDecorator;
|
|
144
|
+
/**
|
|
145
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
146
|
+
*
|
|
147
|
+
* _only for non-static_
|
|
148
|
+
*
|
|
149
|
+
* _experimental decorators_
|
|
150
|
+
*
|
|
151
|
+
* _required `providePublic(classInstance)`_
|
|
152
|
+
*/
|
|
153
|
+
export declare function publicProperty(): PropertyDecorator;
|
|
154
|
+
/**
|
|
155
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
156
|
+
*
|
|
157
|
+
* _only for non-static_
|
|
158
|
+
*
|
|
159
|
+
* _experimental decorators_
|
|
160
|
+
*
|
|
161
|
+
* _required `providePublic(classInstance)`_
|
|
162
|
+
* @param name - name by which the property will be accessed
|
|
163
|
+
*/
|
|
164
|
+
export declare function publicProperty(name: string): PropertyDecorator;
|
|
165
|
+
/**
|
|
166
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
167
|
+
*
|
|
168
|
+
* _only for non-static_
|
|
169
|
+
*
|
|
170
|
+
* _experimental decorators_
|
|
171
|
+
*
|
|
172
|
+
* _required `providePublic(classInstance)`_
|
|
173
|
+
* @param args - publish args
|
|
174
|
+
*/
|
|
175
|
+
export declare function publicProperty(args: IPublishPropertyArgs): PropertyDecorator;
|
|
176
|
+
/**
|
|
177
|
+
* Binds all public entities of a class to a specific instance.
|
|
178
|
+
*
|
|
179
|
+
* _only for class instance_
|
|
180
|
+
*/
|
|
181
|
+
export declare function providePublic<InstanceType>(instance: InstanceType): InstanceType;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addAccess, addScope, publicFunction, publicVariable } from './publish';
|
|
1
|
+
import { addAccess, addScope, publicEvent, publicFunction, publicVariable } from './publish';
|
|
2
2
|
export function publicStaticMethod(arg) {
|
|
3
3
|
return (target, key) => {
|
|
4
4
|
let name = String(key);
|
|
@@ -25,6 +25,32 @@ export function publicMethod(arg) {
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
+
export function publicStaticEvent(arg) {
|
|
29
|
+
return (target, key) => {
|
|
30
|
+
let name = String(key);
|
|
31
|
+
if (arg) {
|
|
32
|
+
if (typeof arg === 'string') {
|
|
33
|
+
name = arg;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
name = arg.name ?? String(key);
|
|
37
|
+
if (arg.scope)
|
|
38
|
+
addScope(name, arg.scope);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
target[key] = publicEvent(name, target[key].bind(target));
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export function publicClassEvent(arg) {
|
|
45
|
+
return (tgt, key, _) => {
|
|
46
|
+
const target = tgt;
|
|
47
|
+
const prevRegister = target.__register__ ?? (() => { });
|
|
48
|
+
target.__register__ = instance => {
|
|
49
|
+
prevRegister(instance);
|
|
50
|
+
publicStaticEvent(arg)(instance, key, _);
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
}
|
|
28
54
|
export function publicStaticProperty(arg) {
|
|
29
55
|
return (target, key) => {
|
|
30
56
|
let name = String(key);
|
|
@@ -21,7 +21,8 @@ function provideFromMain(contextIsolation = true) {
|
|
|
21
21
|
},
|
|
22
22
|
provided: {
|
|
23
23
|
properties: {},
|
|
24
|
-
functions: {}
|
|
24
|
+
functions: {},
|
|
25
|
+
events: {}
|
|
25
26
|
}
|
|
26
27
|
};
|
|
27
28
|
info.functions.forEach(funcName => {
|
|
@@ -32,6 +33,24 @@ function provideFromMain(contextIsolation = true) {
|
|
|
32
33
|
enumerable: true
|
|
33
34
|
});
|
|
34
35
|
});
|
|
36
|
+
info.events.forEach(eventName => {
|
|
37
|
+
Object.defineProperty(provider.provided.events, eventName, {
|
|
38
|
+
value(type, handler) {
|
|
39
|
+
const listener = (_, result) => handler(result);
|
|
40
|
+
const emitChannel = IPCChannel.eventEmit + eventName;
|
|
41
|
+
if (type === 'on') {
|
|
42
|
+
ipcRenderer.send(IPCChannel.eventHandleOn + eventName);
|
|
43
|
+
ipcRenderer.on(emitChannel, listener);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
ipcRenderer.send(IPCChannel.eventHandleOnce + eventName);
|
|
47
|
+
ipcRenderer.once(emitChannel, listener);
|
|
48
|
+
}
|
|
49
|
+
return () => ipcRenderer.removeListener(emitChannel, listener);
|
|
50
|
+
},
|
|
51
|
+
enumerable: true
|
|
52
|
+
});
|
|
53
|
+
});
|
|
35
54
|
info.properties.forEach(propName => {
|
|
36
55
|
Object.defineProperty(provider.provided.properties, propName, {
|
|
37
56
|
get() {
|
|
@@ -3,7 +3,7 @@ function errorHandler(error, channel) {
|
|
|
3
3
|
console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
4
4
|
}
|
|
5
5
|
function createProvider(args) {
|
|
6
|
-
const { info, scope, getVariable, setVariable, callFunction, waitPromise } = args;
|
|
6
|
+
const { info, scope, getVariable, setVariable, callFunction, handleEvent, waitPromise } = args;
|
|
7
7
|
const target = {
|
|
8
8
|
as: () => proxy
|
|
9
9
|
};
|
|
@@ -29,6 +29,50 @@ function createProvider(args) {
|
|
|
29
29
|
enumerable: true
|
|
30
30
|
});
|
|
31
31
|
});
|
|
32
|
+
info.events.forEach(eventName => {
|
|
33
|
+
if (!info.scopes[eventName].has(scope))
|
|
34
|
+
return;
|
|
35
|
+
const eventNameOn = `on${eventName[0].toUpperCase()}${eventName.slice(1)}`;
|
|
36
|
+
const eventNameOnce = `once${eventName[0].toUpperCase()}${eventName.slice(1)}`;
|
|
37
|
+
Object.defineProperty(target, eventNameOn, {
|
|
38
|
+
get() {
|
|
39
|
+
return (handler) => {
|
|
40
|
+
return handleEvent(eventName, 'on', (result) => {
|
|
41
|
+
if (result.error) {
|
|
42
|
+
errorHandler(result.error, eventName);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (result.promiseChannel)
|
|
46
|
+
waitPromise(result.promiseChannel).then(handler);
|
|
47
|
+
handler(result.value);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
set() {
|
|
52
|
+
throw new Error(`Function '${eventNameOn}' is readonly`);
|
|
53
|
+
},
|
|
54
|
+
enumerable: true
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(target, eventNameOnce, {
|
|
57
|
+
get() {
|
|
58
|
+
return (handler) => {
|
|
59
|
+
return handleEvent(eventName, 'once', (result) => {
|
|
60
|
+
if (result.error) {
|
|
61
|
+
errorHandler(result.error, eventName);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (result.promiseChannel)
|
|
65
|
+
waitPromise(result.promiseChannel).then(handler);
|
|
66
|
+
handler(result.value);
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
set() {
|
|
71
|
+
throw new Error(`Function '${eventNameOnce}' is readonly`);
|
|
72
|
+
},
|
|
73
|
+
enumerable: true
|
|
74
|
+
});
|
|
75
|
+
});
|
|
32
76
|
info.properties.forEach(propName => {
|
|
33
77
|
if (!info.scopes[propName].has(scope))
|
|
34
78
|
return;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Access, Scope } from '../enums';
|
|
2
|
+
import type { EventEmitter, PublicFunction, PublicProperty } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
5
|
+
*
|
|
6
|
+
* _only for event_
|
|
7
|
+
* @param name - name by which the event will be accessed
|
|
8
|
+
* @param func - linked emitter
|
|
9
|
+
* @param scopes - event scopes
|
|
10
|
+
*/
|
|
11
|
+
export declare function publicEvent<Emitter extends EventEmitter>(name: string, func?: Emitter, scopes?: Scope[]): (...args: Parameters<Emitter>) => void;
|
|
12
|
+
/**
|
|
13
|
+
* Makes the function available for **renderer** and **preload** processes.
|
|
14
|
+
*
|
|
15
|
+
* _only for function_
|
|
16
|
+
* @param name - name by which the function will be accessed
|
|
17
|
+
* @param func - linked function
|
|
18
|
+
* @param scopes - function scopes
|
|
19
|
+
*/
|
|
20
|
+
export declare function publicFunction(name: string, func: PublicFunction, scopes?: Scope[]): void;
|
|
21
|
+
/**
|
|
22
|
+
* Makes the function available for **renderer** and **preload** processes.
|
|
23
|
+
*
|
|
24
|
+
* _only for variables_
|
|
25
|
+
* @param name - name by which the variable will be accessed
|
|
26
|
+
* @param value - variable getter/setter
|
|
27
|
+
* @param scopes - variable scopes
|
|
28
|
+
*/
|
|
29
|
+
export declare function publicVariable(name: string, value: PublicProperty, scopes?: Scope[]): void;
|
|
30
|
+
export declare function addScope(key: string, scope: Scope): void;
|
|
31
|
+
export declare function addAccess(key: string, access: Access): void;
|
|
@@ -3,10 +3,68 @@ import { Access, IPCChannel, Scope } from '../enums';
|
|
|
3
3
|
const publicInfo = {
|
|
4
4
|
properties: new Set(),
|
|
5
5
|
functions: new Set(),
|
|
6
|
+
events: new Set(),
|
|
6
7
|
accesses: {},
|
|
7
8
|
scopes: {}
|
|
8
9
|
};
|
|
9
10
|
ipcMain.on(IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
|
|
11
|
+
/**
|
|
12
|
+
* Makes the event available for **renderer** and **preload** processes.
|
|
13
|
+
*
|
|
14
|
+
* _only for event_
|
|
15
|
+
* @param name - name by which the event will be accessed
|
|
16
|
+
* @param func - linked emitter
|
|
17
|
+
* @param scopes - event scopes
|
|
18
|
+
*/
|
|
19
|
+
export function publicEvent(name, func = (() => { }), scopes = [Scope.preload, Scope.renderer]) {
|
|
20
|
+
const { events } = publicInfo;
|
|
21
|
+
const emitChannel = IPCChannel.eventEmit + name;
|
|
22
|
+
const handleChannel = IPCChannel.eventHandleOn + name;
|
|
23
|
+
const handleChannelOnce = IPCChannel.eventHandleOnce + name;
|
|
24
|
+
let senders = [];
|
|
25
|
+
let sendersOnce = [];
|
|
26
|
+
events.add(name);
|
|
27
|
+
if (!publicInfo.scopes[name])
|
|
28
|
+
publicInfo.scopes[name] = new Set(scopes);
|
|
29
|
+
ipcMain.removeAllListeners(handleChannel);
|
|
30
|
+
ipcMain.removeAllListeners(handleChannelOnce);
|
|
31
|
+
ipcMain.on(handleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
|
|
32
|
+
ipcMain.on(handleChannelOnce, ({ sender }) => !sendersOnce.includes(sender) && sendersOnce.push(sender));
|
|
33
|
+
return (...args) => {
|
|
34
|
+
function filterSenders() {
|
|
35
|
+
senders = senders.filter(sender => !sender.isDestroyed());
|
|
36
|
+
sendersOnce = sendersOnce.filter(sender => !sender.isDestroyed());
|
|
37
|
+
}
|
|
38
|
+
filterSenders();
|
|
39
|
+
const allSenders = [...senders, ...sendersOnce];
|
|
40
|
+
try {
|
|
41
|
+
const result = func(...args);
|
|
42
|
+
if (result instanceof Promise) {
|
|
43
|
+
const promiseChannel = emitChannel + IPCChannel.promisePostfix;
|
|
44
|
+
result
|
|
45
|
+
.then(value => {
|
|
46
|
+
filterSenders();
|
|
47
|
+
[...senders, ...sendersOnce].forEach(sender => sender.send(promiseChannel, { value }));
|
|
48
|
+
sendersOnce = [];
|
|
49
|
+
})
|
|
50
|
+
.catch(reason => {
|
|
51
|
+
filterSenders();
|
|
52
|
+
[...senders, ...sendersOnce].forEach(sender => sender.send(promiseChannel, { error: String(reason) }));
|
|
53
|
+
sendersOnce = [];
|
|
54
|
+
});
|
|
55
|
+
allSenders.forEach(sender => sender.send(promiseChannel, { promiseChannel }));
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
allSenders.forEach(sender => sender.send(emitChannel, { value: result }));
|
|
59
|
+
sendersOnce = [];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
allSenders.forEach(sender => sender.send(emitChannel, { error: String(error) }));
|
|
64
|
+
sendersOnce = [];
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
10
68
|
/**
|
|
11
69
|
* Makes the function available for **renderer** and **preload** processes.
|
|
12
70
|
*
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Scope } from './enums';
|
|
2
|
+
import { Access } from './enums';
|
|
3
|
+
export interface ITarget {
|
|
4
|
+
as<T>(): T;
|
|
5
|
+
}
|
|
6
|
+
export type ClassMethodDecorator = <This, Args extends any[], Return>(method: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>) => void;
|
|
7
|
+
export type ClassPropertyDecorator = <This, Type>(target: undefined, context: ClassFieldDecoratorContext<This, Type>) => void;
|
|
8
|
+
export type ClassGetterDecorator = <This, Type>(getter: () => Type, context: ClassGetterDecoratorContext<This, Type>) => void;
|
|
9
|
+
export type ClassSetterDecorator = <This, Type>(setter: (value: Type) => void, context: ClassSetterDecoratorContext<This, Type>) => void;
|
|
10
|
+
export type PublishSetterArgs = Omit<IPublishPropertyArgs, 'access'>;
|
|
11
|
+
export type PublishGetterArgs = Omit<IPublishPropertyArgs, 'access'>;
|
|
12
|
+
export interface IIPCResult {
|
|
13
|
+
value?: any;
|
|
14
|
+
error?: string;
|
|
15
|
+
promiseChannel?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface IPublishMethodArgs {
|
|
18
|
+
name?: string;
|
|
19
|
+
scope?: Scope;
|
|
20
|
+
}
|
|
21
|
+
export interface IPublishEventArgs {
|
|
22
|
+
name?: string;
|
|
23
|
+
scope?: Scope;
|
|
24
|
+
}
|
|
25
|
+
export interface IPublishPropertyArgs {
|
|
26
|
+
name?: string;
|
|
27
|
+
scope?: Scope;
|
|
28
|
+
access?: Access;
|
|
29
|
+
}
|
|
30
|
+
export interface ICreateProviderArgs {
|
|
31
|
+
info: IInfo;
|
|
32
|
+
scope: Scope;
|
|
33
|
+
handleEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
|
|
34
|
+
callFunction(name: string, ...args: any[]): IIPCResult;
|
|
35
|
+
getVariable(name: string): IIPCResult;
|
|
36
|
+
setVariable(name: string, value: any): IIPCResult | undefined;
|
|
37
|
+
waitPromise(channel: string): Promise<any>;
|
|
38
|
+
}
|
|
39
|
+
export type Scopes = Record<string, Set<Scope>>;
|
|
40
|
+
export type Accesses = Record<string, Set<Access>>;
|
|
41
|
+
export type MainEvent<T = undefined> = (handler: EventHandler<T>) => EventUnsubscriber;
|
|
42
|
+
export type EventEmitter = (...args: any[]) => any;
|
|
43
|
+
export type EventUnsubscriber = () => void;
|
|
44
|
+
export type EventHandler<T> = (result: T) => void;
|
|
45
|
+
export interface IRendererPublic {
|
|
46
|
+
__register__?(instance: any): void;
|
|
47
|
+
}
|
|
48
|
+
export interface IProvider {
|
|
49
|
+
getInfo(): IInfo;
|
|
50
|
+
waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
|
|
51
|
+
provided: {
|
|
52
|
+
functions: Record<string, (...args: any[]) => IIPCResult>;
|
|
53
|
+
events: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
|
|
54
|
+
properties: Record<string, IIPCResult>;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export type PublicFunction = (...args: any[]) => any;
|
|
58
|
+
export type PublicProperty = {
|
|
59
|
+
get?(): any;
|
|
60
|
+
set?(value: any): void;
|
|
61
|
+
};
|
|
62
|
+
export interface IInfo {
|
|
63
|
+
properties: Set<string>;
|
|
64
|
+
functions: Set<string>;
|
|
65
|
+
events: Set<string>;
|
|
66
|
+
scopes: Scopes;
|
|
67
|
+
accesses: Accesses;
|
|
68
|
+
}
|