/* Three independent uploads. File bytes live only in the browser and bounded server buffers. */ (function(root){ 'use strict'; class Queue{ constructor(run,changed=()=>{},limit=3){this.run=run;this.changed=changed;this.limit=limit;this.tasks=[];this.active=0;this.epoch=0;this.sequence=0} add(file,meta={}){const task={key:++this.sequence,file,name:file.name,size:file.size,status:'queued',progress:0,error:'',...meta};this.tasks.push(task);this.emit();this.pump();return task} emit(){this.changed(this.tasks)} pump(){for(const task of this.tasks){if(this.active>=this.limit)break;if(task.status!=='queued')continue;this.start(task)}} async start(task){ const epoch=this.epoch;this.active++;task.status='hashing';task.controller=new AbortController();this.emit() const current=()=>epoch===this.epoch&&!task.controller.signal.aborted const update=patch=>{if(current()){Object.assign(task,patch);this.emit()}} try{await this.run(task,update,current);if(current()){task.status='ready';task.progress=100;task.error='';task.file=null}} catch(e){if(epoch===this.epoch){task.status=task.controller.signal.aborted?'paused':'error';task.error=task.status==='paused'?'已暂停,可继续上传':e.message}} finally{if(epoch===this.epoch){this.active--;task.controller=null;this.emit();this.pump()}} } pause(key){const task=this.tasks.find(t=>t.key===key);if(!task)return;if(task.status==='queued'){task.status='paused';this.emit()}else if(task.controller){task.status='pausing';task.controller.abort();this.emit()}} retry(key){const task=this.tasks.find(t=>t.key===key);if(task&&!task.controller&&task.file&&['error','paused'].includes(task.status)){task.status='queued';task.error='';this.emit();this.pump()}} clear(){this.epoch++;for(const task of this.tasks)task.controller?.abort();this.tasks=[];this.active=0;this.emit()} pending(){return this.tasks.some(t=>['queued','hashing','preparing','uploading','finishing','pausing'].includes(t.status))} } if(typeof module!=='undefined'&&module.exports)module.exports=Queue;else root.ZhaoJournalQueue=Queue })(typeof window==='undefined'?globalThis:window);