智慧水务管理系统 - 精河县供水工程综合管理平台

BufferLoader.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import Frozen from "../Core/Frozen.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. * @private
  13. */
  14. class BufferLoader extends 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. constructor(options) {
  24. super();
  25. options = options ?? Frozen.EMPTY_OBJECT;
  26. const typedArray = options.typedArray;
  27. const resource = options.resource;
  28. const cacheKey = options.cacheKey;
  29. //>>includeStart('debug', pragmas.debug);
  30. if (defined(typedArray) === defined(resource)) {
  31. throw new DeveloperError(
  32. "One of options.typedArray and options.resource must be defined.",
  33. );
  34. }
  35. //>>includeEnd('debug');
  36. this._typedArray = typedArray;
  37. this._resource = resource;
  38. this._cacheKey = cacheKey;
  39. this._state = ResourceLoaderState.UNLOADED;
  40. this._promise = undefined;
  41. }
  42. /**
  43. * The cache key of the resource.
  44. *
  45. *
  46. * @type {string}
  47. * @readonly
  48. * @private
  49. */
  50. get cacheKey() {
  51. return this._cacheKey;
  52. }
  53. /**
  54. * The typed array containing the embedded buffer contents.
  55. *
  56. *
  57. * @type {Uint8Array}
  58. * @readonly
  59. * @private
  60. */
  61. get typedArray() {
  62. return this._typedArray;
  63. }
  64. /**
  65. * Loads the resource.
  66. * @returns {Promise<BufferLoader>} A promise which resolves to the loader when the resource loading is completed.
  67. * @private
  68. */
  69. async load() {
  70. if (defined(this._promise)) {
  71. return this._promise;
  72. }
  73. if (defined(this._typedArray)) {
  74. this._promise = Promise.resolve(this);
  75. return this._promise;
  76. }
  77. this._promise = loadExternalBuffer(this);
  78. return this._promise;
  79. }
  80. /**
  81. * Exposed for testing
  82. * @private
  83. */
  84. static _fetchArrayBuffer(resource) {
  85. return resource.fetchArrayBuffer();
  86. }
  87. /**
  88. * Unloads the resource.
  89. * @private
  90. */
  91. unload() {
  92. this._typedArray = undefined;
  93. }
  94. }
  95. async function loadExternalBuffer(bufferLoader) {
  96. const resource = bufferLoader._resource;
  97. bufferLoader._state = ResourceLoaderState.LOADING;
  98. try {
  99. const arrayBuffer = await BufferLoader._fetchArrayBuffer(resource);
  100. if (bufferLoader.isDestroyed()) {
  101. return;
  102. }
  103. bufferLoader._typedArray = new Uint8Array(arrayBuffer);
  104. bufferLoader._state = ResourceLoaderState.READY;
  105. return bufferLoader;
  106. } catch (error) {
  107. if (bufferLoader.isDestroyed()) {
  108. return;
  109. }
  110. bufferLoader._state = ResourceLoaderState.FAILED;
  111. const errorMessage = `Failed to load external buffer: ${resource.url}`;
  112. throw bufferLoader.getError(errorMessage, error);
  113. }
  114. }
  115. export default BufferLoader;