emr-bridge 1.3.0 → 1.4.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.
@@ -15,19 +15,32 @@ const Main = createProvider({
15
15
  callFunction(name, ...args) {
16
16
  return ipcRenderer.sendSync(IPCChannel.functionCall + name, ...args);
17
17
  },
18
- handleEvent(name, type, handler) {
18
+ handleMainEvent(name, type, handler) {
19
19
  const listener = (_, result) => handler(result);
20
- const emitChannel = IPCChannel.eventEmit + name;
20
+ const emitChannel = IPCChannel.mainEventEmit + name;
21
21
  if (type === 'on') {
22
- ipcRenderer.send(IPCChannel.eventHandleOn + name);
22
+ ipcRenderer.send(IPCChannel.mainEventOn + name);
23
23
  ipcRenderer.on(emitChannel, listener);
24
24
  }
25
25
  else {
26
- ipcRenderer.send(IPCChannel.eventHandleOnce + name);
26
+ ipcRenderer.send(IPCChannel.mainEventOnce + name);
27
27
  ipcRenderer.once(emitChannel, listener);
28
28
  }
29
29
  return () => ipcRenderer.removeListener(emitChannel, listener);
30
30
  },
31
+ emitRendererEvent(name, arg) {
32
+ const emitChannel = IPCChannel.rendererEventEmit + name;
33
+ if (arg instanceof Promise) {
34
+ const promiseChannel = emitChannel + IPCChannel.promisePostfix;
35
+ ipcRenderer.sendSync(emitChannel, { promiseChannel });
36
+ arg
37
+ .then(value => ipcRenderer.send(promiseChannel, { value }))
38
+ .catch(reason => ipcRenderer.send(promiseChannel, { error: String(reason) }));
39
+ }
40
+ else {
41
+ ipcRenderer.send(emitChannel, { value: arg });
42
+ }
43
+ },
31
44
  getVariable(name) {
32
45
  return ipcRenderer.sendSync(IPCChannel.propertyGet + name);
33
46
  },
@@ -1,4 +1,4 @@
1
- import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs, PublishGetterArgs, PublishSetterArgs } from '../types';
1
+ import type { ClassGetterDecorator, ClassMethodDecorator, ClassPropertyDecorator, ClassSetterDecorator, EventReceiver, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs, PublishGetterArgs, PublishSetterArgs } from '../types';
2
2
  /**
3
3
  * Makes the method available for **renderer** and **preload** processes.
4
4
  *
@@ -24,21 +24,63 @@ export declare function publicMethod(args: IPublishMethodArgs): ClassMethodDecor
24
24
  *
25
25
  * _only new decorators_
26
26
  */
27
- export declare function publicClassEvent(): ClassMethodDecorator;
27
+ export declare function publicClassMainEvent(): ClassMethodDecorator;
28
28
  /**
29
29
  * Makes the event available for **renderer** and **preload** processes.
30
30
  *
31
31
  * _only new decorators_
32
32
  * @param name - name by which the event will be accessed
33
33
  */
34
- export declare function publicClassEvent(name: string): ClassMethodDecorator;
34
+ export declare function publicClassMainEvent(name: string): ClassMethodDecorator;
35
35
  /**
36
36
  * Makes the event available for **renderer** and **preload** processes.
37
37
  *
38
38
  * _only new decorators_
39
39
  * @param args - publish args
40
40
  */
41
- export declare function publicClassEvent(args: IPublishEventArgs): ClassMethodDecorator;
41
+ export declare function publicClassMainEvent(args: IPublishMainEventArgs): ClassMethodDecorator;
42
+ /**
43
+ * Makes the event from **renderer** and **preload** available for **main** process.
44
+ *
45
+ * _only new decorators_
46
+ */
47
+ export declare function publicClassRendererEvent(): ClassPropertyDecorator;
48
+ /**
49
+ * Makes the event from **renderer** and **preload** available for **main** process.
50
+ *
51
+ * _only new decorators_
52
+ * @param name - name by which the event will be accessed
53
+ */
54
+ export declare function publicClassRendererEvent(name: string): ClassPropertyDecorator;
55
+ /**
56
+ * Makes the event from **renderer** and **preload** available for **main** process.
57
+ *
58
+ * _only new decorators_
59
+ * @param receiver - linked receiver
60
+ */
61
+ export declare function publicClassRendererEvent(receiver: EventReceiver): ClassPropertyDecorator;
62
+ /**
63
+ * Makes the event from **renderer** and **preload** available for **main** process.
64
+ *
65
+ *
66
+ * @param name - name by which the event will be accessed
67
+ * @param receiver - linked receiver
68
+ */
69
+ export declare function publicClassRendererEvent(name: string, receiver: EventReceiver): ClassPropertyDecorator;
70
+ /**
71
+ * Makes the event from **renderer** and **preload** available for **main** process.
72
+ *
73
+ * _only new decorators_
74
+ * @param args - publish args
75
+ */
76
+ export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): ClassPropertyDecorator;
77
+ /**
78
+ * Makes the event from **renderer** and **preload** available for **main** process.
79
+ *
80
+ * _only new decorators_
81
+ * @param args - publish args
82
+ */
83
+ export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): ClassPropertyDecorator;
42
84
  /**
43
85
  * Makes the property available for **renderer** and **preload** processes.
44
86
  *
@@ -1,4 +1,4 @@
1
- import { addAccess, addScope, publicEvent, publicFunction, publicVariable } from './publish';
1
+ import { addAccess, addScope, publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './publish';
2
2
  export function publicMethod(arg) {
3
3
  return (method, context) => {
4
4
  let name = String(context.name);
@@ -17,7 +17,7 @@ export function publicMethod(arg) {
17
17
  });
18
18
  };
19
19
  }
20
- export function publicClassEvent(arg) {
20
+ export function publicClassMainEvent(arg) {
21
21
  return (method, context) => {
22
22
  let name = String(context.name);
23
23
  if (arg) {
@@ -32,14 +32,38 @@ export function publicClassEvent(arg) {
32
32
  }
33
33
  context.addInitializer(function () {
34
34
  // @ts-ignore
35
- this[name] = publicEvent(name, method.bind(this));
35
+ this[context.name] = publicMainEvent(name, method.bind(this));
36
+ });
37
+ };
38
+ }
39
+ export function publicClassRendererEvent(arg) {
40
+ return (_, context) => {
41
+ let name = String(context.name);
42
+ let receiver = (v) => v;
43
+ if (arg) {
44
+ if (typeof arg === 'string') {
45
+ name = arg;
46
+ }
47
+ else if (typeof arg === 'object') {
48
+ name = arg.name ?? String(context.name);
49
+ receiver = arg.receiver ?? receiver;
50
+ if (arg.scope)
51
+ addScope(name, arg.scope);
52
+ }
53
+ else {
54
+ receiver = arg;
55
+ }
56
+ }
57
+ context.addInitializer(function () {
58
+ // @ts-ignore
59
+ this[context.name] = publicRendererEvent(name, receiver);
36
60
  });
37
61
  };
38
62
  }
39
63
  export function publicProperty(arg) {
40
64
  return (_, context) => {
41
- const propName = String(context.name);
42
- let name = propName;
65
+ const key = String(context.name);
66
+ let name = key;
43
67
  if (arg) {
44
68
  if (typeof arg === 'string') {
45
69
  name = arg;
@@ -56,11 +80,11 @@ export function publicProperty(arg) {
56
80
  publicVariable(name, {
57
81
  get: () => {
58
82
  // @ts-ignore
59
- return this[propName];
83
+ return this[key];
60
84
  },
61
85
  set: (value) => {
62
86
  // @ts-ignore
63
- this[propName] = value;
87
+ this[key] = value;
64
88
  }
65
89
  });
66
90
  });
@@ -1,4 +1,4 @@
1
- import type { IPublishEventArgs, IPublishMethodArgs, IPublishPropertyArgs } from '../types';
1
+ import type { EventReceiver, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs } from '../types';
2
2
  /**
3
3
  * Makes the method available for **renderer** and **preload** processes.
4
4
  *
@@ -64,7 +64,7 @@ export declare function publicMethod(args: IPublishMethodArgs): MethodDecorator;
64
64
  *
65
65
  * _experimental decorators_
66
66
  */
