emr-bridge 1.4.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -20
- package/cjs/enums.js +3 -3
- package/cjs/exp.d.ts +1 -1
- package/cjs/exp.js +1 -3
- package/cjs/index.d.ts +2 -0
- package/cjs/index.js +16 -0
- package/cjs/main.d.ts +4 -0
- package/cjs/main.js +18 -0
- package/cjs/scripts/Bridge.d.ts +2 -1
- package/cjs/scripts/Bridge.js +44 -28
- package/cjs/scripts/Main.d.ts +2 -1
- package/cjs/scripts/Main.js +71 -52
- package/cjs/scripts/newDecorators.d.ts +13 -6
- package/cjs/scripts/newDecorators.js +3 -3
- package/cjs/scripts/oldDecorators.d.ts +13 -117
- package/cjs/scripts/oldDecorators.js +30 -42
- package/cjs/scripts/preloadProvider.js +29 -16
- package/cjs/scripts/provider.js +86 -133
- package/cjs/scripts/publish.d.ts +9 -5
- package/cjs/scripts/publish.js +59 -33
- package/cjs/types.d.ts +10 -10
- package/esm/exp.d.ts +1 -1
- package/esm/exp.js +1 -1
- package/esm/index.d.ts +2 -0
- package/esm/index.js +2 -0
- package/esm/main.d.ts +4 -0
- package/esm/main.js +3 -0
- package/esm/scripts/Bridge.d.ts +2 -1
- package/esm/scripts/Bridge.js +44 -28
- package/esm/scripts/Main.d.ts +2 -1
- package/esm/scripts/Main.js +71 -52
- package/esm/scripts/newDecorators.d.ts +13 -6
- package/esm/scripts/newDecorators.js +3 -3
- package/esm/scripts/oldDecorators.d.ts +13 -117
- package/esm/scripts/oldDecorators.js +29 -39
- package/esm/scripts/preloadProvider.js +13 -3
- package/esm/scripts/provider.js +87 -134
- package/esm/scripts/publish.d.ts +9 -5
- package/esm/scripts/publish.js +44 -21
- package/esm/types.d.ts +10 -10
- package/package.json +3 -35
|
@@ -25,51 +25,25 @@ export function publicMethod(arg) {
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
export function publicStaticMainEvent(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] = publicMainEvent(name, target[key].bind(target));
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
28
|
export function publicClassMainEvent(arg) {
|
|
45
29
|
return (tgt, key, _) => {
|
|
46
30
|
const target = tgt;
|
|
47
31
|
const prevRegister = target.__register__ ?? (() => { });
|
|
48
32
|
target.__register__ = instance => {
|
|
49
33
|
prevRegister(instance);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
name = arg;
|
|
34
|
+
let name = String(key);
|
|
35
|
+
if (arg) {
|
|
36
|
+
if (typeof arg === 'string') {
|
|
37
|
+
name = arg;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
name = arg.name ?? String(key);
|
|
41
|
+
if (arg.scope)
|
|
42
|
+
addScope(name, arg.scope);
|
|
43
|
+
}
|
|
61
44
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
receiver = arg.receiver ?? receiver;
|
|
65
|
-
if (arg.scope)
|
|
66
|
-
addScope(name, arg.scope);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
receiver = arg;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
target[key] = publicRendererEvent(name, receiver);
|
|
45
|
+
instance[key] = publicMainEvent(name, instance[key].bind(instance));
|
|
46
|
+
};
|
|
73
47
|
};
|
|
74
48
|
}
|
|
75
49
|
export function publicClassRendererEvent(arg) {
|
|
@@ -78,7 +52,23 @@ export function publicClassRendererEvent(arg) {
|
|
|
78
52
|
const prevRegister = target.__register__ ?? (() => { });
|
|
79
53
|
target.__register__ = instance => {
|
|
80
54
|
prevRegister(instance);
|
|
81
|
-
|
|
55
|
+
let name = String(key);
|
|
56
|
+
let receiver = (v) => v;
|
|
57
|
+
if (arg) {
|
|
58
|
+
if (typeof arg === 'string') {
|
|
59
|
+
name = arg;
|
|
60
|
+
}
|
|
61
|
+
else if (typeof arg === 'object') {
|
|
62
|
+
name = arg.name ?? String(key);
|
|
63
|
+
receiver = arg.procFn ?? receiver;
|
|
64
|
+
if (arg.scope)
|
|
65
|
+
addScope(name, arg.scope);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
receiver = arg;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
instance[key] = publicRendererEvent(name, receiver);
|
|
82
72
|
};
|
|
83
73
|
};
|
|
84
74
|
}
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Electron from 'electron';
|
|
2
2
|
import { IPCChannel } from '../enums';
|
|
3
|
-
|
|
3
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
4
|
+
let mayThrowError = () => { };
|
|
5
|
+
if (typeof window === 'undefined') {
|
|
6
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in main process.'); };
|
|
7
|
+
}
|
|
8
|
+
else if (!Electron.contextBridge) {
|
|
9
|
+
mayThrowError = () => { throw new Error('"provideFromMain" is unavailable in renderer process.'); };
|
|
10
|
+
}
|
|
4
11
|
/**
|
|
5
12
|
* Sets access to main from renderer
|
|
6
13
|
*
|
|
7
14
|
* @param contextIsolation - _default_: `true`
|
|
8
15
|
*/
|
|
9
16
|
function provideFromMain(contextIsolation = true) {
|
|
17
|
+
mayThrowError();
|
|
18
|
+
const { contextBridge, ipcRenderer } = Electron;
|
|
19
|
+
const info = ipcRenderer.sendSync(IPCChannel.getPublicInfo);
|
|
10
20
|
const provider = {
|
|
11
21
|
getInfo() {
|
|
12
22
|
return info;
|
|
@@ -23,7 +33,7 @@ function provideFromMain(contextIsolation = true) {
|
|
|
23
33
|
properties: {},
|
|
24
34
|
functions: {},
|
|
25
35
|
mainEvents: {},
|
|
26
|
-
rendererEvents: {}
|
|
36
|
+
rendererEvents: {},
|
|
27
37
|
}
|
|
28
38
|
};
|
|
29
39
|
info.functions.forEach(funcName => {
|
package/esm/scripts/provider.js
CHANGED
|
@@ -1,157 +1,110 @@
|
|
|
1
|
-
import { Access
|
|
1
|
+
import { Access } from '../enums';
|
|
2
2
|
function errorHandler(error, channel) {
|
|
3
3
|
console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
|
|
4
4
|
}
|
|
5
5
|
function createProvider(args) {
|
|
6
6
|
const { info, scope, getVariable, setVariable, callFunction, handleMainEvent, waitPromise, emitRendererEvent } = args;
|
|
7
7
|
const target = {
|
|
8
|
-
as: () =>
|
|
8
|
+
as: () => target
|
|
9
9
|
};
|
|
10
|
+
function define(name, attrs) {
|
|
11
|
+
let descriptor;
|
|
12
|
+
if (!info.scopes.get(name).has(scope)) {
|
|
13
|
+
descriptor = {
|
|
14
|
+
get() { throw new Error(`'${name}' is not provided into ${scope} scope`); },
|
|
15
|
+
set() { throw new Error(`'${name}' is not provided into ${scope} scope`); }
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
else if (attrs?.value) {
|
|
19
|
+
descriptor = {
|
|
20
|
+
get: () => attrs.value,
|
|
21
|
+
set: () => { throw new Error(`'${name}' is readonly`); },
|
|
22
|
+
enumerable: true
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
descriptor = {
|
|
27
|
+
get: attrs?.get || (() => { throw new Error(`No access to getting '${name}'`); }),
|
|
28
|
+
set: attrs?.set || (() => { throw new Error(`'${name}' is readonly`); }),
|
|
29
|
+
enumerable: true
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
Object.defineProperty(target, name, descriptor);
|
|
33
|
+
}
|
|
10
34
|
info.functions.forEach(funcName => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
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
|
+
}
|
|
30
46
|
});
|
|
31
47
|
});
|
|
32
|
-
info.rendererEvents.forEach(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return (arg) => emitRendererEvent(rendererEventName, arg);
|
|
38
|
-
},
|
|
39
|
-
set() {
|
|
40
|
-
throw new Error(`Function '${rendererEventName}' is readonly`);
|
|
41
|
-
},
|
|
42
|
-
enumerable: true
|
|
48
|
+
info.rendererEvents.forEach(eventName => {
|
|
49
|
+
define(eventName, {
|
|
50
|
+
value(arg) {
|
|
51
|
+
emitRendererEvent(eventName, arg);
|
|
52
|
+
}
|
|
43
53
|
});
|
|
44
54
|
});
|
|
45
55
|
info.mainEvents.forEach(mainEventName => {
|
|
46
|
-
if (!info.scopes[mainEventName].has(scope))
|
|
47
|
-
return;
|
|
48
56
|
const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
49
57
|
const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return (
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
};
|
|
63
|
-
},
|
|
64
|
-
set() {
|
|
65
|
-
throw new Error(`Function '${eventNameOn}' is readonly`);
|
|
66
|
-
},
|
|
67
|
-
enumerable: true
|
|
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
|
+
}
|
|
68
70
|
});
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
};
|
|
82
|
-
},
|
|
83
|
-
set() {
|
|
84
|
-
throw new Error(`Function '${eventNameOnce}' is readonly`);
|
|
85
|
-
},
|
|
86
|
-
enumerable: true
|
|
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
|
+
}
|
|
87
83
|
});
|
|
88
84
|
});
|
|
89
85
|
info.properties.forEach(propName => {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
setter = () => { throw new Error(`No access to setting the '${propName}' property`); };
|
|
111
|
-
Object.defineProperty(target, propName, {
|
|
112
|
-
get: getter,
|
|
113
|
-
set: setter,
|
|
114
|
-
enumerable: true
|
|
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`); }
|
|
115
106
|
});
|
|
116
107
|
});
|
|
117
|
-
|
|
118
|
-
get(target, key) {
|
|
119
|
-
if (key in target)
|
|
120
|
-
return target[key];
|
|
121
|
-
const isFunction = info.functions.has(key);
|
|
122
|
-
const isProperty = info.properties.has(key);
|
|
123
|
-
if (!isFunction && !isProperty)
|
|
124
|
-
throw new Error(`'${key}' is not provided from main`);
|
|
125
|
-
if (!info.scopes[key].has(Scope.renderer))
|
|
126
|
-
throw new Error(`'${key}' is not provided into renderer scope`);
|
|
127
|
-
if (isProperty && !info.accesses[key].has(Access.get))
|
|
128
|
-
throw new Error(`No access to getting the '${key}' property`);
|
|
129
|
-
throw new Error(`Unknown error on get '${key}'`);
|
|
130
|
-
},
|
|
131
|
-
set(target, key, newValue) {
|
|
132
|
-
if (key in target) {
|
|
133
|
-
target[key] = newValue;
|
|
134
|
-
return true;
|
|
135
|
-
}
|
|
136
|
-
const isFunction = info.functions.has(key);
|
|
137
|
-
const isProperty = info.properties.has(key);
|
|
138
|
-
if (isFunction)
|
|
139
|
-
throw new Error(`Function '${key}' is readonly`);
|
|
140
|
-
if (!isProperty)
|
|
141
|
-
throw new Error(`Property '${key}' is not provided from main`);
|
|
142
|
-
if (!info.scopes[key].has(Scope.renderer))
|
|
143
|
-
throw new Error(`'${key}' is not provided into renderer scope`);
|
|
144
|
-
if (!info.accesses[key].has(Access.set))
|
|
145
|
-
throw new Error(`No access to setting the '${key}' property`);
|
|
146
|
-
throw new Error(`Unknown error on set '${key}'`);
|
|
147
|
-
},
|
|
148
|
-
ownKeys(target) {
|
|
149
|
-
return Object.keys(target);
|
|
150
|
-
},
|
|
151
|
-
has(target, key) {
|
|
152
|
-
return key in target;
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
return proxy;
|
|
108
|
+
return target;
|
|
156
109
|
}
|
|
157
110
|
export default createProvider;
|
package/esm/scripts/publish.d.ts
CHANGED
|
@@ -1,36 +1,40 @@
|
|
|
1
1
|
import { Access, Scope } from '../enums';
|
|
2
|
-
import type {
|
|
2
|
+
import type { PublicFunction, PublicProperty, RendererEvent } from '../types';
|
|
3
3
|
/**
|
|
4
4
|
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
5
5
|
*
|
|
6
6
|
* @param name - name by which the event will be accessed
|
|
7
|
+
* @param procFn - function of processing before sending the event
|
|
7
8
|
* @param scopes - event scopes
|
|
8
9
|
*/
|
|
9
|
-
export declare function publicMainEvent<
|
|
10
|
+
export declare function publicMainEvent<In = void, Out = In extends Array<any> ? In[0] : In, ProcFn extends (...args: any[]) => any = In extends Array<any> ? (...args: In) => Out : (val: In) => Out>(name: string, procFn?: ProcFn, scopes?: Scope[]): (...args: Parameters<ProcFn>) => void;
|
|
10
11
|
/**
|
|
11
12
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
12
13
|
*
|
|
13
14
|
* @param name - name by which the event will be accessed
|
|
15
|
+
* @param procFn - processing function before calling handlers
|
|
14
16
|
* @param scopes - event scopes
|
|
15
17
|
*/
|
|
16
|
-
export declare function publicRendererEvent<
|
|
18
|
+
export declare function publicRendererEvent<In = void, Out = In, ProcFn extends (...args: any[]) => any = (v?: In) => Out>(name: string, procFn?: ProcFn, scopes?: Scope[]): RendererEvent<Out>;
|
|
17
19
|
/**
|
|
18
20
|
* Makes the function available for **renderer** and **preload** processes.
|
|
19
21
|
*
|
|
20
22
|
* _only for function_
|
|
23
|
+
*
|
|
21
24
|
* @param name - name by which the function will be accessed
|
|
22
25
|
* @param func - linked function
|
|
23
26
|
* @param scopes - function scopes
|
|
24
27
|
*/
|
|
25
|
-
export declare function publicFunction(name: string, func:
|
|
28
|
+
export declare function publicFunction<F extends PublicFunction>(name: string, func: F, scopes?: Scope[]): void;
|
|
26
29
|
/**
|
|
27
30
|
* Makes the function available for **renderer** and **preload** processes.
|
|
28
31
|
*
|
|
29
32
|
* _only for variables_
|
|
33
|
+
*
|
|
30
34
|
* @param name - name by which the variable will be accessed
|
|
31
35
|
* @param value - variable getter/setter
|
|
32
36
|
* @param scopes - variable scopes
|
|
33
37
|
*/
|
|
34
|
-
export declare function publicVariable(name: string, value: PublicProperty
|
|
38
|
+
export declare function publicVariable<T>(name: string, value: PublicProperty<T>, scopes?: Scope[]): void;
|
|
35
39
|
export declare function addScope(key: string, scope: Scope): void;
|
|
36
40
|
export declare function addAccess(key: string, access: Access): void;
|
package/esm/scripts/publish.js
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Electron from 'electron';
|
|
2
2
|
import { Access, IPCChannel, Scope } from '../enums';
|
|
3
3
|
const publicInfo = {
|
|
4
4
|
properties: new Set(),
|
|
5
5
|
functions: new Set(),
|
|
6
6
|
mainEvents: new Set(),
|
|
7
7
|
rendererEvents: new Set(),
|
|
8
|
-
accesses:
|
|
9
|
-
scopes:
|
|
8
|
+
accesses: new Map(),
|
|
9
|
+
scopes: new Map()
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
/** Выбрасывает ошибку если используется не в том процессе */
|
|
12
|
+
let mayThrowError = () => { };
|
|
13
|
+
if (typeof window !== 'undefined' || !Electron.ipcMain) {
|
|
14
|
+
mayThrowError = () => { throw new Error('Publish methods is available only in main process.'); };
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
Electron.ipcMain.on(IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
|
|
18
|
+
}
|
|
12
19
|
/**
|
|
13
20
|
* Makes the event from **main** available for **renderer** and **preload** processes.
|
|
14
21
|
*
|
|
15
22
|
* @param name - name by which the event will be accessed
|
|
23
|
+
* @param procFn - function of processing before sending the event
|
|
16
24
|
* @param scopes - event scopes
|
|
17
25
|
*/
|
|
18
|
-
export function publicMainEvent(name,
|
|
26
|
+
export function publicMainEvent(name, procFn = ((...args) => args[0]), scopes = [Scope.preload, Scope.renderer]) {
|
|
27
|
+
mayThrowError();
|
|
28
|
+
const { ipcMain } = Electron;
|
|
19
29
|
const { mainEvents } = publicInfo;
|
|
20
30
|
const mainEmitChannel = IPCChannel.mainEventEmit + name;
|
|
21
31
|
const mainHandleChannel = IPCChannel.mainEventOn + name;
|
|
@@ -23,8 +33,12 @@ export function publicMainEvent(name, receiver = ((v = undefined) => v), scopes
|
|
|
23
33
|
let senders = [];
|
|
24
34
|
let sendersOnce = [];
|
|
25
35
|
mainEvents.add(name);
|
|
26
|
-
|
|
27
|
-
|
|
36
|
+
const rendererNameOn = `on${name[0].toUpperCase()}${name.slice(1)}`;
|
|
37
|
+
const rendererNameOnce = `once${name[0].toUpperCase()}${name.slice(1)}`;
|
|
38
|
+
if (!publicInfo.scopes.has(rendererNameOn)) {
|
|
39
|
+
publicInfo.scopes.set(rendererNameOn, new Set(scopes));
|
|
40
|
+
publicInfo.scopes.set(rendererNameOnce, new Set(scopes));
|
|
41
|
+
}
|
|
28
42
|
ipcMain.removeAllListeners(mainHandleChannel);
|
|
29
43
|
ipcMain.removeAllListeners(mainHandleChannelOnce);
|
|
30
44
|
ipcMain.on(mainHandleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
|
|
@@ -37,7 +51,7 @@ export function publicMainEvent(name, receiver = ((v = undefined) => v), scopes
|
|
|
37
51
|
filterSenders();
|
|
38
52
|
const allSenders = [...senders, ...sendersOnce];
|
|
39
53
|
try {
|
|
40
|
-
const result =
|
|
54
|
+
const result = procFn(...args);
|
|
41
55
|
if (result instanceof Promise) {
|
|
42
56
|
result
|
|
43
57
|
.then(value => {
|
|
@@ -66,9 +80,12 @@ export function publicMainEvent(name, receiver = ((v = undefined) => v), scopes
|
|
|
66
80
|
* Makes the event from **renderer** and **preload** available for **main** process.
|
|
67
81
|
*
|
|
68
82
|
* @param name - name by which the event will be accessed
|
|
83
|
+
* @param procFn - processing function before calling handlers
|
|
69
84
|
* @param scopes - event scopes
|
|
70
85
|
*/
|
|
71
|
-
export function publicRendererEvent(name,
|
|
86
|
+
export function publicRendererEvent(name, procFn = ((v) => v), scopes = [Scope.preload, Scope.renderer]) {
|
|
87
|
+
mayThrowError();
|
|
88
|
+
const { ipcMain } = Electron;
|
|
72
89
|
if (name.startsWith('on')) {
|
|
73
90
|
name = name.replace('on', '');
|
|
74
91
|
name = `${name[0].toLowerCase()}${name.slice(1)}`;
|
|
@@ -78,13 +95,13 @@ export function publicRendererEvent(name, receiver = ((v = undefined) => v), sco
|
|
|
78
95
|
const handlers = new Set();
|
|
79
96
|
const handlersOnce = new Set();
|
|
80
97
|
rendererEvents.add(name);
|
|
81
|
-
if (!publicInfo.scopes
|
|
82
|
-
publicInfo.scopes
|
|
98
|
+
if (!publicInfo.scopes.has(name))
|
|
99
|
+
publicInfo.scopes.set(name, new Set(scopes));
|
|
83
100
|
ipcMain.removeAllListeners(rendererEmitChannel);
|
|
84
101
|
ipcMain.on(rendererEmitChannel, (_, result) => {
|
|
85
102
|
if (result.error)
|
|
86
103
|
throw new Error(result.error);
|
|
87
|
-
const converted =
|
|
104
|
+
const converted = procFn(result.value);
|
|
88
105
|
handlers.forEach(handler => handler(converted));
|
|
89
106
|
handlersOnce.forEach(handler => handler(converted));
|
|
90
107
|
handlersOnce.clear();
|
|
@@ -104,16 +121,19 @@ export function publicRendererEvent(name, receiver = ((v = undefined) => v), sco
|
|
|
104
121
|
* Makes the function available for **renderer** and **preload** processes.
|
|
105
122
|
*
|
|
106
123
|
* _only for function_
|
|
124
|
+
*
|
|
107
125
|
* @param name - name by which the function will be accessed
|
|
108
126
|
* @param func - linked function
|
|
109
127
|
* @param scopes - function scopes
|
|
110
128
|
*/
|
|
111
129
|
export function publicFunction(name, func, scopes = [Scope.preload, Scope.renderer]) {
|
|
130
|
+
mayThrowError();
|
|
131
|
+
const { ipcMain } = Electron;
|
|
112
132
|
const { functions } = publicInfo;
|
|
113
133
|
const channel = IPCChannel.functionCall + name;
|
|
114
134
|
functions.add(name);
|
|
115
|
-
if (!publicInfo.scopes
|
|
116
|
-
publicInfo.scopes
|
|
135
|
+
if (!publicInfo.scopes.has(name))
|
|
136
|
+
publicInfo.scopes.set(name, new Set(scopes));
|
|
117
137
|
ipcMain.removeAllListeners(channel);
|
|
118
138
|
ipcMain.on(channel, (e, ...args) => {
|
|
119
139
|
try {
|
|
@@ -138,26 +158,29 @@ export function publicFunction(name, func, scopes = [Scope.preload, Scope.render
|
|
|
138
158
|
* Makes the function available for **renderer** and **preload** processes.
|
|
139
159
|
*
|
|
140
160
|
* _only for variables_
|
|
161
|
+
*
|
|
141
162
|
* @param name - name by which the variable will be accessed
|
|
142
163
|
* @param value - variable getter/setter
|
|
143
164
|
* @param scopes - variable scopes
|
|
144
165
|
*/
|
|
145
166
|
export function publicVariable(name, value, scopes = [Scope.renderer, Scope.preload]) {
|
|
167
|
+
mayThrowError();
|
|
168
|
+
const { ipcMain } = Electron;
|
|
146
169
|
const { properties } = publicInfo;
|
|
147
170
|
const getChannel = IPCChannel.propertyGet + name;
|
|
148
171
|
const setChannel = IPCChannel.propertySet + name;
|
|
149
172
|
const { get: getter, set: setter } = value;
|
|
150
173
|
properties.add(name);
|
|
151
|
-
if (!publicInfo.scopes
|
|
152
|
-
publicInfo.scopes
|
|
153
|
-
if (!publicInfo.accesses
|
|
174
|
+
if (!publicInfo.scopes.has(name))
|
|
175
|
+
publicInfo.scopes.set(name, new Set(scopes));
|
|
176
|
+
if (!publicInfo.accesses.has(name)) {
|
|
154
177
|
const accesses = [];
|
|
155
178
|
getter && accesses.push(Access.get);
|
|
156
179
|
setter && accesses.push(Access.set);
|
|
157
|
-
publicInfo.accesses
|
|
180
|
+
publicInfo.accesses.set(name, new Set(accesses));
|
|
158
181
|
}
|
|
159
182
|
else {
|
|
160
|
-
const accesses = publicInfo.accesses
|
|
183
|
+
const accesses = publicInfo.accesses.get(name);
|
|
161
184
|
getter && accesses.add(Access.get);
|
|
162
185
|
setter && accesses.add(Access.set);
|
|
163
186
|
}
|
|
@@ -187,8 +210,8 @@ export function publicVariable(name, value, scopes = [Scope.renderer, Scope.prel
|
|
|
187
210
|
}
|
|
188
211
|
}
|
|
189
212
|
export function addScope(key, scope) {
|
|
190
|
-
publicInfo.scopes
|
|
213
|
+
publicInfo.scopes.set(key, new Set([scope]));
|
|
191
214
|
}
|
|
192
215
|
export function addAccess(key, access) {
|
|
193
|
-
publicInfo.accesses
|
|
216
|
+
publicInfo.accesses.set(key, new Set([access]));
|
|
194
217
|
}
|
package/esm/types.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export interface IPublishMainEventArgs {
|
|
|
25
25
|
export interface IPublishRendererEventArgs {
|
|
26
26
|
name?: string;
|
|
27
27
|
scope?: Scope;
|
|
28
|
-
|
|
28
|
+
procFn?: EventReceiver;
|
|
29
29
|
}
|
|
30
30
|
export interface IPublishPropertyArgs {
|
|
31
31
|
name?: string;
|
|
@@ -42,14 +42,14 @@ export interface ICreateProviderArgs {
|
|
|
42
42
|
setVariable(name: string, value: any): IIPCResult | undefined;
|
|
43
43
|
waitPromise(channel: string): Promise<any>;
|
|
44
44
|
}
|
|
45
|
-
export type Scopes =
|
|
46
|
-
export type Accesses =
|
|
47
|
-
export type RendererEvent<T =
|
|
48
|
-
export type MainEvent<T =
|
|
45
|
+
export type Scopes = Map<string, Set<Scope>>;
|
|
46
|
+
export type Accesses = Map<string, Set<Access>>;
|
|
47
|
+
export type RendererEvent<T = void> = (handler: EventHandler<T>, isOnce?: boolean) => EventUnsubscriber;
|
|
48
|
+
export type MainEvent<T = void> = (handler: EventHandler<T>) => EventUnsubscriber;
|
|
49
49
|
export type EventEmitter = (...args: any[]) => any;
|
|
50
|
-
export type EventReceiver<I =
|
|
50
|
+
export type EventReceiver<I = void, O = I> = (input: I) => O;
|
|
51
51
|
export type EventUnsubscriber = () => void;
|
|
52
|
-
export type EventHandler<T> = (result: T) => void;
|
|
52
|
+
export type EventHandler<T = void> = (result: T) => void;
|
|
53
53
|
export interface IRendererPublic {
|
|
54
54
|
__register__?(instance: any): void;
|
|
55
55
|
}
|
|
@@ -67,9 +67,9 @@ export interface IProvider {
|
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
69
|
export type PublicFunction = (...args: any[]) => any;
|
|
70
|
-
export type PublicProperty = {
|
|
71
|
-
get?():
|
|
72
|
-
set?(value:
|
|
70
|
+
export type PublicProperty<T> = {
|
|
71
|
+
get?(): T;
|
|
72
|
+
set?(value: T): void;
|
|
73
73
|
};
|
|
74
74
|
export interface IInfo {
|
|
75
75
|
properties: Set<string>;
|