emr-bridge 1.3.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/types.d.ts CHANGED
@@ -18,10 +18,15 @@ export interface IPublishMethodArgs {
18
18
  name?: string;
19
19
  scope?: Scope;
20
20
  }
21
- export interface IPublishEventArgs {
21
+ export interface IPublishMainEventArgs {
22
22
  name?: string;
23
23
  scope?: Scope;
24
24
  }
25
+ export interface IPublishRendererEventArgs {
26
+ name?: string;
27
+ scope?: Scope;
28
+ receiver?: EventReceiver;
29
+ }
25
30
  export interface IPublishPropertyArgs {
26
31
  name?: string;
27
32
  scope?: Scope;
@@ -30,7 +35,8 @@ export interface IPublishPropertyArgs {
30
35
  export interface ICreateProviderArgs {
31
36
  info: IInfo;
32
37
  scope: Scope;
33
- handleEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
38
+ handleMainEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
39
+ emitRendererEvent(name: string, arg: any): void;
34
40
  callFunction(name: string, ...args: any[]): IIPCResult;
35
41
  getVariable(name: string): IIPCResult;
36
42
  setVariable(name: string, value: any): IIPCResult | undefined;
@@ -38,8 +44,10 @@ export interface ICreateProviderArgs {
38
44
  }
39
45
  export type Scopes = Record<string, Set<Scope>>;
40
46
  export type Accesses = Record<string, Set<Access>>;
47
+ export type RendererEvent<T = undefined> = (handler: EventHandler<T>, isOnce?: boolean) => EventUnsubscriber;
41
48
  export type MainEvent<T = undefined> = (handler: EventHandler<T>) => EventUnsubscriber;
42
49
  export type EventEmitter = (...args: any[]) => any;
50
+ export type EventReceiver<I = any, O = any> = (input: I) => O;
43
51
  export type EventUnsubscriber = () => void;
44
52
  export type EventHandler<T> = (result: T) => void;
45
53
  export interface IRendererPublic {
@@ -50,8 +58,12 @@ export interface IProvider {
50
58
  waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
51
59
  provided: {
52
60
  functions: Record<string, (...args: any[]) => IIPCResult>;
53
- events: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
54
- properties: Record<string, IIPCResult>;
61
+ mainEvents: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
62
+ rendererEvents: Record<string, (arg: any) => void>;
63
+ properties: Record<string, {
64
+ get(): IIPCResult;
65
+ set(val: any): void;
66
+ }>;
55
67
  };
56
68
  }
57
69
  export type PublicFunction = (...args: any[]) => any;
@@ -62,7 +74,8 @@ export type PublicProperty = {
62
74
  export interface IInfo {
63
75
  properties: Set<string>;
64
76
  functions: Set<string>;
65
- events: Set<string>;
77
+ mainEvents: Set<string>;
78
+ rendererEvents: Set<string>;
66
79
  scopes: Scopes;
67
80
  accesses: Accesses;
68
81
  }
package/esm/enums.d.ts CHANGED
@@ -9,10 +9,11 @@ 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
+ mainEventOn = "MAIN_EVENT_ON_",
13
+ mainEventOnce = "MAIN_EVENT_ONCE_",
14
+ mainEventEmit = "MAIN_EVENT_EMIT_",
15
15
  propertyGet = "PROPERTY_GET_",
16
16
  propertySet = "PROPERTY_SET_",
17
- promisePostfix = "_PROMISE"
17
+ promisePostfix = "_PROMISE",
18
+ rendererEventEmit = "RENDERER_EMIT_"
18
19
  }
package/esm/enums.js CHANGED
@@ -12,10 +12,11 @@ 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
+ IPCChannel["mainEventOn"] = "MAIN_EVENT_ON_";
16
+ IPCChannel["mainEventOnce"] = "MAIN_EVENT_ONCE_";
17
+ IPCChannel["mainEventEmit"] = "MAIN_EVENT_EMIT_";
18
18
  IPCChannel["propertyGet"] = "PROPERTY_GET_";
19
19
  IPCChannel["propertySet"] = "PROPERTY_SET_";
20
20
  IPCChannel["promisePostfix"] = "_PROMISE";
21
+ IPCChannel["rendererEventEmit"] = "RENDERER_EMIT_";
21
22
  })(IPCChannel || (IPCChannel = {}));
package/esm/exp.d.ts CHANGED
@@ -1,4 +1,4 @@
1
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';
2
+ export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMainEvent, publicStaticMethod, publicStaticProperty, publicStaticRendererEvent } from './scripts/oldDecorators';
3
+ export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
4
+ export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs, MainEvent, PublicFunction, PublicProperty, RendererEvent } from './types';
package/esm/exp.js CHANGED
@@ -1,3 +1,3 @@
1
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';
2
+ export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMainEvent, publicStaticMethod, publicStaticProperty, publicStaticRendererEvent } from './scripts/oldDecorators';
3
+ export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
package/esm/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
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';
2
+ export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
3
+ export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
4
+ export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, IPublishMainEventArgs, IPublishMethodArgs, IPublishPropertyArgs, IPublishRendererEventArgs, MainEvent, PublicFunction, PublicProperty, RendererEvent } from './types';
package/esm/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Access, Scope } from './enums';
2
- export { publicClassEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
3
- export { publicEvent, publicFunction, publicVariable } from './scripts/publish';
2
+ export { publicClassMainEvent, publicClassRendererEvent, publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators';
3
+ export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
@@ -15,15 +15,18 @@ 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);
18
+ emitRendererEvent(name, arg) {
19
+ provider.provided.rendererEvents[name](arg);
20
+ },
21
+ handleMainEvent(name, type, handler) {
22
+ return provider.provided.mainEvents[name](type, handler);
20
23
  },
21
24
  getVariable(name) {
22
- return provider.provided.properties[name];
25
+ return provider.provided.properties[name].get();
23
26
  },
24
27
  setVariable(name, value) {
25
- provider.provided.properties[name] = value;
26
- return;
28
+ provider.provided.properties[name].set(value);
29
+ return undefined;
27
30
  },
28
31
  waitPromise(channel) {
29
32
  return new Promise((resolve, reject) => {
@@ -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,45 +22,52 @@ 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 => {
29
- Object.defineProperty(provider.provided.functions, funcName, {
30
- value(...args) {
31
- return ipcRenderer.sendSync(IPCChannel.functionCall + funcName, ...args);
32
- },
33
- enumerable: true
34
- });
30
+ provider.provided.functions[funcName] = (...args) => {
31
+ return ipcRenderer.sendSync(IPCChannel.functionCall + funcName, ...args);
32
+ };
35
33
  });
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
- });
34
+ info.rendererEvents.forEach(eventName => {
35
+ provider.provided.rendererEvents[eventName] = (arg) => {
36
+ const emitChannel = IPCChannel.rendererEventEmit + eventName;
37
+ if (arg instanceof Promise) {
38
+ arg
39
+ .then(value => ipcRenderer.send(emitChannel, { value }))
40
+ .catch(reason => ipcRenderer.send(emitChannel, { error: String(reason) }));
41
+ }
42
+ else {
43
+ ipcRenderer.send(emitChannel, { value: arg });
44
+ }
45
+ };
46
+ });
47
+ info.mainEvents.forEach(eventName => {
48
+ provider.provided.mainEvents[eventName] = (type, handler) => {
49
+ const listener = (_, result) => handler(result);
50
+ const emitChannel = IPCChannel.mainEventEmit + eventName;
51
+ if (type === 'on') {
52
+ ipcRenderer.on(emitChannel, listener);
53
+ ipcRenderer.send(IPCChannel.mainEventOn + eventName);
54
+ }
55
+ else {
56
+ ipcRenderer.once(emitChannel, listener);
57
+ ipcRenderer.send(IPCChannel.mainEventOnce + eventName);
58
+ }
59
+ return () => ipcRenderer.removeListener(emitChannel, listener);
60
+ };
53
61
  });
54
62
  info.properties.forEach(propName => {
55
- Object.defineProperty(provider.provided.properties, propName, {
63
+ provider.provided.properties[propName] = {
56
64
  get() {
57
65
  return ipcRenderer.sendSync(IPCChannel.propertyGet + propName);
58
66
  },
59
67
  set(value) {
60
68
  ipcRenderer.sendSync(IPCChannel.propertySet + propName, value);
61
- },
62
- enumerable: true
63
- });
69
+ }
70
+ };
64
71
  });
65
72
  if (contextIsolation)
66
73
  contextBridge.exposeInMainWorld('__provider__', provider);