MetadataSchemaLoader.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import MetadataSchema from "./MetadataSchema.js";
  5. import ResourceLoader from "./ResourceLoader.js";
  6. import ResourceLoaderState from "./ResourceLoaderState.js";
  7. /**
  8. * A {@link MetadataSchema} loader.
  9. * <p>
  10. * Implements the {@link ResourceLoader} interface.
  11. * </p>
  12. *
  13. * @alias MetadataSchemaLoader
  14. * @constructor
  15. * @augments ResourceLoader
  16. *
  17. * @param {object} options Object with the following properties:
  18. * @param {object} [options.schema] An object that explicitly defines a schema JSON. Mutually exclusive with options.resource.
  19. * @param {Resource} [options.resource] The {@link Resource} pointing to the schema JSON. Mutually exclusive with options.schema.
  20. * @param {string} [options.cacheKey] The cache key of the resource.
  21. *
  22. * @exception {DeveloperError} One of options.schema and options.resource must be defined.
  23. *
  24. * @private
  25. * @experimental This feature is using part of the 3D Tiles spec that is not final and is subject to change without Cesium's standard deprecation policy.
  26. */
  27. function MetadataSchemaLoader(options) {
  28. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  29. const schema = options.schema;
  30. const resource = options.resource;
  31. const cacheKey = options.cacheKey;
  32. //>>includeStart('debug', pragmas.debug);
  33. if (defined(schema) === defined(resource)) {
  34. throw new DeveloperError(
  35. "One of options.schema and options.resource must be defined."
  36. );
  37. }
  38. //>>includeEnd('debug');
  39. this._schema = defined(schema) ? MetadataSchema.fromJson(schema) : undefined;
  40. this._resource = resource;
  41. this._cacheKey = cacheKey;
  42. this._state = ResourceLoaderState.UNLOADED;
  43. this._promise = undefined;
  44. }
  45. if (defined(Object.create)) {
  46. MetadataSchemaLoader.prototype = Object.create(ResourceLoader.prototype);
  47. MetadataSchemaLoader.prototype.constructor = MetadataSchemaLoader;
  48. }
  49. Object.defineProperties(MetadataSchemaLoader.prototype, {
  50. /**
  51. * The cache key of the resource.
  52. *
  53. * @memberof MetadataSchemaLoader.prototype
  54. *
  55. * @type {string}
  56. * @readonly
  57. * @private
  58. */
  59. cacheKey: {
  60. get: function () {
  61. return this._cacheKey;
  62. },
  63. },
  64. /**
  65. * The metadata schema object.
  66. *
  67. * @memberof MetadataSchemaLoader.prototype
  68. *
  69. * @type {MetadataSchema}
  70. * @readonly
  71. * @private
  72. */
  73. schema: {
  74. get: function () {
  75. return this._schema;
  76. },
  77. },
  78. });
  79. /**
  80. * Loads the resource.
  81. * @returns {Promise<MetadataSchemaLoader>} A promise which resolves to the loader when the resource loading is completed.
  82. * @private
  83. */
  84. MetadataSchemaLoader.prototype.load = async function () {
  85. if (defined(this._promise)) {
  86. return this._promise;
  87. }
  88. if (defined(this._schema)) {
  89. this._promise = Promise.resolve(this);
  90. return this._promise;
  91. }
  92. this._promise = loadExternalSchema(this);
  93. return this._promise;
  94. };
  95. async function loadExternalSchema(schemaLoader) {
  96. const resource = schemaLoader._resource;
  97. schemaLoader._state = ResourceLoaderState.LOADING;
  98. try {
  99. const json = await resource.fetchJson();
  100. if (schemaLoader.isDestroyed()) {
  101. return;
  102. }
  103. schemaLoader._schema = MetadataSchema.fromJson(json);
  104. schemaLoader._state = ResourceLoaderState.READY;
  105. return schemaLoader;
  106. } catch (error) {
  107. if (schemaLoader.isDestroyed()) {
  108. return;
  109. }
  110. schemaLoader._state = ResourceLoaderState.FAILED;
  111. const errorMessage = `Failed to load schema: ${resource.url}`;
  112. throw schemaLoader.getError(errorMessage, error);
  113. }
  114. }
  115. /**
  116. * Unloads the resource.
  117. * @private
  118. */
  119. MetadataSchemaLoader.prototype.unload = function () {
  120. this._schema = undefined;
  121. };
  122. export default MetadataSchemaLoader;