67
- export declare function publicStaticEvent(): MethodDecorator;
67
+ export declare function publicStaticMainEvent(): MethodDecorator;
68
68
  /**
69
69
  * Makes the event available for **renderer** and **preload** processes.
70
70
  *
@@ -73,7 +73,7 @@ export declare function publicStaticEvent(): MethodDecorator;
73
73
  * _experimental decorators_
74
74
  * @param name - name by which the event will be accessed
75
75
  */
76
- export declare function publicStaticEvent(name: string): MethodDecorator;
76
+ export declare function publicStaticMainEvent(name: string): MethodDecorator;
77
77
  /**
78
78
  * Makes the event available for **renderer** and **preload** processes.
79
79
  *
@@ -82,7 +82,7 @@ export declare function publicStaticEvent(name: string): MethodDecorator;
82
82
  * _experimental decorators_
83
83
  * @param args - publish args
84
84
  */
85
- export declare function publicStaticEvent(args: IPublishEventArgs): MethodDecorator;
85
+ export declare function publicStaticMainEvent(args: IPublishMainEventArgs): MethodDecorator;
86
86
  /**
87
87
  * Makes the event available for **renderer** and **preload** processes.
88
88
  *
@@ -92,7 +92,7 @@ export declare function publicStaticEvent(args: IPublishEventArgs): MethodDecora
92
92
  *
93
93
  * _required `providePublic(classInstance)`_
94
94
  */
