@lynker-desktop/electron-ipc 0.0.9-alpha.5 → 0.0.9-alpha.51

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/main/index.js CHANGED
@@ -1,8 +1,17 @@
1
1
  const events = require('events');
2
2
  const electron = require('electron');
3
+ const index = require('../common/index.js');
3
4
 
4
- // import { getRandomUUID } from '../common'
5
5
  exports.isInitialized = false;
6
+ /**
7
+ * 主进程 IPC 通信类
8
+ * 负责处理主进程与渲染进程之间的消息通信
9
+ * 使用单例模式确保全局唯一实例
10
+ *
11
+ * 修复说明:
12
+ * - 为每个请求生成唯一的 requestId,解决并发请求数据错乱问题
13
+ * - 确保请求和响应能够正确匹配,避免多个并发请求互相干扰
14
+ */
6
15
  class MainIPC {
7
16
  constructor() {
8
17
  this.eventEmitter = new events.EventEmitter();
@@ -14,54 +23,109 @@ class MainIPC {
14
23
  }
15
24
  /**
16
25
  * 发送给主进程消息
17
- * @param channel
18
- * @param args
19
- * @returns
26
+ * 使用唯一的 requestId 确保并发请求不会互相干扰
27
+ *
28
+ * @param channel 消息通道名称
29
+ * @param args 传递给处理器的参数
30
+ * @returns Promise<any> 返回处理结果
31
+ *
32
+ * 修复说明:
33
+ * - 为每个请求生成唯一的 requestId
34
+ * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复
35
+ * - 发送请求时包含 requestId,让处理器知道如何回复
20
36
  */
21
37
  async invokeMain(channel, ...args) {
22
- return new Promise(resolve => {
23
- this.eventEmitter.once(`${channel}-reply`, result => {
24
- resolve(result);
25
- });
26
- this.eventEmitter.emit(channel, ...args);
27
- });
28
- }
29
- /**
30
- * 处理一次性主进程发送过来的消息
31
- * @param channel
32
- * @param handler
33
- */
34
- handleMainOnce(channel, handler) {
35
- this.eventEmitter.once(channel, async (...args) => {
36
- const result = await handler(...args);
37
- this.eventEmitter.emit(`${channel}-reply`, result);
38
+ return new Promise((resolve, reject) => {
39
+ const requestId = index.getRandomUUID();
40
+ const replyChannel = `${channel}-reply-${requestId}`;
41
+ let isResolved = false;
42
+ const replyHandler = (result) => {
43
+ if (!isResolved) {
44
+ isResolved = true;
45
+ this.eventEmitter.removeListener(replyChannel, replyHandler);
46
+ clearTimeout(timeoutId);
47
+ resolve(result);
48
+ }
49
+ };
50
+ this.eventEmitter.once(replyChannel, replyHandler);
51
+ // 添加超时机制,确保监听器最终会被清理
52
+ const timeoutId = setTimeout(() => {
53
+ if (!isResolved) {
54
+ isResolved = true;
55
+ this.eventEmitter.removeListener(replyChannel, replyHandler);
56
+ reject(new Error(`IPC request timeout: ${channel} (30s)`));
57
+ }
58
+ }, 30000); // 30秒超时
59
+ this.eventEmitter.emit(channel, requestId, ...args);
38
60
  });
39
61
  }
40
62
  /**
41
63
  * 处理主进程发送过来的消息
42
- * @param channel
43
- * @param handler
64
+ * 持续监听指定通道的消息
65
+ *
66
+ * @param channel 消息通道名称
67
+ * @param handler 处理函数,接收除 requestId 外的所有参数
68
+ *
69
+ * 修复说明:
70
+ * - 接收 requestId 作为第一个参数
71
+ * - 使用 `${channel}-reply-${requestId}` 发送回复,确保回复给正确的请求
72
+ * - 支持多个并发请求,每个请求都有独立的回复通道
44
73
  */
45
74
  handleMain(channel, handler) {
46
- this.eventEmitter.on(channel, async (...args) => {
75
+ this.eventEmitter.on(channel, async (requestId, ...args) => {
47
76
  const result = await handler(...args);
48
- this.eventEmitter.emit(`${channel}-reply`, result);
77
+ this.eventEmitter.emit(`${channel}-reply-${requestId}`, result);
49
78
  });
79
+ return {
80
+ cancel: () => {
81
+ this.eventEmitter.removeAllListeners(channel);
82
+ }
83
+ };
50
84
  }
51
85
  /**
52
86
  * 发送给渲染进程消息
53
- * @param webContents
54
- * @param channel
55
- * @param args
56
- * @returns
87
+ * 使用唯一的 requestId 确保并发请求不会互相干扰
88
+ *
89
+ * @param webContents 目标渲染进程的 WebContents 对象
90
+ * @param channel 消息通道名称
91
+ * @param args 传递给渲染进程的参数
92
+ * @returns Promise<any> 返回渲染进程的处理结果
93
+ *
94
+ * 修复说明:
95
+ * - 为每个请求生成唯一的 requestId
96
+ * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复
97
+ * - 发送请求时包含 requestId,让渲染进程知道如何回复
98
+ * - 等待渲染进程加载完成后再发送消息,确保消息能够被正确接收
57
99
  */
58
100
  invokeRenderer(webContents, channel, ...args) {
59
- return new Promise(resolve => {
101
+ return new Promise((resolve, reject) => {
102
+ const requestId = index.getRandomUUID();
103
+ const replyChannel = `${channel}-reply-${requestId}`;
104
+ let isResolved = false;
60
105
  const sendMessage = () => {
61
- electron.ipcMain.once(`${channel}-reply`, (_event, result) => {
62
- resolve(result);
63
- });
64
- webContents.send(channel, ...args);
106
+ // 检查 webContents 是否已销毁
107
+ if (webContents.isDestroyed()) {
108
+ reject(new Error('WebContents has been destroyed'));
109
+ return;
110
+ }
111
+ const replyHandler = (_event, result) => {
112
+ if (!isResolved) {
113
+ isResolved = true;
114
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
115
+ clearTimeout(timeoutId);
116
+ resolve(result);
117
+ }
118
+ };
119
+ electron.ipcMain.once(replyChannel, replyHandler);
120
+ // 添加超时机制,确保监听器最终会被清理
121
+ const timeoutId = setTimeout(() => {
122
+ if (!isResolved) {
123
+ isResolved = true;
124
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
125
+ reject(new Error(`IPC request timeout: ${channel} (30s)`));
126
+ }
127
+ }, 30000); // 30秒超时
128
+ webContents.send(channel, requestId, ...args);
65
129
  };
66
130
  // 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息
67
131
  if (webContents.isLoading()) {
@@ -75,19 +139,55 @@ class MainIPC {
75
139
  });
76
140
  }
77
141
  /**
78
- * 发送给所有渲染进程消息
79
- * @param channel
80
- * @param args
81
- * @returns
82
- */
142
+ * 发送给所有渲染进程消息
143
+ * 向所有渲染进程发送消息并收集所有响应
144
+ *
145
+ * @param channel 消息通道名称
146
+ * @param args 传递给所有渲染进程的参数
147
+ * @returns Promise<any[]> 返回所有渲染进程的处理结果数组
148
+ *
149
+ * 修复说明:
150
+ * - 为每个请求生成唯一的 requestId
151
+ * - 收集所有渲染进程的响应,当所有响应都收到时才 resolve
152
+ * - 使用 `${channel}-reply-${requestId}` 确保只接收对应请求的回复
153
+ * - 如果没有渲染进程,直接返回空数组
154
+ * - 等待每个渲染进程加载完成后再发送消息
155
+ */
83
156
  invokeAllRenderer(channel, ...args) {
84
157
  return new Promise(resolve => {
158
+ const requestId = index.getRandomUUID();
159
+ let responseCount = 0;
160
+ const totalWebContents = electron.webContents.getAllWebContents().length;
161
+ if (totalWebContents === 0) {
162
+ resolve([]);
163
+ return;
164
+ }
165
+ const responses = [];
166
+ const replyChannel = `${channel}-reply-${requestId}`;
167
+ // 只注册一个监听器,避免内存泄漏
168
+ // 使用 on 而不是 once,因为需要接收多个响应
169
+ const replyHandler = (_event, result) => {
170
+ responses.push(result);
171
+ responseCount++;
172
+ if (responseCount === totalWebContents) {
173
+ // 清理监听器
174
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
175
+ clearTimeout(timeoutId);
176
+ resolve(responses);
177
+ }
178
+ };
179
+ electron.ipcMain.on(replyChannel, replyHandler);
180
+ // 添加超时机制,确保监听器最终会被清理
181
+ const timeoutId = setTimeout(() => {
182
+ electron.ipcMain.removeListener(replyChannel, replyHandler);
183
+ if (responseCount < totalWebContents) {
184
+ // 超时后返回已收到的响应
185
+ resolve(responses);
186
+ }
187
+ }, 30000); // 30秒超时
85
188
  electron.webContents.getAllWebContents().forEach(webContent => {
86
189
  const sendMessage = () => {
87
- electron.ipcMain.once(`${channel}-reply`, (_event, result) => {
88
- resolve(result);
89
- });
90
- webContent.send(channel, ...args);
190
+ webContent.send(channel, requestId, ...args);
91
191
  };
92
192
  // 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息
93
193
  if (webContent.isLoading()) {
@@ -101,39 +201,36 @@ class MainIPC {
101
201
  });
102
202
  });
103
203
  }
104
- /**
105
- * 处理一次性渲染进程发送过来的消息
106
- * @param channel
107
- * @param handler
108
- */
109
- handleRendererOnce(channel, handler) {
110
- electron.ipcMain.once(channel, async (_event, ...args) => {
111
- const result = await handler(...args);
112
- _event.sender.send(`${channel}-reply`, result);
113
- });
114
- }
115
204
  /**
116
205
  * 处理渲染进程发送过来的消息
117
- * @param channel
118
- * @param handler
206
+ * 持续监听指定通道的消息,支持超时处理
207
+ *
208
+ * @param channel 消息通道名称
209
+ * @param handler 处理函数,接收除 requestId 外的所有参数
210
+ *
211
+ * 修复说明:
212
+ * - 接收 requestId 作为第一个参数
213
+ * - 使用 ipcMain.handle 替代 ipcMain.on,提供更好的错误处理
214
+ * - 支持 8 秒超时机制,避免长时间等待
215
+ * - 支持并发请求,每个请求都有独立的处理流程
216
+ * - 提供详细的错误日志记录
119
217
  */
120
218
  handleRenderer(channel, handler) {
121
219
  try {
122
220
  electron.ipcMain.handle(channel, async (event, ...args) => {
123
221
  try {
124
222
  const timeout = () => {
125
- return new Promise((_, reject) => {
223
+ return new Promise((resolve, reject) => {
126
224
  setTimeout(() => {
127
225
  reject(new Error('请求超时'));
128
226
  }, 1000 * 8); // 设定超时时间为8秒
129
227
  });
130
228
  };
131
229
  const processing = () => {
132
- return new Promise(async (_, reject) => {
230
+ return new Promise(async (resolve, reject) => {
133
231
  try {
134
232
  const data = await handler(...args);
135
- // console.error(`[${channel}] processing data: `, data, args);
136
- _(data);
233
+ resolve(data);
137
234
  }
138
235
  catch (error) {
139
236
  reject(error);
@@ -146,53 +243,80 @@ class MainIPC {
146
243
  return result; // 返回正常结果
147
244
  }
148
245
  catch (error) {
149
- console.error('SDK handleRenderer error: ', `channel: ${channel}`, error);
246
+ log('error', 'SDK handleRenderer error: ', `channel: ${channel}`, error);
150
247
  // @ts-ignore
151
248
  return error?.message; // 返回超时错误信息
152
249
  }
153
250
  });
154
251
  }
155
252
  catch (error) {
156
- console.error('SDK handleRenderer error: ', error);
253
+ log('error', 'SDK handleRenderer error: ', error);
157
254
  }
255
+ return {
256
+ cancel: () => {
257
+ electron.ipcMain.removeHandler(channel);
258
+ }
259
+ };
158
260
  }
159
261
  /**
160
- * 初始化
262
+ * 初始化消息中继功能
263
+ * 设置消息转发和回复机制,支持跨渲染进程通信
264
+ *
265
+ * 功能说明:
266
+ * - relay-message: 转发消息到指定的渲染进程或所有渲染进程
267
+ * - relay-reply: 处理回复消息,广播给所有渲染进程
268
+ * - __GetCurrentWebContentId__: 获取当前 WebContent ID
269
+ * - __OpenCurrentWebContentDevTools__: 打开当前 WebContent 的开发者工具
161
270
  */
162
271
  relayMessage() {
163
- electron.ipcMain.on('relay-message', (_event, { targetWebContentId, channel, args }) => {
272
+ // 防止重复注册监听器:先移除再添加
273
+ const relayMessageHandler = (_event, { targetWebContentId, channel, requestId, args }) => {
274
+ log('log', 'relay-message', { targetWebContentId, channel, requestId, args });
164
275
  if (targetWebContentId) {
276
+ // 转发到指定的渲染进程
165
277
  const targetWebContent = electron.webContents.fromId(targetWebContentId);
166
278
  if (targetWebContent) {
167
- targetWebContent.send(channel, ...args);
279
+ targetWebContent.send(channel, requestId, ...args);
168
280
  }
169
281
  }
170
282
  else {
283
+ // 广播到所有渲染进程
171
284
  electron.webContents.getAllWebContents().forEach(webContent => {
172
- webContent.send(channel, ...args);
285
+ webContent.send(channel, requestId, ...args);
173
286
  });
174
287
  }
175
- });
176
- electron.ipcMain.on('relay-reply', (_event, { originalChannel, result }) => {
177
- // 所有渲染进程都会接收 ${originalChannel}-reply 事件,要求仅需要的渲染进程处理该事件的回复。
178
- // 这样可以避免需要指定窗口 ID 回复消息,并确保消息能够正确传递和接收
288
+ };
289
+ electron.ipcMain.removeListener('relay-message', relayMessageHandler);
290
+ electron.ipcMain.on('relay-message', relayMessageHandler);
291
+ // 处理回复消息广播
292
+ const relayReplyHandler = (_event, { originalChannel, requestId, result }) => {
293
+ log('log', 'relay-reply', { originalChannel, requestId, result });
294
+ // 使用 requestId 确保回复发送给正确的请求
295
+ // 所有渲染进程都会接收 ${originalChannel}-reply-${requestId} 事件
296
+ // 只有对应的请求才会处理该回复,避免数据错乱
179
297
  electron.webContents.getAllWebContents().forEach(webContent => {
180
- webContent.send(`${originalChannel}-reply`, result);
298
+ webContent.send(`${originalChannel}-reply-${requestId}`, result);
181
299
  });
182
- });
300
+ };
301
+ electron.ipcMain.removeListener('relay-reply', relayReplyHandler);
302
+ electron.ipcMain.on('relay-reply', relayReplyHandler);
303
+ // 获取当前 WebContent ID 的通道
183
304
  const getCurrentWebContentIdChannel = '__GetCurrentWebContentId__';
184
- electron.ipcMain.on(getCurrentWebContentIdChannel, (_event) => {
305
+ const getCurrentWebContentIdHandler = (_event) => {
185
306
  try {
186
307
  _event.frameId;
187
308
  const webContentId = _event?.sender?.id;
188
309
  _event.sender.send(`${getCurrentWebContentIdChannel}-reply`, webContentId);
189
310
  }
190
311
  catch (error) {
191
- return undefined;
312
+ log('error', 'getCurrentWebContentId error:', error);
192
313
  }
193
- });
314
+ };
315
+ electron.ipcMain.removeListener(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler);
316
+ electron.ipcMain.on(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler);
317
+ // 打开当前 WebContent 开发者工具的通道
194
318
  const openCurrentWebContentDevToolsChannel = '__OpenCurrentWebContentDevTools__';
195
- electron.ipcMain.on(openCurrentWebContentDevToolsChannel, (_event) => {
319
+ const openCurrentWebContentDevToolsHandler = (_event) => {
196
320
  try {
197
321
  _event.frameId;
198
322
  const webContentId = _event?.sender?.id;
@@ -200,13 +324,30 @@ class MainIPC {
200
324
  _event.sender.send(`${openCurrentWebContentDevToolsChannel}-reply`, webContentId);
201
325
  }
202
326
  catch (error) {
203
- return undefined;
327
+ log('error', 'openCurrentWebContentDevTools error:', error);
204
328
  }
205
- });
329
+ };
330
+ electron.ipcMain.removeListener(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler);
331
+ electron.ipcMain.on(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler);
206
332
  }
207
333
  }
334
+ /**
335
+ * 全局 MainIPC 实例
336
+ * 使用全局变量确保单例模式,避免重复创建实例
337
+ */
208
338
  // @ts-ignore
209
339
  const mainIPC = global['__ELECTRON_IPC__'] ? global['__ELECTRON_IPC__'] : (global['__ELECTRON_IPC__'] = new MainIPC());
340
+ /**
341
+ * 初始化 IPC 通信系统
342
+ * 设置消息中继功能,确保跨进程通信正常工作
343
+ *
344
+ * @returns MainIPC 实例
345
+ *
346
+ * 功能说明:
347
+ * - 检查是否已经初始化,避免重复初始化
348
+ * - 设置消息中继功能,支持跨渲染进程通信
349
+ * - 返回全局 MainIPC 实例
350
+ */
210
351
  const initialize = () => {
211
352
  // @ts-ignore
212
353
  if (exports.isInitialized && global['__ELECTRON_IPC__']) {
@@ -219,6 +360,22 @@ const initialize = () => {
219
360
  // @ts-ignore
220
361
  return global['__ELECTRON_IPC__'];
221
362
  };
363
+ /**
364
+ * 日志记录工具函数
365
+ * 提供统一的日志格式和错误处理
366
+ *
367
+ * @param type 日志类型:'log' 或 'error'
368
+ * @param data 要记录的数据
369
+ */
370
+ const log = (type, ...data) => {
371
+ const key = `[electron-ipc]: `;
372
+ try {
373
+ console[type](key, ...data);
374
+ }
375
+ catch (error) {
376
+ console.error(key, error);
377
+ }
378
+ };
222
379
 
223
380
  exports.initialize = initialize;
224
381
  exports.mainIPC = mainIPC;
package/main/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/src/main/index.ts"],"sourcesContent":["import { EventEmitter } from 'events'\nimport { ipcMain, webContents } from 'electron'\n// import { getRandomUUID } from '../common'\n\nexport let isInitialized = false;\nclass MainIPC {\n\tstatic instance: MainIPC\n\tprivate eventEmitter: EventEmitter = new EventEmitter()\n\n\tconstructor() {\n\t\tif (!MainIPC.instance) {\n\t\t\tthis.eventEmitter = new EventEmitter()\n\t\t\tMainIPC.instance = this\n\t\t}\n\t\treturn MainIPC.instance\n\t}\n\n /**\n * 发送给主进程消息\n * @param channel\n * @param args\n * @returns\n */\n\tasync invokeMain(channel: string, ...args: any[]) {\n\t\treturn new Promise(resolve => {\n\t\t\tthis.eventEmitter.once(`${channel}-reply`, result => {\n\t\t\t\tresolve(result)\n\t\t\t})\n\t\t\tthis.eventEmitter.emit(channel, ...args)\n\t\t})\n\t}\n\n /**\n * 处理一次性主进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleMainOnce(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tthis.eventEmitter.once(channel, async (...args) => {\n\t\t\tconst result = await handler(...args)\n\t\t\tthis.eventEmitter.emit(`${channel}-reply`, result)\n\t\t})\n\t}\n\n /**\n * 处理主进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleMain(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tthis.eventEmitter.on(channel, async (...args) => {\n\t\t\tconst result = await handler(...args)\n\t\t\tthis.eventEmitter.emit(`${channel}-reply`, result)\n\t\t})\n\t}\n\n /**\n * 发送给渲染进程消息\n * @param webContents\n * @param channel\n * @param args\n * @returns\n */\n\tinvokeRenderer(webContents: Electron.WebContents, channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise(resolve => {\n\t\t\tconst sendMessage = () => {\n\t\t\t\tipcMain.once(`${channel}-reply`, (_event, result) => {\n\t\t\t\t\tresolve(result)\n\t\t\t\t})\n\t\t\t\twebContents.send(channel, ...args)\n\t\t\t}\n\n\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\tif (webContents.isLoading()) {\n\t\t\t\twebContents.once('did-finish-load', () => {\n\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tsendMessage()\n\t\t\t}\n\t\t})\n\t}\n\n /**\n * 发送给所有渲染进程消息\n * @param channel\n * @param args\n * @returns\n */\n\tinvokeAllRenderer(channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise(resolve => {\n\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\tconst sendMessage = () => {\n\t\t\t\t\tipcMain.once(`${channel}-reply`, (_event, result) => {\n\t\t\t\t\t\tresolve(result)\n\t\t\t\t\t})\n\t\t\t\t\twebContent.send(channel, ...args)\n\t\t\t\t}\n\n\t\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\t\tif (webContent.isLoading()) {\n\t\t\t\t\twebContent.once('did-finish-load', () => {\n\t\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tsendMessage()\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n /**\n * 处理一次性渲染进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleRendererOnce(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tipcMain.once(channel, async (_event, ...args: any[]) => {\n\t\t\tconst result = await handler(...args)\n\t\t\t_event.sender.send(`${channel}-reply`, result)\n\t\t})\n\t}\n /**\n * 处理渲染进程发送过来的消息\n * @param channel\n * @param handler\n */\n\thandleRenderer(channel: string, handler: (...args: any[]) => Promise<any>) {\n try {\n ipcMain.handle(channel, async (event, ...args) => {\n try {\n const timeout = () => {\n return new Promise((_, reject) => {\n setTimeout(() => {\n reject(new Error('请求超时'));\n }, 1000 * 8); // 设定超时时间为8秒\n });\n }\n\n const processing = () => {\n return new Promise(async (_, reject) => {\n try {\n const data = await handler(...args);\n // console.error(`[${channel}] processing data: `, data, args);\n _(data);\n } catch (error) {\n reject(error);\n }\n });\n }\n // 这里是实际的异步操作\n const result = await Promise.race([processing(), timeout()]);\n // console.error(`[${channel}] handleRenderer result: `, result)\n return result; // 返回正常结果\n } catch (error) {\n console.error('SDK handleRenderer error: ', `channel: ${channel}`, error);\n // @ts-ignore\n return error?.message; // 返回超时错误信息\n }\n });\n } catch (error) {\n console.error('SDK handleRenderer error: ', error);\n }\n\t}\n /**\n * 初始化\n */\n\trelayMessage() {\n\t\tipcMain.on('relay-message', (_event, { targetWebContentId, channel, args }) => {\n\t\t\tif (targetWebContentId) {\n\t\t\t\tconst targetWebContent = webContents.fromId(targetWebContentId)\n\t\t\t\tif (targetWebContent) {\n\t\t\t\t\ttargetWebContent.send(channel, ...args)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\t\twebContent.send(channel, ...args)\n\t\t\t\t})\n\t\t\t}\n\t\t})\n\n\t\tipcMain.on('relay-reply', (_event, { originalChannel, result }) => {\n\t\t\t// 所有渲染进程都会接收 ${originalChannel}-reply 事件,要求仅需要的渲染进程处理该事件的回复。\n\t\t\t// 这样可以避免需要指定窗口 ID 回复消息,并确保消息能够正确传递和接收\n webContents.getAllWebContents().forEach(webContent => {\n webContent.send(`${originalChannel}-reply`, result)\n })\n\t\t})\n\n const getCurrentWebContentIdChannel = '__GetCurrentWebContentId__'\n ipcMain.on(getCurrentWebContentIdChannel, (_event) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n _event.sender.send(`${getCurrentWebContentIdChannel}-reply`, webContentId)\n } catch (error) {\n return undefined\n }\n\t\t})\n const openCurrentWebContentDevToolsChannel = '__OpenCurrentWebContentDevTools__'\n ipcMain.on(openCurrentWebContentDevToolsChannel, (_event) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n webContents.fromId(webContentId)?.openDevTools();\n _event.sender.send(`${openCurrentWebContentDevToolsChannel}-reply`, webContentId)\n } catch (error) {\n return undefined\n }\n\t\t})\n\n\t}\n\n}\n// @ts-ignore\nexport const mainIPC: MainIPC = global['__ELECTRON_IPC__'] ? global['__ELECTRON_IPC__'] : (global['__ELECTRON_IPC__'] = new MainIPC());\n\nexport const initialize = () => {\n // @ts-ignore\n if (isInitialized && global['__ELECTRON_IPC__']){\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n }\n isInitialized = true;\n // @ts-ignore\n global['__ELECTRON_IPC__'].relayMessage()\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n}\n\n\nconst log = (type: 'log' | 'error', ...data: any[]) => {\n const key = `[electron-ipc]: `\n try {\n console[type](key, ...data);\n } catch (error) {\n console.error(key, error)\n }\n}\n\n"],"names":["isInitialized","EventEmitter","ipcMain","webContents"],"mappings":";;;AAEA;AAEWA,qBAAa,GAAG,MAAM;AACjC,MAAM,OAAO,CAAA;AAIZ,IAAA,WAAA,GAAA;AAFQ,QAAA,IAAA,CAAA,YAAY,GAAiB,IAAIC,mBAAY,EAAE,CAAA;AAGtD,QAAA,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;AACtB,YAAA,IAAI,CAAC,YAAY,GAAG,IAAIA,mBAAY,EAAE,CAAA;AACtC,YAAA,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAA;SACvB;QACD,OAAO,OAAO,CAAC,QAAQ,CAAA;KACvB;AAEA;;;;;AAKG;AACJ,IAAA,MAAM,UAAU,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;AAC/C,QAAA,OAAO,IAAI,OAAO,CAAC,OAAO,IAAG;YAC5B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAG,EAAA,OAAO,CAAQ,MAAA,CAAA,EAAE,MAAM,IAAG;gBACnD,OAAO,CAAC,MAAM,CAAC,CAAA;AAChB,aAAC,CAAC,CAAA;YACF,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,CAAA;AACzC,SAAC,CAAC,CAAA;KACF;AAEA;;;;AAIG;IACJ,cAAc,CAAC,OAAe,EAAE,OAAyC,EAAA;AACxE,QAAA,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,KAAI;YACjD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,IAAI,CAAC,CAAA;YACrC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAG,EAAA,OAAO,CAAQ,MAAA,CAAA,EAAE,MAAM,CAAC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,EAAE,CAAC;gBACxCC,oBAAW,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,YAAY,EAAE,CAAC;gBACjD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAG,EAAA,oCAAoC,CAAQ,MAAA,CAAA,EAAE,YAAY,CAAC,CAAA;aAClF;YAAC,OAAO,KAAK,EAAE;AACd,gBAAA,OAAO,SAAS,CAAA;aACjB;AACL,SAAC,CAAC,CAAA;KAEF;AAED,CAAA;AACD;AACO,MAAM,OAAO,GAAY,MAAM,CAAC,kBAAkB,CAAC,GAAG,MAAM,CAAC,kBAAkB,CAAC,IAAI,MAAM,CAAC,kBAAkB,CAAC,GAAG,IAAI,OAAO,EAAE,EAAE;AAEhI,MAAM,UAAU,GAAG,MAAK;;AAE7B,IAAA,IAAIH,qBAAa,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAC;;AAE9C,QAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;KAClC;IACDA,qBAAa,GAAG,IAAI,CAAC;;AAErB,IAAA,MAAM,CAAC,kBAAkB,CAAC,CAAC,YAAY,EAAE,CAAA;;AAEzC,IAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;AACnC;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/src/main/index.ts"],"sourcesContent":["import { EventEmitter } from 'events'\nimport { ipcMain, webContents } from 'electron'\nimport { getRandomUUID } from '../common'\n\nexport let isInitialized = false;\n\n/**\n * 主进程 IPC 通信类\n * 负责处理主进程与渲染进程之间的消息通信\n * 使用单例模式确保全局唯一实例\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId,解决并发请求数据错乱问题\n * - 确保请求和响应能够正确匹配,避免多个并发请求互相干扰\n */\nclass MainIPC {\n\tstatic instance: MainIPC\n\tprivate eventEmitter: EventEmitter = new EventEmitter()\n\n\tconstructor() {\n\t\tif (!MainIPC.instance) {\n\t\t\tthis.eventEmitter = new EventEmitter()\n\t\t\tMainIPC.instance = this\n\t\t}\n\t\treturn MainIPC.instance\n\t}\n\n /**\n * 发送给主进程消息\n * 使用唯一的 requestId 确保并发请求不会互相干扰\n *\n * @param channel 消息通道名称\n * @param args 传递给处理器的参数\n * @returns Promise<any> 返回处理结果\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId\n * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复\n * - 发送请求时包含 requestId,让处理器知道如何回复\n */\n\tasync invokeMain(channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst requestId = getRandomUUID()\n\t\t\tconst replyChannel = `${channel}-reply-${requestId}`\n\t\t\tlet isResolved = false\n\n\t\t\tconst replyHandler = (result: any) => {\n\t\t\t\tif (!isResolved) {\n\t\t\t\t\tisResolved = true\n\t\t\t\t\tthis.eventEmitter.removeListener(replyChannel, replyHandler)\n\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\tresolve(result)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthis.eventEmitter.once(replyChannel, replyHandler)\n\n\t\t\t// 添加超时机制,确保监听器最终会被清理\n\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\tif (!isResolved) {\n\t\t\t\t\tisResolved = true\n\t\t\t\t\tthis.eventEmitter.removeListener(replyChannel, replyHandler)\n\t\t\t\t\treject(new Error(`IPC request timeout: ${channel} (30s)`))\n\t\t\t\t}\n\t\t\t}, 30000) // 30秒超时\n\n\t\t\tthis.eventEmitter.emit(channel, requestId, ...args)\n\t\t})\n\t}\n\n /**\n * 处理主进程发送过来的消息\n * 持续监听指定通道的消息\n *\n * @param channel 消息通道名称\n * @param handler 处理函数,接收除 requestId 外的所有参数\n *\n * 修复说明:\n * - 接收 requestId 作为第一个参数\n * - 使用 `${channel}-reply-${requestId}` 发送回复,确保回复给正确的请求\n * - 支持多个并发请求,每个请求都有独立的回复通道\n */\n\thandleMain(channel: string, handler: (...args: any[]) => Promise<any>) {\n\t\tthis.eventEmitter.on(channel, async (requestId: string, ...args) => {\n\t\t\tconst result = await handler(...args)\n\t\t\tthis.eventEmitter.emit(`${channel}-reply-${requestId}`, result)\n\t\t})\n return {\n cancel: () => {\n this.eventEmitter.removeAllListeners(channel)\n }\n };\n\t}\n\n /**\n * 发送给渲染进程消息\n * 使用唯一的 requestId 确保并发请求不会互相干扰\n *\n * @param webContents 目标渲染进程的 WebContents 对象\n * @param channel 消息通道名称\n * @param args 传递给渲染进程的参数\n * @returns Promise<any> 返回渲染进程的处理结果\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId\n * - 监听 `${channel}-reply-${requestId}` 事件,确保只接收对应请求的回复\n * - 发送请求时包含 requestId,让渲染进程知道如何回复\n * - 等待渲染进程加载完成后再发送消息,确保消息能够被正确接收\n */\n\tinvokeRenderer(webContents: Electron.WebContents, channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst requestId = getRandomUUID()\n\t\t\tconst replyChannel = `${channel}-reply-${requestId}`\n\t\t\tlet isResolved = false\n\n\t\t\tconst sendMessage = () => {\n\t\t\t\t// 检查 webContents 是否已销毁\n\t\t\t\tif (webContents.isDestroyed()) {\n\t\t\t\t\treject(new Error('WebContents has been destroyed'))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst replyHandler = (_event: Electron.IpcMainEvent, result: any) => {\n\t\t\t\t\tif (!isResolved) {\n\t\t\t\t\t\tisResolved = true\n\t\t\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\t\tresolve(result)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tipcMain.once(replyChannel, replyHandler)\n\n\t\t\t\t// 添加超时机制,确保监听器最终会被清理\n\t\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\t\tif (!isResolved) {\n\t\t\t\t\t\tisResolved = true\n\t\t\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\t\t\treject(new Error(`IPC request timeout: ${channel} (30s)`))\n\t\t\t\t\t}\n\t\t\t\t}, 30000) // 30秒超时\n\n\t\t\t\twebContents.send(channel, requestId, ...args)\n\t\t\t}\n\n\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\tif (webContents.isLoading()) {\n\t\t\t\twebContents.once('did-finish-load', () => {\n\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t})\n\t\t\t} else {\n\t\t\t\tsendMessage()\n\t\t\t}\n\t\t})\n\t}\n\n /**\n * 发送给所有渲染进程消息\n * 向所有渲染进程发送消息并收集所有响应\n *\n * @param channel 消息通道名称\n * @param args 传递给所有渲染进程的参数\n * @returns Promise<any[]> 返回所有渲染进程的处理结果数组\n *\n * 修复说明:\n * - 为每个请求生成唯一的 requestId\n * - 收集所有渲染进程的响应,当所有响应都收到时才 resolve\n * - 使用 `${channel}-reply-${requestId}` 确保只接收对应请求的回复\n * - 如果没有渲染进程,直接返回空数组\n * - 等待每个渲染进程加载完成后再发送消息\n */\n\tinvokeAllRenderer(channel: string, ...args: any[]): Promise<any> {\n\t\treturn new Promise(resolve => {\n\t\t\tconst requestId = getRandomUUID()\n\t\t\tlet responseCount = 0\n\t\t\tconst totalWebContents = webContents.getAllWebContents().length\n\n\t\t\tif (totalWebContents === 0) {\n\t\t\t\tresolve([])\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst responses: any[] = []\n\t\t\tconst replyChannel = `${channel}-reply-${requestId}`\n\n\t\t\t// 只注册一个监听器,避免内存泄漏\n\t\t\t// 使用 on 而不是 once,因为需要接收多个响应\n\t\t\tconst replyHandler = (_event: Electron.IpcMainEvent, result: any) => {\n\t\t\t\tresponses.push(result)\n\t\t\t\tresponseCount++\n\t\t\t\tif (responseCount === totalWebContents) {\n\t\t\t\t\t// 清理监听器\n\t\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\t\tclearTimeout(timeoutId)\n\t\t\t\t\tresolve(responses)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tipcMain.on(replyChannel, replyHandler)\n\n\t\t\t// 添加超时机制,确保监听器最终会被清理\n\t\t\tconst timeoutId = setTimeout(() => {\n\t\t\t\tipcMain.removeListener(replyChannel, replyHandler)\n\t\t\t\tif (responseCount < totalWebContents) {\n\t\t\t\t\t// 超时后返回已收到的响应\n\t\t\t\t\tresolve(responses)\n\t\t\t\t}\n\t\t\t}, 30000) // 30秒超时\n\n\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\tconst sendMessage = () => {\n\t\t\t\t\twebContent.send(channel, requestId, ...args)\n\t\t\t\t}\n\n\t\t\t\t// 等待渲染进程加载完成后再推送,否则会导致渲染进程收不到消息\n\t\t\t\tif (webContent.isLoading()) {\n\t\t\t\t\twebContent.once('did-finish-load', () => {\n\t\t\t\t\t\tsetTimeout(() => sendMessage())\n\t\t\t\t\t})\n\t\t\t\t} else {\n\t\t\t\t\tsendMessage()\n\t\t\t\t}\n\t\t\t})\n\t\t})\n\t}\n\n /**\n * 处理渲染进程发送过来的消息\n * 持续监听指定通道的消息,支持超时处理\n *\n * @param channel 消息通道名称\n * @param handler 处理函数,接收除 requestId 外的所有参数\n *\n * 修复说明:\n * - 接收 requestId 作为第一个参数\n * - 使用 ipcMain.handle 替代 ipcMain.on,提供更好的错误处理\n * - 支持 8 秒超时机制,避免长时间等待\n * - 支持并发请求,每个请求都有独立的处理流程\n * - 提供详细的错误日志记录\n */\n\thandleRenderer(channel: string, handler: (...args: any[]) => Promise<any>) {\n try {\n ipcMain.handle(channel, async (event, ...args) => {\n try {\n const timeout = () => {\n return new Promise((resolve, reject) => {\n setTimeout(() => {\n reject(new Error('请求超时'));\n }, 1000 * 8); // 设定超时时间为8秒\n });\n }\n\n const processing = () => {\n return new Promise(async (resolve, reject) => {\n try {\n const data = await handler(...args);\n resolve(data);\n } catch (error) {\n reject(error);\n }\n });\n }\n // 这里是实际的异步操作\n const result = await Promise.race([processing(), timeout()]);\n // console.error(`[${channel}] handleRenderer result: `, result)\n return result; // 返回正常结果\n } catch (error) {\n log('error', 'SDK handleRenderer error: ', `channel: ${channel}`, error);\n // @ts-ignore\n return error?.message; // 返回超时错误信息\n }\n });\n } catch (error) {\n log('error', 'SDK handleRenderer error: ', error);\n }\n return {\n cancel: () => {\n ipcMain.removeHandler(channel)\n }\n };\n\t}\n /**\n * 初始化消息中继功能\n * 设置消息转发和回复机制,支持跨渲染进程通信\n *\n * 功能说明:\n * - relay-message: 转发消息到指定的渲染进程或所有渲染进程\n * - relay-reply: 处理回复消息,广播给所有渲染进程\n * - __GetCurrentWebContentId__: 获取当前 WebContent ID\n * - __OpenCurrentWebContentDevTools__: 打开当前 WebContent 的开发者工具\n */\n\trelayMessage() {\n\t\t// 防止重复注册监听器:先移除再添加\n\t\tconst relayMessageHandler = (_event: Electron.IpcMainEvent, { targetWebContentId, channel, requestId, args }: any) => {\n log('log', 'relay-message', { targetWebContentId, channel, requestId, args })\n\t\t\tif (targetWebContentId) {\n\t\t\t\t// 转发到指定的渲染进程\n\t\t\t\tconst targetWebContent = webContents.fromId(targetWebContentId)\n\t\t\t\tif (targetWebContent) {\n\t\t\t\t\ttargetWebContent.send(channel, requestId, ...args)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// 广播到所有渲染进程\n\t\t\t\twebContents.getAllWebContents().forEach(webContent => {\n\t\t\t\t\twebContent.send(channel, requestId, ...args)\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\n\t\tipcMain.removeListener('relay-message', relayMessageHandler)\n\t\tipcMain.on('relay-message', relayMessageHandler)\n\n\t\t// 处理回复消息广播\n\t\tconst relayReplyHandler = (_event: Electron.IpcMainEvent, { originalChannel, requestId, result }: any) => {\n log('log', 'relay-reply', { originalChannel, requestId, result })\n\t\t\t// 使用 requestId 确保回复发送给正确的请求\n\t\t\t// 所有渲染进程都会接收 ${originalChannel}-reply-${requestId} 事件\n\t\t\t// 只有对应的请求才会处理该回复,避免数据错乱\n webContents.getAllWebContents().forEach(webContent => {\n webContent.send(`${originalChannel}-reply-${requestId}`, result)\n })\n\t\t}\n\n\t\tipcMain.removeListener('relay-reply', relayReplyHandler)\n\t\tipcMain.on('relay-reply', relayReplyHandler)\n\n // 获取当前 WebContent ID 的通道\n const getCurrentWebContentIdChannel = '__GetCurrentWebContentId__'\n const getCurrentWebContentIdHandler = (_event: Electron.IpcMainEvent) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n _event.sender.send(`${getCurrentWebContentIdChannel}-reply`, webContentId)\n } catch (error) {\n log('error', 'getCurrentWebContentId error:', error)\n }\n\t\t}\n\t\tipcMain.removeListener(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler)\n ipcMain.on(getCurrentWebContentIdChannel, getCurrentWebContentIdHandler)\n\n // 打开当前 WebContent 开发者工具的通道\n const openCurrentWebContentDevToolsChannel = '__OpenCurrentWebContentDevTools__'\n const openCurrentWebContentDevToolsHandler = (_event: Electron.IpcMainEvent) => {\n try {\n _event.frameId\n const webContentId = _event?.sender?.id;\n webContents.fromId(webContentId)?.openDevTools();\n _event.sender.send(`${openCurrentWebContentDevToolsChannel}-reply`, webContentId)\n } catch (error) {\n log('error', 'openCurrentWebContentDevTools error:', error)\n }\n\t\t}\n\t\tipcMain.removeListener(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler)\n ipcMain.on(openCurrentWebContentDevToolsChannel, openCurrentWebContentDevToolsHandler)\n\n\t}\n\n}\n\n/**\n * 全局 MainIPC 实例\n * 使用全局变量确保单例模式,避免重复创建实例\n */\n// @ts-ignore\nexport const mainIPC: MainIPC = global['__ELECTRON_IPC__'] ? global['__ELECTRON_IPC__'] : (global['__ELECTRON_IPC__'] = new MainIPC());\n\n/**\n * 初始化 IPC 通信系统\n * 设置消息中继功能,确保跨进程通信正常工作\n *\n * @returns MainIPC 实例\n *\n * 功能说明:\n * - 检查是否已经初始化,避免重复初始化\n * - 设置消息中继功能,支持跨渲染进程通信\n * - 返回全局 MainIPC 实例\n */\nexport const initialize = () => {\n // @ts-ignore\n if (isInitialized && global['__ELECTRON_IPC__']){\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n }\n isInitialized = true;\n // @ts-ignore\n global['__ELECTRON_IPC__'].relayMessage()\n // @ts-ignore\n return global['__ELECTRON_IPC__']\n}\n\n\n/**\n * 日志记录工具函数\n * 提供统一的日志格式和错误处理\n *\n * @param type 日志类型:'log' 或 'error'\n * @param data 要记录的数据\n */\nconst log = (type: 'log' | 'error', ...data: any[]) => {\n const key = `[electron-ipc]: `\n try {\n console[type](key, ...data);\n } catch (error) {\n console.error(key, error)\n }\n}\n\n"],"names":["isInitialized","EventEmitter","getRandomUUID","ipcMain","webContents"],"mappings":";;;;AAIWA,qBAAa,GAAG,MAAM;AAEjC;;;;;;;;AAQG;AACH,MAAM,OAAO,CAAA;AAIZ,IAAA,WAAA,GAAA;AAFQ,QAAA,IAAA,CAAA,YAAY,GAAiB,IAAIC,mBAAY,EAAE,CAAA;AAGtD,QAAA,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;AACtB,YAAA,IAAI,CAAC,YAAY,GAAG,IAAIA,mBAAY,EAAE,CAAA;AACtC,YAAA,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAA;SACvB;QACD,OAAO,OAAO,CAAC,QAAQ,CAAA;KACvB;AAEA;;;;;;;;;;;;AAYG;AACJ,IAAA,MAAM,UAAU,CAAC,OAAe,EAAE,GAAG,IAAW,EAAA;QAC/C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,KAAI;AACtC,YAAA,MAAM,SAAS,GAAGC,mBAAa,EAAE,CAAA;AACjC,YAAA,MAAM,YAAY,GAAG,CAAA,EAAG,OAAO,CAAU,OAAA,EAAA,SAAS,EAAE,CAAA;YACpD,IAAI,UAAU,GAAG,KAAK,CAAA;AAEtB,YAAA,MAAM,YAAY,GAAG,CAAC,MAAW,KAAI;gBACpC,IAAI,CAAC,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AAG,IAAI,OAAO,EAAE,EAAE;AAEvI;;;;;;;;;;AAUG;AACI,MAAM,UAAU,GAAG,MAAK;;AAE7B,IAAA,IAAIH,qBAAa,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAC;;AAE9C,QAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;KAClC;IACDA,qBAAa,GAAG,IAAI,CAAC;;AAErB,IAAA,MAAM,CAAC,kBAAkB,CAAC,CAAC,YAAY,EAAE,CAAA;;AAEzC,IAAA,OAAO,MAAM,CAAC,kBAAkB,CAAC,CAAA;AACnC,EAAC;AAGD;;;;;;AAMG;AACH,MAAM,GAAG,GAAG,CAAC,IAAqB,EAAE,GAAG,IAAW,KAAI;IACpD,MAAM,GAAG,GAAG,CAAA,gBAAA,CAAkB,CAAA;AAC9B,IAAA,IAAI;QACF,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC;KAC7B;IAAC,OAAO,KAAK,EAAE;AACd,QAAA,OAAO,CAAC,KAAK,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;KAC1B;AACH,CAAC;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynker-desktop/electron-ipc",
3
- "version": "0.0.9-alpha.5",
3
+ "version": "0.0.9-alpha.51",
4
4
  "description": "electron-ipc",
5
5
  "keywords": [
6
6
  "electron",
@@ -73,5 +73,5 @@
73
73
  "tslib": "^2.6.3",
74
74
  "typescript": "^5.5.3"
75
75
  },
76
- "gitHead": "45e9134428d22091f538fa4e7df03f6ab815ab25"
76
+ "gitHead": "9b3844af603fa2f3c541de33bcb20e9190dde4ec"
77
77
  }
package/preload/index.js CHANGED
@@ -16,7 +16,22 @@ else {
16
16
  // This will fail if contextIsolation is not enabled
17
17
  try {
18
18
  // @ts-ignore
19
- electron.contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, electron.ipcRenderer);
19
+ electron.contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, {
20
+ ...electron.ipcRenderer,
21
+ emit: electron.ipcRenderer.emit.bind(electron.ipcRenderer),
22
+ prependListener: electron.ipcRenderer.prependListener.bind(electron.ipcRenderer),
23
+ prependOnceListener: electron.ipcRenderer.prependOnceListener.bind(electron.ipcRenderer),
24
+ once: electron.ipcRenderer.once.bind(electron.ipcRenderer),
25
+ on: electron.ipcRenderer.on.bind(electron.ipcRenderer),
26
+ off: electron.ipcRenderer.off.bind(electron.ipcRenderer),
27
+ send: electron.ipcRenderer.send.bind(electron.ipcRenderer),
28
+ sendSync: electron.ipcRenderer.sendSync.bind(electron.ipcRenderer),
29
+ sendToHost: electron.ipcRenderer.sendToHost.bind(electron.ipcRenderer),
30
+ invoke: electron.ipcRenderer.invoke.bind(electron.ipcRenderer),
31
+ addListener: electron.ipcRenderer.addListener.bind(electron.ipcRenderer),
32
+ removeAllListeners: electron.ipcRenderer.removeAllListeners.bind(electron.ipcRenderer),
33
+ removeListener: electron.ipcRenderer.removeListener.bind(electron.ipcRenderer),
34
+ });
20
35
  }
21
36
  catch (e) {
22
37
  // console.error(e)
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/src/common/index.ts","../src/src/preload/index.ts"],"sourcesContent":["import type { BrowserWindow } from 'electron';\nimport md5 from 'md5';\nimport { v5 as uuidv5 } from 'uuid';\n\nexport const WINDOWL_GLOBAL_KEY = `__ELECTRON_IPC__`;\n\n// @ts-ignore\ndeclare global {\n interface Window {\n __ELECTRON_WINDOW_MANAGER__?: any;\n }\n}\n\n/** Tab ID */\nexport type TabID = number;\n\nexport interface Tab {\n /** 标签URL */\n url: string;\n /** 标签href */\n href: string;\n /** 标签标题 */\n title: string;\n /** 标签favicon */\n favicon: string;\n /** loading */\n isLoading: boolean;\n /** 是否可返回上一页 */\n canGoBack: boolean;\n canGoForward: boolean;\n}\n\nexport interface Tabs {\n [key: number]: Tab;\n}\n\nexport interface ElectronWindowsManagerOptions {\n name: string;\n loadingView?: {\n url: string;\n };\n browserWindow?: any;\n openDevTools?: boolean;\n preventOriginClose?: boolean;\n preventOriginNavigate?: boolean;\n}\n\nexport interface WindowItem extends BrowserWindow {\n /** BW别名 */\n _name: string;\n}\n\nexport function getRandomUUID(key: string = `${Date.now()}`): string {\n let webcrypto!: Crypto;\n let randomValue = Math?.random()?.toString()?.replace('0.', '');\n try {\n if (!webcrypto && !!window && window?.crypto) {\n webcrypto = window.crypto;\n }\n } catch (error) {console.log}\n try {\n const ar = webcrypto.getRandomValues(new Uint8Array(12));\n randomValue = `${md5(ar)}` || randomValue;\n } catch (error) {}\n const uuid = uuidv5(\n `${JSON.stringify(key)}_${Date.now()}_${randomValue}`,\n uuidv5.URL,\n );\n return uuid;\n}\n","import { ipcRenderer, contextBridge } from 'electron';\nimport { WINDOWL_GLOBAL_KEY } from '../common';\n\n// @ts-ignore\nif (window[WINDOWL_GLOBAL_KEY]) {\n // eslint-disable-next-line no-console\n console.log('electron-ipc Electron preload has already been run');\n} else {\n // @ts-ignore\n window[WINDOWL_GLOBAL_KEY] = ipcRenderer;\n if (contextBridge) {\n // This will fail if contextIsolation is not enabled\n try {\n // @ts-ignore\n contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, ipcRenderer);\n } catch (e) {\n // console.error(e)\n }\n }\n}\n"],"names":["ipcRenderer","contextBridge"],"mappings":";;;;AAIO,MAAM,kBAAkB,GAAG,kBAAkB;;ACDpD;AACA,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAE;;AAE9B,IAAA,OAAO,CAAC,GAAG,CAAC,oDAAoD,CAAC,CAAC;AACpE,CAAC;KAAM;;AAEL,IAAA,MAAM,CAAC,kBAAkB,CAAC,GAAGA,oBAAW,CAAC;IACzC,IAAIC,sBAAa,EAAE;;AAEjB,QAAA,IAAI;;AAEF,YAAAA,sBAAa,CAAC,iBAAiB,CAAC,kBAAkB,EAAED,oBAAW,CAAC,CAAC;SAClE;QAAC,OAAO,CAAC,EAAE;;SAEX;KACF;AACH;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/src/common/index.ts","../src/src/preload/index.ts"],"sourcesContent":["import type { BrowserWindow } from 'electron';\nimport md5 from 'md5';\nimport { v5 as uuidv5 } from 'uuid';\n\nexport const WINDOWL_GLOBAL_KEY = `__ELECTRON_IPC__`;\n\n// @ts-ignore\ndeclare global {\n interface Window {\n __ELECTRON_WINDOW_MANAGER__?: any;\n }\n}\n\n/** Tab ID */\nexport type TabID = number;\n\nexport interface Tab {\n /** 标签URL */\n url: string;\n /** 标签href */\n href: string;\n /** 标签标题 */\n title: string;\n /** 标签favicon */\n favicon: string;\n /** loading */\n isLoading: boolean;\n /** 是否可返回上一页 */\n canGoBack: boolean;\n canGoForward: boolean;\n}\n\nexport interface Tabs {\n [key: number]: Tab;\n}\n\nexport interface ElectronWindowsManagerOptions {\n name: string;\n loadingView?: {\n url: string;\n };\n browserWindow?: any;\n openDevTools?: boolean;\n preventOriginClose?: boolean;\n preventOriginNavigate?: boolean;\n}\n\nexport interface WindowItem extends BrowserWindow {\n /** BW别名 */\n _name: string;\n}\n\nexport const getRandomUUID = (key: string = `${Date.now()}`): string => {\n let webcrypto!: Crypto;\n let randomValue = Math?.random()?.toString()?.replace('0.', '');\n try {\n if (!webcrypto && !!window && window?.crypto) {\n webcrypto = window.crypto;\n }\n } catch (error) {console.log}\n try {\n const ar = webcrypto.getRandomValues(new Uint8Array(12));\n randomValue = `${md5(ar)}` || randomValue;\n } catch (error) {}\n const uuid = uuidv5(\n `${JSON.stringify(key)}_${Date.now()}_${randomValue}`,\n uuidv5.URL,\n );\n return uuid;\n}\n","import { ipcRenderer, contextBridge } from 'electron';\nimport { WINDOWL_GLOBAL_KEY } from '../common';\n\n// @ts-ignore\nif (window[WINDOWL_GLOBAL_KEY]) {\n // eslint-disable-next-line no-console\n console.log('electron-ipc Electron preload has already been run');\n} else {\n // @ts-ignore\n window[WINDOWL_GLOBAL_KEY] = ipcRenderer;\n if (contextBridge) {\n // This will fail if contextIsolation is not enabled\n try {\n // @ts-ignore\n contextBridge.exposeInMainWorld(WINDOWL_GLOBAL_KEY, {\n ...ipcRenderer,\n emit: ipcRenderer.emit.bind(ipcRenderer),\n prependListener: ipcRenderer.prependListener.bind(ipcRenderer),\n prependOnceListener: ipcRenderer.prependOnceListener.bind(ipcRenderer),\n once: ipcRenderer.once.bind(ipcRenderer),\n on: ipcRenderer.on.bind(ipcRenderer),\n off: ipcRenderer.off.bind(ipcRenderer),\n send: ipcRenderer.send.bind(ipcRenderer),\n sendSync: ipcRenderer.sendSync.bind(ipcRenderer),\n sendToHost: ipcRenderer.sendToHost.bind(ipcRenderer),\n invoke: ipcRenderer.invoke.bind(ipcRenderer),\n addListener: ipcRenderer.addListener.bind(ipcRenderer),\n removeAllListeners: ipcRenderer.removeAllListeners.bind(ipcRenderer),\n removeListener: ipcRenderer.removeListener.bind(ipcRenderer),\n });\n } catch (e) {\n // console.error(e)\n }\n }\n}\n"],"names":["ipcRenderer","contextBridge"],"mappings":";;;;AAIO,MAAM,kBAAkB,GAAG,kBAAkB;;ACDpD;AACA,IAAI,MAAM,CAAC,kBAAkB,CAAC,EAAE;;AAE9B,IAAA,OAAO,CAAC,GAAG,CAAC,oDAAoD,CAAC,CAAC;AACpE,CAAC;KAAM;;AAEL,IAAA,MAAM,CAAC,kBAAkB,CAAC,GAAGA,oBAAW,CAAC;IACzC,IAAIC,sBAAa,EAAE;;AAEjB,QAAA,IAAI;;AAEF,YAAAA,sBAAa,CAAC,iBAAiB,CAAC,kBAAkB,EAAE;AAClD,gBAAA,GAAGD,oBAAW;gBACd,IAAI,EAAEA,oBAAW,CAAC,IAAI,CAAC,IAAI,CAACA,oBAAW,CAAC;gBACxC,eAAe,EAAEA,oBAAW,CAAC,eAAe,CAAC,IAAI,CAACA,oBAAW,CAAC;gBAC9D,mBAAmB,EAAEA,oBAAW,CAAC,mBAAmB,CAAC,IAAI,CAACA,oBAAW,CAAC;gBACtE,IAAI,EAAEA,oBAAW,CAAC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