emr-bridge 1.6.1 → 1.7.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 (63) hide show
  1. package/cjs/errors.d.ts +28 -0
  2. package/cjs/errors.js +39 -0
  3. package/cjs/exp.d.ts +2 -2
  4. package/cjs/exp.js +8 -8
  5. package/cjs/index.d.ts +2 -2
  6. package/cjs/index.js +7 -7
  7. package/cjs/main.d.ts +2 -2
  8. package/cjs/main.js +7 -7
  9. package/cjs/preload.d.ts +3 -3
  10. package/cjs/preload.js +4 -4
  11. package/cjs/renderer.d.ts +2 -2
  12. package/cjs/renderer.js +2 -2
  13. package/cjs/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
  14. package/cjs/scripts/bridge.js +57 -0
  15. package/{esm/scripts/Main.d.ts → cjs/scripts/main.d.ts} +2 -2
  16. package/cjs/scripts/main.js +72 -0
  17. package/cjs/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
  18. package/cjs/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
  19. package/cjs/scripts/preload-provider.js +96 -0
  20. package/cjs/scripts/provider.d.ts +25 -3
  21. package/cjs/scripts/provider.js +96 -92
  22. package/cjs/scripts/publish.js +21 -18
  23. package/cjs/scripts/renderer-tunnels.d.ts +47 -0
  24. package/cjs/scripts/renderer-tunnels.js +115 -0
  25. package/cjs/types.d.ts +32 -39
  26. package/esm/errors.d.ts +28 -0
  27. package/esm/errors.js +27 -0
  28. package/esm/exp.d.ts +2 -2
  29. package/esm/exp.js +1 -1
  30. package/esm/index.d.ts +2 -2
  31. package/esm/index.js +1 -1
  32. package/esm/main.d.ts +2 -2
  33. package/esm/main.js +1 -1
  34. package/esm/preload.d.ts +3 -3
  35. package/esm/preload.js +2 -2
  36. package/esm/renderer.d.ts +2 -2
  37. package/esm/renderer.js +1 -1
  38. package/esm/scripts/{Bridge.d.ts → bridge.d.ts} +2 -2
  39. package/esm/scripts/bridge.js +52 -0
  40. package/{cjs/scripts/Main.d.ts → esm/scripts/main.d.ts} +2 -2
  41. package/esm/scripts/main.js +67 -0
  42. package/esm/scripts/{newDecorators.d.ts → new-decorators.d.ts} +5 -5
  43. package/esm/scripts/{oldDecorators.d.ts → old-decorators.d.ts} +7 -7
  44. package/esm/scripts/preload-provider.js +91 -0
  45. package/esm/scripts/provider.d.ts +25 -3
  46. package/esm/scripts/provider.js +95 -92
  47. package/esm/scripts/publish.js +21 -18
  48. package/esm/scripts/renderer-tunnels.d.ts +47 -0
  49. package/esm/scripts/renderer-tunnels.js +109 -0
  50. package/esm/types.d.ts +32 -39
  51. package/package.json +1 -1
  52. package/cjs/scripts/Bridge.js +0 -58
  53. package/cjs/scripts/Main.js +0 -85
  54. package/cjs/scripts/preloadProvider.js +0 -92
  55. package/esm/scripts/Bridge.js +0 -53
  56. package/esm/scripts/Main.js +0 -80
  57. package/esm/scripts/preloadProvider.js +0 -87
  58. /package/cjs/scripts/{newDecorators.js → new-decorators.js} +0 -0
  59. /package/cjs/scripts/{oldDecorators.js → old-decorators.js} +0 -0
  60. /package/cjs/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
  61. /package/esm/scripts/{newDecorators.js → new-decorators.js} +0 -0
  62. /package/esm/scripts/{oldDecorators.js → old-decorators.js} +0 -0
  63. /package/esm/scripts/{preloadProvider.d.ts → preload-provider.d.ts} +0 -0
