perf: load page modules on demand and avoid repeated startup ACL writes

This commit is contained in:
abaishu0730 committed 2026-10-28 15:20:28 +08:00
1 parent 73773f9526
commit 7ead1e7cb7
19 files changed
+330 -24

No files matched your search

+3 -1
View File
@@ -174,6 +174,7 @@ async function renderView() {
document.querySelector('#view-title').textContent=views[currentView][0];document.querySelector('#view-description').textContent=views[currentView][1];
const container=document.querySelector('#content');if(!(wasRecord&&['records','records-pdf'].includes(currentView)))container.innerHTML='<div class="card"><div class="card-body text-secondary">正在加载…</div></div>';
try{await window.AppModules.load(currentView);if(!isActive())return;configureLoadedViews();}catch(error){if(isActive()){container.innerHTML=`<div class="alert alert-danger" role="alert">${esc(error.message)} <button class="btn btn-sm" type="button" data-module-retry>重试加载页面</button> <button class="btn btn-sm" type="button" data-module-reload>重新加载工作台</button></div>`;container.querySelector('[data-module-retry]').onclick=()=>renderView();container.querySelector('[data-module-reload]').onclick=()=>location.reload();}return;}
const notificationSections={'order-notifications':'workbench','notification-execution':'execution','notification-orders':'orders','notification-batches':'pending','notification-history':'history','notification-settings':'settings'};
const fn={'rule-fields':(host,active)=>window.OrderRuleViews.render(host,active,'fields'),...Object.fromEntries(Object.entries(notificationSections).map(([route,section])=>[route,(host,active)=>window.OrderNotificationViews.render(host,active,section)])),'assistant-settings':window.AssistantViews?.settings,account:window.AccountViews?.account,'order-rules':window.OrderRuleViews?.render,health:window.HealthViews?.render,assistant:window.AssistantViews?.render,storage:window.StorageViews?.render,overview:viewOverview,calendar:window.WorkspaceViews?.calendar,exceptions:window.WorkspaceViews?.exceptions,bpm:window.WorkspaceViews?.bpm,records:viewRecords,'records-pdf':viewPdfRecords,schedule:viewSchedule,templates:viewTemplates,mail:window.MailViews?.accounts,users:window.AccountViews?.users,audit:viewAudit,rules:viewRules}[currentView];
void refreshTimer?.run().catch(()=>{});
@@ -186,7 +187,8 @@ async function authScreen() {
}
async function passwordChanged(actorId){if(currentUser&&currentUser.id!==actorId)return false;currentUser=null;++viewGeneration;refreshTimer?.dispose();await window.PageLifecycle?.dispose();await window.BpmLogin?.dispose().catch(()=>{});await boot();return true;}
function enter(){window.PageLifecycle.configureRegions({read:(signal,region)=>api('/refresh/revisions?regions='+encodeURIComponent(region),undefined,{signal}),user:()=>currentUser});window.RecordState.configure(currentUser,({hash,push})=>{if(push&&location.hash!==hash)history.pushState({appIndex:++historyIndex},'',hash);else history.replaceState({...history.state,appIndex:historyIndex},'',hash);});window.PdfRecordViews?.configure({api,esc,formatDate,notify,user:currentUser});window.AccountViews?.configure({api,esc,formatDate,notify,user:currentUser,onPasswordChanged:passwordChanged});window.OrderRuleViews?.configure({api,esc,formatDate,notify,user:currentUser});window.OrderNotificationViews?.configure({api,esc,formatDate,notify,user:currentUser});window.HealthViews?.configure({api,esc,formatDate,user:currentUser});window.AssistantViews?.configure({api,esc,formatDate,notify,user:currentUser});window.StorageViews?.configure({api,esc,formatDate,notify,user:currentUser});window.NotificationViews?.configure({api,esc,formatDate,notify,user:currentUser});window.OrderViews?.configure({api,esc,formatDate,notify,user:currentUser});window.MailViews?.configure({api,esc,formatDate,notify,user:currentUser});window.WorkspaceViews?.configure({api,esc,formatDate,notify,user:currentUser});shell();refreshTimer?.dispose();const statusUser=currentUser;refreshTimer=window.PageLifecycle.regionPoll('orderRules',{refresh:signal=>updateAutomationStatus(()=>currentUser===statusUser,{signal}),onError:error=>{const node=document.querySelector('#automation-status');if(node&&currentUser===statusUser){node.textContent='通知状态待确认';node.className='badge bg-yellow-lt text-decoration-none';node.title=error.message+';请刷新重试。';}}},15000,{isActive:()=>!!currentUser&&currentUser===statusUser,owned:false});renderView();void checkSystemUpdate(()=>!!currentUser);}
function configureLoadedViews(){window.PdfRecordViews?.configure({api,esc,formatDate,notify,user:currentUser});window.AccountViews?.configure({api,esc,formatDate,notify,user:currentUser,onPasswordChanged:passwordChanged});window.OrderRuleViews?.configure({api,esc,formatDate,notify,user:currentUser});window.OrderNotificationViews?.configure({api,esc,formatDate,notify,user:currentUser});window.HealthViews?.configure({api,esc,formatDate,user:currentUser});window.AssistantViews?.configure({api,esc,formatDate,notify,user:currentUser});window.StorageViews?.configure({api,esc,formatDate,notify,user:currentUser});window.NotificationViews?.configure({api,esc,formatDate,notify,user:currentUser});window.OrderViews?.configure({api,esc,formatDate,notify,user:currentUser});window.MailViews?.configure({api,esc,formatDate,notify,user:currentUser});window.WorkspaceViews?.configure({api,esc,formatDate,notify,user:currentUser});}
function enter(){window.PageLifecycle.configureRegions({read:(signal,region)=>api('/refresh/revisions?regions='+encodeURIComponent(region),undefined,{signal}),user:()=>currentUser});window.RecordState.configure(currentUser,({hash,push})=>{if(push&&location.hash!==hash)history.pushState({appIndex:++historyIndex},'',hash);else history.replaceState({...history.state,appIndex:historyIndex},'',hash);});configureLoadedViews();shell();refreshTimer?.dispose();const statusUser=currentUser;refreshTimer=window.PageLifecycle.regionPoll('orderRules',{refresh:signal=>updateAutomationStatus(()=>currentUser===statusUser,{signal}),onError:error=>{const node=document.querySelector('#automation-status');if(node&&currentUser===statusUser){node.textContent='通知状态待确认';node.className='badge bg-yellow-lt text-decoration-none';node.title=error.message+';请刷新重试。';}}},15000,{isActive:()=>!!currentUser&&currentUser===statusUser,owned:false});renderView();void checkSystemUpdate(()=>!!currentUser);}
async function boot(){try{currentUser=(await api('/me')).user;enter();}catch{await authScreen();}}
window.addEventListener('page-operation-error',event=>notify(event.detail.message,'danger'));
window.addEventListener('pagehide',()=>{window.BpmLogin?.dispose({unload:true});});
+2 -2
View File
@@ -1,3 +1,3 @@
<!doctype html>
<html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>订单通知中心</title><link href="/vendor/tabler.css" rel="stylesheet"><link href="/vendor/tom-select.css" rel="stylesheet"><link href="/app.css" rel="stylesheet"><link href="/field-mappings.css" rel="stylesheet"><link href="/business-home.css" rel="stylesheet"></head>
<body><div id="app"><div class="page page-center"><div class="container-tight py-4"><div class="card"><div class="card-body text-center">正在连接订单通知中心…</div></div></div></div></div><script src="/vendor/tabler.js" defer></script><script src="/vendor/tom-select.js" defer></script><script src="/bpm-sync-settings.js" defer></script><script src="/bpm.js" defer></script><script src="/workspace.js" defer></script><script src="/mail-history.js" defer></script><script src="/mail-template-editor.js" defer></script><script src="/mail.js" defer></script><script src="/record-state.js" defer></script><script src="/record-table.js" defer></script><script src="/record-ui.js" defer></script><script src="/orders.js" defer></script><script src="/pdf-records.js" defer></script><script src="/storage-preview-host.js" defer></script><script src="/storage.js" defer></script><script src="/notifications.js" defer></script><script src="/field-sample-picker.js" defer></script><script src="/field-recognition-panel.js" defer></script><script src="/order-rules-scopes.js" defer></script><script src="/order-rules-resources.js" defer></script><script src="/order-rules-import.js" defer></script><script src="/order-rules-editor.js" defer></script><script src="/order-rules-preview.js" defer></script><script src="/field-mappings.js" defer></script><script src="/order-execution.js" defer></script><script src="/order-notifications-shared.js" defer></script><script src="/modules/order-notifications/batch-editor.js" defer></script><script src="/modules/order-notifications/detail.js" defer></script><script src="/modules/order-notifications/records.js" defer></script><script src="/business-home.js" defer></script><script src="/order-notifications.js" defer></script><script src="/order-rules.js" defer></script><script src="/health.js" defer></script><script src="/assistant-operations.js" defer></script><script src="/assistant.js" defer></script><script src="/account.js" defer></script><script src="/navigation.js" defer></script><script src="/page-lifecycle.js" defer></script><script src="/app.js" defer></script></body></html>
<html lang="zh-CN"><head><meta name="app-modules" content=""><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>订单通知中心</title><link href="/vendor/tabler.css" rel="stylesheet"><link href="/vendor/tom-select.css" rel="stylesheet"><link href="/app.css" rel="stylesheet"><link href="/field-mappings.css" rel="stylesheet"><link href="/business-home.css" rel="stylesheet"></head>
<body><div id="app"><div class="page page-center"><div class="container-tight py-4"><div class="card"><div class="card-body text-center">正在连接订单通知中心…</div></div></div></div></div><script src="/vendor/tabler.js" defer></script><script src="/navigation.js" defer></script><script src="/page-lifecycle.js" defer></script><script src="/record-state.js" defer></script><script src="/modules/navigation/module-loader.js" defer></script><script src="/app.js" defer></script></body></html>
+40
View File
@@ -0,0 +1,40 @@
'use strict';
window.AppModules=(()=>{
const manifest=JSON.parse(document.querySelector('meta[name="app-modules"]')?.content||'{}');
const groups=manifest.groups||{},urls=manifest.urls||{},loaded=new Map(),preloads=new Map();
function assetFor(route){
const src=urls['/'+route];
if(typeof src!=='string'||!/^\/(?!\/)[\w./-]+\.js\?v=[a-f0-9]{64}$/.test(src))throw Error('页面资源版本无效,请重新加载工作台。');
const hash=src.slice(src.lastIndexOf('=')+1),integrity='sha256-'+btoa(String.fromCharCode(...hash.match(/../g).map(byte=>parseInt(byte,16))));
return {src,integrity};
}
function preload(asset){
if(loaded.has(asset.src)||preloads.has(asset.src))return;
const link=document.createElement('link');link.rel='preload';link.as='script';link.href=asset.src;link.integrity=asset.integrity;link.crossOrigin='anonymous';preloads.set(asset.src,link);document.head.appendChild(link);
}
function loadOne(route,asset){
const {src,integrity}=asset;
if(loaded.has(src))return loaded.get(src);
const script=document.createElement('script');script.async=true;script.src=src;script.integrity=integrity;script.crossOrigin='anonymous';
const promise=new Promise((resolve,reject)=>{script.onload=resolve;script.onerror=()=>reject(Error('无法加载页面资源:/'+route));});
loaded.set(src,promise);
document.head.appendChild(script);
promise.catch(()=>{if(loaded.get(src)===promise){loaded.delete(src);script.remove();}});
const cleanup=()=>{preloads.get(src)?.remove();preloads.delete(src);};promise.then(cleanup,cleanup);
return promise;
}
function filesFor(route,parents=new Set()){
if(parents.has(route)||!Object.hasOwn(groups,route))throw Error('页面模块配置无效,请重新加载工作台。');
const next=new Set(parents).add(route);
return groups[route].flatMap(item=>item.startsWith('$')?filesFor(item,next):[item]);
}
async function load(route){
const files=[...new Set(filesFor(route))].map(file=>({file,asset:assetFor(file)}));
// Download dependencies together, but execute only this route's sequence.
// A stalled script from an abandoned route cannot block another route.
for(const {asset} of files)preload(asset);
try{for(const {file,asset} of files)await loadOne(file,asset);}
finally{for(const {asset} of files){preloads.get(asset.src)?.remove();preloads.delete(asset.src);}}
}
return {load};
})();
@@ -0,0 +1,27 @@
{
"$records": ["record-ui.js", "record-table.js"],
"$business": ["$records", "order-notifications-shared.js", "mail-template-editor.js", "modules/order-notifications/batch-editor.js", "modules/order-notifications/detail.js", "modules/order-notifications/records.js", "order-notifications.js"],
"$mail": ["mail-history.js", "mail-template-editor.js", "mail.js"],
"$rules": ["vendor/tom-select.js", "order-notifications-shared.js", "field-sample-picker.js", "field-recognition-panel.js", "order-rules-scopes.js", "order-rules-resources.js", "order-rules-import.js", "order-rules-editor.js", "order-rules-preview.js", "field-mappings.js", "order-execution.js", "order-rules.js"],
"overview": ["business-home.js", "order-notifications.js"],
"notification-orders": ["$business"],
"notification-batches": ["$business"],
"notification-settings": ["order-notifications.js"],
"assistant": ["assistant-operations.js", "assistant.js"],
"assistant-settings": ["assistant-operations.js", "assistant.js"],
"bpm": ["workspace.js", "bpm-sync-settings.js", "bpm.js"],
"mail": ["$mail"],
"storage": ["storage-preview-host.js", "storage.js"],
"records": ["$records", "orders.js", "notifications.js"],
"records-pdf": ["$records", "pdf-records.js", "notifications.js"],
"rule-fields": ["$rules"],
"order-rules": ["$rules"],
"schedule": [],
"templates": ["$mail"],
"calendar": ["workspace.js"],
"exceptions": ["workspace.js"],
"audit": ["workspace.js"],
"users": ["account.js"],
"account": ["account.js"],
"health": ["health.js"]
}