From 1f4e8976110bb5000d4f971ece9b55610d3cd71b Mon Sep 17 00:00:00 2001 From: Lauren Dukes Date: Wed, 23 Oct 2019 16:23:20 -0400 Subject: [PATCH] Simplify resource-code-creator's promise structure for clarity. Change-Id: I4637bfd8916c62928591ef2a23c96e34c2b34443 GitOrigin-RevId: 7f2a69e3b36a8be385b481e1f92f68d28bc2ea4f --- .../conceptmappings/resource-code-creator.ts | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/medtimeline/src/app/conceptmappings/resource-code-creator.ts b/medtimeline/src/app/conceptmappings/resource-code-creator.ts index edb55e8a..6818141e 100644 --- a/medtimeline/src/app/conceptmappings/resource-code-creator.ts +++ b/medtimeline/src/app/conceptmappings/resource-code-creator.ts @@ -52,9 +52,7 @@ export class ConceptConfiguration { */ @Injectable() export class ResourceCodeCreator { - constructor(private http: HttpClient) { - this.createConceptConfiguration(); - } + constructor(private http: HttpClient) {} private static assetPath = './assets/' + environment.conceptsFolder; @@ -95,37 +93,32 @@ export class ResourceCodeCreator { * Mapping from DisplayGrouping to a Promise that resolves to the * ConceptConfiguration object for that DisplayGrouping. */ - loadConfigurationFromFiles: - Map>; + loadConfigurationFromFiles = + new Map>( + Array.from(this.fileMap).map((entry) => { + const displayGrouping: DisplayGrouping = entry[0]; + const files: string[] = entry[1]; - /** - * Load in both the display groups and the clinical conepts, and return - * a ConceptConfiguration that represents them. - */ - private createConceptConfiguration(): - Map> { - if (!this.loadConfigurationFromFiles) { - const configurationsMap = - new Map>(); - this.fileMap.forEach((files, displayGrouping) => { - configurationsMap.set( - displayGrouping, - Promise - .all([ - this.loadDisplayGroupsFromFile( - ResourceCodeCreator.assetPath + '/' + files[0]), - this.createConceptsFromFile( - ResourceCodeCreator.assetPath + '/' + files[1], - displayGrouping) - ]) - .then((results) => { - return new ConceptConfiguration(results[0], results[1]); - })); - }); - this.loadConfigurationFromFiles = configurationsMap; - } - return this.loadConfigurationFromFiles; - } + // These statements could be collapsed but without the type + // hinting the compiler had a hard time figuring out what to + // do, and it was less readable. + let res: [DisplayGrouping, Promise]; + res = [ + displayGrouping, + Promise + .all([ + this.loadDisplayGroupsFromFile( + ResourceCodeCreator.assetPath + '/' + files[0]), + this.createConceptsFromFile( + ResourceCodeCreator.assetPath + '/' + files[1], + displayGrouping) + ]) + .then((results) => { + return new ConceptConfiguration(results[0], results[1]); + }) + ]; + return res; + })); /** * Creates a ResourceCode. The type of ResourceCode is determined by the