MetadataSchemaLoader.js 3.8 KB

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