Reference Source

src/viewer/scene/utils/LoadingManager.js

  1. class LoadingManager {
  2.  
  3. constructor(onLoad, onProgress, onError) {
  4.  
  5. this.isLoading = false;
  6. this.itemsLoaded = 0;
  7. this.itemsTotal = 0;
  8. this.urlModifier = undefined;
  9. this.handlers = [];
  10.  
  11. this.onStart = undefined;
  12. this.onLoad = onLoad;
  13. this.onProgress = onProgress;
  14. this.onError = onError;
  15. }
  16.  
  17. itemStart(url) {
  18. this.itemsTotal++;
  19. if (this.isLoading === false) {
  20. if (this.onStart !== undefined) {
  21. this.onStart(url, this.itemsLoaded, this.itemsTotal);
  22. }
  23. }
  24. this.isLoading = true;
  25. }
  26.  
  27. itemEnd(url) {
  28. this.itemsLoaded++;
  29. if (this.onProgress !== undefined) {
  30. this.onProgress(url, this.itemsLoaded, this.itemsTotal);
  31. }
  32. if (this.itemsLoaded === this.itemsTotal) {
  33. this.isLoading = false;
  34. if (this.onLoad !== undefined) {
  35. this.onLoad();
  36. }
  37. }
  38. }
  39.  
  40. itemError(url) {
  41. if (this.onError !== undefined) {
  42. this.onError(url);
  43. }
  44. }
  45.  
  46. resolveURL(url) {
  47. if (this.urlModifier) {
  48. return this.urlModifier(url);
  49. }
  50. return url;
  51. }
  52.  
  53. setURLModifier(transform) {
  54. this.urlModifier = transform;
  55. return this;
  56. }
  57.  
  58. addHandler(regex, loader) {
  59. this.handlers.push(regex, loader);
  60. return this;
  61. }
  62.  
  63. removeHandler(regex) {
  64. const index = this.handlers.indexOf(regex);
  65. if (index !== -1) {
  66. this.handlers.splice(index, 2);
  67. }
  68. return this;
  69. }
  70.  
  71. getHandler(file) {
  72. for (let i = 0, l = this.handlers.length; i < l; i += 2) {
  73. const regex = this.handlers[i];
  74. const loader = this.handlers[i + 1];
  75. if (regex.global) regex.lastIndex = 0; // see #17920
  76. if (regex.test(file)) {
  77. return loader;
  78. }
  79. }
  80. return null;
  81. }
  82. }
  83.  
  84. const DefaultLoadingManager = new LoadingManager();
  85.  
  86. export {DefaultLoadingManager, LoadingManager};