MetadataLoader.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory'], function (APIFactory) {
  9. 'use strict';
  10. var MetadataLoader = function () {
  11. /*
  12. The MetadataLoader will improve performance by starting to load metadata as soon as possible in the dashboard life cycle
  13. The feature will also prevent hogging all network connections by leveraging a queue which only allows up to 3 metadata requests at once
  14. */
  15. function MetadataLoader(options) {
  16. _classCallCheck(this, MetadataLoader);
  17. this.options = options;
  18. this.API = options.features.API;
  19. }
  20. MetadataLoader.prototype.initialize = function initialize() {
  21. var dataSources = this.API.getFeature('DataSources').getDataSourceList();
  22. this.loadMetadata(dataSources);
  23. };
  24. MetadataLoader.prototype.getAPI = function getAPI() {
  25. return APIFactory.createAPI(this, []);
  26. };
  27. MetadataLoader.prototype.loadMetadata = function loadMetadata(dataSources) {
  28. var loaderPromises = [];
  29. dataSources.forEach(function (dataSource) {
  30. return loaderPromises.push(dataSource.loadMetadata);
  31. });
  32. var MAX_WORKERS = 3; //Avoid hogging all network connections by only allowing up to 3 concurrent metadata requests
  33. var promiseResults = [];
  34. var workerCount = 0;
  35. var promiseIdx = 0;
  36. return new Promise(function (done) {
  37. var getNextLoaderPromise = function getNextLoaderPromise() {
  38. if (workerCount < MAX_WORKERS && promiseIdx < loaderPromises.length) {
  39. loaderPromises[promiseIdx]().then(onSettlePromise).fail(onSettlePromise);
  40. promiseIdx++;
  41. workerCount++;
  42. getNextLoaderPromise();
  43. } else if (workerCount === 0 && promiseIdx === loaderPromises.length) {
  44. done(promiseResults);
  45. }
  46. };
  47. var onSettlePromise = function onSettlePromise(result) {
  48. promiseResults.push(result);
  49. workerCount--;
  50. getNextLoaderPromise();
  51. };
  52. getNextLoaderPromise();
  53. });
  54. };
  55. return MetadataLoader;
  56. }();
  57. return MetadataLoader;
  58. });
  59. //# sourceMappingURL=MetadataLoader.js.map