统一全系统资源加载、轻量趋势与缓存恢复
This commit is contained in:
@@ -7,13 +7,13 @@ function fixture() {
|
||||
const source=fs.readFileSync(process.env.QUERY_CHART_SOURCE || new URL('../src/QueryPage.tsx',import.meta.url),'utf8');
|
||||
const end=source.includes('export function TrendTable(')?source.indexOf('export function TrendTable('):source.indexOf('function TrendTable(');
|
||||
const code=source.slice(source.indexOf('export function TrendChart('),end).replace('export function','function');
|
||||
const slots=new Map(),effects=new Map(),observers=[],charts=[];let cursor=0,dirty=false,queued=[],active=true;
|
||||
const slots=new Map(),effects=new Map(),observers=[],charts=[];let cursor=0,dirty=false,queued=[],active=true,visible=true;
|
||||
const node={clientWidth:900,clientHeight:380};
|
||||
const hooks={useRef(initial){const i=cursor++;if(!slots.has(i))slots.set(i,{current:initial});return slots.get(i);},useState(initial){const i=cursor++;if(!slots.has(i))slots.set(i,initial);return [slots.get(i),v=>{const next=typeof v==='function'?v(slots.get(i)):v;if(!Object.is(next,slots.get(i))){slots.set(i,next);dirty=true;}}];},useCallback(fn,deps){const i=cursor++;const old=slots.get(i);if(!old||deps.some((d,j)=>!Object.is(d,old.deps[j])))slots.set(i,{fn,deps});return slots.get(i).fn;},useEffect(fn,deps){const i=cursor++,old=effects.get(i);if(!old||deps.some((d,j)=>!Object.is(d,old.deps[j])))queued.push(()=>{old?.cleanup?.();effects.set(i,{deps,cleanup:fn()});});}};
|
||||
hooks.useLayoutEffect=hooks.useEffect;
|
||||
const ec={init(_node,_theme,opts){const c={opts,options:[],resizes:[],disposed:false,getWidth:()=>c.opts.width??100,getHeight:()=>c.opts.height??380,on(){},resize(o){c.resizes.push(o);},setOption(o){c.options.push(o);},convertToPixel:()=>0,dispose(){c.disposed=true;}};charts.push(c);return c;}};
|
||||
const prelude=`const {useRef,useState,useEffect,useLayoutEffect,useCallback}=hooks;
|
||||
const usePageCache=()=>({active:getActive()});
|
||||
const usePageCache=()=>({active:getActive()}),useResourceVisibility=()=>getActive()&&getVisible();
|
||||
const Empty=Object.assign('Empty',{PRESENTED_IMAGE_SIMPLE:'simple'});
|
||||
const MutationObserver=class{observe(){}disconnect(){}},ResizeObserver=class{constructor(fn){this.fn=fn;observers.push(this)}observe(){}disconnect(){this.disconnected=true}};
|
||||
const document={documentElement:{dataset:{}}},window={matchMedia:()=>({matches:false,addEventListener(){},removeEventListener(){}})};
|
||||
@@ -22,11 +22,11 @@ function fixture() {
|
||||
const trendBarWidth=()=>32,hasStageRows=()=>true,stageValue=(r,s,f)=>r[s][f],visibleCompositeRate=()=>.95,visibleStageRate=()=>.97,rateNumber=v=>v*100,numberText=String,escapeTooltipText=String,defectWeightRate=()=>0;
|
||||
`;
|
||||
const compiled=ts.transpileModule(prelude+code+'\nreturn TrendChart;',{fileName:'motion.tsx',compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText;
|
||||
const Component=new Function('React','hooks','echarts','getActive','observers',compiled)(React,hooks,ec,()=>active,observers);
|
||||
const Component=new Function('React','hooks','echarts','getActive','getVisible','observers',compiled)(React,hooks,ec,()=>active,()=>visible,observers);
|
||||
const stage={qualifiedWeight:90,scrapWeight:10,totalWeight:100,defects:[]};const props={rows:[{month:1,black:stage,bright:stage}],mode:'production',stage:'bright',rateBasis:'original',targetRate:null,onMonth(){}};
|
||||
function attach(t){if(!t||typeof t!=='object')return;if(t.props?.ref)t.props.ref.current=node;React.Children.forEach(t.props?.children,attach);}
|
||||
function render(){let n=0;do{cursor=0;dirty=false;queued=[];attach(Component(props));queued.forEach(f=>f());assert.ok(++n<10);}while(dirty);}
|
||||
return {charts,node,render,setActive(v){active=v;render();},resize(){for(const o of observers)if(!o.disconnected)o.fn();},unmount(){effects.forEach(e=>e.cleanup?.());}};
|
||||
return {charts,node,render,setVisible(v){visible=v;render();},setActive(v){active=v;render();},resize(){for(const o of observers)if(!o.disconnected)o.fn();},unmount(){effects.forEach(e=>e.cleanup?.());}};
|
||||
}
|
||||
test('visible entry uses the actual chart size and enables native vertical bar growth',()=>{
|
||||
const f=fixture();f.render();const c=f.charts[0];assert.equal(c.opts.width,900);assert.equal(c.opts.height,380);
|
||||
@@ -37,3 +37,7 @@ test('cached page reentry creates a fresh vertical reveal and never resizes a hi
|
||||
f.setActive(false);assert.equal(f.charts[0].disposed,true);assert.equal(f.charts.length,1);
|
||||
f.node.clientWidth=900;f.setActive(true);assert.equal(f.charts.length,2);assert.equal(f.charts[1].opts.width,900);assert.ok(f.charts[1].options.find(o=>o.series).animationDuration>0);f.unmount();
|
||||
});
|
||||
|
||||
test('offscreen chart defers initialization until its region becomes visible',()=>{
|
||||
const f=fixture();f.setVisible(false);assert.equal(f.charts.length,0);f.setVisible(true);assert.equal(f.charts.length,1);f.unmount();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user