loader.ts 4.9 KB

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