ResourceLoader.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import Check from "../Core/Check.js";
  2. import defined from "../Core/defined.js";
  3. import destroyObject from "../Core/destroyObject.js";
  4. import DeveloperError from "../Core/DeveloperError.js";
  5. import RuntimeError from "../Core/RuntimeError.js";
  6. /**
  7. * A cache resource.
  8. * <p>
  9. * This type describes an interface and is not intended to be instantiated directly.
  10. * </p>
  11. *
  12. * @alias ResourceLoader
  13. * @constructor
  14. *
  15. * @see ResourceCache
  16. *
  17. * @private
  18. */
  19. function ResourceLoader() {}
  20. Object.defineProperties(ResourceLoader.prototype, {
  21. /**
  22. * The cache key of the resource.
  23. *
  24. * @memberof ResourceLoader.prototype
  25. *
  26. * @type {string}
  27. * @readonly
  28. * @private
  29. */
  30. cacheKey: {
  31. // eslint-disable-next-line getter-return
  32. get: function () {
  33. DeveloperError.throwInstantiationError();
  34. },
  35. },
  36. });
  37. /**
  38. * Loads the resource.
  39. * @returns {Promise<ResourceLoader>} A promise which resolves to the loader when the resource loading is completed.
  40. * @private
  41. */
  42. ResourceLoader.prototype.load = function () {
  43. DeveloperError.throwInstantiationError();
  44. };
  45. /**
  46. * Unloads the resource.
  47. * @private
  48. */
  49. ResourceLoader.prototype.unload = function () {};
  50. /**
  51. * Processes the resource until it becomes ready.
  52. *
  53. * @param {FrameState} frameState The frame state.
  54. * @returns {boolean} true once all resourced are ready.
  55. * @private
  56. */
  57. ResourceLoader.prototype.process = function (frameState) {
  58. return false;
  59. };
  60. /**
  61. * Constructs a {@link RuntimeError} from an errorMessage and an error.
  62. *
  63. * @param {string} errorMessage The error message.
  64. * @param {Error} [error] The error.
  65. *
  66. * @returns {RuntimeError} The runtime error.
  67. * @private
  68. */
  69. ResourceLoader.prototype.getError = function (errorMessage, error) {
  70. //>>includeStart('debug', pragmas.debug);
  71. Check.typeOf.string("errorMessage", errorMessage);
  72. //>>includeEnd('debug');
  73. if (defined(error) && defined(error.message)) {
  74. errorMessage += `\n${error.message}`;
  75. }
  76. const runtimeError = new RuntimeError(errorMessage);
  77. if (defined(error)) {
  78. runtimeError.stack = `Original stack:\n${error.stack}\nHandler stack:\n${runtimeError.stack}`;
  79. }
  80. return runtimeError;
  81. };
  82. /**
  83. * Returns true if this object was destroyed; otherwise, false.
  84. * <br /><br />
  85. * If this object was destroyed, it should not be used; calling any function other than
  86. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  87. *
  88. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  89. *
  90. * @see ResourceLoader#destroy
  91. * @private
  92. */
  93. ResourceLoader.prototype.isDestroyed = function () {
  94. return false;
  95. };
  96. /**
  97. * Destroys the loaded resource.
  98. * <br /><br />
  99. * Once an object is destroyed, it should not be used; calling any function other than
  100. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  101. * assign the return value (<code>undefined</code>) to the object as done in the example.
  102. *
  103. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  104. *
  105. * @example
  106. * resourceLoader = resourceLoader && resourceLoader.destroy();
  107. *
  108. * @see ResourceLoader#isDestroyed
  109. * @private
  110. */
  111. ResourceLoader.prototype.destroy = function () {
  112. this.unload();
  113. return destroyObject(this);
  114. };
  115. export default ResourceLoader;