95
- export declare function publicClassEvent(): MethodDecorator;
95
+ export declare function publicClassMainEvent(): MethodDecorator;
96
96
  /**
97
97
  * Makes the event available for **renderer** and **preload** processes.
98
98
  *
@@ -103,7 +103,7 @@ export declare function publicClassEvent(): MethodDecorator;
103
103
  * _required `providePublic(classInstance)`_
104
104
  * @param name - name by which the event will be accessed
105
105
  */
106
- export declare function publicClassEvent(name: string): MethodDecorator;
106
+ export declare function publicClassMainEvent(name: string): MethodDecorator;
107
107
  /**
108
108
  * Makes the event available for **renderer** and **preload** processes.
109
109
  *
@@ -114,7 +114,115 @@ export declare function publicClassEvent(name: string): MethodDecorator;
114
114
  * _required `providePublic(classInstance)`_
115
115
  * @param args - publish args
116
116
  */
117
- export declare function publicClassEvent(args: IPublishEventArgs): MethodDecorator;
117
+ export declare function publicClassMainEvent(args: IPublishMainEventArgs): MethodDecorator;
118
+ /**
119
+ * Makes the event from **renderer** and **preload** available for **main** process.
120
+ *
121
+ * _only for static_
122
+ *
123
+ * _experimental decorators_
124
+ */
125
+ export declare function publicStaticRendererEvent(): PropertyDecorator;
126
+ /**
127
+ * Makes the event from **renderer** and **preload** available for **main** process.
128
+ *
129
+ * _only for static_
130
+ *
131
+ * _experimental decorators_
132
+ * @param name - name by which the event will be accessed
133
+ */
134
+ export declare function publicStaticRendererEvent(name: string): PropertyDecorator;
135
+ /**
136
+ * Makes the event from **renderer** and **preload** available for **main** process.
137
+ *
138
+ * _only for static_
139
+ *
140
+ * _experimental decorators_
141
+ * @param receiver - linked receiver
142
+ */
143
+ export declare function publicStaticRendererEvent(receiver: EventReceiver): PropertyDecorator;
144
+ /**
145
+ * Makes the event from **renderer** and **preload** available for **main** process.
146
+ *
147
+ * _only for static_
148
+ *
149
+ * _experimental decorators_
150
+ * @param name - name by which the event will be accessed
151
+ * @param receiver - linked receiver
152
+ */
153
+ export declare function publicStaticRendererEvent(name: string, receiver: EventReceiver): PropertyDecorator;
154
+ /**
155
+ * Makes the event from **renderer** and **preload** available for **main** process.
156
+ *
157
+ * _only for static_
158
+ *
159
+ * _experimental decorators_
160
+ * @param args - publish args
161
+ */
162
+ export declare function publicStaticRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
163
+ /**
164
+ * Makes the event from **renderer** and **preload** available for **main** process.
165
+ *
166
+ * _only for static_
167
+ *
168
+ * _experimental decorators_
169
+ * @param args - publish args
170
+ */
171
+ export declare function publicStaticRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
172
+ /**
173
+ * Makes the event from **renderer** and **preload** available for **main** process.
174
+ *
175
+ * _only for non-static_
176
+ *
177
+ * _experimental decorators_
178
+ */
179
+ export declare function publicClassRendererEvent(): PropertyDecorator;
180
+ /**
181
+ * Makes the event from **renderer** and **preload** available for **main** process.
182
+ *
183
+ * _only for non-static_
184
+ *
185
+ * _experimental decorators_
186
+ * @param name - name by which the event will be accessed
187
+ */
188
+ export declare function publicClassRendererEvent(name: string): PropertyDecorator;
189
+ /**
190
+ * Makes the event from **renderer** and **preload** available for **main** process.
191
+ *
192
+ * _only for non-static_
193
+ *
194
+ * _experimental decorators_
195
+ * @param receiver - linked receiver
196
+ */
197
+ export declare function publicClassRendererEvent(receiver: EventReceiver): PropertyDecorator;
198
+ /**
199
+ * Makes the event from **renderer** and **preload** available for **main** process.
200
+ *
201
+ * _only for non-static_
202
+ *
203
+ * _experimental decorators_
204
+ * @param name - name by which the event will be accessed
205
+ * @param receiver - linked receiver
206
+ */
207
+ export declare function publicClassRendererEvent(name: string, receiver: EventReceiver): PropertyDecorator;
208
+ /**
209
+ * Makes the event from **renderer** and **preload** available for **main** process.
210
+ *
211
+ * _only for non-static_
212
+ *
213
+ * _experimental decorators_
214
+ * @param args - publish args
215
+ */
216
+ export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
217
+ /**
218
+ * Makes the event from **renderer** and **preload** available for **main** process.
219
+ *
220
+ * _only for non-static_
221
+ *
222
+ * _experimental decorators_
223
+ * @param args - publish args
224
+ */
225
+ export declare function publicClassRendererEvent(args: IPublishRendererEventArgs): PropertyDecorator;
118
226
  /**
119
227
  * Makes the property available for **renderer** and **preload** processes.
120
228
  *
@@ -1,4 +1,4 @@
1
- import { addAccess, addScope, publicEvent, publicFunction, publicVariable } from './publish';
1
+ import { addAccess, addScope, publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './publish';
2
2
  export function publicStaticMethod(arg) {
3
3
  return (target, key) => {
4
4
  let name = String(key);
@@ -25,7 +25,7 @@ export function publicMethod(arg) {
25
25
  };
26
26
  };
27
27
  }
28
- export function publicStaticEvent(arg) {
28
+ export function publicStaticMainEvent(arg) {
29
29
  return (target, key) => {
30
30
  let name = String(key);
31
31
  if (arg) {
@@ -38,16 +38,47 @@ export function publicStaticEvent(arg) {
38
38
  addScope(name, arg.scope);
39
39
  }
40
40
  }
41
- target[key] = publicEvent(name, target[key].bind(target));
41
+ target[key] = publicMainEvent(name, target[key].bind(target));
42
42
  };
43
43
  }
44
- export function publicClassEvent(arg) {
44
+ export function publicClassMainEvent(arg) {
45
45
  return (tgt, key, _) => {
46
46
  const target = tgt;
47
47
  const prevRegister = target.__register__ ?? (() => { });
48
48
  target.__register__ = instance => {
49
49
  prevRegister(instance);
50
- publicStaticEvent(arg)(instance, key, _);
50
+ publicStaticMainEvent(arg)(instance, key, _);
51
+ };
52
+ };
53
+ }
54
+ export function publicStaticRendererEvent(arg) {
55
+ return (target, key) => {
56
+ let name = String(key);
57
+ let receiver = (v) => v;
58
+ if (arg) {
59
+ if (typeof arg === 'string') {
60
+ name = arg;
61
+ }
62
+ else if (typeof arg === 'object') {
63
+ name = arg.name ?? String(key);
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);
73
+ };
74
+ }
75
+ export function publicClassRendererEvent(arg) {
76
+ return (tgt, key) => {
77
+ const target = tgt;
78
+ const prevRegister = target.__register__ ?? (() => { });
79
+ target.__register__ = instance => {
80
+ prevRegister(instance);
81
+ publicStaticRendererEvent(arg)(instance, key);
51
82
  };
52
83
  };
53
84
  }
@@ -22,7 +22,8 @@ function provideFromMain(contextIsolation = true) {
22
22
  provided: {
23
23
  properties: {},
24
24
  functions: {},
25
- events: {}
25
+ mainEvents: {},
26
+ rendererEvents: {}
26
27
  }
27
28
  };
28
29
  info.functions.forEach(funcName => {
@@ -33,18 +34,34 @@ function provideFromMain(contextIsolation = true) {
33
34
  enumerable: true
34
35
  });
35
36
  });
36
- info.events.forEach(eventName => {
37
- Object.defineProperty(provider.provided.events, eventName, {
37
+ info.rendererEvents.forEach(rendererEventName => {
38
+ Object.defineProperty(provider.provided.rendererEvents, rendererEventName, {
39
+ value(arg) {
40
+ const emitChannel = IPCChannel.rendererEventEmit + rendererEventName;
41
+ if (arg instanceof Promise) {
42
+ arg
43
+ .then(value => ipcRenderer.send(emitChannel, { value }))
44
+ .catch(reason => ipcRenderer.send(emitChannel, { error: String(reason) }));
45
+ }
46
+ else {
47
+ ipcRenderer.send(emitChannel, { value: arg });
48
+ }
49
+ },
50
+ enumerable: true
51
+ });
52
+ });
53
+ info.mainEvents.forEach(mainEventName => {
54
+ Object.defineProperty(provider.provided.mainEvents, mainEventName, {
38
55
  value(type, handler) {
39
56
  const listener = (_, result) => handler(result);
40
- const emitChannel = IPCChannel.eventEmit + eventName;
57
+ const emitChannel = IPCChannel.mainEventEmit + mainEventName;
41
58
  if (type === 'on') {
42
- ipcRenderer.send(IPCChannel.eventHandleOn + eventName);
43
59
  ipcRenderer.on(emitChannel, listener);
60
+ ipcRenderer.send(IPCChannel.mainEventOn + mainEventName);
44
61
  }
45
62
  else {
46
- ipcRenderer.send(IPCChannel.eventHandleOnce + eventName);
47
63
  ipcRenderer.once(emitChannel, listener);
64
+ ipcRenderer.send(IPCChannel.mainEventOnce + mainEventName);
48
65
  }
49
66
  return () => ipcRenderer.removeListener(emitChannel, listener);
50
67
  },
@@ -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, handleEvent, waitPromise } = args;
6
+ const { info, scope, getVariable, setVariable, callFunction, handleMainEvent, waitPromise, emitRendererEvent } = args;
7
7
  const target = {
8
8
  as: () => proxy
9
9
  };
@@ -29,17 +29,30 @@ function createProvider(args) {
29
29
  enumerable: true
30
30
  });
31
31
  });
32
- info.events.forEach(eventName => {
33
- if (!info.scopes[eventName].has(scope))
32
+ info.rendererEvents.forEach(rendererEventName => {
33
+ if (!info.scopes[rendererEventName].has(scope))
34
34
  return;
35
- const eventNameOn = `on${eventName[0].toUpperCase()}${eventName.slice(1)}`;
36
- const eventNameOnce = `once${eventName[0].toUpperCase()}${eventName.slice(1)}`;
35
+ Object.defineProperty(target, rendererEventName, {
36
+ get() {
37
+ return (arg) => emitRendererEvent(rendererEventName, arg);
38
+ },
39
+ set() {
40
+ throw new Error(`Function '${rendererEventName}' is readonly`);
41
+ },
42
+ enumerable: true
43
+ });
44
+ });
45
+ info.mainEvents.forEach(mainEventName => {
46
+ if (!info.scopes[mainEventName].has(scope))
47
+ return;
48
+ const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
49
+ const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
37
50
  Object.defineProperty(target, eventNameOn, {
38
51
  get() {
39
52
  return (handler) => {
40
- return handleEvent(eventName, 'on', (result) => {
53
+ return handleMainEvent(mainEventName, 'on', (result) => {
41
54
  if (result.error) {
42
- errorHandler(result.error, eventName);
55
+ errorHandler(result.error, mainEventName);
43
56
  return;
44
57
  }
45
58
  if (result.promiseChannel)
@@ -56,9 +69,9 @@ function createProvider(args) {
56
69
  Object.defineProperty(target, eventNameOnce, {
57
70
  get() {
58
71
  return (handler) => {
59
- return handleEvent(eventName, 'once', (result) => {
72
+ return handleMainEvent(mainEventName, 'once', (result) => {
60
73
  if (result.error) {
61
- errorHandler(result.error, eventName);
74
+ errorHandler(result.error, mainEventName);
62
75
  return;
63
76
  }
64
77
  if (result.promiseChannel)
@@ -1,14 +1,19 @@
1
1
  import { Access, Scope } from '../enums';
2
- import type { EventEmitter, PublicFunction, PublicProperty } from '../types';
2
+ import type { EventEmitter, PublicFunction, PublicProperty, RendererEvent } from '../types';
3
3
  /**
4
- * Makes the event available for **renderer** and **preload** processes.
4
+ * Makes the event from **main** available for **renderer** and **preload** processes.
5
5
  *
6
- * _only for event_
7
6
  * @param name - name by which the event will be accessed
8
- * @param func - linked emitter
9
7
  * @param scopes - event scopes
10
8
  */
