emr-bridge 1.4.0 → 1.5.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.
@@ -8,8 +8,8 @@ const publicInfo = {
8
8
  functions: new Set(),
9
9
  mainEvents: new Set(),
10
10
  rendererEvents: new Set(),
11
- accesses: {},
12
- scopes: {}
11
+ accesses: new Map(),
12
+ scopes: new Map()
13
13
  };
14
14
  electron_1.ipcMain.on(enums_1.IPCChannel.getPublicInfo, e => e.returnValue = publicInfo);
15
15
  /**
@@ -26,8 +26,12 @@ function publicMainEvent(name, receiver = ((v = undefined) => v), scopes = [enum
26
26
  let senders = [];
27
27
  let sendersOnce = [];
28
28
  mainEvents.add(name);
29
- if (!publicInfo.scopes[name])
30
- publicInfo.scopes[name] = new Set(scopes);
29
+ const rendererNameOn = `on${name[0].toUpperCase()}${name.slice(1)}`;
30
+ const rendererNameOnce = `once${name[0].toUpperCase()}${name.slice(1)}`;
31
+ if (!publicInfo.scopes.has(rendererNameOn)) {
32
+ publicInfo.scopes.set(rendererNameOn, new Set(scopes));
33
+ publicInfo.scopes.set(rendererNameOnce, new Set(scopes));
34
+ }
31
35
  electron_1.ipcMain.removeAllListeners(mainHandleChannel);
32
36
  electron_1.ipcMain.removeAllListeners(mainHandleChannelOnce);
33
37
  electron_1.ipcMain.on(mainHandleChannel, ({ sender }) => !senders.includes(sender) && senders.push(sender));
@@ -82,8 +86,8 @@ function publicRendererEvent(name, receiver = ((v = undefined) => v), scopes = [
82
86
  const handlers = new Set();
83
87
  const handlersOnce = new Set();
84
88
  rendererEvents.add(name);
85
- if (!publicInfo.scopes[name])
86
- publicInfo.scopes[name] = new Set(scopes);
89
+ if (!publicInfo.scopes.has(name))
90
+ publicInfo.scopes.set(name, new Set(scopes));
87
91
  electron_1.ipcMain.removeAllListeners(rendererEmitChannel);
88
92
  electron_1.ipcMain.on(rendererEmitChannel, (_, result) => {
89
93
  if (result.error)
@@ -117,8 +121,8 @@ function publicFunction(name, func, scopes = [enums_1.Scope.preload, enums_1.Sco
117
121
  const { functions } = publicInfo;
118
122
  const channel = enums_1.IPCChannel.functionCall + name;
119
123
  functions.add(name);
120
- if (!publicInfo.scopes[name])
121
- publicInfo.scopes[name] = new Set(scopes);
124
+ if (!publicInfo.scopes.has(name))
125
+ publicInfo.scopes.set(name, new Set(scopes));
122
126
  electron_1.ipcMain.removeAllListeners(channel);
123
127
  electron_1.ipcMain.on(channel, (e, ...args) => {
124
128
  try {
@@ -154,16 +158,16 @@ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.S
154
158
  const setChannel = enums_1.IPCChannel.propertySet + name;
155
159
  const { get: getter, set: setter } = value;
156
160
  properties.add(name);
157
- if (!publicInfo.scopes[name])
158
- publicInfo.scopes[name] = new Set(scopes);
159
- if (!publicInfo.accesses[name]) {
161
+ if (!publicInfo.scopes.has(name))
162
+ publicInfo.scopes.set(name, new Set(scopes));
163
+ if (!publicInfo.accesses.has(name)) {
160
164
  const accesses = [];
161
165
  getter && accesses.push(enums_1.Access.get);
162
166
  setter && accesses.push(enums_1.Access.set);
163
- publicInfo.accesses[name] = new Set(accesses);
167
+ publicInfo.accesses.set(name, new Set(accesses));
164
168
  }
165
169
  else {
166
- const accesses = publicInfo.accesses[name];
170
+ const accesses = publicInfo.accesses.get(name);
167
171
  getter && accesses.add(enums_1.Access.get);
168
172
  setter && accesses.add(enums_1.Access.set);
169
173
  }
@@ -194,10 +198,10 @@ function publicVariable(name, value, scopes = [enums_1.Scope.renderer, enums_1.S
194
198
  }
195
199
  exports.publicVariable = publicVariable;
196
200
  function addScope(key, scope) {
197
- publicInfo.scopes[key] = new Set([scope]);
201
+ publicInfo.scopes.set(key, new Set([scope]));
198
202
  }
199
203
  exports.addScope = addScope;
200
204
  function addAccess(key, access) {
201
- publicInfo.accesses[key] = new Set([access]);
205
+ publicInfo.accesses.set(key, new Set([access]));
202
206
  }
203
207
  exports.addAccess = addAccess;
package/cjs/types.d.ts CHANGED
@@ -42,8 +42,8 @@ export interface ICreateProviderArgs {
42
42
  setVariable(name: string, value: any): IIPCResult | undefined;
43
43
  waitPromise(channel: string): Promise<any>;
44
44
  }
45
- export type Scopes = Record<string, Set<Scope>>;
46
- export type Accesses = Record<string, Set<Access>>;
45
+ export type Scopes = Map<string, Set<Scope>>;
46
+ export type Accesses = Map<string, Set<Access>>;
47
47
  export type RendererEvent<T = undefined> = (handler: EventHandler<T>, isOnce?: boolean) => EventUnsubscriber;
48
48
  export type MainEvent<T = undefined> = (handler: EventHandler<T>) => EventUnsubscriber;
49
49
  export type EventEmitter = (...args: any[]) => any;
@@ -60,7 +60,10 @@ export interface IProvider {
60
60
  functions: Record<string, (...args: any[]) => IIPCResult>;
61
61
  mainEvents: Record<string, (type: 'on' | 'once', handler: EventHandler<IIPCResult>) => EventUnsubscriber>;
62
62
  rendererEvents: Record<string, (arg: any) => void>;
63
- properties: Record<string, IIPCResult>;
63
+ properties: Record<string, {
64
+ get(): IIPCResult;
65
+ set(val: any): void;
66
+ }>;
64
67
  };
65
68
  }
66
69
  export type PublicFunction = (...args: any[]) => any;
package/esm/exp.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export { Access, Scope } from './enums';
2
- export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMainEvent, publicStaticMethod, publicStaticProperty, publicStaticRendererEvent } from './scripts/oldDecorators';
2
+ export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty } from './scripts/oldDecorators';
3
3
  export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
4
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, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMainEvent, publicStaticMethod, publicStaticProperty, publicStaticRendererEvent } from './scripts/oldDecorators';
2
+ export { providePublic, publicClassMainEvent, publicClassRendererEvent, publicMethod, publicProperty, publicStaticMethod, publicStaticProperty } from './scripts/oldDecorators';
3
3
  export { publicFunction, publicMainEvent, publicRendererEvent, publicVariable } from './scripts/publish';
@@ -10,8 +10,7 @@ const info = provider.getInfo();
10
10
  * _only for renderer process_
11
11
  */
