Skip to content

Commit ab5c0d1

Browse files
authored
Port (#250)
* init * [bug]:fix lin can't send id ==0 [opt]:opt main and ipc
1 parent 2826b52 commit ab5c0d1

File tree

9 files changed

+188
-117
lines changed

9 files changed

+188
-117
lines changed

src/main/dolin/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ export default abstract class LinBase {
516516
}
517517
}
518518

519-
if ((frameId || frame?.id) && (frame || eventFrame)) {
519+
if ((frameId || frame?.id != undefined) && (frame || eventFrame)) {
520520
const data = frameData || getFrameData(db, frame)
521521
const id = frameId || frame.id
522522
const checksum =

src/main/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ class ElectronLog extends Transport {
9595
}
9696

9797
log(info: any, callback: () => void) {
98-
if (info.message?.method) {
99-
this.q.list.push(info)
100-
} else {
101-
this.q.win.forEach((win) => {
102-
win.webContents.send('ipc-log-main', info)
103-
})
98+
if (!info.message.method) {
99+
info.message = {
100+
method: 'ipc-log-main',
101+
message: info.message
102+
}
104103
}
104+
this.q.list.push(info)
105105
callback()
106106
}
107107
}

src/main/multiWin.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
import { is } from '@electron-toolkit/utils'
2-
import { app, shell, BrowserWindow, ipcMain, dialog, protocol as eProtocol, net } from 'electron'
2+
import {
3+
app,
4+
shell,
5+
BrowserWindow,
6+
ipcMain,
7+
dialog,
8+
protocol as eProtocol,
9+
net,
10+
MessageChannelMain,
11+
MessagePortMain
12+
} from 'electron'
313
import path, { join } from 'path'
414
import icon from '../../resources/icon.png?asset'
515

16+
ipcMain.on('ipc-get-port', (event) => {
17+
const win = BrowserWindow.fromWebContents(event.sender)
18+
if (win) {
19+
const port = logQ.portMap.get(win)
20+
if (port) {
21+
port.close()
22+
}
23+
const { port1, port2 } = new MessageChannelMain()
24+
logQ.portMap.set(win, port1)
25+
event.sender.postMessage('port', null, [port2])
26+
// port2.start()
27+
}
28+
})
629
class LogQueue {
730
private static instance: LogQueue | null = null
831
list: any[] = []
932
timer: any
1033
mainWin: BrowserWindow | undefined
34+
portMap: Map<BrowserWindow, MessagePortMain> = new Map()
1135

1236
private constructor(
1337
public win: BrowserWindow[] = [],
@@ -32,7 +56,9 @@ class LogQueue {
3256
protected startTimer() {
3357
this.timer = setInterval(() => {
3458
if (this.list.length) {
35-
this.mainWin!.webContents.send('ipc-log', this.list)
59+
this.portMap.forEach((port, win) => {
60+
port.postMessage(this.list)
61+
})
3662
this.list = []
3763
}
3864
}, this.period)
@@ -117,6 +143,7 @@ ipcMain.on('ipc-close-others-windows', (event, arg) => {
117143
export function closeAllWindows() {
118144
winMap.forEach((win, key) => {
119145
//store pos
146+
120147
const pos = win.getBounds()
121148
winPosMap.set(key, {
122149
x: pos?.x,
@@ -125,6 +152,10 @@ export function closeAllWindows() {
125152
height: pos?.height
126153
})
127154
win.close()
155+
const port = logQ.portMap.get(win)
156+
if (port) {
157+
port.close()
158+
}
128159
})
129160
}
130161

@@ -140,6 +171,10 @@ export function closeWindow(id: string) {
140171
height: pos?.height
141172
})
142173
win.close()
174+
const port = logQ.portMap.get(win)
175+
if (port) {
176+
port.close()
177+
}
143178
}
144179
}
145180
export function minimizeWindow(id: string) {

src/preload/api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export type Api = {
77
writeFile: (path: string, data: string) => Promise<void>
88
readdir: (path: string) => Promise<Dirent[]>
99
state: (path: string) => Promise<Stats>
10+
getPort: () => void
1011
}
11-

src/preload/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ path.relative = (from: string, to: string) => {
2121
return ipcRenderer.sendSync('ipc-path-relative', from, to)
2222
}
2323

24+
const getPort = (): void => {
25+
ipcRenderer.once('port', (event, ports) => {
26+
const portCache = event.ports?.[0]
27+
if (portCache) {
28+
window.postMessage('port', '*', [portCache])
29+
}
30+
})
31+
ipcRenderer.send('ipc-get-port')
32+
}
33+
2434
// Custom APIs for renderer
2535
const api: Api = {
2636
glob: async (pattern: string | string[], options?: GlobOptionsWithFileTypesFalse) => {
@@ -37,7 +47,8 @@ const api: Api = {
3747
},
3848
state: async (path: string) => {
3949
return ipcRenderer.invoke('ipc-fs-stat', path)
40-
}
50+
},
51+
getPort
4152
}
4253

4354
// Use `contextBridge` APIs to expose Electron APIs to
@@ -51,4 +62,3 @@ if (process.contextIsolated) {
5162
} else {
5263
throw new Error('contextBridge is not enabled')
5364
}
54-

src/renderer/src/main.ts

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,22 @@ dataParseWorker.onmessage = (event) => {
5454
}
5555

5656
window.serviceDetail = window.electron?.ipcRenderer.sendSync('ipc-service-detail')
57-
window.electron?.ipcRenderer.on('ipc-log', (event, data) => {
58-
const groups: { method: string; data: any[] }[] = [] // 存储所有分组,每个元素是 {method, data} 对象
59-
let currentGroup: { method: string; data: any[] } | null = null
6057

61-
data.forEach((item: any) => {
62-
const method = item.message.method
63-
64-
// 如果是新的method或者当前组的method不同,创建新组
65-
if (!currentGroup || currentGroup.method !== method) {
66-
if (currentGroup) {
67-
groups.push(currentGroup)
68-
}
69-
currentGroup = {
70-
method: method,
71-
data: []
72-
}
73-
}
74-
75-
currentGroup.data.push(item)
76-
})
77-
78-
// 添加最后一组
79-
if (currentGroup) {
80-
groups.push(currentGroup)
58+
window.onmessage = (event) => {
59+
// event.source === window means the message is coming from the preload
60+
// script, as opposed to from an <iframe> or other source.
61+
if (event.source === window && event.data === 'port') {
62+
const [port] = event.ports
63+
dataParseWorker.postMessage(
64+
{
65+
method: 'onmessage',
66+
data: port
67+
},
68+
[port]
69+
)
8170
}
82-
83-
// 按顺序发送每个组的数据
84-
groups.forEach((group) => {
85-
// window.logBus.emit(group.method, undefined, group.data)
86-
dataParseWorker.postMessage({
87-
method: group.method,
88-
data: group.data
89-
})
90-
})
91-
})
92-
71+
}
72+
window.api.getPort()
9373
VxeUI.use(VxeUIPluginRenderElement)
9474
VxeUI.setI18n('en-US', enUS)
9575
VxeUI.setLanguage('en-US')

src/renderer/src/views/uds/log.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ function udsLog({ values }: { values: any[] }) {
215215
writeToTerminal(time, data.label, data.level, data.message.data.msg)
216216
})
217217
}
218-
218+
function normalLog({ values }: { values: any[] }) {
219+
values.forEach((data) => {
220+
const time = new Date().toLocaleTimeString()
221+
writeToTerminal(time, data.label, data.level, data.message.message)
222+
})
223+
}
219224
function testLog({
220225
values
221226
}: {
@@ -291,7 +296,6 @@ function testLog({
291296
}
292297
}
293298
294-
let mainLog: (() => void) | undefined
295299
let keydownHandler: ((event: KeyboardEvent) => void) | undefined
296300
297301
onMounted(async () => {
@@ -364,10 +368,7 @@ onMounted(async () => {
364368
}
365369
366370
if (props.captureSystem) {
367-
mainLog = window.electron.ipcRenderer.on('ipc-log-main', (event, data) => {
368-
const time = new Date().toLocaleTimeString()
369-
writeToTerminal(time, data.label, data.level, data.message)
370-
})
371+
window.logBus.on('ipc-log-main', normalLog)
371372
}
372373
if (props.captureTest) {
373374
window.logBus.on('testInfo', testLog)
@@ -379,8 +380,8 @@ onMounted(async () => {
379380
380381
onUnmounted(() => {
381382
// Clean up event listeners first
382-
if (props.captureSystem && mainLog) {
383-
mainLog()
383+
if (props.captureSystem) {
384+
window.logBus.off('ipc-log-main', normalLog)
384385
}
385386
if (props.captureTest) {
386387
window.logBus.off('testInfo', testLog)

src/renderer/src/views/uds/uds.vue

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,7 @@ watch([contentH, contentW], (val) => {
16651665
opacity: 0.8;
16661666
/* 模糊效果 */
16671667
/* filter: blur(1px); */
1668-
z-index: v-bind(zIndex - 1);
1668+
/* z-index: v-bind(zIndex - 1); */
16691669
}
16701670
16711671
.left2 {
@@ -1680,7 +1680,7 @@ watch([contentH, contentW], (val) => {
16801680
opacity: 0.8;
16811681
/* 模糊效果 */
16821682
/* filter: blur(1px); */
1683-
z-index: v-bind(zIndex - 1);
1683+
/* z-index: v-bind(zIndex - 1); */
16841684
}
16851685
16861686
.left {
@@ -1694,7 +1694,7 @@ watch([contentH, contentW], (val) => {
16941694
opacity: 0.8;
16951695
/* 模糊效果 */
16961696
/* filter: blur(1px); */
1697-
z-index: v-bind(zIndex - 1);
1697+
/* z-index: v-bind(zIndex - 1); */
16981698
}
16991699
17001700
.right1 {
@@ -1709,7 +1709,7 @@ watch([contentH, contentW], (val) => {
17091709
opacity: 0.8;
17101710
/* 模糊效果 */
17111711
/* filter: blur(1px); */
1712-
z-index: v-bind(zIndex - 1);
1712+
/* z-index: v-bind(zIndex - 1); */
17131713
}
17141714
17151715
.right2 {
@@ -1724,7 +1724,7 @@ watch([contentH, contentW], (val) => {
17241724
opacity: 0.8;
17251725
/* 模糊效果 */
17261726
/* filter: blur(1px); */
1727-
z-index: v-bind(zIndex - 1);
1727+
/* z-index: v-bind(zIndex - 1); */
17281728
}
17291729
17301730
.right {
@@ -1738,7 +1738,7 @@ watch([contentH, contentW], (val) => {
17381738
opacity: 0.8;
17391739
/* 模糊效果 */
17401740
/* filter: blur(1px); */
1741-
z-index: v-bind(zIndex - 1);
1741+
/* z-index: v-bind(zIndex - 1); */
17421742
}
17431743
17441744
.el-dropdown-link {

0 commit comments

Comments
 (0)