feat(fleet): preserve operator VESC integration before final driver merge
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
export const boardSections = ['computer', 'settings', 'devices'] as const;
|
||||
export type BoardSection = typeof boardSections[number];
|
||||
export interface BoardLayout {schema:'missioncore.board-layout/v1';revision:number;open_sections:BoardSection[]}
|
||||
export interface BoardLayoutTransport {
|
||||
read:()=>Promise<BoardLayout>;
|
||||
patch:(section:BoardSection,open:boolean)=>Promise<BoardLayout>;
|
||||
}
|
||||
export const defaultBoardLayout:BoardLayout={schema:'missioncore.board-layout/v1',revision:0,open_sections:[...boardSections]};
|
||||
type Change={section:BoardSection;open:boolean};
|
||||
type Snapshot={value:BoardLayout;ready:boolean;error:string|null;saving:boolean};
|
||||
function apply(value:BoardLayout,change:Change):BoardLayout {
|
||||
const open=new Set(value.open_sections);
|
||||
if(change.open)open.add(change.section);else open.delete(change.section);
|
||||
return {...value,open_sections:boardSections.filter(section=>open.has(section))};
|
||||
}
|
||||
function validate(value:BoardLayout):BoardLayout {
|
||||
if(value.schema!==defaultBoardLayout.schema||!Number.isSafeInteger(value.revision)||value.revision<0||
|
||||
!Array.isArray(value.open_sections)||new Set(value.open_sections).size!==value.open_sections.length||
|
||||
value.open_sections.some(id=>!boardSections.includes(id)))throw new Error('Не удалось прочитать раскладку блоков.');
|
||||
return value;
|
||||
}
|
||||
// The queue belongs to the application resource, not a mounted accordion.
|
||||
// Navigation cannot discard a pending save or let an older reply win a toggle.
|
||||
export function createBoardLayoutStore(transport:BoardLayoutTransport){
|
||||
let saved=defaultBoardLayout;
|
||||
let snapshot:Snapshot={value:saved,ready:false,error:null,saving:false};
|
||||
let pending:Change[]=[];
|
||||
let reading:Promise<void>|null=null;
|
||||
let writing=false;
|
||||
const listeners=new Set<()=>void>();
|
||||
const emit=(patch:Partial<Snapshot>={})=>{
|
||||
snapshot={...snapshot,...patch,value:pending.reduce(apply,saved),saving:writing||pending.length>0};
|
||||
listeners.forEach(listener=>listener());
|
||||
};
|
||||
const load=():Promise<void>=>{
|
||||
if(reading)return reading;
|
||||
if(writing)return Promise.resolve();
|
||||
reading=transport.read().then(value=>{saved=validate(value);emit({ready:true,error:null});})
|
||||
.catch(()=>emit({error:'Раскладка блоков не загружена. Повторите подключение.'}))
|
||||
.finally(()=>{reading=null;});
|
||||
return reading;
|
||||
};
|
||||
const flush=async()=>{
|
||||
if(writing)return;
|
||||
writing=true;emit({error:null});
|
||||
while(pending.length){
|
||||
const change=pending[0];
|
||||
try{saved=validate(await transport.patch(change.section,change.open));pending.shift();emit();}
|
||||
catch{pending=[];emit({error:'Не удалось сохранить раскладку блоков. Изменение отменено.'});break;}
|
||||
}
|
||||
writing=false;emit();
|
||||
};
|
||||
return {
|
||||
getSnapshot:()=>snapshot,
|
||||
subscribe:(listener:()=>void)=>{listeners.add(listener);return()=>{listeners.delete(listener);};},
|
||||
load,
|
||||
change:(open:string[])=>{
|
||||
if(!snapshot.ready||reading)return;
|
||||
for(const section of boardSections){
|
||||
if(open.includes(section)!==snapshot.value.open_sections.includes(section))pending.push({section,open:open.includes(section)});
|
||||
}
|
||||
emit();void flush();
|
||||
},
|
||||
};
|
||||
}
|
||||
export type BoardLayoutStore=ReturnType<typeof createBoardLayoutStore>;
|
||||
Reference in New Issue
Block a user