11
- export declare function publicEvent<Emitter extends EventEmitter>(name: string, func?: Emitter, scopes?: Scope[]): (...args: Parameters<Emitter>) => void;
9
+ export declare function publicMainEvent<Emitter extends EventEmitter>(name: string, receiver?: Emitter, scopes?: Scope[]): (...args: Parameters<Emitter>) => void;
10
+ /**
11
+ * Makes the event from **renderer** and **preload** available for **main** process.
12
+ *
13
+ * @param name - name by which the event will be accessed
14
+ * @param scopes - event scopes
15
+ */
16
+ export declare function publicRendererEvent<I = undefined, O = I>(name: string, receiver?: (v?: I) => O, scopes?: Scope[]): RendererEvent<O>;
12
17
  /**
13
18
  * Makes the function available for **renderer** and **preload** processes.
14
19
  *
@@ -3,33 +3,32 @@ 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
+ mainEvents: new Set(),
7
+ rendererEvents: new Set(),
7
8
  accesses: {},
8
9
  scopes: {}
9
10
  };
10
11
  ipcMain.on(IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
11
12
  /**
12
- * Makes the event available for **renderer** and **preload** processes.
13
+ * Makes the event from **main** available for **renderer** and **preload** processes.
13
14
  *
14
- * _only for event_
15
15
  * @param name - name by which the event will be accessed
16
- * @param func - linked emitter
17
16
  * @param scopes - event scopes
18
17
  */
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;
18
+ export function publicMainEvent(name, receiver = ((v = undefined) => v), scopes = [Scope.preload, Scope.renderer]) {
19
+ const { mainEvents } = publicInfo;
20
+ const mainEmitChannel = IPCChannel.mainEventEmit + name;
21
+ const mainHandleChannel = IPCChannel.mainEventOn + name;
22
+ const mainHandleChannelOnce = IPCChannel.mainEventOnce + name;
24
23
  let senders = [];
25
24
  let sendersOnce = [];
26
- events.add(name);
25
+ mainEvents.add(name);
27
26
  if (!publicInfo.scopes[name])
28
27
  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));
