Progress.js 2.1 KB

123456789101112131415161718192021222324252627282930
  1. export class Progress{setTotalWork(totalWork){}
  2. setTitle(title){}
  3. setWorked(worked,title){}
  4. worked(worked){}
  5. done(){}
  6. isCanceled(){return false;}}
  7. export class CompositeProgress{constructor(parent){this._parent=parent;this._children=[];this._childrenDone=0;this._parent.setTotalWork(1);this._parent.setWorked(0);}
  8. _childDone(){if(++this._childrenDone!==this._children.length){return;}
  9. this._parent.done();}
  10. createSubProgress(weight){const child=new SubProgress(this,weight);this._children.push(child);return child;}
  11. _update(){let totalWeights=0;let done=0;for(let i=0;i<this._children.length;++i){const child=this._children[i];if(child._totalWork){done+=child._weight*child._worked/child._totalWork;}
  12. totalWeights+=child._weight;}
  13. this._parent.setWorked(done/totalWeights);}}
  14. export class SubProgress{constructor(composite,weight){this._composite=composite;this._weight=weight||1;this._worked=0;}
  15. isCanceled(){return this._composite._parent.isCanceled();}
  16. setTitle(title){this._composite._parent.setTitle(title);}
  17. done(){this.setWorked(this._totalWork);this._composite._childDone();}
  18. setTotalWork(totalWork){this._totalWork=totalWork;this._composite._update();}
  19. setWorked(worked,title){this._worked=worked;if(typeof title!=='undefined'){this.setTitle(title);}
  20. this._composite._update();}
  21. worked(worked){this.setWorked(this._worked+(worked||1));}}
  22. export class ProgressProxy{constructor(delegate,doneCallback){this._delegate=delegate;this._doneCallback=doneCallback;}
  23. isCanceled(){return this._delegate?this._delegate.isCanceled():false;}
  24. setTitle(title){if(this._delegate){this._delegate.setTitle(title);}}
  25. done(){if(this._delegate){this._delegate.done();}
  26. if(this._doneCallback){this._doneCallback();}}
  27. setTotalWork(totalWork){if(this._delegate){this._delegate.setTotalWork(totalWork);}}
  28. setWorked(worked,title){if(this._delegate){this._delegate.setWorked(worked,title);}}
  29. worked(worked){if(this._delegate){this._delegate.worked(worked);}}}
  30. self.Common=self.Common||{};Common=Common||{};Common.Progress=Progress;Common.CompositeProgress=CompositeProgress;Common.SubProgress=SubProgress;Common.ProgressProxy=ProgressProxy;