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.
Files changed (48) hide show
  1. package/LICENCE +1 -1
  2. package/README.md +22 -22
  3. package/cjs/enums.d.ts +3 -0
  4. package/cjs/enums.js +3 -0
  5. package/cjs/exp.d.ts +4 -0
  6. package/cjs/exp.js +18 -0
  7. package/cjs/index.d.ts +3 -4
  8. package/cjs/index.js +3 -7
  9. package/cjs/scripts/Bridge.js +3 -0
  10. package/cjs/scripts/Main.js +13 -0
  11. package/cjs/scripts/newDecorators.d.ts +21 -1
  12. package/cjs/scripts/newDecorators.js +21 -1
  13. package/cjs/scripts/oldDecorators.d.ts +59 -1
  14. package/cjs/scripts/oldDecorators.js +29 -1
  15. package/cjs/scripts/preloadProvider.js +20 -1
  16. package/cjs/scripts/provider.js +45 -1
  17. package/cjs/scripts/publish.d.ts +10 -1
  18. package/cjs/scripts/publish.js +60 -1
  19. package/cjs/types.d.ts +11 -0
  20. package/{lib → esm}/enums.d.ts +3 -0
  21. package/{lib → esm}/enums.js +3 -0
  22. package/esm/exp.d.ts +4 -0
  23. package/esm/exp.js +3 -0
  24. package/esm/index.d.ts +4 -0
  25. package/esm/index.js +3 -0
  26. package/{lib → esm}/scripts/Bridge.js +3 -0
  27. package/{lib → esm}/scripts/Main.js +13 -0
  28. package/{lib → esm}/scripts/newDecorators.d.ts +21 -1
  29. package/{lib → esm}/scripts/newDecorators.js +20 -1
  30. package/{lib → esm}/scripts/oldDecorators.d.ts +59 -1
  31. package/{lib → esm}/scripts/oldDecorators.js +27 -1
  32. package/{lib → esm}/scripts/preloadProvider.js +20 -1
  33. package/{lib → esm}/scripts/provider.js +45 -1
  34. package/{lib → esm}/scripts/publish.d.ts +10 -1
  35. package/{lib → esm}/scripts/publish.js +58 -0
  36. package/{lib → esm}/types.d.ts +11 -0
  37. package/package.json +29 -13
  38. package/lib/index.d.ts +0 -5
  39. package/lib/index.js +0 -4
  40. /package/{lib → esm}/preload.d.ts +0 -0
  41. /package/{lib → esm}/preload.js +0 -0
  42. /package/{lib → esm}/renderer.d.ts +0 -0
  43. /package/{lib → esm}/renderer.js +0 -0
  44. /package/{lib → esm}/scripts/Bridge.d.ts +0 -0
  45. /package/{lib → esm}/scripts/Main.d.ts +0 -0
  46. /package/{lib → esm}/scripts/preloadProvider.d.ts +0 -0
  47. /package/{lib → esm}/scripts/provider.d.ts +0 -0
  48. /package/{lib → esm}/types.js +0 -0
package/LICENCE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 VerZsuT
3
+ Copyright (c) 2023 VerZsuT
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -23,6 +23,8 @@ npm i emr-bridge
23
23
 
24
24
  ## Usage
25
25
 
26
+ _If you need CommonJS modules, then use `emr-bridge/cjs`_
27
+
26
28
  There are three ways to use
27
29
 
28
30
  **IMPORTANT**: In any of the cases, you need to insert this code into preload
@@ -31,28 +33,26 @@ There are three ways to use
31
33
  // Preload process
32
34
  import { provideFromMain } from 'emr-bridge/preload'
33
35
 
34
- const contextIsolation = true
35
-
36
- provideFromMain(contextIsolation)
36
+ provideFromMain(true /* context isolation */)
37
37
  ```
38
38
 
39
39
  ### Static methods and properties
40
40
 
41
41
  **Experimental decorators**
42
42
 
43
- Use **publicStaticMethodExp**/**publicStaticPropertyExp**.
43
+ Use **publicStaticMethod**/**publicStaticProperty**.
44
44
 
45
45
  ```ts
46
46
  // Main process
47
47
 
48
- import { publicStaticMethodExp, publicStaticPropertyExp, Access } from 'emr-bridge'
48
+ import { publicStaticMethod, publicStaticProperty, Access } from 'emr-bridge/exp'
49
49
 
