perf: optimize full interface loading and refresh paths
This commit is contained in:
1 parent
649a0df832
commit
f317314a3c
57 files changed
+791
-136
No files matched your search
+11
-11
@@ -15,12 +15,12 @@ window.BpmLogin=(()=>{
|
||||
const status=(c,kind='account')=>{const state=observation(c,kind),[color,label]=({healthy:['green','健康'],error:['red','异常'],pending:['yellow','警告'],paused:['yellow','已暂停']})[state.status]||['yellow','待确认'];return `<span class="bpm-account-status bpm-status-${color}"><span class="bpm-status-light" aria-hidden="true"></span>${kind==='account'?'账号':'同步'} · ${label}</span>`;};
|
||||
function feedback(message,type='danger'){if(!live())return;const el=$('[data-bpm-feedback]');if(el){el.hidden=false;el.className=`alert alert-${type}`;el.textContent=message;}notify(message,type);}
|
||||
function release(r,{unload=false}={}){
|
||||
r.closed=true;clearTimeout(r.timer);r.controller?.abort();if(r.url){URL.revokeObjectURL(r.url);r.url=null;}
|
||||
r.closed=true;r.polling?.dispose();r.controller?.abort();if(r.url){URL.revokeObjectURL(r.url);r.url=null;}
|
||||
if(unload){if(!r.closeRequest)r.closeRequest=fetch('/api'+route(r.id,'login/close'),{method:'POST',credentials:'same-origin',keepalive:true,headers:{'Content-Type':'application/json'},body:'{}'}).then(()=>{}).catch(()=>{});return r.closeRequest;}
|
||||
if(r.release)return r.release;
|
||||
r.release=(async()=>{await r.opening.catch(()=>{});await r.queue.catch(()=>{});await r.operation?.catch(()=>{});if(!r.closeRequest)r.closeRequest=api(route(r.id,'login/close'),{}).catch(()=>{});await r.closeRequest;})();return r.release;
|
||||
}
|
||||
const handle={dispose(options){disposed=true;++statusRevision;clearTimeout(statusTimer);container.querySelectorAll('[data-account-form]').forEach(form=>form.reset());return remote?release(remote,options):Promise.resolve();}};
|
||||
const handle={dispose(options){disposed=true;++statusRevision;statusTimer?.dispose();container.querySelectorAll('[data-account-form]').forEach(form=>form.reset());return remote?release(remote,options):Promise.resolve();}};
|
||||
active=handle;
|
||||
const valid=r=>live()&&remote===r&&!r.closed;
|
||||
function controls(r){if(!valid(r))return;container.querySelectorAll('[data-bpm-control]').forEach(el=>el.disabled=!!r.busy);const input=$('[data-bpm-input]');if(input)input.disabled=!!r.busy;}
|
||||
@@ -94,14 +94,14 @@ window.BpmLogin=(()=>{
|
||||
+row('投递重试',delivery.retryAt?formatDate(delivery.retryAt):'无')
|
||||
+(delivery.message?row('投递说明',delivery.message):'')+'</details>';
|
||||
}
|
||||
async function refreshStatus(){
|
||||
async function refreshStatus(signal){
|
||||
if(!live())return;
|
||||
try{
|
||||
if(accountBusy||mutationBusy)return;const request=statusRevision,next=await api('/workspace');if(!live()||request!==statusRevision)return;
|
||||
if(accountBusy||mutationBusy)return;const request=statusRevision,next=await api('/workspace',undefined,{signal,ownerCheck:live});if(!live()||request!==statusRevision)return;
|
||||
const rows=next.connections||[],changed=JSON.stringify(connections.map(c=>[c.id,c.status,c.version,c.owner]))!==JSON.stringify(rows.map(c=>[c.id,c.status,c.version,c.owner]));connections=rows;
|
||||
if(changed&&!remote&&!container.querySelector('[data-account-form]')&&!window.PageLifecycle?.dirty())drawCards();
|
||||
else for(const c of connections){const badge=container.querySelector('[data-connection-status="'+c.id+'"]'),health=container.querySelector('[data-connection-health="'+c.id+'"]');if(badge)badge.innerHTML=status(c);if(health){const expanded=health.querySelector('details')?.open;health.innerHTML=observations(c);if(expanded)health.querySelector('details').open=true;}}
|
||||
}catch{}finally{if(live())statusTimer=setTimeout(refreshStatus,5000);}
|
||||
}catch{}
|
||||
}
|
||||
function ownerForm(c){
|
||||
if(user.role!=='admin')return '';
|
||||
@@ -138,21 +138,21 @@ window.BpmLogin=(()=>{
|
||||
catch(error){feedback(error.message);}finally{accountControls(false);}
|
||||
}
|
||||
async function mutate(button,action,message){++statusRevision;mutationBusy=true;button.disabled=true;try{const id=button.dataset[action],connection=connections.find(c=>c.id===id);const result=await api(route(id,action),['enable','disable'].includes(action)?{version:connection.version||0}:{});if(!live())return;update(result,button.dataset[action]);feedback(message,'success');}catch(error){feedback(error.message);}finally{mutationBusy=false;if(live())button.disabled=false;}}
|
||||
async function frame(r){
|
||||
async function frame(r,signal){
|
||||
if(!valid(r))return;
|
||||
try{
|
||||
r.controller=new AbortController();
|
||||
const response=await fetch('/api'+route(r.id,'login/frame'),{credentials:'same-origin',cache:'no-store',signal:r.controller.signal});
|
||||
const response=await fetch('/api'+route(r.id,'login/frame'),{credentials:'same-origin',cache:'no-store',signal:AbortSignal.any([signal,r.controller.signal])});
|
||||
if(!response.ok){let message='登录画面加载失败,请重新打开窗口。';try{message=(await response.json()).error||message;}catch{}throw new Error(message);}
|
||||
const blob=await response.blob();if(!valid(r))return;
|
||||
const url=URL.createObjectURL(blob),old=r.url;r.url=url;const image=$('[data-bpm-frame]');image.src=url;image.hidden=false;const loading=$('[data-bpm-loading]');if(loading)loading.hidden=true;if(old)URL.revokeObjectURL(old);
|
||||
}catch(error){if(valid(r)&&error.name!=='AbortError'){feedback(error.message);await close(r);return;}}
|
||||
if(valid(r))r.timer=setTimeout(()=>frame(r),650);
|
||||
|
||||
}
|
||||
async function close(r){if(remote===r)remote=null;await release(r);if(live()){$('#bpm-remote').innerHTML='';drawCards();}}
|
||||
async function open(id){
|
||||
if(remote||accountBusy||!live())return;
|
||||
const r={id,closed:false,queue:Promise.resolve(),busy:false,failed:false,url:null,timer:null};remote=r;drawCards();
|
||||
const r={id,closed:false,queue:Promise.resolve(),busy:false,failed:false,url:null};remote=r;drawCards();
|
||||
r.opening=api(route(id,'login/open'),{});
|
||||
try{
|
||||
const session=await r.opening;if(!valid(r)){await release(r);return;}
|
||||
@@ -171,7 +171,7 @@ window.BpmLogin=(()=>{
|
||||
try{await r.queue;if(!valid(r))return;if(r.failed)throw Error('输入未送达,请关闭后重新打开登录窗口。');r.operation=api(route(id,'login/save'),{});const result=await r.operation;if(!valid(r))return;update(result,id);const saved=result.connection||result;feedback(saved.status==='connected'?'连接已检查并保存。':'请完成登录后再次检查。',saved.status==='connected'?'success':'warning');}
|
||||
catch(error){feedback(error.message);}finally{r.busy=false;if(valid(r)){controls(r);$('[data-bpm-close]').disabled=false;$('[data-bpm-save-state]').textContent='完成登录后保存连接';}}
|
||||
};
|
||||
frame(r);
|
||||
r.polling=window.PageLifecycle.poll(signal=>frame(r,signal),650,{isActive:()=>valid(r)});void r.polling.run();
|
||||
}catch(error){if(valid(r)){remote=null;drawCards();feedback(error.message);}}
|
||||
}
|
||||
function bindInput(r){
|
||||
@@ -199,7 +199,7 @@ window.BpmLogin=(()=>{
|
||||
if(user.role==='admin')$('#connection-create').onsubmit=async event=>{event.preventDefault();const button=event.target.querySelector('button[type=submit]');button.disabled=true;try{const result=await api('/connections',{name:$('#connection-name').value.trim(),owner:$('#connection-owner').value});if(!live())return;++statusRevision;connections.unshift(result.connection||result);event.target.reset();drawCards();feedback('连接已创建。','success');}catch(error){feedback(error.message);}finally{if(live())button.disabled=false;}};
|
||||
drawCards();
|
||||
connections.filter(c=>c.status!=='disabled'&&authorized(c)).forEach(c=>loadAccount(c.id).then(refreshAccountSummary).catch(error=>feedback(error.message)));
|
||||
statusTimer=setTimeout(refreshStatus,5000);
|
||||
statusTimer=window.PageLifecycle.poll(refreshStatus,5000,{isActive:live});
|
||||
await window.BpmSyncSettings?.mount($('#bpm-sync-settings'),{api,user,notify,isActive:live});
|
||||
}catch(error){if(live())throw error;}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user