BufferLoader.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import ResourceLoader from "./ResourceLoader.js";
  5. import ResourceLoaderState from "./ResourceLoaderState.js";
  6. /**
  7. * Loads an embedded or external buffer.
  8. * <p>
  9. * Implements the {@link ResourceLoader} interface.
  10. * </p>
  11. *
  12. * @alias BufferLoader
  13. * @constructor
  14. * @augments ResourceLoader
  15. *
  16. * @param {object} options Object with the following properties:
  17. * @param {Uint8Array} [options.typedArray] The typed array containing the embedded buffer contents. Mutually exclusive with options.resource.
  18. * @param {Resource} [options.resource] The {@link Resource} pointing to the external buffer. Mutually exclusive with options.typedArray.
  19. * @param {string} [options.cacheKey] The cache key of the resource.
  20. *
  21. * @exception {DeveloperError} One of options.typedArray and options.resource must be defined.
  22. *
  23. * @private
  24. */
  25. function BufferLoader(options) {
  26. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  27. const typedArray = options.typedArray;
  28. const resource = options.resource;
  29. const cacheKey = options.cacheKey;
  30. //>>includeStart('debug', pragmas.debug);
  31. if (defined(typedArray) === defined(resource)) {
  32. throw new DeveloperError(
  33. "One of options.typedArray and options.resource must be defined."
  34. );
  35. }
  36. //>>includeEnd('debug');
  37. this._typedArray = typedArray;
  38. this._resource = resource;
  39. this._cacheKey = cacheKey;
  40. this._state = ResourceLoaderState.UNLOADED;
  41. this._promise = undefined;
  42. }
  43. if (defined(Object.create)) {
  44. BufferLoader.prototype = Object.create(ResourceLoader.prototype);
  45. BufferLoader.prototype.constructor = BufferLoader;
  46. }
  47. Object.defineProperties(BufferLoader.prototype, {
  48. /**
  49. * The cache key of the resource.
  50. *
  51. * @memberof BufferLoader.prototype
  52. *
  53. * @type {string}
  54. * @readonly
  55. * @private
  56. */
  57. cacheKey: {
  58. get: function () {
  59. return this._cacheKey;
  60. },
  61. },
  62. /**
  63. * The typed array containing the embedded buffer contents.
  64. *
  65. * @memberof BufferLoader.prototype
  66. *
  67. * @type {Uint8Array}
  68. * @readonly
  69. * @private
  70. */
  71. typedArray: {
  72. get: function () {
  73. return this._typedArray;
  74. },
  75. },
  76. });
  77. /**
  78. * Loads the resource.
  79. * @returns {Promise<BufferLoader>} A promise which resolves to the loader when the resource loading is completed.
  80. * @private
  81. */
  82. BufferLoader.prototype.load = async function () {
  83. if (defined(this._promise)) {
  84. return this._promise;
  85. }
  86. if (defined(this._typedArray)) {
  87. this._promise = Promise.resolve(this);
  88. return this._promise;
  89. }
  90. this._promise = loadExternalBuffer(this);
  91. return this._promise;
  92. };
  93. async function loadExternalBuffer(bufferLoader) {
  94. const resource = bufferLoader._resource;
  95. bufferLoader._state = ResourceLoaderState.LOADING;
  96. try {
  97. const arrayBuffer = await BufferLoader._fetchArrayBuffer(resource);
  98. if (bufferLoader.isDestroyed()) {
  99. return;
  100. }
  101. bufferLoader._typedArray = new Uint8Array(arrayBuffer);
  102. bufferLoader._state = ResourceLoaderState.READY;
  103. return bufferLoader;
  104. } catch (error) {
  105. if (bufferLoader.isDestroyed()) {
  106. return;
  107. }
  108. bufferLoader._state = ResourceLoaderState.FAILED;
  109. const errorMessage = `Failed to load external buffer: ${resource.url}`;
  110. throw bufferLoader.getError(errorMessage, error);
  111. }
  112. }
  113. /**
  114. * Exposed for testing
  115. * @private
  116. */
  117. BufferLoader._fetchArrayBuffer = function (resource) {
  118. return resource.fetchArrayBuffer();
  119. };
  120. /**
  121. * Unloads the resource.
  122. * @private
  123. */
  124. BufferLoader.prototype.unload = function () {
  125. this._typedArray = undefined;
  126. };
  127. export default BufferLoader;