@@ -1,112 +1,116 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const enums_1 = require("../enums");
4
- function errorHandler(error, channel) {
5
- console.error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
6
- }
7
- function createProvider(args) {
8
- const { info, scope, getVariable, setVariable, callFunction, handleMainEvent, waitPromise, emitRendererEvent } = args;
9
- const target = {
10
- as: () => target
11
- };
12
- function define(name, attrs) {
4
+ const errors_1 = require("../errors");
5
+ class Provider {
6
+ as() { return this.target; }
7
+ target = {};
8
+ init() {
9
+ this.initFunctions();
10
+ this.initProperties();
11
+ this.initMainEvents();
12
+ this.initRendererEvents();
13
+ }
14
+ errorHandler(error, channel) {
15
+ throw new Error(`Error on ${channel}.\n${error}`.replace('Error: ', ''));
16
+ }
17
+ initFunctions() {
18
+ for (const name of this.info.functions) {
19
+ this.define(name, {
20
+ value: (...args) => {
21
+ const secret = Math.random();
22
+ const { error, promiseChannel, value } = this.callFunction(name, secret, args);
23
+ if (error) {
24
+ this.errorHandler(error, name);
25
+ }
26
+ if (promiseChannel)
27
+ return this.waitPromise(promiseChannel, secret);
28
+ return value;
29
+ }
30
+ });
31
+ }
32
+ }
33
+ initRendererEvents() {
34
+ for (const name of this.info.rendererEvents) {
35
+ this.define(name, {
36
+ value: (arg) => {
37
+ this.emitRendererEvent(name, arg);
38
+ }
39
+ });
40
+ }
41
+ }
42
+ initMainEvents() {
43
+ for (const name of this.info.mainEvents) {
44
+ const eventNameOn = `on${name[0].toUpperCase()}${name.slice(1)}`;
45
+ const eventNameOnce = `once${name[0].toUpperCase()}${name.slice(1)}`;
46
+ this.define(eventNameOn, {
47
+ value: (handler) => {
48
+ return this.handleMainEvent(name, 'on', ({ error, value }) => {
49
+ if (error) {
50
+ this.errorHandler(error, name);
51
+ }
52
+ handler(value);
53
+ });
54
+ }
55
+ });
56
+ this.define(eventNameOnce, {
57
+ value: (handler) => {
58
+ return this.handleMainEvent(name, 'once', ({ error, value }) => {
59
+ if (error) {
60
+ this.errorHandler(error, name);
61
+ }
62
+ handler(value);
63
+ });
64
+ }
65
+ });
66
+ }
67
+ }
68
+ initProperties() {
69
+ for (const name of this.info.properties) {
70
+ this.define(name, {
71
+ get: this.info.accesses.get(name).has(enums_1.Access.get)
72
+ ? () => {
73
+ const { error, value } = this.getVariable(name);
74
+ if (error) {
75
+ this.errorHandler(error, name);
76
+ }
77
+ return value;
78
+ }
79
+ : () => { throw new errors_1.NoGetAccessError(name); },
80
+ set: this.info.accesses.get(name).has(enums_1.Access.set)
81
+ ? (value) => {
82
+ const { error } = this.setVariable(name, value);
83
+ if (error) {
84
+ this.errorHandler(error, name);
85
+ }
86
+ }
87
+ : () => { throw new errors_1.NoSetAccessError(name); }
88
+ });
89
+ }
90
+ }
91
+ define(name, attrs) {
13
92
  let descriptor;
14
- if (!info.scopes.get(name).has(scope)) {
93
+ if (!this.info.scopes.get(name).has(this.scope)) {
15
94
  descriptor = {
16
- get() { throw new Error(`'${name}' is not provided into ${scope} scope`); },
17
- set() { throw new Error(`'${name}' is not provided into ${scope} scope`); }
95
+ get: () => { throw new errors_1.NotProvidedIntoScopeError(name, this.scope); },
96
+ set: () => { throw new errors_1.NotProvidedIntoScopeError(name, this.scope); }
18
97
  };
19
98
  }
20
99
  else if (attrs?.value) {
21
100
  descriptor = {
22
101
  get: () => attrs.value,
23
- set: () => { throw new Error(`'${name}' is readonly`); },
102
+ set: () => { throw new errors_1.TrySetReadonlyError(name); },
24
103
  enumerable: true
25
104
  };
26
105
  }
27
106
  else {
28
107
  descriptor = {
29
- get: attrs?.get || (() => { throw new Error(`No access to getting '${name}'`); }),
30
- set: attrs?.set || (() => { throw new Error(`'${name}' is readonly`); }),
108
+ get: attrs?.get || (() => { throw new errors_1.NoSetAccessError(name); }),
109
+ set: attrs?.set || (() => { throw new errors_1.TrySetReadonlyError(name); }),
31
110
  enumerable: true
32
111
  };
33
112
  }
34
- Object.defineProperty(target, name, descriptor);
113
+ Object.defineProperty(this.target, name, descriptor);
35
114
  }
36
- info.functions.forEach(funcName => {
37
- define(funcName, {
38
- value(...args) {
39
- const result = callFunction(funcName, ...args);
40
- if (result.error) {
41
- errorHandler(result.error, funcName);
42
- return;
43
- }
44
- if (result.promiseChannel)
45
- return waitPromise(result.promiseChannel);
46
- return result.value;
47
- }
48
- });
49
- });
50
- info.rendererEvents.forEach(eventName => {
51
- define(eventName, {
52
- value(arg) {
53
- emitRendererEvent(eventName, arg);
54
- }
55
- });
56
- });
57
- info.mainEvents.forEach(mainEventName => {
58
- const eventNameOn = `on${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
59
- const eventNameOnce = `once${mainEventName[0].toUpperCase()}${mainEventName.slice(1)}`;
60
- define(eventNameOn, {
61
- value(handler) {
62
- return handleMainEvent(mainEventName, 'on', (result) => {
63
- if (result.error) {
64
- errorHandler(result.error, mainEventName);
65
- return;
66
- }
67
- if (result.promiseChannel)
68
- waitPromise(result.promiseChannel).then(handler);
69
- handler(result.value);
70
- });
71
- }
72
- });
73
- define(eventNameOnce, {
74
- value(handler) {
75
- return handleMainEvent(mainEventName, 'once', (result) => {
76
- if (result.error) {
77
- errorHandler(result.error, mainEventName);
78
- return;
79
- }
80
- if (result.promiseChannel)
81
- waitPromise(result.promiseChannel).then(handler);
82
- handler(result.value);
83
- });
84
- }
85
- });
86
- });
87
- info.properties.forEach(propName => {
88
- define(propName, {
89
- get: info.accesses.get(propName).has(enums_1.Access.get)
90
- ? () => {
91
- const result = getVariable(propName);
92
- if (result.error) {
93
- errorHandler(result.error, propName);
94
- return;
95
- }
96
- return result.value;
97
- }
98
- : () => { throw new Error(`No access to getting the '${propName}' property`); },
99
- set: info.accesses.get(propName).has(enums_1.Access.set)
100
- ? (value) => {
101
- const result = setVariable(propName, value);
102
- if (result?.error) {
103
- errorHandler(result.error, propName);
104
- return;
105
- }
106
- }
107
- : () => { throw new Error(`No access to setting the '${propName}' property`); }
108
- });
109
- });
110
- return target;
111
115
  }
112
- exports.default = createProvider;
116
+ exports.default = Provider;
@@ -67,7 +67,7 @@ function publicMainEvent(name, procFn = ((...args) => args[0]), scopes = [enums_
67
67
  })
68
68
  .catch(reason => {
69
69
  filterSenders();
70
- [...senders, ...sendersOnce].forEach(sender => sender.send(mainEmitChannel, { error: String(reason) }));
70
+ [...senders, ...sendersOnce].forEach(sender => sender.send(mainEmitChannel, { error: String(reason?.stack || reason) }));
71
71
  sendersOnce = [];
72
72
  });
73
73
  }
@@ -77,7 +77,7 @@ function publicMainEvent(name, procFn = ((...args) => args[0]), scopes = [enums_
77
77
  }
78
78
  }
79
79
  catch (error) {
80
- allSenders.forEach(sender => sender.send(mainEmitChannel, { error: String(error) }));
80
+ allSenders.forEach(sender => sender.send(mainEmitChannel, { error: String(error?.stack || error) }));
81
81
  sendersOnce = [];
82
82
  }
83
83
  };
@@ -102,13 +102,14 @@ function publicRendererEvent(name, procFn = ((v) => v), scopes = [enums_1.Scope.
102
102
  const handlers = new Set();
103
103
  const handlersOnce = new Set();
104
104
  rendererEvents.add(name);
105
- if (!publicInfo.scopes.has(name))
105
+ if (!publicInfo.scopes.has(name)) {
106
106
  publicInfo.scopes.set(name, new Set(scopes));
107
+ }
107
108
  ipcMain.removeAllListeners(rendererEmitChannel);
108
- ipcMain.on(rendererEmitChannel, (_, result) => {
109
- if (result.error)
110
- throw new Error(result.error);
111
- const converted = procFn(result.value);
109
+ ipcMain.on(rendererEmitChannel, (_, { error, value }) => {
110
+ if (error)
111
+ throw new Error(error);
112
+ const converted = procFn(value);
112
113
  handlers.forEach(handler => handler(converted));
113
114
  handlersOnce.forEach(handler => handler(converted));
114
115
  handlersOnce.clear();
@@ -140,17 +141,18 @@ function publicFunction(name, func, scopes = [enums_1.Scope.preload, enums_1.Sco
140
141
  const { functions } = publicInfo;
141
142
  const channel = enums_1.IPCChannel.functionCall + name;
142
143
  functions.add(name);
143
- if (!publicInfo.scopes.has(name))
144
+ if (!publicInfo.scopes.has(name)) {
144
145
  publicInfo.scopes.set(name, new Set(scopes));
146
+ }
145
147
  ipcMain.removeAllListeners(channel);
146
- ipcMain.on(channel, (e, ...args) => {
148
+ ipcMain.on(channel, (e, { args, secret }) => {
147
149
  try {
148
150
  const result = func(...args);
149
151
  if (result instanceof Promise) {
150
152
  const promiseChannel = channel + enums_1.IPCChannel.promisePostfix;
151
153
  result
152
- .then(value => e.sender.send(promiseChannel, { value }))
153
- .catch(reason => e.sender.send(promiseChannel, { error: String(reason) }));
154
+ .then(value => e.sender.send(promiseChannel, { value, secret }))
155
+ .catch(reason => e.sender.send(promiseChannel, { error: String(reason?.stack || reason), secret }));
154
156
  e.returnValue = { promiseChannel };
155
157
  }
156
158
  else {
@@ -158,7 +160,7 @@ function publicFunction(name, func, scopes = [enums_1.Scope.preload, enums_1.Sco
158
160
  }
159
161
  }
160
162
  catch (error) {
161
- e.returnValue = { error: String(error) };
163
+ e.returnValue = { error: String(error?.stack || error) };
162
164
  }
163
165
  });
164
166
  }
@@ -180,8 +182,9 @@ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.S
180
182
  const setChannel = enums_1.IPCChannel.propertySet + name;
181
183
  const { get: getter, set: setter } = value;
182
184
  properties.add(name);
183
- if (!publicInfo.scopes.has(name))
185
+ if (!publicInfo.scopes.has(name)) {
184
186
  publicInfo.scopes.set(name, new Set(scopes));
187
+ }
185
188
  if (!publicInfo.accesses.has(name)) {
186
189
  const accesses = [];
187
190
  getter && accesses.push(enums_1.Access.get);
@@ -195,25 +198,25 @@ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.S
195
198
  }
196
199
  if (getter) {
197
200
  ipcMain.removeAllListeners(getChannel);
198
- ipcMain.on(getChannel, event => {
201
+ ipcMain.on(getChannel, (event) => {
199
202
  try {
200
203
  const result = getter();
201
204
  event.returnValue = { value: result };
202
205
  }
203
206
  catch (error) {
204
- event.returnValue = { error };
207
+ event.returnValue = { error: String(error?.stack || error) };
205
208
  }
206
209
  });
207
210
  }
208
211
  if (setter) {
209
212
  ipcMain.removeAllListeners(setChannel);
210
- ipcMain.on(setChannel, (event, ...args) => {
213
+ ipcMain.on(setChannel, (event, { args }) => {
211
214
  try {
212
- setter(args[0]);
215
+ setter(args.at(0));
213
216
  event.returnValue = { value: null };
214
217
  }
215
218
  catch (error) {
216
- event.returnValue = { error };
219
+ event.returnValue = { error: String(error?.stack || error) };
217
220
  }
218
221
  });
219
222
  }
@@ -0,0 +1,47 @@
1
+ import type { IpcRendererEvent } from 'electron';
2
+ import type { EventHandler, EventUnsubscriber, IPCRequest, IPCResult } from '../types';
3
+ declare class FunctionTunnel {
4
+ #private;
5
+ constructor(name: string);
6
+ call(args?: IPCRequest): IPCResult;
7
+ }
8
+ declare class PropertyTunnel {
9
+ #private;
10
+ constructor(name: string);
11
+ get(): IPCResult;
12
+ set(value: any): IPCResult;
13
+ }
14
+ declare class PromiseTunnel {
15
+ #private;
16
+ constructor(channel: string);
17
+ wait(secret?: number): Promise<any>;
18
+ }
19
+ declare class MainEventTunnel {
20
+ #private;
21
+ constructor(name: string);
22
+ on(handler: EventHandler<IPCResult>): EventUnsubscriber;
23
+ once(handler: EventHandler<IPCResult>): EventUnsubscriber;
24
+ }
25
+ declare class RendererEventTunnel {
26
+ #private;
27
+ constructor(name: string);
28
+ emit(value: any): void;
29
+ error(error: string): void;
30
+ }
31
+ export default class IPCTunnel {
32
+ #private;
33
+ private channel;
34
+ static readonly Function: typeof FunctionTunnel;
35
+ static readonly Property: typeof PropertyTunnel;
36
+ static readonly Promise: typeof PromiseTunnel;
37
+ static readonly MainEvent: typeof MainEventTunnel;
38
+ static readonly RendererEvent: typeof RendererEventTunnel;
39
+ constructor(channel: string);
40
+ request(args?: IPCRequest): IPCResult;
41
+ response(args?: IPCResult): void;
42
+ on(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
43
+ once(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
44
+ removeListener(listener: (event: IpcRendererEvent, ...args: any[]) => void): void;
45
+ removeAllListeners(): void;
46
+ }
47
+ export {};
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const electron_1 = __importDefault(require("electron"));
7
+ const enums_1 = require("../enums");
8
+ class FunctionTunnel {
9
+ #tunnel;
10
+ constructor(name) {
11
+ this.#tunnel = new IPCTunnel(enums_1.IPCChannel.functionCall + name);
12
+ }
13
+ call(args = { args: [] }) {
14
+ return this.#tunnel.request(args);
15
+ }
16
+ }
17
+ class PropertyTunnel {
18
+ #getTunnel;
19
+ #setTunnel;
20
+ constructor(name) {
21
+ this.#getTunnel = new IPCTunnel(enums_1.IPCChannel.propertyGet + name);
22
+ this.#setTunnel = new IPCTunnel(enums_1.IPCChannel.propertySet + name);
23
+ }
24
+ get() {
25
+ return this.#getTunnel.request();
26
+ }
27
+ set(value) {
28
+ return this.#setTunnel.request({ args: [value] });
29
+ }
30
+ }
31
+ class PromiseTunnel {
32
+ #tunnel;
33
+ constructor(channel) {
34
+ this.#tunnel = new IPCTunnel(channel);
35
+ }
36
+ wait(secret) {
37
+ return new Promise((resolve, reject) => {
38
+ const handler = (_, { error, value, secret: resSecret }) => {
39
+ if (secret !== undefined && resSecret !== secret)
40
+ return;
41
+ if (error)
42
+ reject(error);
43
+ else
44
+ resolve(value);
45
+ this.#tunnel.removeListener(handler);
46
+ };
47
+ this.#tunnel.on(handler);
48
+ });
49
+ }
50
+ }
51
+ class MainEventTunnel {
52
+ #emitTunnel;
53
+ #onTunnel;
54
+ #onceTunnel;
55
+ constructor(name) {
56
+ this.#emitTunnel = new IPCTunnel(enums_1.IPCChannel.mainEventEmit + name);
57
+ this.#onTunnel = new IPCTunnel(enums_1.IPCChannel.mainEventOn + name);
58
+ this.#onceTunnel = new IPCTunnel(enums_1.IPCChannel.mainEventOnce + name);
59
+ }
60
+ on(handler) {
61
+ const listener = (_, result) => handler(result);
62
+ this.#onTunnel.response();
63
+ this.#emitTunnel.on(listener);
64
+ return () => this.#emitTunnel.removeListener(listener);
65
+ }
66
+ once(handler) {
67
+ const listener = (_, result) => handler(result);
68
+ this.#onceTunnel.response();
69
+ this.#emitTunnel.once(listener);
70
+ return () => this.#emitTunnel.removeListener(listener);
71
+ }
72
+ }
73
+ class RendererEventTunnel {
74
+ #emitTunnel;
75
+ constructor(name) {
76
+ this.#emitTunnel = new IPCTunnel(enums_1.IPCChannel.rendererEventEmit + name);
77
+ }
78
+ emit(value) {
79
+ this.#emitTunnel.response({ value });
80
+ }
81
+ error(error) {
82
+ this.#emitTunnel.response({ error });
83
+ }
84
+ }
85
+ class IPCTunnel {
86
+ channel;
87
+ #ipcRenderer = electron_1.default.ipcRenderer;
88
+ static Function = FunctionTunnel;
89
+ static Property = PropertyTunnel;
90
+ static Promise = PromiseTunnel;
91
+ static MainEvent = MainEventTunnel;
92
+ static RendererEvent = RendererEventTunnel;
93
+ constructor(channel) {
94
+ this.channel = channel;
95
+ }
96
+ request(args = { args: [] }) {
97
+ return this.#ipcRenderer.sendSync(this.channel, args);
98
+ }
99
+ response(args = {}) {
100
+ this.#ipcRenderer.send(this.channel, args);
101
+ }
102
+ on(listener) {
103
+ this.#ipcRenderer.on(this.channel, listener);
104
+ }
105
+ once(listener) {
106
+ this.#ipcRenderer.once(this.channel, listener);
107
+ }
108
+ removeListener(listener) {
109
+ this.#ipcRenderer.removeListener(this.channel, listener);
110
+ }
111
+ removeAllListeners() {
112
+ this.#ipcRenderer.removeAllListeners(this.channel);
113
+ }
114
+ }
115
+ exports.default = IPCTunnel;
package/cjs/types.d.ts CHANGED
@@ -1,81 +1,74 @@
1
1
  import type { Scope } from './enums';
2
2
  import { Access } from './enums';
3
- export interface ITarget {
3
+ export interface Target {
4
4
  as<T>(): T;
5
5
  }
6
6
  export type ClassMethodDecorator = <This, Args extends any[], Return>(method: (this: This, ...args: Args) => Return, context: ClassMethodDecoratorContext<This, (this: This, ...args: Args) => Return>) => void;
7
- export type ClassPropertyDecorator = <This, Type>(target: undefined, context: ClassFieldDecoratorContext<This, Type>) => void;
8
- export type ClassGetterDecorator = <This, Type>(getter: () => Type, context: ClassGetterDecoratorContext<This, Type>) => void;
9
- export type ClassSetterDecorator = <This, Type>(setter: (value: Type) => void, context: ClassSetterDecoratorContext<This, Type>) => void;
10
- export type PublishSetterArgs = Omit<IPublishPropertyArgs, 'access'>;
11
- export type PublishGetterArgs = Omit<IPublishPropertyArgs, 'access'>;
12
- export interface IIPCResult {
7
+ export type ClassPropertyDecorator = <This, Value>(target: undefined, context: ClassFieldDecoratorContext<This, Value>) => void;
8
+ export type ClassGetterDecorator = <This, Value>(getter: () => Value, context: ClassGetterDecoratorContext<This, Value>) => void;
9
+ export type ClassSetterDecorator = <This, Value>(setter: (value: Value) => void, context: ClassSetterDecoratorContext<This, Value>) => void;
10
+ export type PublishSetterArgs = Omit<PublishPropertyArgs, 'access'>;
11
+ export type PublishGetterArgs = Omit<PublishPropertyArgs, 'access'>;
12
+ export interface IPCResult {
13
13
  value?: any;
14
14
  error?: string;
15
15
  promiseChannel?: string;
16
+ secret?: number;
16
17
  }
17
- export interface IPublishMethodArgs {
18
+ export interface IPCRequest {
19
+ args: any[];
20
+ secret?: number;
21
+ }
22
+ export interface PublishMethodArgs {
18
23
  name?: string;
19
24
  scope?: Scope;
20
25
  }
21
- export interface IPublishMainEventArgs {
26
+ export interface PublishMainEventArgs {
22
27
  name?: string;
23
28
  scope?: Scope;
24
29
  }
25
- export interface IPublishRendererEventArgs {
30
+ export interface PublishRendererEventArgs {
26
31
  name?: string;
27
32
  scope?: Scope;
28
33
  procFn?: EventReceiver;
29
34
  }
30
- export interface IPublishPropertyArgs {
35
+ export interface PublishPropertyArgs {
31
36
  name?: string;
32
37
  scope?: Scope;
33
38
  access?: Access;
34
39
  }
35
- export interface ICreateProviderArgs {
36
- info: IInfo;
37
- scope: Scope;
38
- handleMainEvent(name: string, type: 'on' | 'once', handler: EventHandler<IIPCResult>): EventUnsubscriber;
39
- emitRendererEvent(name: string, arg: any): void;
40
- callFunction(name: string, ...args: any[]): IIPCResult;
41
- getVariable(name: string): IIPCResult;
42
- setVariable(name: string, value: any): IIPCResult | undefined;
43
- waitPromise(channel: string): Promise<any>;
44
- }
45
- export type Scopes = Map<string, Set<Scope>>;
46
- export type Accesses = Map<string, Set<Access>>;
47
40
  export type RendererEvent<T = void> = (handler: EventHandler<T>, isOnce?: boolean) => EventUnsubscriber;
48
41
  export type MainEvent<T = void> = (handler: EventHandler<T>) => EventUnsubscriber;
49
42
  export type EventEmitter = (...args: any[]) => any;
50
43
  export type EventReceiver<I = void, O = I> = (input: I) => O;
51
44
  export type EventUnsubscriber = () => void;
52
45
  export type EventHandler<T = void> = (result: T) => void;
53
- export interface IRendererPublic {
46
+ export type PublicFunction = (...args: any[]) => any;
47
+ export interface PublicProperty<T> {
48
+ get?(): T;
49
+ set?(value: T): void;
50
+ }
51
+ export interface RendererPublic {
54
52
  __register__?(instance: any): void;
55
53
  }
56
- export interface IProvider {
57
- getInfo(): IInfo;
58
- waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void): void;
54
+ export interface GlobalProvider {
55
+ getInfo(): Info;
56
+ waitPromise(name: string, resolve: (value: any) => void, reject?: (reason?: any) => void, secret?: number): void;
59
57
  provided: {
60
- functions: Record<string, (...args: any[]) => IIPCResult>;
61
- mainEvents: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
58
+ functions: Record<string, (secret: number, args: any[]) => IPCResult>;
59
+ mainEvents: Record<string, (type: 'on' | 'once', handler: EventHandler<IPCResult>) => EventUnsubscriber>;
62
60
  rendererEvents: Record<string, (arg: any) => void>;
63
61
  properties: Record<string, {
64
- get(): IIPCResult;
65
- set(val: any): void;
62
+ get(): IPCResult;
63
+ set(val: any): IPCResult;
66
64
  }>;
67
65
  };
68
66
  }
69
- export type PublicFunction = (...args: any[]) => any;
70
- export type PublicProperty<T> = {
71
- get?(): T;
72
- set?(value: T): void;
73
- };
74
- export interface IInfo {
67
+ export interface Info {
75
68
  properties: Set<string>;
76
69
  functions: Set<string>;
77
70
  mainEvents: Set<string>;
78
71
  rendererEvents: Set<string>;
79
- scopes: Scopes;
80
- accesses: Accesses;
72
+ scopes: Map<string, Set<Scope>>;
73
+ accesses: Map<string, Set<Access>>;
81
74
  }
@@ -0,0 +1,28 @@
1
+ import { Scope } from './enums';
2
+ export declare class NoSetAccessError extends Error {
3
+ constructor(name: string);
4
+ }
5
+ export declare class NoGetAccessError extends Error {
6
+ constructor(name: string);
7
+ }
8
+ export declare class NotProvidedIntoScopeError extends Error {
9
+ constructor(name: string, scope: Scope);
10
+ }
11
+ export declare class NotProvidedFromMain extends Error {
12
+ constructor();
13
+ }
14
+ export declare class GlobalProviderNotFoundError extends Error {
15
+ constructor();
16
+ }
17
+ export declare class TrySetReadonlyError extends Error {
18
+ constructor(name: string);
19
+ }
20
+ export declare class TryUseBridgeInPreload extends Error {
21
+ constructor();
22
+ }
23
+ export declare class TryUseMainInRenderer extends Error {
24
+ constructor();
25
+ }
26
+ export declare class TryUseMainInMain extends Error {
27
+ constructor();
28
+ }
package/esm/errors.js ADDED
@@ -0,0 +1,27 @@
1
+ export class NoSetAccessError extends Error {
2
+ constructor(name) { super(`No access to setting '${name}'`); }
3
+ }
4
+ export class NoGetAccessError extends Error {
5
+ constructor(name) { super(`No access to getting '${name}'`); }
6
+ }
7
+ export class NotProvidedIntoScopeError extends Error {
8
+ constructor(name, scope) { super(`'${name}' is not provided into ${scope} scope`); }
9
+ }
10
+ export class NotProvidedFromMain extends Error {
11
+ constructor() { super('Public methods from main is not provided. Call any publish methods to provide it from main process.'); }
12
+ }
13
+ export class GlobalProviderNotFoundError extends Error {
14
+ constructor() { super('Required methods not provided. Call "provideFromMain" in preload process.'); }
15
+ }
16
+ export class TrySetReadonlyError extends Error {
17
+ constructor(name) { super(`'${name}' is readonly`); }
18
+ }
19
+ export class TryUseBridgeInPreload extends Error {
20
+ constructor() { super('"Bridge" is unavailable in main process. In preload use "Main" instead.'); }
21
+ }
22
+ export class TryUseMainInRenderer extends Error {
23
+ constructor() { super('"Main" is unavailable in renderer process. Use "Bridge" instead'); }
24
+ }
25
+ export class TryUseMainInMain extends Error {
26
+ constructor() { super('"Main" is unavailable in main process.'); }
27
+ }
package/esm/exp.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { Access, Scope } from './enums';
2
- export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty } from './scripts/oldDecorators';
2
+ export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty } from './scripts/old-decorators';
3
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';
4
+ export type { EventEmitter, EventHandler, EventReceiver, EventUnsubscriber, MainEvent, PublicFunction, PublicProperty, PublishMainEventArgs, PublishMethodArgs, PublishPropertyArgs, PublishRendererEventArgs, RendererEvent } from './types';
package/esm/exp.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { Access, Scope } from './enums';
2
- export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty } from './scripts/oldDecorators';
2
+ export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty } from './scripts/old-decorators';
3
3
  export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';