loader.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { mapperManager } from './mappers';
  2. import { ViewerConfiguration } from './configuration';
  3. import { getConfigurationType } from './types';
  4. import { processConfigurationCompatibility } from './configurationCompatibility';
  5. import { deepmerge } from '../helper/';
  6. import { Tools, IFileRequest } from 'babylonjs';
  7. /**
  8. * The configuration loader will load the configuration object from any source and will use the defined mapper to
  9. * parse the object and return a conform ViewerConfiguration.
  10. * It is a private member of the scene.
  11. */
  12. export class ConfigurationLoader {
  13. private _configurationCache: { [url: string]: any };
  14. private _loadRequests: Array<IFileRequest>;
  15. constructor(private _enableCache: boolean = false) {
  16. this._configurationCache = {};
  17. this._loadRequests = [];
  18. }
  19. /**
  20. * load a configuration object that is defined in the initial configuration provided.
  21. * The viewer configuration can extend different types of configuration objects and have an extra configuration defined.
  22. *
  23. * @param initConfig the initial configuration that has the definitions of further configuration to load.
  24. * @param callback an optional callback that will be called sync, if noconfiguration needs to be loaded or configuration is payload-only
  25. * @returns A promise that delivers the extended viewer configuration, when done.
  26. */
  27. public loadConfiguration(initConfig: ViewerConfiguration = {}, callback?: (config: ViewerConfiguration) => void): Promise<ViewerConfiguration> {
  28. let loadedConfig: ViewerConfiguration = deepmerge({}, initConfig);
  29. this._processInitialConfiguration(loadedConfig);
  30. let extendedConfiguration = getConfigurationType(loadedConfig.extends || "extended");
  31. if (loadedConfig.configuration) {
  32. let mapperType = "json";
  33. return Promise.resolve().then(() => {
  34. if (typeof loadedConfig.configuration === "string" || (loadedConfig.configuration && loadedConfig.configuration.url)) {
  35. // a file to load
  36. let url: string = '';
  37. if (typeof loadedConfig.configuration === "string") {
  38. url = loadedConfig.configuration;
  39. }
  40. // if configuration is an object
  41. if (typeof loadedConfig.configuration === "object" && loadedConfig.configuration.url) {
  42. url = loadedConfig.configuration.url;
  43. let type = loadedConfig.configuration.mapper;
  44. // empty string?
  45. if (!type) {
  46. // load mapper type from filename / url
  47. type = loadedConfig.configuration.url.split('.').pop();
  48. }
  49. mapperType = type || mapperType;
  50. }
  51. return this._loadFile(url);
  52. } else {
  53. if (typeof loadedConfig.configuration === "object") {
  54. mapperType = loadedConfig.configuration.mapper || mapperType;
  55. return loadedConfig.configuration.payload || {};
  56. }
  57. return {};
  58. }
  59. }).then((data: any) => {
  60. let mapper = mapperManager.getMapper(mapperType);
  61. let parsed = deepmerge(mapper.map(data), loadedConfig);
  62. let merged = deepmerge(extendedConfiguration, parsed);
  63. processConfigurationCompatibility(merged);
  64. if (callback) { callback(merged); }
  65. return merged;
  66. });
  67. } else {
  68. loadedConfig = deepmerge(extendedConfiguration, loadedConfig);
  69. processConfigurationCompatibility(loadedConfig);
  70. if (callback) { callback(loadedConfig); }
  71. return Promise.resolve(loadedConfig);
  72. }
  73. }
  74. /**
  75. * Dispose the configuration loader. This will cancel file requests, if active.
  76. */
  77. public dispose() {
  78. this._loadRequests.forEach((request) => {
  79. request.abort();
  80. });
  81. this._loadRequests.length = 0;
  82. }
  83. /**
  84. * This function will process the initial configuration and make needed changes for the viewer to work.
  85. * @param config the mutable(!) initial configuration to process
  86. */
  87. private _processInitialConfiguration(config: ViewerConfiguration) {
  88. if (config.model) {
  89. if (typeof config.model === "string") {
  90. config.model = {
  91. url: config.model
  92. };
  93. }
  94. }
  95. }
  96. private _loadFile(url: string): Promise<any> {
  97. let cacheReference = this._configurationCache;
  98. if (this._enableCache && cacheReference[url]) {
  99. return Promise.resolve(cacheReference[url]);
  100. }
  101. return new Promise((resolve, reject) => {
  102. let fileRequest = Tools.LoadFile(url, (result) => {
  103. let idx = this._loadRequests.indexOf(fileRequest);
  104. if (idx !== -1) {
  105. this._loadRequests.splice(idx, 1);
  106. }
  107. if (this._enableCache) { cacheReference[url] = result; }
  108. resolve(result);
  109. }, undefined, undefined, false, (request, error: any) => {
  110. let idx = this._loadRequests.indexOf(fileRequest);
  111. if (idx !== -1) {
  112. this._loadRequests.splice(idx, 1);
  113. }
  114. reject(error);
  115. });
  116. this._loadRequests.push(fileRequest);
  117. });
  118. }
  119. }