emr-bridge 1.2.2 → 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/cjs/enums.d.ts +3 -0
- package/cjs/enums.js +3 -0
- package/cjs/exp.d.ts +4 -0
- package/cjs/exp.js +18 -0
- package/cjs/index.d.ts +3 -4
- package/cjs/index.js +3 -7
- package/cjs/scripts/Bridge.js +3 -0
- package/cjs/scripts/Main.js +13 -0
- package/cjs/scripts/newDecorators.d.ts +21 -1
- package/cjs/scripts/newDecorators.js +21 -1
- package/cjs/scripts/oldDecorators.d.ts +59 -1
- package/cjs/scripts/oldDecorators.js +29 -1
- package/cjs/scripts/preloadProvider.js +20 -1
- package/cjs/scripts/provider.js +45 -1
- package/cjs/scripts/publish.d.ts +10 -1
- package/cjs/scripts/publish.js +60 -1
- package/cjs/types.d.ts +11 -0
- package/{lib → esm}/enums.d.ts +3 -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/{lib → esm}/scripts/Bridge.js +3 -0
- package/{lib → esm}/scripts/Main.js +13 -0
- package/{lib → esm}/scripts/newDecorators.d.ts +21 -1
- package/{lib → esm}/scripts/newDecorators.js +20 -1
- package/{lib → esm}/scripts/oldDecorators.d.ts +59 -1
- package/{lib → esm}/scripts/oldDecorators.js +27 -1
- package/{lib → esm}/scripts/preloadProvider.js +20 -1
- package/{lib → esm}/scripts/provider.js +45 -1
- package/{lib → esm}/scripts/publish.d.ts +10 -1
- package/{lib → esm}/scripts/publish.js +58 -0
- package/{lib → esm}/types.d.ts +11 -0
- package/package.json +29 -13
- package/lib/index.d.ts +0 -5
- package/lib/index.js +0 -4
- /package/{lib → esm}/preload.d.ts +0 -0
- /package/{lib → esm}/preload.js +0 -0
- /package/{lib → esm}/renderer.d.ts +0 -0
- /package/{lib → esm}/renderer.js +0 -0
- /package/{lib → esm}/scripts/Bridge.d.ts +0 -0
- /package/{lib → esm}/scripts/Main.d.ts +0 -0
- /package/{lib → esm}/scripts/preloadProvider.d.ts +0 -0
- /package/{lib → esm}/scripts/provider.d.ts +0 -0
- /package/{lib → esm}/types.js +0 -0
package/cjs/types.d.ts
CHANGED
|
@@ -18,6 +18,10 @@ export interface IPublishMethodArgs {
|
|
|
18
18
|
name?: string;
|
|
19
19
|
scope?: Scope;
|
|
20
20
|
}
|
|
21
|
+
export interface IPublishEventArgs {
|
|
22
|
+
name?: string;
|
|
23
|
+
scope?: Scope;
|
|
24
|
+
}
|
|
21
25
|
export interface IPublishPropertyArgs {
|
|
22
26
|
name?: string;
|
|
23
27
|
scope?: Scope;
|
|
@@ -26,6 +30,7 @@ export interface IPublishPropertyArgs {
|
|
|
26
30
|
export interface ICreateProviderArgs {
|
|
27
31
|
info: IInfo;
|
|
28
32
|
scope: Scope;
|
|
33
|
+
handleEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
|
|
29
34
|
callFunction(name: string, ...args: any[]): IIPCResult;
|
|
30
35
|
getVariable(name: string): IIPCResult;
|
|
31
36
|
setVariable(name: string, value: any): IIPCResult | undefined;
|
|
@@ -33,6 +38,10 @@ export interface ICreateProviderArgs {
|
|
|
33
38
|
}
|
|
34
39
|
export type Scopes = Record<string, Set<Scope>>;
|
|
35
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;
|
|
36
45
|
export interface IRendererPublic {
|
|
37
46
|
__register__?(instance: any): void;
|
|
38
47
|
}
|
|
@@ -41,6 +50,7 @@ export interface IProvider {
|
|
|
41
50
|
waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
|
|
42
51
|
provided: {
|
|
43
52
|
functions: Record<string, (...args: any[]) => IIPCResult>;
|
|
53
|
+
events: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
|
|
44
54
|
properties: Record<string, IIPCResult>;
|
|
45
55
|
};
|
|
46
56
|
}
|
|
@@ -52,6 +62,7 @@ export type PublicProperty = {
|
|
|
52
62
|
export interface IInfo {
|
|
53
63
|
properties: Set<string>;
|
|
54
64
|
functions: Set<string>;
|
|
65
|
+
events: Set<string>;
|
|
55
66
|
scopes: Scopes;
|
|
56
67
|
accesses: Accesses;
|
|
57
68
|
}
|
package/{lib → esm}/enums.d.ts
RENAMED
|
@@ -9,6 +9,9 @@ export declare enum Scope {
|
|
|
9
9
|
export declare enum IPCChannel {
|
|
10
10
|
getPublicInfo = "GET_PUBLIC_INFO",
|
|
11
11
|
functionCall = "FUNCTION_CALL_",
|
|
12
|
+
eventHandleOn = "EVENT_HANDLE_ON_",
|
|
13
|
+
eventHandleOnce = "EVENT_HANDLE_ONCE_",
|
|
14
|
+
eventEmit = "EVENT_EMIT_",
|
|
12
15
|
propertyGet = "PROPERTY_GET_",
|
|
13
16
|
propertySet = "PROPERTY_SET_",
|
|
14
17
|
promisePostfix = "_PROMISE"
|
package/{lib → esm}/enums.js
RENAMED
|
@@ -12,6 +12,9 @@ export var IPCChannel;
|
|
|
12
12
|
(function (IPCChannel) {
|
|
13
13
|
IPCChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
|
|
14
14
|
IPCChannel["functionCall"] = "FUNCTION_CALL_";
|
|
15
|
+
IPCChannel["eventHandleOn"] = "EVENT_HANDLE_ON_";
|
|
16
|
+
IPCChannel["eventHandleOnce"] = "EVENT_HANDLE_ONCE_";
|
|
17
|
+
IPCChannel["eventEmit"] = "EVENT_EMIT_";
|
|
15
18
|
IPCChannel["propertyGet"] = "PROPERTY_GET_";
|
|
16
19
|
IPCChannel["propertySet"] = "PROPERTY_SET_";
|
|
17
20
|
IPCChannel["promisePostfix"] = "_PROMISE";
|
package/esm/exp.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Access, Scope } from './enums';
|
|
2
|
+
export { providePublic, publicClassEvent, publicMethod, publicProperty, publicStaticEvent, publicStaticMethod, publicStaticProperty } from './scripts/oldDecorators';
|
|
3
|
+
export { publicEvent, publicFunction, publicVariable } from './scripts/publish';
|
|
4
|
+
export type { EventEmitter, EventHandler, EventUnsubscriber, IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs, MainEvent, PublicFunction, PublicProperty } from './types';
|
package/esm/exp.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { Access, Scope } from './enums';
|
|
2
|
+
export { providePublic, publicClassEvent, publicMethod, publicProperty, publicStaticEvent, publicStaticMethod, publicStaticProperty } from './scripts/oldDecorators';
|
|
3
|
+
export { publicEvent, publicFunction, publicVariable } from './scripts/publish';
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { Access, Scope } from './enums';
|
|
2
|
+
export { publicClassEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
|
+
export { publicEvent, publicFunction, publicVariable } from './scripts/publish';
|
|
4
|
+
export type { EventEmitter, EventHandler, EventUnsubscriber, IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs, MainEvent, PublicFunction, PublicProperty } from './types';
|
package/esm/index.js
ADDED
|
@@ -15,6 +15,9 @@ const Bridge = createProvider({
|
|
|
15
15
|
callFunction(name, ...args) {
|
|
16
16
|
return provider.provided.functions[name](...args);
|
|
17
17
|
},
|
|
18
|
+
handleEvent(name, type, handler) {
|
|
19
|
+
return provider.provided.events[name](type, handler);
|
|
20
|
+
},
|
|
18
21
|
getVariable(name) {
|
|
19
22
|
return provider.provided.properties[name];
|
|
20
23
|
},
|
|
@@ -15,6 +15,19 @@ const Main = createProvider({
|
|
|
15
15
|
callFunction(name, ...args) {
|
|
16
16
|
return ipcRenderer.sendSync(IPCChannel.functionCall + name, ...args);
|
|
17
17
|
},
|
|
18
|
+
handleEvent(name, type, handler) {
|
|
19
|
+
const listener = (_, result) => handler(result);
|
|
20
|
+
const emitChannel = IPCChannel.eventEmit + name;
|
|
21
|
+
if (type === 'on') {
|
|
22
|
+
ipcRenderer.send(IPCChannel.eventHandleOn + name);
|
|
23
|
+
ipcRenderer.on(emitChannel, listener);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
ipcRenderer.send(IPCChannel.eventHandleOnce + name);
|
|
27
|
+
ipcRenderer.once(emitChannel, listener);
|
|
28
|
+
}
|
|
29
|
+
return () => ipcRenderer.removeListener(emitChannel, listener);
|
|
30
|
+
},
|
|
18
31
|
getVariable(name) {
|
|
19
32
|
return ipcRenderer.sendSync(IPCChannel.propertyGet + name);
|
|
20
33
|
},
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, IPublishMethodArgs, IPublishPropertyArgs, PublishGetterArgs, PublishSetterArgs } from '../types';
|
|
1
|
+
import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs, PublishGetterArgs, PublishSetterArgs } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Makes the method available for **renderer** and **preload** processes.
|
|
4
4
|
*
|
|
@@ -19,6 +19,26 @@ export declare function publicMethod(name: string): ClassMethodDecorator;
|
|
|
19
19
|
* @param args - publish args
|
|
20
20
|
*/
|
|
21
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;
|
|
22
42
|
/**
|
|
23
43
|
* Makes the property available for **renderer** and **preload** processes.
|
|
24
44
|
*
|
|
@@ -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);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IPublishMethodArgs, IPublishPropertyArgs } from '../types';
|
|
1
|
+
import type { IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Makes the method available for **renderer** and **preload** processes.
|
|
4
4
|
*
|
|
@@ -57,6 +57,64 @@ export declare function publicMethod(name: string): MethodDecorator;
|
|
|
57
57
|
* @param args - publish args
|
|
58
58
|
*/
|
|
59
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;
|
|
60
118
|
/**
|
|
61
119
|
* Makes the property available for **renderer** and **preload** processes.
|
|
62
120
|
*
|
|
@@ -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;
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { Access, Scope } from '../enums';
|
|
2
|
-
import type { PublicFunction, PublicProperty } from '../types';
|
|
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;
|
|
3
12
|
/**
|
|
4
13
|
* Makes the function available for **renderer** and **preload** processes.
|
|
5
14
|
*
|
|
@@ -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/{lib → esm}/types.d.ts
RENAMED
|
@@ -18,6 +18,10 @@ export interface IPublishMethodArgs {
|
|
|
18
18
|
name?: string;
|
|
19
19
|
scope?: Scope;
|
|
20
20
|
}
|
|
21
|
+
export interface IPublishEventArgs {
|
|
22
|
+
name?: string;
|
|
23
|
+
scope?: Scope;
|
|
24
|
+
}
|
|
21
25
|
export interface IPublishPropertyArgs {
|
|
22
26
|
name?: string;
|
|
23
27
|
scope?: Scope;
|
|
@@ -26,6 +30,7 @@ export interface IPublishPropertyArgs {
|
|
|
26
30
|
export interface ICreateProviderArgs {
|
|
27
31
|
info: IInfo;
|
|
28
32
|
scope: Scope;
|
|
33
|
+
handleEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
|
|
29
34
|
callFunction(name: string, ...args: any[]): IIPCResult;
|
|
30
35
|
getVariable(name: string): IIPCResult;
|
|
31
36
|
setVariable(name: string, value: any): IIPCResult | undefined;
|
|
@@ -33,6 +38,10 @@ export interface ICreateProviderArgs {
|
|
|
33
38
|
}
|
|
34
39
|
export type Scopes = Record<string, Set<Scope>>;
|
|
35
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;
|
|
36
45
|
export interface IRendererPublic {
|
|
37
46
|
__register__?(instance: any): void;
|
|
38
47
|
}
|
|
@@ -41,6 +50,7 @@ export interface IProvider {
|
|
|
41
50
|
waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
|
|
42
51
|
provided: {
|
|
43
52
|
functions: Record<string, (...args: any[]) => IIPCResult>;
|
|
53
|
+
events: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
|
|
44
54
|
properties: Record<string, IIPCResult>;
|
|
45
55
|
};
|
|
46
56
|
}
|
|
@@ -52,6 +62,7 @@ export type PublicProperty = {
|
|
|
52
62
|
export interface IInfo {
|
|
53
63
|
properties: Set<string>;
|
|
54
64
|
functions: Set<string>;
|
|
65
|
+
events: Set<string>;
|
|
55
66
|
scopes: Scopes;
|
|
56
67
|
accesses: Accesses;
|
|
57
68
|
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "emr-bridge",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Allows you to easily give access to main methods from the renderer process",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "esm/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"compile": "tsc --project ./tsconfig.json && tsc --project ./tsconfig.cjs.json"
|
|
8
8
|
},
|
|
9
|
-
"types": "
|
|
9
|
+
"types": "esm/index.d.ts",
|
|
10
10
|
"files": [
|
|
11
|
-
"
|
|
11
|
+
"esm",
|
|
12
12
|
"cjs"
|
|
13
13
|
],
|
|
14
14
|
"typesVersions": {
|
|
15
15
|
"*": {
|
|
16
16
|
".": [
|
|
17
|
-
"./
|
|
17
|
+
"./esm/index.d.ts"
|
|
18
18
|
],
|
|
19
19
|
"renderer": [
|
|
20
|
-
"./
|
|
20
|
+
"./esm/renderer.d.ts"
|
|
21
21
|
],
|
|
22
22
|
"preload": [
|
|
23
|
-
"./
|
|
23
|
+
"./esm/preload.d.ts"
|
|
24
|
+
],
|
|
25
|
+
"exp": [
|
|
26
|
+
"./esm/exp.d.ts"
|
|
24
27
|
],
|
|
25
28
|
"main": [
|
|
26
|
-
"./
|
|
29
|
+
"./esm/index.d.ts"
|
|
30
|
+
],
|
|
31
|
+
"main/exp": [
|
|
32
|
+
"./esm/exp.d.ts"
|
|
27
33
|
],
|
|
28
34
|
"cjs": [
|
|
29
35
|
"./cjs/index.d.ts"
|
|
@@ -34,21 +40,31 @@
|
|
|
34
40
|
"cjs/preload": [
|
|
35
41
|
"./cjs/preload.d.ts"
|
|
36
42
|
],
|
|
43
|
+
"cjs/exp": [
|
|
44
|
+
"./cjs/exp.d.ts"
|
|
45
|
+
],
|
|
37
46
|
"cjs/main": [
|
|
38
47
|
"./cjs/index.d.ts"
|
|
48
|
+
],
|
|
49
|
+
"cjs/main/exp": [
|
|
50
|
+
"./cjs/exp.d.ts"
|
|
39
51
|
]
|
|
40
52
|
}
|
|
41
53
|
},
|
|
42
54
|
"exports": {
|
|
43
|
-
".": "./
|
|
55
|
+
".": "./esm/index.js",
|
|
56
|
+
"./renderer": "./esm/renderer.js",
|
|
57
|
+
"./preload": "./esm/preload.js",
|
|
58
|
+
"./exp": "./esm/exp.js",
|
|
59
|
+
"./main": "./esm/index.js",
|
|
60
|
+
"./main/exp": "./esm/exp.js",
|
|
61
|
+
"./package.json": "./package.json",
|
|
44
62
|
"./cjs": "./cjs/index.js",
|
|
63
|
+
"./cjs/exp": "./cjs/exp.js",
|
|
45
64
|
"./cjs/renderer": "./cjs/renderer.js",
|
|
46
65
|
"./cjs/preload": "./cjs/preload.js",
|
|
47
66
|
"./cjs/main": "./cjs/index.js",
|
|
48
|
-
"./
|
|
49
|
-
"./preload": "./lib/preload.js",
|
|
50
|
-
"./main": "./lib/index.js",
|
|
51
|
-
"./package.json": "./package.json"
|
|
67
|
+
"./cjs/main/exp": "./cjs/exp.js"
|
|
52
68
|
},
|
|
53
69
|
"repository": {
|
|
54
70
|
"type": "git",
|
package/lib/index.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export { Access, Scope } from './enums';
|
|
2
|
-
export { publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
|
-
export { providePublic, publicMethod as publicMethodExp, publicProperty as publicPropertyExp, publicStaticMethod as publicStaticMethodExp, publicStaticProperty as publicStaticPropertyExp } from './scripts/oldDecorators';
|
|
4
|
-
export { publicFunction, publicVariable } from './scripts/publish';
|
|
5
|
-
export type { IPublishMethodArgs, IPublishPropertyArgs, PublicFunction, PublicProperty } from './types';
|
package/lib/index.js
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { Access, Scope } from './enums';
|
|
2
|
-
export { publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
|
|
3
|
-
export { providePublic, publicMethod as publicMethodExp, publicProperty as publicPropertyExp, publicStaticMethod as publicStaticMethodExp, publicStaticProperty as publicStaticPropertyExp } from './scripts/oldDecorators';
|
|
4
|
-
export { publicFunction, publicVariable } from './scripts/publish';
|
|
File without changes
|
/package/{lib → esm}/preload.js
RENAMED
|
File without changes
|
|
File without changes
|
/package/{lib → esm}/renderer.js
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/{lib → esm}/types.js
RENAMED
|
File without changes
|