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

MetadataSchemaLoader.js 3.3KB

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