emr-bridge 1.0.0 → 1.1.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/LICENCE +21 -0
- package/README.md +89 -34
- package/lib/enums.d.ts +0 -2
- package/lib/enums.js +0 -2
- package/lib/index.d.ts +2 -2
- package/lib/index.js +1 -1
- package/lib/preload.d.ts +2 -2
- package/lib/preload.js +2 -2
- package/lib/renderer.d.ts +1 -1
- package/lib/renderer.js +1 -1
- package/lib/scripts/Bridge.d.ts +6 -0
- package/lib/scripts/Bridge.js +30 -0
- package/lib/scripts/Main.d.ts +6 -0
- package/lib/scripts/Main.js +34 -0
- package/lib/{provider.d.ts → scripts/preloadProvider.d.ts} +2 -0
- package/lib/scripts/preloadProvider.js +50 -0
- package/lib/scripts/provider.d.ts +2 -0
- package/lib/scripts/provider.js +99 -0
- package/lib/scripts/publish.d.ts +118 -0
- package/lib/{publish.js → scripts/publish.js} +59 -75
- package/lib/types.d.ts +26 -14
- package/package.json +5 -3
- package/lib/bridge.d.ts +0 -10
- package/lib/bridge.js +0 -98
- package/lib/main.d.ts +0 -10
- package/lib/main.js +0 -110
- package/lib/provider.js +0 -63
- package/lib/publish.d.ts +0 -66
- package/lib/tsconfig.tsbuildinfo +0 -1
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 VerZsuT
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -13,10 +13,11 @@ JS library for easily providing access to the **main** from the **renderer** pro
|
|
|
13
13
|
- [scopes](#scopes)
|
|
14
14
|
- [access](#restricting-access-to-variables)
|
|
15
15
|
- [usage in renderer](#access-from-renderer-and-preload)
|
|
16
|
+
- [promises](#promises)
|
|
16
17
|
|
|
17
18
|
## Installation
|
|
18
19
|
|
|
19
|
-
```
|
|
20
|
+
```text
|
|
20
21
|
npm i emr-bridge
|
|
21
22
|
```
|
|
22
23
|
|
|
@@ -26,7 +27,7 @@ There are three ways to use
|
|
|
26
27
|
|
|
27
28
|
**IMPORTANT**: In any of the cases, you need to insert this code into preload
|
|
28
29
|
|
|
29
|
-
```
|
|
30
|
+
```js
|
|
30
31
|
// Preload process
|
|
31
32
|
import { provideFromMain } from 'emr-bridge/preload'
|
|
32
33
|
|
|
@@ -41,14 +42,14 @@ Use **publicStaticMethod**/**publicStaticProperty** decorators
|
|
|
41
42
|
|
|
42
43
|
```ts
|
|
43
44
|
// Main process
|
|
44
|
-
import { publicStaticMethod, publicStaticProperty,
|
|
45
|
+
import { publicStaticMethod, publicStaticProperty, Access } from 'emr-bridge'
|
|
45
46
|
|
|
46
47
|
class User {
|
|
47
48
|
private static name = 'Name'
|
|
48
49
|
private static age = 20
|
|
49
50
|
private static money = 1000
|
|
50
51
|
|
|
51
|
-
@publicStaticProperty()
|
|
52
|
+
@publicStaticProperty('userBalance')
|
|
52
53
|
static get balance(): string {
|
|
53
54
|
return `${this.money}$`
|
|
54
55
|
}
|
|
@@ -57,12 +58,14 @@ class User {
|
|
|
57
58
|
this.money = Number(newBalance.split('$')[0])
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
@publicStaticProperty(
|
|
61
|
+
@publicStaticProperty({
|
|
62
|
+
name: 'userInfo',
|
|
63
|
+
access: Access.get
|
|
64
|
+
})
|
|
61
65
|
static get info(): string {
|
|
62
66
|
return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
|
|
63
67
|
}
|
|
64
68
|
|
|
65
|
-
@access(Access.get)
|
|
66
69
|
@publicStaticMethod()
|
|
67
70
|
static setName(newName: string): void {
|
|
68
71
|
this.name = newName
|
|
@@ -79,9 +82,11 @@ class User {
|
|
|
79
82
|
|
|
80
83
|
Use **providePublic** and **publicMethod**/**publicProperty** decorators.
|
|
81
84
|
|
|
85
|
+
Without calling **providePublic**, public methods will not be accessible from renderer.
|
|
86
|
+
|
|
82
87
|
```ts
|
|
83
88
|
// Main process
|
|
84
|
-
import { providePublic, publicMethod, publicProperty,
|
|
89
|
+
import { providePublic, publicMethod, publicProperty, Access } from 'emr-bridge'
|
|
85
90
|
|
|
86
91
|
class User {
|
|
87
92
|
private name = 'Name'
|
|
@@ -97,8 +102,10 @@ class User {
|
|
|
97
102
|
this.money = Number(newBalance.split('$')[0])
|
|
98
103
|
}
|
|
99
104
|
|
|
100
|
-
@
|
|
101
|
-
|
|
105
|
+
@publicProperty({
|
|
106
|
+
name: 'userInfo',
|
|
107
|
+
access: Access.get
|
|
108
|
+
})
|
|
102
109
|
get info(): string {
|
|
103
110
|
return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
|
|
104
111
|
}
|
|
@@ -125,11 +132,15 @@ Use **publicFunction** and **publicVariable**.
|
|
|
125
132
|
// Main process
|
|
126
133
|
import { publicFunction, publicVariable } from 'emr-bridge'
|
|
127
134
|
|
|
128
|
-
publicFunction(sayHello,
|
|
129
|
-
publicFunction(
|
|
135
|
+
publicFunction('sayHello', sayHello)
|
|
136
|
+
publicFunction('getUserName', getName)
|
|
130
137
|
publicVariable('count', {
|
|
131
|
-
get
|
|
132
|
-
|
|
138
|
+
get(): number {
|
|
139
|
+
return count
|
|
140
|
+
},
|
|
141
|
+
set(value: number) {
|
|
142
|
+
count = value
|
|
143
|
+
}
|
|
133
144
|
})
|
|
134
145
|
|
|
135
146
|
let count = 0
|
|
@@ -150,32 +161,37 @@ function getName(): string {
|
|
|
150
161
|
|
|
151
162
|
### Scopes
|
|
152
163
|
|
|
153
|
-
In order to set a specific scope, use the **scope**
|
|
164
|
+
In order to set a specific scope, use the **scope** property.
|
|
165
|
+
|
|
166
|
+
Accessing an entity outside the specified scope will result in an error being thrown.
|
|
154
167
|
|
|
155
168
|
```ts
|
|
156
169
|
// Main process
|
|
157
170
|
|
|
158
171
|
// static and non-static
|
|
159
|
-
import {
|
|
172
|
+
import { Scope, Access, providePublic, publicStaticMethod, publicMethod, publicProperty } from 'emr-bridge'
|
|
160
173
|
|
|
161
174
|
class User {
|
|
162
175
|
private static age = 20
|
|
163
176
|
private name = 'Name'
|
|
164
|
-
|
|
165
|
-
@
|
|
166
|
-
|
|
177
|
+
|
|
178
|
+
@publicProperty({
|
|
179
|
+
scope: Scope.preload,
|
|
180
|
+
access: Access.get
|
|
181
|
+
})
|
|
167
182
|
get count(): number {
|
|
168
183
|
return 30
|
|
169
184
|
}
|
|
170
|
-
|
|
171
|
-
@scope
|
|
172
|
-
@publicStaticMethod()
|
|
185
|
+
|
|
186
|
+
@publicStaticMethod({ scope: Scope.preload })
|
|
173
187
|
static getAge(): number {
|
|
174
188
|
return this.age
|
|
175
189
|
}
|
|
176
|
-
|
|
177
|
-
@
|
|
178
|
-
|
|
190
|
+
|
|
191
|
+
@publicMethod({
|
|
192
|
+
name: 'getUserName',
|
|
193
|
+
scope: Scope.renderer
|
|
194
|
+
})
|
|
179
195
|
getName(): string {
|
|
180
196
|
return this.name
|
|
181
197
|
}
|
|
@@ -186,10 +202,14 @@ providePublic(new User())
|
|
|
186
202
|
// functions and variables
|
|
187
203
|
import { publicFunction, publicVariable, Scope } from 'emr-bridge'
|
|
188
204
|
|
|
189
|
-
publicFunction(getName,
|
|
205
|
+
publicFunction('getName', getName, [Scope.preload])
|
|
190
206
|
publicVariable('count', {
|
|
191
|
-
get
|
|
192
|
-
|
|
207
|
+
get(): number {
|
|
208
|
+
return count
|
|
209
|
+
},
|
|
210
|
+
set(value: number) {
|
|
211
|
+
count = value
|
|
212
|
+
}
|
|
193
213
|
}, [Scope.renderer])
|
|
194
214
|
|
|
195
215
|
let count = 0
|
|
@@ -208,20 +228,21 @@ Using an entity outside the specified scope _will cause an error_.
|
|
|
208
228
|
// Main process
|
|
209
229
|
|
|
210
230
|
// static and non-static
|
|
211
|
-
import { publicStaticProperty, publicProperty, providePublic,
|
|
231
|
+
import { publicStaticProperty, publicProperty, providePublic, Access } from 'emr-bridge'
|
|
212
232
|
|
|
213
233
|
class User {
|
|
214
234
|
private static name = 'Name'
|
|
215
235
|
private static surname = 'Surname'
|
|
216
236
|
|
|
217
|
-
@access
|
|
218
|
-
@publicStaticProperty()
|
|
237
|
+
@publicStaticProperty({ access: Access.get })
|
|
219
238
|
static get fullName(): string {
|
|
220
239
|
return `${this.name} ${this.surname}`
|
|
221
240
|
}
|
|
222
241
|
|
|
223
|
-
@
|
|
224
|
-
|
|
242
|
+
@publicProperty({
|
|
243
|
+
name: 'userAge',
|
|
244
|
+
access: Access.get
|
|
245
|
+
})
|
|
225
246
|
get age(): number {
|
|
226
247
|
return 20
|
|
227
248
|
}
|
|
@@ -233,8 +254,14 @@ providePublic(new User())
|
|
|
233
254
|
import { publicVariable } from 'emr-bridge'
|
|
234
255
|
|
|
235
256
|
publicVariable('count', {
|
|
236
|
-
|
|
237
|
-
|
|
257
|
+
// get access
|
|
258
|
+
get(): number {
|
|
259
|
+
return count
|
|
260
|
+
},
|
|
261
|
+
// set access
|
|
262
|
+
set(value: number) {
|
|
263
|
+
count = value
|
|
264
|
+
}
|
|
238
265
|
})
|
|
239
266
|
|
|
240
267
|
let count = 0
|
|
@@ -281,3 +308,31 @@ bridge.getUserName()
|
|
|
281
308
|
bridge.count--
|
|
282
309
|
bridge.setUserAge(30)
|
|
283
310
|
```
|
|
311
|
+
|
|
312
|
+
### Promises
|
|
313
|
+
|
|
314
|
+
The library also allows you to pass Promises from main to renderer
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
// Main process
|
|
318
|
+
import { publicFunction } from 'emr-bridge'
|
|
319
|
+
|
|
320
|
+
publicFunction('delay1s', () => {
|
|
321
|
+
return new Promise<void>(resolve => {
|
|
322
|
+
setTimeout(resolve, 1000)
|
|
323
|
+
})
|
|
324
|
+
})
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
// Renderer process
|
|
329
|
+
import { Bridge } from 'emr-bridge/renderer'
|
|
330
|
+
|
|
331
|
+
interface IPublic {
|
|
332
|
+
delay1s(): Promise<void>
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const bridge = Bridge.as<IPublic>()
|
|
336
|
+
|
|
337
|
+
bridge.delay1s().then(() => console.log('After 1 second'))
|
|
338
|
+
```
|
package/lib/enums.d.ts
CHANGED
|
@@ -7,9 +7,7 @@ export declare enum Scope {
|
|
|
7
7
|
preload = "preload"
|
|
8
8
|
}
|
|
9
9
|
export declare enum IPCChannel {
|
|
10
|
-
getPublicScopes = "GET_PUBLIC_SCOPES",
|
|
11
10
|
getPublicInfo = "GET_PUBLIC_INFO",
|
|
12
|
-
getPublicAccesses = "GET_PUBLIC_ACCESSES",
|
|
13
11
|
functionCall = "FUNCTION_CALL_",
|
|
14
12
|
propertyGet = "PROPERTY_GET_",
|
|
15
13
|
propertySet = "PROPERTY_SET_",
|
package/lib/enums.js
CHANGED
|
@@ -10,9 +10,7 @@ export var Scope;
|
|
|
10
10
|
})(Scope || (Scope = {}));
|
|
11
11
|
export var IPCChannel;
|
|
12
12
|
(function (IPCChannel) {
|
|
13
|
-
IPCChannel["getPublicScopes"] = "GET_PUBLIC_SCOPES";
|
|
14
13
|
IPCChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
|
|
15
|
-
IPCChannel["getPublicAccesses"] = "GET_PUBLIC_ACCESSES";
|
|
16
14
|
IPCChannel["functionCall"] = "FUNCTION_CALL_";
|
|
17
15
|
IPCChannel["propertyGet"] = "PROPERTY_GET_";
|
|
18
16
|
IPCChannel["propertySet"] = "PROPERTY_SET_";
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { scope, access, publicMethod, publicProperty, publicFunction, publicVariable, publicStaticMethod, publicStaticProperty, providePublic } from './publish';
|
|
2
1
|
export { Access, Scope } from './enums';
|
|
3
|
-
export
|
|
2
|
+
export { providePublic, publicFunction, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty, publicVariable } from './scripts/publish';
|
|
3
|
+
export type { IPublishMethodArgs, IPublishPropertyArgs, PublicFunction, PublicProperty } from './types';
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { scope, access, publicMethod, publicProperty, publicFunction, publicVariable, publicStaticMethod, publicStaticProperty, providePublic } from './publish';
|
|
2
1
|
export { Access, Scope } from './enums';
|
|
2
|
+
export { providePublic, publicFunction, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty, publicVariable } from './scripts/publish';
|
package/lib/preload.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { Main } from './
|
|
2
|
-
export { provideFromMain } from './
|
|
1
|
+
export { Main } from './scripts/Main';
|
|
2
|
+
export { provideFromMain } from './scripts/preloadProvider';
|
package/lib/preload.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { Main } from './
|
|
2
|
-
export { provideFromMain } from './
|
|
1
|
+
export { Main } from './scripts/Main';
|
|
2
|
+
export { provideFromMain } from './scripts/preloadProvider';
|
package/lib/renderer.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Bridge } from './
|
|
1
|
+
export { Bridge } from './scripts/Bridge';
|
package/lib/renderer.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { Bridge } from './
|
|
1
|
+
export { Bridge } from './scripts/Bridge';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Scope } from '../enums';
|
|
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
|
+
/**
|
|
8
|
+
* Bridge between `renderer` and `main` processes.
|
|
9
|
+
*
|
|
10
|
+
* _only for renderer process_
|
|
11
|
+
*/
|
|
12
|
+
export const Bridge = createProvider({
|
|
13
|
+
info,
|
|
14
|
+
scope: Scope.renderer,
|
|
15
|
+
callFunction(name, ...args) {
|
|
16
|
+
return provider.provided.functions[name](...args);
|
|
17
|
+
},
|
|
18
|
+
getVariable(name) {
|
|
19
|
+
return provider.provided.properties[name];
|
|
20
|
+
},
|
|
21
|
+
setVariable(name, value) {
|
|
22
|
+
provider.provided.properties[name] = value;
|
|
23
|
+
return;
|
|
24
|
+
},
|
|
25
|
+
waitPromise(channel) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
provider.waitPromise(channel, resolve, reject);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ipcRenderer } from 'electron';
|
|
2
|
+
import { IPCChannel, Scope } from '../enums';
|
|
3
|
+
import { createProvider } from './provider';
|
|
4
|
+
const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
|
|
5
|
+
if (!info)
|
|
6
|
+
throw new Error('Public methods from main is not provided');
|
|
7
|
+
/**
|
|
8
|
+
* Bridge between `preload` and `main` processes.
|
|
9
|
+
*
|
|
10
|
+
* _only for preload process_
|
|
11
|
+
*/
|
|
12
|
+
export const Main = createProvider({
|
|
13
|
+
info,
|
|
14
|
+
scope: Scope.preload,
|
|
15
|
+
callFunction(name, ...args) {
|
|
16
|
+
return ipcRenderer.sendSync(IPCChannel.functionCall + name, ...args);
|
|
17
|
+
},
|
|
18
|
+
getVariable(name) {
|
|
19
|
+
return ipcRenderer.sendSync(IPCChannel.propertyGet + name);
|
|
20
|
+
},
|
|
21
|
+
setVariable(name, value) {
|
|
22
|
+
return ipcRenderer.sendSync(IPCChannel.propertySet + name, value);
|
|
23
|
+
},
|
|
24
|
+
waitPromise(channel) {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
ipcRenderer.once(channel, (_, result) => {
|
|
27
|
+
if (result.error)
|
|
28
|
+
reject(result.error);
|
|
29
|
+
else
|
|
30
|
+
resolve(result.value);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { contextBridge, ipcRenderer } from 'electron';
|
|
2
|
+
import { IPCChannel } from '../enums';
|
|
3
|
+
const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
|
|
4
|
+
/**
|
|
5
|
+
* Sets access to main from renderer
|
|
6
|
+
*
|
|
7
|
+
* @param contextIsolation - _default_: `true`
|
|
8
|
+
*/
|
|
9
|
+
export function provideFromMain(contextIsolation = true) {
|
|
10
|
+
const provider = {
|
|
11
|
+
getInfo() {
|
|
12
|
+
return info;
|
|
13
|
+
},
|
|
14
|
+
waitPromise(channel, resolve, reject) {
|
|
15
|
+
ipcRenderer.once(channel, (_, result) => {
|
|
16
|
+
if (result.error)
|
|
17
|
+
reject?.(result.error);
|
|
18
|
+
else
|
|
19
|
+
resolve(result.value);
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
provided: {
|
|
23
|
+
properties: {},
|
|
24
|
+
functions: {}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
info.functions.forEach(funcName => {
|
|
28
|
+
Object.defineProperty(provider.provided.functions, funcName, {
|
|
29
|
+
value(...args) {
|
|
30
|
+
return ipcRenderer.sendSync(IPCChannel.functionCall + funcName, ...args);
|
|
31
|
+
},
|
|
32
|
+
enumerable: true
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
info.properties.forEach(propName => {
|
|
36
|
+
Object.defineProperty(provider.provided.properties, propName, {
|
|
37
|
+
get() {
|
|
38
|
+
return ipcRenderer.sendSync(IPCChannel.propertyGet + propName);
|
|
39
|
+
},
|
|
40
|
+
set(value) {
|
|
41
|
+
ipcRenderer.sendSync(IPCChannel.propertySet + propName, value);
|
|
42
|
+
},
|
|
43
|
+
enumerable: true
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
if (contextIsolation)
|
|
47
|
+
contextBridge.exposeInMainWorld('__provider__', provider);
|
|
48
|
+
else
|
|
49
|
+
window.__provider__ = provider;
|
|
50
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Access, Scope } from '../enums';
|
|
2
|
+
function errorHandler(error, channel) {
|
|
3
|
+
console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
4
|
+
}
|
|
5
|
+
export function createProvider(args) {
|
|
6
|
+
const { info, scope, getVariable, setVariable, callFunction, waitPromise } = args;
|
|
7
|
+
const target = {
|
|
8
|
+
as: () => proxy
|
|
9
|
+
};
|
|
10
|
+
info.functions.forEach(funcName => {
|
|
11
|
+
if (!info.scopes[funcName].has(scope))
|
|
12
|
+
return;
|
|
13
|
+
Object.defineProperty(target, funcName, {
|
|
14
|
+
get() {
|
|
15
|
+
return (...args) => {
|
|
16
|
+
const result = callFunction(funcName, ...args);
|
|
17
|
+
if (result.error) {
|
|
18
|
+
errorHandler(result.error, funcName);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (result.promiseChannel)
|
|
22
|
+
return waitPromise(result.promiseChannel);
|
|
23
|
+
return result.value;
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
set() {
|
|
27
|
+
throw new Error(`Function '${funcName}' is readonly`);
|
|
28
|
+
},
|
|
29
|
+
enumerable: true
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
info.properties.forEach(propName => {
|
|
33
|
+
if (!info.scopes[propName].has(scope))
|
|
34
|
+
return;
|
|
35
|
+
let getter = () => {
|
|
36
|
+
const result = getVariable(propName);
|
|
37
|
+
if (result.error) {
|
|
38
|
+
errorHandler(result.error, propName);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return result.value;
|
|
42
|
+
};
|
|
43
|
+
let setter = (value) => {
|
|
44
|
+
const result = setVariable(propName, value);
|
|
45
|
+
if (result?.error) {
|
|
46
|
+
errorHandler(result.error, propName);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
if (!info.accesses[propName].has(Access.get))
|
|
51
|
+
getter = () => { throw new Error(`No access to getting the '${propName}' property`); };
|
|
52
|
+
if (!info.accesses[propName].has(Access.set))
|
|
53
|
+
setter = () => { throw new Error(`No access to setting the '${propName}' property`); };
|
|
54
|
+
Object.defineProperty(target, propName, {
|
|
55
|
+
get: getter,
|
|
56
|
+
set: setter,
|
|
57
|
+
enumerable: true
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
const proxy = new Proxy(target, {
|
|
61
|
+
get(target, key) {
|
|
62
|
+
if (key in target)
|
|
63
|
+
return target[key];
|
|
64
|
+
const isFunction = info.functions.has(key);
|
|
65
|
+
const isProperty = info.properties.has(key);
|
|
66
|
+
if (!isFunction && !isProperty)
|
|
67
|
+
throw new Error(`'${key}' is not provided from main`);
|
|
68
|
+
if (!info.scopes[key].has(Scope.renderer))
|
|
69
|
+
throw new Error(`'${key}' is not provided into renderer scope`);
|
|
70
|
+
if (isProperty && !info.accesses[key].has(Access.get))
|
|
71
|
+
throw new Error(`No access to getting the '${key}' property`);
|
|
72
|
+
throw new Error(`Unknown error on get '${key}'`);
|
|
73
|
+
},
|
|
74
|
+
set(target, key, newValue) {
|
|
75
|
+
if (key in target) {
|
|
76
|
+
target[key] = newValue;
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
const isFunction = info.functions.has(key);
|
|
80
|
+
const isProperty = info.properties.has(key);
|
|
81
|
+
if (isFunction)
|
|
82
|
+
throw new Error(`Function '${key}' is readonly`);
|
|
83
|
+
if (!isProperty)
|
|
84
|
+
throw new Error(`Property '${key}' is not provided from main`);
|
|
85
|
+
if (!info.scopes[key].has(Scope.renderer))
|
|
86
|
+
throw new Error(`'${key}' is not provided into renderer scope`);
|
|
87
|
+
if (!info.accesses[key].has(Access.set))
|
|
88
|
+
throw new Error(`No access to setting the '${key}' property`);
|
|
89
|
+
throw new Error(`Unknown error on set '${key}'`);
|
|
90
|
+
},
|
|
91
|
+
ownKeys(target) {
|
|
92
|
+
return Object.keys(target);
|
|
93
|
+
},
|
|
94
|
+
has(target, key) {
|
|
95
|
+
return key in target;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return proxy;
|
|
99
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { Scope } from '../enums';
|
|
2
|
+
import type { IPublishMethodArgs, IPublishPropertyArgs, PublicFunction, PublicProperty } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
5
|
+
*
|
|
6
|
+
* _only for static_
|
|
7
|
+
*/
|
|
8
|
+
export declare function publicStaticMethod(): MethodDecorator;
|
|
9
|
+
/**
|
|
10
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
11
|
+
*
|
|
12
|
+
* _only for static_
|
|
13
|
+
* @param name - name by which the method will be accessed
|
|
14
|
+
*/
|
|
15
|
+
export declare function publicStaticMethod(name: string): MethodDecorator;
|
|
16
|
+
/**
|
|
17
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
18
|
+
*
|
|
19
|
+
* _only for static_
|
|
20
|
+
* @param args - publish args
|
|
21
|
+
*/
|
|
22
|
+
export declare function publicStaticMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
23
|
+
/**
|
|
24
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
25
|
+
*
|
|
26
|
+
* _only for non-static_
|
|
27
|
+
*
|
|
28
|
+
* _required `providePublic(classInstance)`_
|
|
29
|
+
*/
|
|
30
|
+
export declare function publicMethod(): MethodDecorator;
|
|
31
|
+
/**
|
|
32
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
33
|
+
*
|
|
34
|
+
* _only for non-static_
|
|
35
|
+
*
|
|
36
|
+
* _required `providePublic(classInstance)`_
|
|
37
|
+
* @param name - name by which the method will be accessed
|
|
38
|
+
*/
|
|
39
|
+
export declare function publicMethod(name: string): MethodDecorator;
|
|
40
|
+
/**
|
|
41
|
+
* Makes the method available for **renderer** and **preload** processes.
|
|
42
|
+
*
|
|
43
|
+
* _only for non-static_
|
|
44
|
+
*
|
|
45
|
+
* _required `providePublic(classInstance)`_
|
|
46
|
+
* @param args - publish args
|
|
47
|
+
*/
|
|
48
|
+
export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
|
|
49
|
+
/**
|
|
50
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
51
|
+
*
|
|
52
|
+
* _only for static_
|
|
53
|
+
*/
|
|
54
|
+
export declare function publicStaticProperty(): PropertyDecorator;
|
|
55
|
+
/**
|
|
56
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
57
|
+
*
|
|
58
|
+
* _only for static_
|
|
59
|
+
* @param name - name by which the property will be accessed
|
|
60
|
+
*/
|
|
61
|
+
export declare function publicStaticProperty(name: string): PropertyDecorator;
|
|
62
|
+
/**
|
|
63
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
64
|
+
*
|
|
65
|
+
* _only for static_
|
|
66
|
+
* @param args - publish args
|
|
67
|
+
*/
|
|
68
|
+
export declare function publicStaticProperty(args: IPublishPropertyArgs): PropertyDecorator;
|
|
69
|
+
/**
|
|
70
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
71
|
+
*
|
|
72
|
+
* _only for non-static_
|
|
73
|
+
*
|
|
74
|
+
* _required `providePublic(classInstance)`_
|
|
75
|
+
*/
|
|
76
|
+
export declare function publicProperty(): PropertyDecorator;
|
|
77
|
+
/**
|
|
78
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
79
|
+
*
|
|
80
|
+
* _only for non-static_
|
|
81
|
+
*
|
|
82
|
+
* _required `providePublic(classInstance)`_
|
|
83
|
+
* @param name - name by which the property will be accessed
|
|
84
|
+
*/
|
|
85
|
+
export declare function publicProperty(name: string): PropertyDecorator;
|
|
86
|
+
/**
|
|
87
|
+
* Makes the property available for **renderer** and **preload** processes.
|
|
88
|
+
*
|
|
89
|
+
* _only for non-static_
|
|
90
|
+
*
|
|
91
|
+
* _required `providePublic(classInstance)`_
|
|
92
|
+
* @param args - publish args
|
|
93
|
+
*/
|
|
94
|
+
export declare function publicProperty(args: IPublishPropertyArgs): PropertyDecorator;
|
|
95
|
+
/**
|
|
96
|
+
* Binds all public entities of a class to a specific instance.
|
|
97
|
+
*
|
|
98
|
+
* _only for class instance_
|
|
99
|
+
*/
|
|
100
|
+
export declare function providePublic<InstanceType>(instance: InstanceType): InstanceType;
|
|
101
|
+
/**
|
|
102
|
+
* Makes the function available for **renderer** and **preload** processes.
|
|
103
|
+
*
|
|
104
|
+
* _only for function_
|
|
105
|
+
* @param name - name by which the function will be accessed
|
|
106
|
+
* @param func - linked function
|
|
107
|
+
* @param scopes - function scopes
|
|
108
|
+
*/
|
|
109
|
+
export declare function publicFunction(name: string, func: PublicFunction, scopes?: Scope[]): void;
|
|
110
|
+
/**
|
|
111
|
+
* Makes the function available for **renderer** and **preload** processes.
|
|
112
|
+
*
|
|
113
|
+
* _only for variables_
|
|
114
|
+
* @param name - name by which the variable will be accessed
|
|
115
|
+
* @param value - variable getter/setter
|
|
116
|
+
* @param scopes - variable scopes
|
|
117
|
+
*/
|
|
118
|
+
export declare function publicVariable(name: string, value: PublicProperty, scopes?: Scope[]): void;
|