perf: 优化数据

This commit is contained in:
kuaifan 2025-03-19 15:06:31 +08:00
parent 8bbe9c97e9
commit 8d24be914d
8 changed files with 31 additions and 39 deletions

View File

@ -33,27 +33,25 @@ contextBridge.exposeInMainWorld(
request: (msg, callback, error) => { request: (msg, callback, error) => {
msg.reqId = reqId++; msg.reqId = reqId++;
reqInfo[msg.reqId] = {callback: callback, error: error}; reqInfo[msg.reqId] = {callback: callback, error: error};
//TODO Maybe a special function for this better than this hack?
//File watch special case where the callback is called multiple times
if (msg.action == 'watchFile') { if (msg.action == 'watchFile') {
fileChangedListeners[msg.path] = msg.listener; fileChangedListeners[msg.path] = msg.listener;
delete msg.listener; delete msg.listener;
} }
ipcRenderer.send('rendererReq', msg); ipcRenderer.send('rendererReq', msg);
}, },
registerMsgListener: function (action, callback) {
ipcRenderer.on(action, function (event, args) {
callback(args);
});
},
sendMessage: function (action, args) { sendMessage: function (action, args) {
ipcRenderer.send(action, args); ipcRenderer.send(action, args);
}, },
sendAsync: function (action, args) { sendAsync: function (action, args) {
return ipcRenderer.invoke(action, args) return ipcRenderer.invoke(action, args)
}, },
listener: function (action, callback) {
ipcRenderer.on(action, function (event, args) {
callback(args);
});
},
listenOnce: function (action, callback) { listenOnce: function (action, callback) {
ipcRenderer.once(action, function (event, args) { ipcRenderer.once(action, function (event, args) {
callback(args); callback(args);

19
electron/electron.js vendored
View File

@ -1261,24 +1261,15 @@ ipcMain.on('windowMax', (event) => {
}) })
/** /**
* 给主窗口发送信息 * 给所有窗口广播指令除了本身
* @param args {channel, data}
*/
ipcMain.on('sendForwardMain', (event, args) => {
if (mainWindow) {
mainWindow.webContents.send(args.channel, args.data)
}
event.returnValue = "ok"
})
/**
* 窗口同步执行派遣
* @param args {type, payload} * @param args {type, payload}
*/ */
ipcMain.on('syncDispatch', (event, args) => { ipcMain.on('broadcastCommand', (event, args) => {
const channel = args.channel || args.command
const payload = args.payload || args.data
BrowserWindow.getAllWindows().forEach(window => { BrowserWindow.getAllWindows().forEach(window => {
if (window.webContents.id !== event.sender.id) { if (window.webContents.id !== event.sender.id) {
window.webContents.send('syncDispatch', args) window.webContents.send(channel, payload)
} }
}) })
event.returnValue = "ok" event.returnValue = "ok"

View File

@ -440,13 +440,13 @@ export default {
this.$store.dispatch("openWebTabWindow", url) this.$store.dispatch("openWebTabWindow", url)
return true return true
} }
this.$Electron.registerMsgListener('browserWindowBlur', _ => { this.$Electron.listener('browserWindowBlur', _ => {
this.$store.state.windowActive = false; this.$store.state.windowActive = false;
}) })
this.$Electron.registerMsgListener('browserWindowFocus', _ => { this.$Electron.listener('browserWindowFocus', _ => {
this.$store.state.windowActive = true; this.$store.state.windowActive = true;
}) })
this.$Electron.registerMsgListener('systemThemeChanged', _ => { this.$Electron.listener('systemThemeChanged', _ => {
this.autoTheme() this.autoTheme()
}) })
$A.bindScreenshotKey(this.$store.state.cacheKeyboard); $A.bindScreenshotKey(this.$store.state.cacheKeyboard);

View File

@ -217,14 +217,17 @@ $A.syncDispatch = (action, data) => {
delete data.__sync__; delete data.__sync__;
return false return false
} }
$A.Electron?.sendMessage('syncDispatch', { $A.Electron?.sendMessage('broadcastCommand', {
dispatchId, channel: 'syncDispatch',
action, payload: {
data, dispatchId,
action,
data,
}
}); });
return true return true
}; };
$A.Electron?.registerMsgListener('syncDispatch', async ({dispatchId: targetId, action, data}) => { $A.Electron?.listener('syncDispatch', async ({dispatchId: targetId, action, data}) => {
if (dispatchId === targetId) { if (dispatchId === targetId) {
return return
} }

View File

@ -70,7 +70,7 @@ export default {
// //
if (this.$Electron) { if (this.$Electron) {
emitter.on('updateNotification', this.onUpdateShow); emitter.on('updateNotification', this.onUpdateShow);
this.$Electron.registerMsgListener('updateDownloaded', info => { this.$Electron.listener('updateDownloaded', info => {
this.$store.state.clientNewVersion = info.version this.$store.state.clientNewVersion = info.version
this.updateVersion = info.version; this.updateVersion = info.version;
this.updateNote = info.releaseNotes || this.$L('没有更新描述。'); this.updateNote = info.releaseNotes || this.$L('没有更新描述。');

View File

@ -1208,12 +1208,12 @@ export default {
} }
// //
if (this.$Electron) { if (this.$Electron) {
this.$Electron.registerMsgListener('clickNotification', target => { this.$Electron.listener('clickNotification', target => {
console.log("[Notification] B Click", target); console.log("[Notification] B Click", target);
this.$Electron.sendMessage('mainWindowActive') this.$Electron.sendMessage('mainWindowActive')
this.notificationClick(target) this.notificationClick(target)
}) })
this.$Electron.registerMsgListener('replyNotification', target => { this.$Electron.listener('replyNotification', target => {
console.log("[Notification] B Reply", target); console.log("[Notification] B Reply", target);
this.notificationReply(target) this.notificationReply(target)
}) })

View File

@ -35,12 +35,12 @@ export default {
} }
}, },
methods: { methods: {
saveSuccess(data) { saveSuccess(payload) {
this.detail = data; this.detail = payload;
if (this.$isSubElectron) { if (this.$isSubElectron) {
$A.Electron.sendMessage('sendForwardMain', { $A.Electron.sendMessage('broadcastCommand', {
channel: 'reportSaveSuccess', channel: 'reportSaveSuccess',
data, payload,
}); });
window.close(); window.close();
} }

View File

@ -12,5 +12,5 @@ export default new Vuex.Store({
state, state,
getters, getters,
mutations, mutations,
actions, actions
}) })