emr-bridge 1.6.1 → 1.7.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/cjs/errors.d.ts +28 -0
- package/cjs/errors.js +39 -0
- package/cjs/exp.d.ts +2 -2
- package/cjs/exp.js +8 -8
- package/cjs/index.d.ts +2 -2
- package/cjs/index.js +7 -7
- package/cjs/main.d.ts +2 -2
- package/cjs/main.js +7 -7
- package/cjs/preload.d.ts +3 -3
- package/cjs/preload.js +4 -4
- package/cjs/renderer.d.ts +2 -2
- package/cjs/renderer.js +2 -2
- package/cjs/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
- package/cjs/scripts/bridge.js +57 -0
- package/{esm/scripts/Main.d.ts → cjs/scripts/main.d.ts} +2 -2
- package/cjs/scripts/main.js +72 -0
- package/cjs/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
- package/cjs/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
- package/cjs/scripts/preload-provider.js +96 -0
- package/cjs/scripts/provider.d.ts +25 -3
- package/cjs/scripts/provider.js +96 -92
- package/cjs/scripts/publish.js +21 -18
- package/cjs/scripts/renderer-tunnels.d.ts +47 -0
- package/cjs/scripts/renderer-tunnels.js +115 -0
- package/cjs/types.d.ts +32 -39
- package/esm/errors.d.ts +28 -0
- package/esm/errors.js +27 -0
- package/esm/exp.d.ts +2 -2
- package/esm/exp.js +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.js +1 -1
- package/esm/main.d.ts +2 -2
- package/esm/main.js +1 -1
- package/esm/preload.d.ts +3 -3
- package/esm/preload.js +2 -2
- package/esm/renderer.d.ts +2 -2
- package/esm/renderer.js +1 -1
- package/esm/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
- package/esm/scripts/bridge.js +52 -0
- package/{cjs/scripts/Main.d.ts → esm/scripts/main.d.ts} +2 -2
- package/esm/scripts/main.js +67 -0
- package/esm/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
- package/esm/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
- package/esm/scripts/preload-provider.js +91 -0
- package/esm/scripts/provider.d.ts +25 -3
- package/esm/scripts/provider.js +95 -92
- package/esm/scripts/publish.js +21 -18
- package/esm/scripts/renderer-tunnels.d.ts +47 -0
- package/esm/scripts/renderer-tunnels.js +109 -0
- package/esm/types.d.ts +32 -39
- package/package.json +1 -1
- package/cjs/scripts/Bridge.js +0 -58
- package/cjs/scripts/Main.js +0 -85
- package/cjs/scripts/preloadProvider.js +0 -92
- package/esm/scripts/Bridge.js +0 -53
- package/esm/scripts/Main.js +0 -80
- package/esm/scripts/preloadProvider.js +0 -87
- /package/cjs/scripts/{newDecorators.js → new-decorators.js} +0 -0
- /package/cjs/scripts/{oldDecorators.js → old-decorators.js} +0 -0
- /package/cjs/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
- /package/esm/scripts/{newDecorators.js → new-decorators.js} +0 -0
- /package/esm/scripts/{oldDecorators.js → old-decorators.js} +0 -0
- /package/esm/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
package/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/new-decorators';
|
|
3
3
|
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
|
4
|
-
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber,
|
|
4
|
+
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, MainEvent, PublicFunction, PublicProperty, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs, RendererEvent } from './types';
|
package/esm/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/new-decorators';
|
|
3
3
|
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
package/esm/main.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/new-decorators';
|
|
3
3
|
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
|
4
|
-
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber,
|
|
4
|
+
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, MainEvent, PublicFunction, PublicProperty, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs, RendererEvent } from './types';
|
package/esm/main.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { Access, Scope } from './enums';
|
|
2
|
-
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/
|
|
2
|
+
export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/new-decorators';
|
|
3
3
|
export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
|
package/esm/preload.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { default as Main } from './scripts/
|
|
2
|
-
export { default as provideFromMain } from './scripts/
|
|
3
|
-
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber,
|
|
1
|
+
export { default as Main } from './scripts/main';
|
|
2
|
+
export { default as provideFromMain } from './scripts/preload-provider';
|
|
3
|
+
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, MainEvent, PublicFunction, PublicProperty, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs, RendererEvent } from './types';
|
package/esm/preload.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { default as Main } from './scripts/
|
|
2
|
-
export { default as provideFromMain } from './scripts/
|
|
1
|
+
export { default as Main } from './scripts/main';
|
|
2
|
+
export { default as provideFromMain } from './scripts/preload-provider';
|
package/esm/renderer.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { default as Bridge } from './scripts/
|
|
2
|
-
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber,
|
|
1
|
+
export { default as Bridge } from './scripts/bridge';
|
|
2
|
+
export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, MainEvent, PublicFunction, PublicProperty, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs, RendererEvent } from './types';
|
package/esm/renderer.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default as Bridge } from './scripts/
|
|
1
|
+
export { default as Bridge } from './scripts/bridge';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Target } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Bridge between `renderer` and `main` processes.
|
|
4
4
|
*
|
|
5
5
|
* _only for renderer process_
|
|
6
6
|
*/
|
|
7
|
-
declare let Bridge:
|
|
7
|
+
declare let Bridge: Target;
|
|
8
8
|
export default Bridge;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Scope } from '../enums';
|
|
2
|
+
import { GlobalProviderNotFoundError, TryUseBridgeInPreload } from '../errors';
|
|
3
|
+
import Provider from './provider';
|
|
4
|
+
/**
|
|
5
|
+
* Bridge between `renderer` and `main` processes.
|
|
6
|
+
*
|
|
7
|
+
* _only for renderer process_
|
|
8
|
+
*/
|
|
9
|
+
let Bridge = new Proxy({ as: () => throwError() }, {
|
|
10
|
+
get: () => throwError(),
|
|
11
|
+
set: () => throwError(),
|
|
12
|
+
apply: () => throwError()
|
|
13
|
+
});
|
|
14
|
+
class ProviderIntoRenderer extends Provider {
|
|
15
|
+
#globalProvider = window.__provider__;
|
|
16
|
+
constructor() { super(); this.init(); }
|
|
17
|
+
info = this.#globalProvider.getInfo();
|
|
18
|
+
scope = Scope.renderer;
|
|
19
|
+
callFunction(name, secret, args) {
|
|
20
|
+
return this.#globalProvider.provided.functions[name](secret, args);
|
|
21
|
+
}
|
|
22
|
+
getVariable(name) {
|
|
23
|
+
return this.#globalProvider.provided.properties[name].get();
|
|
24
|
+
}
|
|
25
|
+
setVariable(name, value) {
|
|
26
|
+
return this.#globalProvider.provided.properties[name].set(value);
|
|
27
|
+
}
|
|
28
|
+
waitPromise(channel, secret) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
this.#globalProvider.waitPromise(channel, resolve, reject, secret);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
handleMainEvent(name, type, handler) {
|
|
34
|
+
return this.#globalProvider.provided.mainEvents[name](type, handler);
|
|
35
|
+
}
|
|
36
|
+
emitRendererEvent(name, arg) {
|
|
37
|
+
this.#globalProvider.provided.rendererEvents[name](arg);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
41
|
+
let throwError;
|
|
42
|
+
if (typeof window === 'undefined') {
|
|
43
|
+
throwError = () => { throw new TryUseBridgeInPreload(); };
|
|
44
|
+
}
|
|
45
|
+
else if (!window.__provider__) {
|
|
46
|
+
throwError = () => { throw new GlobalProviderNotFoundError(); };
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Выполняется только в web
|
|
50
|
+
Bridge = new ProviderIntoRenderer();
|
|
51
|
+
}
|
|
52
|
+
export default Bridge;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import electron from 'electron';
|
|
2
|
+
import { IPCChannel, Scope } from '../enums';
|
|
3
|
+
import { NotProvidedFromMain, TryUseMainInMain, TryUseMainInRenderer } from '../errors';
|
|
4
|
+
import Provider from './provider';
|
|
5
|
+
import IPCTunnel from './renderer-tunnels';
|
|
6
|
+
/**
|
|
7
|
+
* Bridge between `preload` and `main` processes.
|
|
8
|
+
*
|
|
9
|
+
* _only for preload process_
|
|
10
|
+
*/
|
|
11
|
+
let Main = new Proxy({ as() { throwError(); } }, {
|
|
12
|
+
get() { throwError(); },
|
|
13
|
+
set() { throwError(); },
|
|
14
|
+
apply() { throwError(); }
|
|
15
|
+
});
|
|
16
|
+
class ProviderIntoPreload extends Provider {
|
|
17
|
+
#ipcRenderer = electron.ipcRenderer;
|
|
18
|
+
info = this.#ipcRenderer.sendSync(IPCChannel.getPublicInfo);
|
|
19
|
+
scope = Scope.preload;
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
if (!this.info)
|
|
23
|
+
throw new NotProvidedFromMain();
|
|
24
|
+
}
|
|
25
|
+
callFunction(name, secret, args) {
|
|
26
|
+
return new IPCTunnel.Function(name).call({ args, secret });
|
|
27
|
+
}
|
|
28
|
+
getVariable(name) {
|
|
29
|
+
return new IPCTunnel.Property(name).get();
|
|
30
|
+
}
|
|
31
|
+
setVariable(name, value) {
|
|
32
|
+
return new IPCTunnel.Property(name).set(value);
|
|
33
|
+
}
|
|
34
|
+
async waitPromise(channel, secret) {
|
|
35
|
+
return await new IPCTunnel.Promise(channel).wait(secret);
|
|
36
|
+
}
|
|
37
|
+
handleMainEvent(name, type, handler) {
|
|
38
|
+
const tunnel = new IPCTunnel.MainEvent(name);
|
|
39
|
+
if (type === 'on')
|
|
40
|
+
return tunnel.on(handler);
|
|
41
|
+
else
|
|
42
|
+
return tunnel.once(handler);
|
|
43
|
+
}
|
|
44
|
+
emitRendererEvent(name, arg) {
|
|
45
|
+
const tunnel = new IPCTunnel.RendererEvent(name);
|
|
46
|
+
if (arg instanceof Promise) {
|
|
47
|
+
arg.then(value => tunnel.emit(value))
|
|
48
|
+
.catch(reason => tunnel.error(String(reason)));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
tunnel.emit(arg);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
56
|
+
let throwError;
|
|
57
|
+
if (typeof window !== 'undefined') {
|
|
58
|
+
throwError = () => { throw new TryUseMainInRenderer(); };
|
|
59
|
+
}
|
|
60
|
+
else if (!('ipcRenderer' in electron)) {
|
|
61
|
+
throwError = () => { throw new TryUseMainInMain(); };
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// Выполняется только в NodeJS
|
|
65
|
+
Main = new ProviderIntoPreload();
|
|
66
|
+
}
|
|
67
|
+
export default Main;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, EventReceiver,
|
|
1
|
+
import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, EventReceiver, PublishGetterArgs, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs, PublishSetterArgs } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Makes the method available for **renderer** and **preload** processes.
|
|
4
4
|
*
|
|
@@ -20,7 +20,7 @@ export declare function publicMethod(name: string): ClassMethodDecorator;
|
|
|
20
20
|
*
|
|
21
21
|
* @param args - publish args
|
|
22
22
|
*/
|
|
23
|
-
export declare function publicMethod(args:
|
|
23
|
+
export declare function publicMethod(args: PublishMethodArgs): ClassMethodDecorator;
|
|
24
24
|
/**
|
|
25
25
|
* Makes the event available for **renderer** and **preload** processes.
|
|
26
26
|
*
|
|
@@ -42,7 +42,7 @@ export declare function publicClassMainEvent(name: string): ClassMethodDecorator
|
|
|
42
42
|
*
|
|
43
43
|
* @param args - publish args
|
|
44
44
|
*/
|
|
45
|
-
export declare function publicClassMainEvent(args:
|
|
45
|
+
export declare function publicClassMainEvent(args: PublishMainEventArgs): ClassMethodDecorator;
|
|
46
46
|
/**
|
|
47
47
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
48
48
|
*
|
|
@@ -81,7 +81,7 @@ export declare function publicClassRendererEvent(name: string, receiver: EventRe
|
|
|
81
81
|
*
|
|
82
82
|
* @param args - publish args
|
|
83
83
|
*/
|
|
84
|
-
export declare function publicClassRendererEvent(args:
|
|
84
|
+
export declare function publicClassRendererEvent(args: PublishRendererEventArgs): ClassPropertyDecorator;
|
|
85
85
|
/**
|
|
86
86
|
* Makes the property available for **renderer** and **preload** processes.
|
|
87
87
|
*
|
|
@@ -103,7 +103,7 @@ export declare function publicProperty(name: string): ClassPropertyDecorator;
|
|
|
103
103
|
*
|
|
104
104
|
* @param args - publish args
|
|
105
105
|
*/
|
|
106
|
-
export declare function publicProperty(args:
|
|
106
|
+
export declare function publicProperty(args: PublishPropertyArgs): ClassPropertyDecorator;
|
|
107
107
|
/**
|
|
108
108
|
* Makes the property getter available for **renderer** and **preload** processes.
|
|
109
109
|
*
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EventReceiver,
|
|
1
|
+
import type { EventReceiver, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Makes the method available for **renderer** and **preload** processes.
|
|
4
4
|
*
|
|
@@ -23,7 +23,7 @@ export declare function publicStaticMethod(name: string): MethodDecorator;
|
|
|
23
23
|
*
|
|
24
24
|
* @param args - publish args
|
|
25
25
|
*/
|
|
26
|
-
export declare function publicStaticMethod(args:
|
|
26
|
+
export declare function publicStaticMethod(args: PublishMethodArgs): MethodDecorator;
|
|
27
27
|
/**
|
|
28
28
|
* Makes the method available for **renderer** and **preload** processes.
|
|
29
29
|
*
|
|
@@ -51,7 +51,7 @@ export declare function publicMethod(name: string): MethodDecorator;
|
|
|
51
51
|
*
|
|
52
52
|
* @param args - publish args
|
|
53
53
|
*/
|
|
54
|
-
export declare function publicMethod(args:
|
|
54
|
+
export declare function publicMethod(args: PublishMethodArgs): MethodDecorator;
|
|
55
55
|
/**
|
|
56
56
|
* Makes the event available for **renderer** and **preload** processes.
|
|
57
57
|
*
|
|
@@ -79,7 +79,7 @@ export declare function publicClassMainEvent(name: string): MethodDecorator;
|
|
|
79
79
|
*
|
|
80
80
|
* @param args - publish args
|
|
81
81
|
*/
|
|
82
|
-
export declare function publicClassMainEvent(args:
|
|
82
|
+
export declare function publicClassMainEvent(args: PublishMainEventArgs): MethodDecorator;
|
|
83
83
|
/**
|
|
84
84
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
85
85
|
*
|
|
@@ -123,7 +123,7 @@ export declare function publicClassRendererEvent(name: string, receiver: EventRe
|
|
|
123
123
|
*
|
|
124
124
|
* @param args - publish args
|
|
125
125
|
*/
|
|
126
|
-
export declare function publicClassRendererEvent(args:
|
|
126
|
+
export declare function publicClassRendererEvent(args: PublishRendererEventArgs): PropertyDecorator;
|
|
127
127
|
/**
|
|
128
128
|
* Makes the property available for **renderer** and **preload** processes.
|
|
129
129
|
*
|
|
@@ -148,7 +148,7 @@ export declare function publicStaticProperty(name: string): PropertyDecorator;
|
|
|
148
148
|
*
|
|
149
149
|
* @param args - publish args
|
|
150
150
|
*/
|
|
151
|
-
export declare function publicStaticProperty(args:
|
|
151
|
+
export declare function publicStaticProperty(args: PublishPropertyArgs): PropertyDecorator;
|
|
152
152
|
/**
|
|
153
153
|
* Makes the property available for **renderer** and **preload** processes.
|
|
154
154
|
*
|
|
@@ -176,7 +176,7 @@ export declare function publicProperty(name: string): PropertyDecorator;
|
|
|
176
176
|
*
|
|
177
177
|
* @param args - publish args
|
|
178
178
|
*/
|
|
179
|
-
export declare function publicProperty(args:
|
|
179
|
+
export declare function publicProperty(args: PublishPropertyArgs): PropertyDecorator;
|
|
180
180
|
/**
|
|
181
181
|
* Binds all public entities of a class to a specific instance.
|
|
182
182
|
*
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import electron from 'electron';
|
|
2
|
+
import { IPCChannel } from '../enums';
|
|
3
|
+
import IPCTunnel from './renderer-tunnels';
|
|
4
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
5
|
+
let mayThrowError = () => { };
|
|
6
|
+
if (typeof window === 'undefined') {
|
|
7
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in main process.'); };
|
|
8
|
+
}
|
|
9
|
+
else if (!('contextBridge' in electron)) {
|
|
10
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in renderer process.'); };
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Sets access to main from renderer
|
|
14
|
+
*
|
|
15
|
+
* @param contextIsolation - _default_: `true`
|
|
16
|
+
*/
|
|
17
|
+
function provideFromMain(contextIsolation = true) {
|
|
18
|
+
mayThrowError();
|
|
19
|
+
const { contextBridge, ipcRenderer } = electron;
|
|
20
|
+
const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
|
|
21
|
+
const provider = {
|
|
22
|
+
getInfo: () => info,
|
|
23
|
+
waitPromise(channel, resolve, reject, secret) {
|
|
24
|
+
new IPCTunnel.Promise(channel).wait(secret)
|
|
25
|
+
.then(resolve)
|
|
26
|
+
.catch(reject);
|
|
27
|
+
},
|
|
28
|
+
provided: {
|
|
29
|
+
functions: getFunctions(),
|
|
30
|
+
properties: getProperties(),
|
|
31
|
+
mainEvents: getMainEvents(),
|
|
32
|
+
rendererEvents: getRendererEvents(),
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
if (contextIsolation) {
|
|
36
|
+
contextBridge.exposeInMainWorld('__provider__', provider);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
window.__provider__ = provider;
|
|
40
|
+
}
|
|
41
|
+
function getFunctions() {
|
|
42
|
+
const result = {};
|
|
43
|
+
for (const name of info.functions) {
|
|
44
|
+
result[name] = (secret, args) => {
|
|
45
|
+
return new IPCTunnel.Function(name).call({ args, secret });
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
function getProperties() {
|
|
51
|
+
const result = {};
|
|
52
|
+
for (const name of info.properties) {
|
|
53
|
+
const tunnel = new IPCTunnel.Property(name);
|
|
54
|
+
result[name] = {
|
|
55
|
+
get: () => tunnel.get(),
|
|
56
|
+
set: (value) => tunnel.set(value)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
function getMainEvents() {
|
|
62
|
+
const result = {};
|
|
63
|
+
for (const name of info.mainEvents) {
|
|
64
|
+
result[name] = (type, handler) => {
|
|
65
|
+
const tunnel = new IPCTunnel.MainEvent(name);
|
|
66
|
+
if (type === 'on')
|
|
67
|
+
return tunnel.on(handler);
|
|
68
|
+
else
|
|
69
|
+
return tunnel.once(handler);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
function getRendererEvents() {
|
|
75
|
+
const result = {};
|
|
76
|
+
for (const name of info.rendererEvents) {
|
|
77
|
+
result[name] = (arg) => {
|
|
78
|
+
const tunnel = new IPCTunnel.RendererEvent(name);
|
|
79
|
+
if (arg instanceof Promise) {
|
|
80
|
+
arg.then(value => tunnel.emit(value))
|
|
81
|
+
.catch(reason => tunnel.error(String(reason)));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
tunnel.emit(arg);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export default provideFromMain;
|
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
export default
|
|
1
|
+
import { Scope } from '../enums';
|
|
2
|
+
import type { EventHandler, EventUnsubscriber, IPCResult, Info, Target } from '../types';
|
|
3
|
+
export default abstract class Provider implements Target {
|
|
4
|
+
protected abstract info: Info;
|
|
5
|
+
protected abstract scope: Scope;
|
|
6
|
+
protected abstract getVariable(name: string): IPCResult;
|
|
7
|
+
protected abstract setVariable(name: string, value: any): IPCResult;
|
|
8
|
+
protected abstract callFunction(name: string, secret: number, args: any[]): IPCResult;
|
|
9
|
+
protected abstract waitPromise(channel: string, secret?: number): Promise<any>;
|
|
10
|
+
protected abstract emitRendererEvent(name: string, arg: any): void;
|
|
11
|
+
protected abstract handleMainEvent(name: string, type: 'on' | 'once', handler: EventHandler<IPCResult>): EventUnsubscriber;
|
|
12
|
+
as<T>(): T;
|
|
13
|
+
protected readonly target: any;
|
|
14
|
+
protected init(): void;
|
|
15
|
+
protected errorHandler(error: string, channel: string): never;
|
|
16
|
+
protected initFunctions(): void;
|
|
17
|
+
protected initRendererEvents(): void;
|
|
18
|
+
protected initMainEvents(): void;
|
|
19
|
+
protected initProperties(): void;
|
|
20
|
+
protected define(name: string, attrs?: {
|
|
21
|
+
get?: () => any;
|
|
22
|
+
set?: (v: any) => void;
|
|
23
|
+
value?: any;
|
|
24
|
+
}): void;
|
|
25
|
+
}
|
package/esm/scripts/provider.js
CHANGED
|
@@ -1,110 +1,113 @@
|
|
|
1
1
|
import { Access } from '../enums';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
import { NoGetAccessError, NoSetAccessError, NotProvidedIntoScopeError, TrySetReadonlyError } from '../errors';
|
|
3
|
+
export default class Provider {
|
|
4
|
+
as() { return this.target; }
|
|
5
|
+
target = {};
|
|
6
|
+
init() {
|
|
7
|
+
this.initFunctions();
|
|
8
|
+
this.initProperties();
|
|
9
|
+
this.initMainEvents();
|
|
10
|
+
this.initRendererEvents();
|
|
11
|
+
}
|
|
12
|
+
errorHandler(error, channel) {
|
|
13
|
+
throw new Error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
14
|
+
}
|
|
15
|
+
initFunctions() {
|
|
16
|
+
for (const name of this.info.functions) {
|
|
17
|
+
this.define(name, {
|
|
18
|
+
value: (...args) => {
|
|
19
|
+
const secret = Math.random();
|
|
20
|
+
const { error, promiseChannel, value } = this.callFunction(name, secret, args);
|
|
21
|
+
if (error) {
|
|
22
|
+
this.errorHandler(error, name);
|
|
23
|
+
}
|
|
24
|
+
if (promiseChannel)
|
|
25
|
+
return this.waitPromise(promiseChannel, secret);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
initRendererEvents() {
|
|
32
|
+
for (const name of this.info.rendererEvents) {
|
|
33
|
+
this.define(name, {
|
|
34
|
+
value: (arg) => {
|
|
35
|
+
this.emitRendererEvent(name, arg);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
initMainEvents() {
|
|
41
|
+
for (const name of this.info.mainEvents) {
|
|
42
|
+
const eventNameOn = `on${name[0].toUpperCase()}${name.slice(1)}`;
|
|
43
|
+
const eventNameOnce = `once${name[0].toUpperCase()}${name.slice(1)}`;
|
|
44
|
+
this.define(eventNameOn, {
|
|
45
|
+
value: (handler) => {
|
|
46
|
+
return this.handleMainEvent(name, 'on', ({ error, value }) => {
|
|
47
|
+
if (error) {
|
|
48
|
+
this.errorHandler(error, name);
|
|
49
|
+
}
|
|
50
|
+
handler(value);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
this.define(eventNameOnce, {
|
|
55
|
+
value: (handler) => {
|
|
56
|
+
return this.handleMainEvent(name, 'once', ({ error, value }) => {
|
|
57
|
+
if (error) {
|
|
58
|
+
this.errorHandler(error, name);
|
|
59
|
+
}
|
|
60
|
+
handler(value);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
initProperties() {
|
|
67
|
+
for (const name of this.info.properties) {
|
|
68
|
+
this.define(name, {
|
|
69
|
+
get: this.info.accesses.get(name).has(Access.get)
|
|
70
|
+
? () => {
|
|
71
|
+
const { error, value } = this.getVariable(name);
|
|
72
|
+
if (error) {
|
|
73
|
+
this.errorHandler(error, name);
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
: () => { throw new NoGetAccessError(name); },
|
|
78
|
+
set: this.info.accesses.get(name).has(Access.set)
|
|
79
|
+
? (value) => {
|
|
80
|
+
const { error } = this.setVariable(name, value);
|
|
81
|
+
if (error) {
|
|
82
|
+
this.errorHandler(error, name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
: () => { throw new NoSetAccessError(name); }
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
define(name, attrs) {
|
|
11
90
|
let descriptor;
|
|
12
|
-
if (!info.scopes.get(name).has(scope)) {
|
|
91
|
+
if (!this.info.scopes.get(name).has(this.scope)) {
|
|
13
92
|
descriptor = {
|
|
14
|
-
get() { throw new
|
|
15
|
-
set() { throw new
|
|
93
|
+
get: () => { throw new NotProvidedIntoScopeError(name, this.scope); },
|
|
94
|
+
set: () => { throw new NotProvidedIntoScopeError(name, this.scope); }
|
|
16
95
|
};
|
|
17
96
|
}
|
|
18
97
|
else if (attrs?.value) {
|
|
19
98
|
descriptor = {
|
|
20
99
|
get: () => attrs.value,
|
|
21
|
-
set: () => { throw new
|
|
100
|
+
set: () => { throw new TrySetReadonlyError(name); },
|
|
22
101
|
enumerable: true
|
|
23
102
|
};
|
|
24
103
|
}
|
|
25
104
|
else {
|
|
26
105
|
descriptor = {
|
|
27
|
-
get: attrs?.get || (() => { throw new
|
|
28
|
-
set: attrs?.set || (() => { throw new
|
|
106
|
+
get: attrs?.get || (() => { throw new NoSetAccessError(name); }),
|
|
107
|
+
set: attrs?.set || (() => { throw new TrySetReadonlyError(name); }),
|
|
29
108
|
enumerable: true
|
|
30
109
|
};
|
|
31
110
|
}
|
|
32
|
-
Object.defineProperty(target, name, descriptor);
|
|
111
|
+
Object.defineProperty(this.target, name, descriptor);
|
|
33
112
|
}
|
|
34
|
-
info.functions.forEach(funcName => {
|
|
35
|
-
define(funcName, {
|
|
36
|
-
value(...args) {
|
|
37
|
-
const result = callFunction(funcName, ...args);
|
|
38
|
-
if (result.error) {
|
|
39
|
-
errorHandler(result.error, funcName);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (result.promiseChannel)
|
|
43
|
-
return waitPromise(result.promiseChannel);
|
|
44
|
-
return result.value;
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
info.rendererEvents.forEach(eventName => {
|
|
49
|
-
define(eventName, {
|
|
50
|
-
value(arg) {
|
|
51
|
-
emitRendererEvent(eventName, arg);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
info.mainEvents.forEach(mainEventName => {
|
|
56
|
-
const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
57
|
-
const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
58
|
-
define(eventNameOn, {
|
|
59
|
-
value(handler) {
|
|
60
|
-
return handleMainEvent(mainEventName, 'on', (result) => {
|
|
61
|
-
if (result.error) {
|
|
62
|
-
errorHandler(result.error, mainEventName);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (result.promiseChannel)
|
|
66
|
-
waitPromise(result.promiseChannel).then(handler);
|
|
67
|
-
handler(result.value);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
define(eventNameOnce, {
|
|
72
|
-
value(handler) {
|
|
73
|
-
return handleMainEvent(mainEventName, 'once', (result) => {
|
|
74
|
-
if (result.error) {
|
|
75
|
-
errorHandler(result.error, mainEventName);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
if (result.promiseChannel)
|
|
79
|
-
waitPromise(result.promiseChannel).then(handler);
|
|
80
|
-
handler(result.value);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
info.properties.forEach(propName => {
|
|
86
|
-
define(propName, {
|
|
87
|
-
get: info.accesses.get(propName).has(Access.get)
|
|
88
|
-
? () => {
|
|
89
|
-
const result = getVariable(propName);
|
|
90
|
-
if (result.error) {
|
|
91
|
-
errorHandler(result.error, propName);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
return result.value;
|
|
95
|
-
}
|
|
96
|
-
: () => { throw new Error(`No access to getting the '${propName}' property`); },
|
|
97
|
-
set: info.accesses.get(propName).has(Access.set)
|
|
98
|
-
? (value) => {
|
|
99
|
-
const result = setVariable(propName, value);
|
|
100
|
-
if (result?.error) {
|
|
101
|
-
errorHandler(result.error, propName);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
: () => { throw new Error(`No access to setting the '${propName}' property`); }
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
return target;
|
|
109
113
|
}
|
|
110
|
-
export default createProvider;
|