BufferLoader.js 3.7 KB

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