perf: 优化初始化逻辑

This commit is contained in:
kuaifan 2025-03-21 13:37:21 +08:00
parent 9716d7fe43
commit 672795ac49

View File

@ -109,8 +109,8 @@ if (!isSoftware) {
} }
// 加载路由 // 加载路由
Vue.prototype.goForward = function(route, isReplace, noBroadcast = false) { Vue.prototype.goForward = function(route, isReplace, autoBroadcast = true) {
if ($A.isSubElectron && !noBroadcast) { if ($A.Ready && $A.isSubElectron && autoBroadcast) {
$A.Electron.sendMessage('broadcastCommand', { $A.Electron.sendMessage('broadcastCommand', {
channel: 'goForward', channel: 'goForward',
payload: {route, isReplace}, payload: {route, isReplace},
@ -195,6 +195,7 @@ Vue.prototype.copyText = function (obj) {
// 全局对象/变量 // 全局对象/变量
$A.L = $L; $A.L = $L;
$A.Ready = false;
$A.Electron = null; $A.Electron = null;
$A.Platform = "web"; $A.Platform = "web";
$A.isMainElectron = false; $A.isMainElectron = false;
@ -215,6 +216,9 @@ if (isElectron) {
// 同步执行派遣 // 同步执行派遣
const dispatchId = $A.randomString(6) + "_" + Date.now().toString() const dispatchId = $A.randomString(6) + "_" + Date.now().toString()
$A.syncDispatch = (action, data) => { $A.syncDispatch = (action, data) => {
if (!$A.Ready) {
return false
}
if (!isElectron) { if (!isElectron) {
return false return false
} }
@ -236,6 +240,9 @@ $A.syncDispatch = (action, data) => {
return true return true
}; };
$A.Electron?.listener('syncDispatch', async ({dispatchId: targetId, action, data}) => { $A.Electron?.listener('syncDispatch', async ({dispatchId: targetId, action, data}) => {
if (!$A.Ready) {
return
}
if (dispatchId === targetId) { if (dispatchId === targetId) {
return return
} }
@ -246,7 +253,10 @@ $A.Electron?.listener('syncDispatch', async ({dispatchId: targetId, action, data
await store.dispatch(action, data) await store.dispatch(action, data)
}) })
$A.Electron?.listener('goForward', ({route, isReplace}) => { $A.Electron?.listener('goForward', ({route, isReplace}) => {
typeof $A.goForward === "function" && $A.goForward(route, isReplace, true) if (!$A.Ready) {
return
}
$A.goForward(route, isReplace, false)
}) })
// 绑定截图快捷键 // 绑定截图快捷键
@ -290,6 +300,7 @@ const $init = async () => {
$A.Message = app.$Message; $A.Message = app.$Message;
$A.Notice = app.$Notice; $A.Notice = app.$Notice;
$A.Modal = app.$Modal; $A.Modal = app.$Modal;
$A.Ready = true;
if (action === "handleClearCache") { if (action === "handleClearCache") {
$A.messageSuccess("清除成功"); $A.messageSuccess("清除成功");
@ -324,11 +335,11 @@ const $preload = async () => {
await store.dispatch("preload"); await store.dispatch("preload");
const hash = (window.location[routeMode === 'history' ? 'pathname' : 'hash']).replace(/^[#\/\s]/, ''); const hash = (window.location[routeMode === 'history' ? 'pathname' : 'hash']).replace(/^[#\/\s]/, '');
if (hash !== 'preload') { if (hash !== 'preload') {
$init().catch(_ => {}) await $init()
return return
} }
window.__initializeApp = (route) => { window.__initializeApp = async (route) => {
if (/^https?:\/\//.test(route)) { if (/^https?:\/\//.test(route)) {
if ($A.getDomain(route) !== $A.getDomain($A.mainUrl())) { if ($A.getDomain(route) !== $A.getDomain($A.mainUrl())) {
window.location.href = url; window.location.href = url;
@ -337,7 +348,7 @@ const $preload = async () => {
route = route.replace(/^https?:\/\/[^\/]+/, ''); route = route.replace(/^https?:\/\/[^\/]+/, '');
} }
window.history.replaceState(null, '', route) window.history.replaceState(null, '', route)
$init().catch(_ => {}) await $init()
} }
} }