50
50
  class User {
51
51
  private static name = 'Name'
52
52
  private static age = 20
53
53
  private static money = 1000
54
54
 
55
- @publicStaticPropertyExp('userBalance')
55
+ @publicStaticProperty('userBalance')
56
56
  static get balance(): string {
57
57
  return `${this.money}$`
58
58
  }
@@ -61,7 +61,7 @@ class User {
61
61
  this.money = Number(newBalance.split('$')[0])
62
62
  }
63
63
 
64
- @publicStaticPropertyExp({
64
+ @publicStaticProperty({
65
65
  name: 'userInfo',
66
66
  access: Access.get
67
67
  })
@@ -69,12 +69,12 @@ class User {
69
69
  return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
70
70
  }
71
71
 
72
- @publicStaticMethodExp()
72
+ @publicStaticMethod()
73
73
  static setName(newName: string): void {
74
74
  this.name = newName
75
75
  }
76
76
 
77
- @publicStaticMethodExp('getUserAge')
77
+ @publicStaticMethod('getUserAge')
78
78
  static getAge(): number {
79
79
  return this.age
80
80
  }
@@ -132,20 +132,20 @@ class User {
132
132
 
133
133
  **Experimental decorators**
134
134
 
135
- Use **providePublic** and **publicMethodExp**/**publicPropertyExp**.
135
+ Use **providePublic** and **publicMethod**/**publicProperty**.
136
136
 
137
137
  Without calling **providePublic**, public methods will not be accessible from renderer.
138
138
 
139
139
  ```ts
140
140
  // Main process
141
- import { providePublic, publicMethodExp, publicPropertyExp, Access } from 'emr-bridge'
141
+ import { providePublic, publicMethod, publicProperty, Access } from 'emr-bridge/exp'
142
142
 
143
143
  class User {
144
144
  private name = 'Name'
145
145
  private age = 20
146
146
  private money = 1000
147
147
 
148
- @publicPropertyExp()
148
+ @publicProperty()
149
149
  get balance(): string {
150
150
  return `${this.money}$`
151
151
  }
@@ -154,7 +154,7 @@ class User {
154
154
  this.money = Number(newBalance.split('$')[0])
155
155
  }
156
156
 
157
- @publicPropertyExp({
157
+ @publicProperty({
158
158
  name: 'userInfo',
159
159
  access: Access.get
160
160
  })
@@ -162,12 +162,12 @@ class User {
162
162
  return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
163
163
  }
164
164
 
165
- @publicMethodExp()
165
+ @publicMethod()
166
166
  setName(newName: string): void {
167
167
  this.name = newName
168
168
  }
169
169
 
170
- @publicMethodExp('getUserAge')
170
+ @publicMethod('getUserAge')
171
171
  getAge(): number {
172
172
  return this.age
173
173
  }
@@ -269,13 +269,13 @@ Accessing an entity outside the specified scope will result in an error being th
269
269
  // Main process
270
270
 
271
271
  // static and non-static
272
- import { Scope, Access, providePublic, publicStaticMethodExp, publicMethodExp, publicPropertyExp } from 'emr-bridge'
272
+ import { Scope, Access, providePublic, publicStaticMethod, publicMethod, publicProperty } from 'emr-bridge/exp'
273
273
 
274
274
  class User {
275
275
  private static age = 20
276
276
  private name = 'Name'
277
277
 
278
- @publicPropertyExp({
278
+ @publicProperty({
279
279
  scope: Scope.preload,
280
280
  access: Access.get
281
281
  })
@@ -283,12 +283,12 @@ class User {
283
283
  return 30
284
284
  }
285
285
 
286
- @publicStaticMethodExp({ scope: Scope.preload })
286
+ @publicStaticMethod({ scope: Scope.preload })
287
287
  static getAge(): number {
288
288
  return this.age
289
289
  }
290
290
 
291
- @publicMethodExp({
291
+ @publicMethod({
292
292
  name: 'getUserName',
293
293
  scope: Scope.renderer
294
294
  })
@@ -328,18 +328,18 @@ Using an entity outside the specified scope _will cause an error_.
328
328
  // Main process
329
329
 
330
330
  // static and non-static
331
- import { publicStaticPropertyExp, publicPropertyExp, providePublic, Access } from 'emr-bridge'
331
+ import { publicStaticProperty, publicProperty, providePublic, Access } from 'emr-bridge/exp'
332
332
 
333
333
  class User {
334
334
  private static name = 'Name'
335
335
  private static surname = 'Surname'
336
336
 
337
- @publicStaticPropertyExp({ access: Access.get })
337
+ @publicStaticProperty({ access: Access.get })
338
338
  static get fullName(): string {
339
339
  return `${this.name} ${this.surname}`
340
340
  }
341
341
 
342
- @publicPropertyExp({
342
+ @publicProperty({
343
343
  name: 'userAge',
344
344
  access: Access.get
345
345
  })
package/cjs/enums.d.ts CHANGED
@@ -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/cjs/enums.js CHANGED
@@ -15,6 +15,9 @@ var IPCChannel;
15
15
  (function (IPCChannel) {
16
16
  IPCChannel["getPublicInfo"] = "GET_PUBLIC_INFO";
17
17
  IPCChannel["functionCall"] = "FUNCTION_CALL_";
18
+ IPCChannel["eventHandleOn"] = "EVENT_HANDLE_ON_";
19
+ IPCChannel["eventHandleOnce"] = "EVENT_HANDLE_ONCE_";
20
+ IPCChannel["eventEmit"] = "EVENT_EMIT_";
18
21
  IPCChannel["propertyGet"] = "PROPERTY_GET_";
19
22
  IPCChannel["propertySet"] = "PROPERTY_SET_";
20
23
  IPCChannel["promisePostfix"] = "_PROMISE";
package/cjs/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/cjs/exp.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.publicVariable = exports.publicFunction = exports.publicEvent = exports.publicStaticProperty = exports.publicStaticMethod = exports.publicStaticEvent = exports.publicProperty = exports.publicMethod = exports.publicClassEvent = exports.providePublic = exports.Scope = exports.Access = void 0;
4
+ var enums_1 = require("./enums");
5
+ Object.defineProperty(exports, "Access", { enumerable: true, get: function () { return enums_1.Access; } });
6
+ Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return enums_1.Scope; } });
7
+ var oldDecorators_1 = require("./scripts/oldDecorators");
8
+ Object.defineProperty(exports, "providePublic", { enumerable: true, get: function () { return oldDecorators_1.providePublic; } });
9
+ Object.defineProperty(exports, "publicClassEvent", { enumerable: true, get: function () { return oldDecorators_1.publicClassEvent; } });
10
+ Object.defineProperty(exports, "publicMethod", { enumerable: true, get: function () { return oldDecorators_1.publicMethod; } });
11
+ Object.defineProperty(exports, "publicProperty", { enumerable: true, get: function () { return oldDecorators_1.publicProperty; } });
12
+ Object.defineProperty(exports, "publicStaticEvent", { enumerable: true, get: function () { return oldDecorators_1.publicStaticEvent; } });
13
+ Object.defineProperty(exports, "publicStaticMethod", { enumerable: true, get: function () { return oldDecorators_1.publicStaticMethod; } });
14
+ Object.defineProperty(exports, "publicStaticProperty", { enumerable: true, get: function () { return oldDecorators_1.publicStaticProperty; } });
15
+ var publish_1 = require("./scripts/publish");
16
+ Object.defineProperty(exports, "publicEvent", { enumerable: true, get: function () { return publish_1.publicEvent; } });
17
+ Object.defineProperty(exports, "publicFunction", { enumerable: true, get: function () { return publish_1.publicFunction; } });
18
+ Object.defineProperty(exports, "publicVariable", { enumerable: true, get: function () { return publish_1.publicVariable; } });
package/cjs/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
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';
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/cjs/index.js CHANGED
@@ -1,20 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.publicVariable = exports.publicFunction = exports.publicStaticPropertyExp = exports.publicStaticMethodExp = exports.publicPropertyExp = exports.publicMethodExp = exports.providePublic = exports.publicSetter = exports.publicProperty = exports.publicMethod = exports.publicGetter = exports.Scope = exports.Access = void 0;
3
+ exports.publicVariable = exports.publicFunction = exports.publicEvent = exports.publicSetter = exports.publicProperty = exports.publicMethod = exports.publicGetter = exports.publicClassEvent = exports.Scope = exports.Access = void 0;
4
4
  var enums_1 = require("./enums");
5
5
  Object.defineProperty(exports, "Access", { enumerable: true, get: function () { return enums_1.Access; } });
6
6
  Object.defineProperty(exports, "Scope", { enumerable: true, get: function () { return enums_1.Scope; } });
7
7
  var newDecorators_1 = require("./scripts/newDecorators");
8
+ Object.defineProperty(exports, "publicClassEvent", { enumerable: true, get: function () { return newDecorators_1.publicClassEvent; } });
8
9
  Object.defineProperty(exports, "publicGetter", { enumerable: true, get: function () { return newDecorators_1.publicGetter; } });
9
10
  Object.defineProperty(exports, "publicMethod", { enumerable: true, get: function () { return newDecorators_1.publicMethod; } });
10
11
  Object.defineProperty(exports, "publicProperty", { enumerable: true, get: function () { return newDecorators_1.publicProperty; } });
11
12
  Object.defineProperty(exports, "publicSetter", { enumerable: true, get: function () { return newDecorators_1.publicSetter; } });
12
- var oldDecorators_1 = require("./scripts/oldDecorators");
13
- Object.defineProperty(exports, "providePublic", { enumerable: true, get: function () { return oldDecorators_1.providePublic; } });
14
- Object.defineProperty(exports, "publicMethodExp", { enumerable: true, get: function () { return oldDecorators_1.publicMethod; } });
15
- Object.defineProperty(exports, "publicPropertyExp", { enumerable: true, get: function () { return oldDecorators_1.publicProperty; } });
16
- Object.defineProperty(exports, "publicStaticMethodExp", { enumerable: true, get: function () { return oldDecorators_1.publicStaticMethod; } });
17
- Object.defineProperty(exports, "publicStaticPropertyExp", { enumerable: true, get: function () { return oldDecorators_1.publicStaticProperty; } });
18
13
  var publish_1 = require("./scripts/publish");
14
+ Object.defineProperty(exports, "publicEvent", { enumerable: true, get: function () { return publish_1.publicEvent; } });
19
15
  Object.defineProperty(exports, "publicFunction", { enumerable: true, get: function () { return publish_1.publicFunction; } });
20
16
  Object.defineProperty(exports, "publicVariable", { enumerable: true, get: function () { return publish_1.publicVariable; } });
@@ -20,6 +20,9 @@ const Bridge = (0, provider_1.default)({
20
20
  callFunction(name, ...args) {
21
21
  return provider.provided.functions[name](...args);
22
22
  },
23
+ handleEvent(name, type, handler) {
24
+ return provider.provided.events[name](type, handler);
25
+ },
23
26
  getVariable(name) {
24
27
  return provider.provided.properties[name];
25
28
  },
@@ -20,6 +20,19 @@ const Main = (0, provider_1.default)({
20
20
  callFunction(name, ...args) {
21
21
  return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.functionCall + name, ...args);
22
22
  },
23
+ handleEvent(name, type, handler) {
24
+ const listener = (_, result) => handler(result);
25
+ const emitChannel = enums_1.IPCChannel.eventEmit + name;
26
+ if (type === 'on') {
27
+ electron_1.ipcRenderer.send(enums_1.IPCChannel.eventHandleOn + name);
28
+ electron_1.ipcRenderer.on(emitChannel, listener);
29
+ }
30
+ else {
31
+ electron_1.ipcRenderer.send(enums_1.IPCChannel.eventHandleOnce + name);
32
+ electron_1.ipcRenderer.once(emitChannel, listener);
33
+ }
34
+ return () => electron_1.ipcRenderer.removeListener(emitChannel, listener);
35
+ },
23
36
  getVariable(name) {
24
37
  return electron_1.ipcRenderer.sendSync(enums_1.IPCChannel.propertyGet + name);
25
38
  },
@@ -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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.publicSetter = exports.publicGetter = exports.publicProperty = exports.publicMethod = void 0;
3
+ exports.publicSetter = exports.publicGetter = exports.publicProperty = exports.publicClassEvent = exports.publicMethod = void 0;
4
4
  const publish_1 = require("./publish");
5
5
  function publicMethod(arg) {
6
6
  return (method, context) => {
@@ -21,6 +21,26 @@ function publicMethod(arg) {
21
21
  };
22
22
  }
23
23
  exports.publicMethod = publicMethod;
24
+ function publicClassEvent(arg) {
25
+ return (method, context) => {
26
+ let name = String(context.name);
27
+ if (arg) {
28
+ if (typeof arg === 'string') {
29
+ name = arg;
30
+ }
31
+ else {
32
+ name = arg.name ?? name;
33
+ if (arg.scope)
34
+ (0, publish_1.addScope)(name, arg.scope);
35
+ }
36
+ }
37
+ context.addInitializer(function () {
38
+ // @ts-ignore
39
+ this[name] = (0, publish_1.publicEvent)(name, method.bind(this));
40
+ });
41
+ };
42
+ }
43
+ exports.publicClassEvent = publicClassEvent;
24
44
  function publicProperty(arg) {
25
45
  return (_, context) => {
26
46
  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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.providePublic = exports.publicProperty = exports.publicStaticProperty = exports.publicMethod = exports.publicStaticMethod = void 0;
3
+ exports.providePublic = exports.publicProperty = exports.publicStaticProperty = exports.publicClassEvent = exports.publicStaticEvent = exports.publicMethod = exports.publicStaticMethod = void 0;
4
4
  const publish_1 = require("./publish");
5
5
  function publicStaticMethod(arg) {
6
6
  return (target, key) => {
@@ -30,6 +30,34 @@ function publicMethod(arg) {
30
30
  };
31
31
  }
32
32
  exports.publicMethod = publicMethod;
33
+ function publicStaticEvent(arg) {
34
+ return (target, key) => {
35
+ let name = String(key);
36
+ if (arg) {
37
+ if (typeof arg === 'string') {
38
+ name = arg;
39
+ }
40
+ else {
41
+ name = arg.name ?? String(key);
42
+ if (arg.scope)
43
+ (0, publish_1.addScope)(name, arg.scope);
44
+ }
45
+ }
46
+ target[key] = (0, publish_1.publicEvent)(name, target[key].bind(target));
47
+ };
48
+ }
49
+ exports.publicStaticEvent = publicStaticEvent;
50
+ function publicClassEvent(arg) {
51
+ return (tgt, key, _) => {
52
+ const target = tgt;
53
+ const prevRegister = target.__register__ ?? (() => { });
54
+ target.__register__ = instance => {
55
+ prevRegister(instance);
56
+ publicStaticEvent(arg)(instance, key, _);
57
+ };
58
+ };
59
+ }
60
+ exports.publicClassEvent = publicClassEvent;
33
61
  function publicStaticProperty(arg) {
34
62
  return (target, key) => {
35
63
  let name = String(key);
@@ -23,7 +23,8 @@ function provideFromMain(contextIsolation = true) {
23
23
  },
24
24
  provided: {
25
25
  properties: {},
26
- functions: {}
26
+ functions: {},
27
+ events: {}
27
28
  }
28
29
  };
29
30
  info.functions.forEach(funcName => {
@@ -34,6 +35,24 @@ function provideFromMain(contextIsolation = true) {
34
35
  enumerable: true
35
36
  });
36
37
  });
38
+ info.events.forEach(eventName => {
39
+ Object.defineProperty(provider.provided.events, eventName, {
40
+ value(type, handler) {
41
+ const listener = (_, result) => handler(result);
42
+ const emitChannel = enums_1.IPCChannel.eventEmit + eventName;
43
+ if (type === 'on') {
44
+ electron_1.ipcRenderer.send(enums_1.IPCChannel.eventHandleOn + eventName);
45
+ electron_1.ipcRenderer.on(emitChannel, listener);
46
+ }
47
+ else {
48
+ electron_1.ipcRenderer.send(enums_1.IPCChannel.eventHandleOnce + eventName);
49
+ electron_1.ipcRenderer.once(emitChannel, listener);
50
+ }
51
+ return () => electron_1.ipcRenderer.removeListener(emitChannel, listener);
52
+ },
53
+ enumerable: true
54
+ });
55
+ });
37
56
  info.properties.forEach(propName => {
38
57
  Object.defineProperty(provider.provided.properties, propName, {
39
58
  get() {
@@ -5,7 +5,7 @@ function errorHandler(error, channel) {
5
5
  console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
6
6
  }
7
7
  function createProvider(args) {
8
- const { info, scope, getVariable, setVariable, callFunction, waitPromise } = args;
8
+ const { info, scope, getVariable, setVariable, callFunction, handleEvent, waitPromise } = args;
9
9
  const target = {
10
10
  as: () => proxy
11
11
  };
@@ -31,6 +31,50 @@ function createProvider(args) {
31
31
  enumerable: true
32
32
  });
33
33
  });
34
+ info.events.forEach(eventName => {
35
+ if (!info.scopes[eventName].has(scope))
36
+ return;
37
+ const eventNameOn = `on${eventName[0].toUpperCase()}${eventName.slice(1)}`;
38
+ const eventNameOnce = `once${eventName[0].toUpperCase()}${eventName.slice(1)}`;
39
+ Object.defineProperty(target, eventNameOn, {
40
+ get() {
41
+ return (handler) => {
42
+ return handleEvent(eventName, 'on', (result) => {
43
+ if (result.error) {
44
+ errorHandler(result.error, eventName);
45
+ return;
46
+ }
47
+ if (result.promiseChannel)
48
+ waitPromise(result.promiseChannel).then(handler);
49
+ handler(result.value);
50
+ });
51
+ };
52
+ },
53
+ set() {
54
+ throw new Error(`Function '${eventNameOn}' is readonly`);
55
+ },
56
+ enumerable: true
57
+ });
58
+ Object.defineProperty(target, eventNameOnce, {
59
+ get() {
60
+ return (handler) => {
61
+ return handleEvent(eventName, 'once', (result) => {
62
+ if (result.error) {
63
+ errorHandler(result.error, eventName);
64
+ return;
65
+ }
66
+ if (result.promiseChannel)
67
+ waitPromise(result.promiseChannel).then(handler);
68
+ handler(result.value);
69
+ });
70
+ };
71
+ },
72
+ set() {
73
+ throw new Error(`Function '${eventNameOnce}' is readonly`);
74
+ },
75
+ enumerable: true
76
+ });
77
+ });
34
78
  info.properties.forEach(propName => {
35
79
  if (!info.scopes[propName].has(scope))
36
80
  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
  *
@@ -1,15 +1,74 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = void 0;
3
+ exports.addAccess = exports.addScope = exports.publicVariable = exports.publicFunction = exports.publicEvent = void 0;
4
4
  const electron_1 = require("electron");
5
5
  const enums_1 = require("../enums");
6
6
  const publicInfo = {
7
7
  properties: new Set(),
8
8
  functions: new Set(),
9
+ events: new Set(),
9
10
  accesses: {},
10
11
  scopes: {}
11
12
  };
12
13
  electron_1.ipcMain.on(enums_1.IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
14
+ /**
15
+ * Makes the event available for **renderer** and **preload** processes.
16
+ *
17
+ * _only for event_
18
+ * @param name - name by which the event will be accessed
19
+ * @param func - linked emitter
20
+ * @param scopes - event scopes
21
+ */
22
+ function publicEvent(name, func = (() => { }), scopes = [enums_1.Scope.preload, enums_1.Scope.renderer]) {
23
+ const { events } = publicInfo;
24
+ const emitChannel = enums_1.IPCChannel.eventEmit + name;
25
+ const handleChannel = enums_1.IPCChannel.eventHandleOn + name;
26
+ const handleChannelOnce = enums_1.IPCChannel.eventHandleOnce + name;
27
+ let senders = [];
28
+ let sendersOnce = [];
29
+ events.add(name);
30
+ if (!publicInfo.scopes[name])
31
+ publicInfo.scopes[name] = new Set(scopes);
32
+ electron_1.ipcMain.removeAllListeners(handleChannel);
33
+ electron_1.ipcMain.removeAllListeners(handleChannelOnce);
34
+ electron_1.ipcMain.on(handleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
35
+ electron_1.ipcMain.on(handleChannelOnce, ({ sender }) => !sendersOnce.includes(sender) && sendersOnce.push(sender));
36
+ return (...args) => {
37
+ function filterSenders() {
38
+ senders = senders.filter(sender => !sender.isDestroyed());
39
+ sendersOnce = sendersOnce.filter(sender => !sender.isDestroyed());
40
+ }
41
+ filterSenders();
42
+ const allSenders = [...senders, ...sendersOnce];
43
+ try {
44
+ const result = func(...args);
45
+ if (result instanceof Promise) {
46
+ const promiseChannel = emitChannel + enums_1.IPCChannel.promisePostfix;
47
+ result
48
+ .then(value => {
49
+ filterSenders();
50
+ [...senders, ...sendersOnce].forEach(sender => sender.send(promiseChannel, { value }));
51
+ sendersOnce = [];
52
+ })
53
+ .catch(reason => {
54
+ filterSenders();
55
+ [...senders, ...sendersOnce].forEach(sender => sender.send(promiseChannel, { error: String(reason) }));
56
+ sendersOnce = [];
57
+ });
58
+ allSenders.forEach(sender => sender.send(promiseChannel, { promiseChannel }));
59
+ }
60
+ else {
61
+ allSenders.forEach(sender => sender.send(emitChannel, { value: result }));
62
+ sendersOnce = [];
63
+ }
64
+ }
65
+ catch (error) {
66
+ allSenders.forEach(sender => sender.send(emitChannel, { error: String(error) }));
67
+ sendersOnce = [];
68
+ }
69
+ };
70
+ }
71
+ exports.publicEvent = publicEvent;
13
72
  /**
14
73
  * Makes the function available for **renderer** and **preload** processes.
15
74
  *