loader.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. export class ConfigurationLoader {
  7. private configurationCache: { [url: string]: any };
  8. private loadRequests: Array<IFileRequest>;
  9. constructor() {
  10. this.configurationCache = {};
  11. this.loadRequests = [];
  12. }
  13. public loadConfiguration(initConfig: ViewerConfiguration = {}, callback?: (config: ViewerConfiguration) => void): Promise<ViewerConfiguration> {
  14. let loadedConfig: ViewerConfiguration = deepmerge({}, initConfig);
  15. let extendedConfiguration = getConfigurationType(loadedConfig.extends || "");
  16. loadedConfig = deepmerge(extendedConfiguration, loadedConfig);
  17. if (loadedConfig.configuration) {
  18. let mapperType = "json";
  19. return Promise.resolve().then(() => {
  20. if (typeof loadedConfig.configuration === "string" || (loadedConfig.configuration && loadedConfig.configuration.url)) {
  21. // a file to load
  22. let url: string = '';
  23. if (typeof loadedConfig.configuration === "string") {
  24. url = loadedConfig.configuration;
  25. }
  26. // if configuration is an object
  27. if (typeof loadedConfig.configuration === "object" && loadedConfig.configuration.url) {
  28. url = loadedConfig.configuration.url;
  29. let type = loadedConfig.configuration.mapper;
  30. // empty string?
  31. if (!type) {
  32. // load mapper type from filename / url
  33. type = loadedConfig.configuration.url.split('.').pop();
  34. }
  35. mapperType = type || mapperType;
  36. }
  37. return this.loadFile(url);
  38. } else {
  39. if (typeof loadedConfig.configuration === "object") {
  40. mapperType = loadedConfig.configuration.mapper || mapperType;
  41. return loadedConfig.configuration.payload || {};
  42. }
  43. return {};
  44. }
  45. }).then((data: any) => {
  46. let mapper = mapperManager.getMapper(mapperType);
  47. let parsed = mapper.map(data);
  48. let merged = deepmerge(loadedConfig, parsed);
  49. if (callback) callback(merged);
  50. return merged;
  51. });
  52. } else {
  53. if (callback) callback(loadedConfig);
  54. return Promise.resolve(loadedConfig);
  55. }
  56. }
  57. public dispose() {
  58. this.loadRequests.forEach(request => {
  59. request.abort();
  60. });
  61. }
  62. private loadFile(url: string): Promise<any> {
  63. let cacheReference = this.configurationCache;
  64. if (cacheReference[url]) {
  65. return Promise.resolve(cacheReference[url]);
  66. }
  67. return new Promise((resolve, reject) => {
  68. let fileRequest = Tools.LoadFile(url, resolve, undefined, undefined, false, (request, error: any) => {
  69. reject(error);
  70. });
  71. this.loadRequests.push(fileRequest);
  72. });
  73. }
  74. }
  75. export let configurationLoader = new ConfigurationLoader();
  76. export default configurationLoader;