fix multiple promises - one canceling the other - call service once

This commit is contained in:
argirok 2022-06-09 16:52:57 +03:00
parent f974e91c25
commit b451a2ea3a
1 changed files with 33 additions and 53 deletions

View File

@ -12,7 +12,7 @@ export class ISVocabulariesService {
private provenanceActionVocabulary: BehaviorSubject<{}> = new BehaviorSubject(null);
private subjectsVocabulary: BehaviorSubject<any> = new BehaviorSubject<any>(null);
private subscriptions = [];
private vocabulariesPromises: Map<string, Promise<any>> = new Map<string, Promise<any>>();
constructor(private http: HttpClient) {}
ngOnDestroy() {
@ -27,90 +27,70 @@ export class ISVocabulariesService {
});
}
getVocabularyByType(field: string, entity: string, properties: EnvProperties): Observable<any> {
//console.log("getVocabulary field: "+ field + " for entity: "+ entity);
var file = "";
var vocabulary = "";
let file = "";
let vocabulary = "";
if (field == "lang") {
// file="languages.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:languages.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "type" && (entity == "publication")) {
// file = "publicationTypes.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:publication_resource.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "type" && (entity == "dataset")) {
// file = "dnet:dataCite_resource.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:dataCite_resource.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "type" && (entity == "software" || entity == "other")) {
return of([]);
} else if (field == "type" && entity == "result" ) {
return zip(this.getVocabularyFromService("dnet:publication_resource.json", properties),this.getVocabularyFromService("dnet:dataCite_resource.json", properties));
} else if (field == "access" && (entity == "publication" || entity == "dataset" || entity == "software" || entity == "other" || entity == "result")) {
// file= "accessMode.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:access_modes.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if ((field == "type") && (entity == "dataprovider")) {
// file = "dataProviderType.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:datasource_typologies.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "compatibility" && (entity == "dataprovider")) {
// file = "dataProviderCompatibility.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:datasourceCompatibilityLevel.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "country") {
// file = "countries.json";
// return this.getVocabularyFromFile(file);
vocabulary = "dnet:countries.json";
//return this.getVocabularyFromService(vocabulary, properties);
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "fos") {
vocabulary = "fos";
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
} else if (field == "sdg") {
vocabulary = "sdg";
return from(this.getVocabularyFromServiceAsync(vocabulary, properties));
return this.getVocabularyFromServiceAsync(vocabulary, properties);
}
return null;
}
async getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties): Promise<AutoCompleteValue[]> {
getVocabularyFromServiceAsync(vocabularyName: string, properties: EnvProperties) {
if(!this.vocabularies.has(vocabularyName)) {
await new Promise<any>(resolve => {
this.vocabularies.set(vocabularyName, new BehaviorSubject<any>(null));
this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
vocabularyRes => {
this.vocabularies.get(vocabularyName).next(vocabularyRes);
resolve();
}, error => {
this.vocabularies.get(vocabularyName).next(null);
resolve();
}
));
});
this.clearSubscriptions();
this.vocabularies.set(vocabularyName, new BehaviorSubject<any>(null));
if(!this.vocabulariesPromises.has(vocabularyName)) {
this.vocabulariesPromises.set(vocabularyName,
new Promise<any>(resolve => {
this.subscriptions.push(this.getVocabularyFromService(vocabularyName, properties).subscribe(
vocabularyRes => {
this.vocabularies.get(vocabularyName).next(vocabularyRes);
resolve();
}, error => {
this.vocabularies.get(vocabularyName).next(null);
resolve();
}
));
}));
}
}
return from(this.getAsync(vocabularyName));
}
async getAsync(vocabularyName: string): Promise<AutoCompleteValue[]> {
await this.vocabulariesPromises.get(vocabularyName);
return this.vocabularies.get(vocabularyName).getValue();
}
getVocabularyFromService(vocabularyName: string, properties: EnvProperties): Observable<AutoCompleteValue[]> {
let url = properties.vocabulariesAPI + vocabularyName;
if(vocabularyName == 'fos' || vocabularyName == 'sdg'){