12
12
  const Bridge = createProvider({
13
- info,
14
- scope: Scope.renderer,
13
+ info, scope: Scope.renderer,
15
14
  callFunction(name, ...args) {
16
15
  return provider.provided.functions[name](...args);
17
16
  },
@@ -22,11 +21,11 @@ const Bridge = createProvider({
22
21
  return provider.provided.mainEvents[name](type, handler);
23
22
  },
24
23
  getVariable(name) {
25
- return provider.provided.properties[name];
24
+ return provider.provided.properties[name].get();
26
25
  },
27
26
  setVariable(name, value) {
28
- provider.provided.properties[name] = value;
29
- return;
27
+ provider.provided.properties[name].set(value);
28
+ return undefined;
30
29
  },
31
30
  waitPromise(channel) {
32
31
  return new Promise((resolve, reject) => {
@@ -10,8 +10,7 @@ if (!info)
10
10
  * _only for preload process_
11
11
  */
12
12
  const Main = createProvider({
13
- info,
14
- scope: Scope.preload,
13
+ info, scope: Scope.preload,
15
14
  callFunction(name, ...args) {
16
15
  return ipcRenderer.sendSync(IPCChannel.functionCall + name, ...args);
17
16
  },
@@ -79,11 +79,11 @@ export function publicProperty(arg) {
79
79
  context.addInitializer(function () {
80
80
  publicVariable(name, {
81
81
  get: () => {
82
- // @ts-ignore
82
+ // @ts-expect-error
83
83
  return this[key];
84
84
  },
85
85
  set: (value) => {
86
- // @ts-ignore
86
+ // @ts-expect-error
87
87
  this[key] = value;
88
88
  }
89
89
  });
@@ -57,32 +57,6 @@ 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 publicStaticMainEvent(): 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 publicStaticMainEvent(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 publicStaticMainEvent(args: IPublishMainEventArgs): MethodDecorator;
86
60
  /**
87
61
  * Makes the event available for **renderer** and **preload** processes.
88
62
  *
@@ -115,60 +89,6 @@ export declare function publicClassMainEvent(name: string): MethodDecorator;
115
89
  * @param args - publish args
116
90
  */
117
91
  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
92
  /**
173
93
  * Makes the event from **renderer** and **preload** available for **main** process.
174
94
  *
@@ -25,51 +25,25 @@ export function publicMethod(arg) {
25
25
  };
26
26
  };
27
27
  }
28
- export function publicStaticMainEvent(arg) {
29
- return (target, key) => {
30
- let name = String(key);
31
- if (arg) {
32
- if (typeof arg === 'string') {
33
- name = arg;
34
- }
35
- else {
36
- name = arg.name ?? String(key);
37
- if (arg.scope)
38
- addScope(name, arg.scope);
39
- }
40
- }
41
- target[key] = publicMainEvent(name, target[key].bind(target));
42
- };
43
- }
44
28
  export function publicClassMainEvent(arg) {
45
29
  return (tgt, key, _) => {
46
30
  const target = tgt;
47
31
  const prevRegister = target.__register__ ?? (() => { });
48
32
  target.__register__ = instance => {
49
33
  prevRegister(instance);
50
- 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;
34
+ let name = String(key);
35
+ if (arg) {
36
+ if (typeof arg === 'string') {
37
+ name = arg;
38
+ }
39
+ else {
40
+ name = arg.name ?? String(key);
41
+ if (arg.scope)
42
+ addScope(name, arg.scope);
43
+ }
61
44
  }
62
- 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);
45
+ instance[key] = publicMainEvent(name, instance[key].bind(instance));
46
+ };
73
47
  };
74
48
  }
75
49
  export function publicClassRendererEvent(arg) {
@@ -78,7 +52,23 @@ export function publicClassRendererEvent(arg) {
78
52
  const prevRegister = target.__register__ ?? (() => { });
79
53
  target.__register__ = instance => {
80
54
  prevRegister(instance);
81
- publicStaticRendererEvent(arg)(instance, key);
55
+ let name = String(key);
56
+ let receiver = (v) => v;
57
+ if (arg) {
58
+ if (typeof arg === 'string') {
59
+ name = arg;
60
+ }
61
+ else if (typeof arg === 'object') {
62
+ name = arg.name ?? String(key);
63
+ receiver = arg.receiver ?? receiver;
64
+ if (arg.scope)
65
+ addScope(name, arg.scope);
66
+ }
67
+ else {
68
+ receiver = arg;
69
+ }
70
+ }
71
+ instance[key] = publicRendererEvent(name, receiver);
82
72
  };
83
73
  };
84
74
  }
@@ -23,61 +23,51 @@ function provideFromMain(contextIsolation = true) {
23
23
  properties: {},
24
24
  functions: {},
25
25
  mainEvents: {},
26
- rendererEvents: {}
26
+ rendererEvents: {},
27
27
  }
28
28
  };
29
29
  info.functions.forEach(funcName => {
30
- Object.defineProperty(provider.provided.functions, funcName, {
31
- value(...args) {
32
- return ipcRenderer.sendSync(IPCChannel.functionCall + funcName, ...args);
33
- },
34
- enumerable: true
35
- });
30
+ provider.provided.functions[funcName] = (...args) => {
31
+ return ipcRenderer.sendSync(IPCChannel.functionCall + funcName, ...args);
32
+ };
36
33
  });
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
- });
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
+ };
52
46
  });
53
- info.mainEvents.forEach(mainEventName => {
54
- Object.defineProperty(provider.provided.mainEvents, mainEventName, {
55
- value(type, handler) {
56
- const listener = (_, result) => handler(result);
57
- const emitChannel = IPCChannel.mainEventEmit + mainEventName;
58
- if (type === 'on') {
59
- ipcRenderer.on(emitChannel, listener);
60
- ipcRenderer.send(IPCChannel.mainEventOn + mainEventName);
61
- }
62
- else {
63
- ipcRenderer.once(emitChannel, listener);
64
- ipcRenderer.send(IPCChannel.mainEventOnce + mainEventName);
65
- }
66
- return () => ipcRenderer.removeListener(emitChannel, listener);
67
- },
68
- enumerable: true
69
- });
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
+ };
70
61
  });
71
62
  info.properties.forEach(propName => {
72
- Object.defineProperty(provider.provided.properties, propName, {
63
+ provider.provided.properties[propName] = {
73
64
  get() {
74
65
  return ipcRenderer.sendSync(IPCChannel.propertyGet + propName);
75
66
  },
76
67
  set(value) {
77
68
  ipcRenderer.sendSync(IPCChannel.propertySet + propName, value);
78
- },
79
- enumerable: true
80
- });
69
+ }
70
+ };
81
71
  });
82
72
  if (contextIsolation)
83
73
  contextBridge.exposeInMainWorld('__provider__', provider);