28
+ ipcMain.removeAllListeners(mainHandleChannel);
29
+ ipcMain.removeAllListeners(mainHandleChannelOnce);
30
+ ipcMain.on(mainHandleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
31
+ ipcMain.on(mainHandleChannelOnce, ({ sender }) => !sendersOnce.includes(sender) && sendersOnce.push(sender));
33
32
  return (...args) => {
34
33
  function filterSenders() {
35
34
  senders = senders.filter(sender => !sender.isDestroyed());
@@ -38,33 +37,69 @@ export function publicEvent(name, func = (() => { }), scopes = [Scope.preload, S
38
37
  filterSenders();
39
38
  const allSenders = [...senders, ...sendersOnce];
40
39
  try {
41
- const result = func(...args);
40
+ const result = receiver(...args);
42
41
  if (result instanceof Promise) {
43
- const promiseChannel = emitChannel + IPCChannel.promisePostfix;
44
42
  result
45
43
  .then(value => {
46
44
  filterSenders();
47
- [...senders, ...sendersOnce].forEach(sender => sender.send(promiseChannel, { value }));
45
+ [...senders, ...sendersOnce].forEach(sender => sender.send(mainEmitChannel, { value }));
48
46
  sendersOnce = [];
49
47
  })
50
48
  .catch(reason => {
51
49
  filterSenders();
52
- [...senders, ...sendersOnce].forEach(sender => sender.send(promiseChannel, { error: String(reason) }));
50
+ [...senders, ...sendersOnce].forEach(sender => sender.send(mainEmitChannel, { error: String(reason) }));
53
51
  sendersOnce = [];
54
52
  });
55
- allSenders.forEach(sender => sender.send(promiseChannel, { promiseChannel }));
56
53
  }
57
54
  else {
58
- allSenders.forEach(sender => sender.send(emitChannel, { value: result }));
55
+ allSenders.forEach(sender => sender.send(mainEmitChannel, { value: result }));
59
56
  sendersOnce = [];
60
57
  }
61
58
  }
62
59
  catch (error) {
63
- allSenders.forEach(sender => sender.send(emitChannel, { error: String(error) }));
60
+ allSenders.forEach(sender => sender.send(mainEmitChannel, { error: String(error) }));
64
61
  sendersOnce = [];
65
62
  }
66
63
  };
67
64
  }
65
+ /**
66
+ * Makes the event from **renderer** and **preload** available for **main** process.
67
+ *
68
+ * @param name - name by which the event will be accessed
69
+ * @param scopes - event scopes
70
+ */
71
+ export function publicRendererEvent(name, receiver = ((v = undefined) => v), scopes = [Scope.preload, Scope.renderer]) {
72
+ if (name.startsWith('on')) {
73
+ name = name.replace('on', '');
74
+ name = `${name[0].toLowerCase()}${name.slice(1)}`;
75
+ }
76
+ const { rendererEvents } = publicInfo;
77
+ const rendererEmitChannel = IPCChannel.rendererEventEmit + name;
78
+ const handlers = new Set();
79
+ const handlersOnce = new Set();
80
+ rendererEvents.add(name);
81
+ if (!publicInfo.scopes[name])
82
+ publicInfo.scopes[name] = new Set(scopes);
83
+ ipcMain.removeAllListeners(rendererEmitChannel);
84
+ ipcMain.on(rendererEmitChannel, (_, result) => {
85
+ if (result.error)
86
+ throw new Error(result.error);
87
+ const converted = receiver(result.value);
88
+ handlers.forEach(handler => handler(converted));
89
+ handlersOnce.forEach(handler => handler(converted));
90
+ handlersOnce.clear();
91
+ });
92
+ return (handler, isOnce = false) => {
93
+ if (isOnce) {
94
+ handlersOnce.add(handler);
95
+ return () => handlersOnce.delete(handler);
96
+ }
97
+ else {
98
+ handlers.add(handler);
99
+ return () => handlers.delete(handler);
100
+ }
101
+ };
102
+ }
68
103
  /**
69
104
  * Makes the function available for **renderer** and **preload** processes.
70
105
  *