Peace out,
i want to show document model ids as enum in the UI. Thanks to @malcolm-silver-ice i got a hint with:
I didn’t see any possibility to retrieve these model ids from the state, but this might be due to my lack of experience. I believe I need to obtain these IDs from the backend. Unfortunately, every fetch operation is asynchronous, while the IExternalEnumerationProvider returns a direct value. I have struggled to connect the asynchronous fetch with the synchronous provider, except by implementing a (potentially) race condition with the following code. The fetch request is performed in the constructor.
export function createExternalEnumerationProvider(): IExternalEnumerationProvider {
const documentModelIdExternalEnumProvider = new DocumentModelIdExternalEnumProvider();
return (source: string) => {
switch (source) {
case "documentModelIds": {
return documentModelIdExternalEnumProvider.getModelIdAsExternalEnum();
}
default:
throw new Error("unknown external enumeration source: " + source);
}
};
}
export class DocumentModelIdExternalEnumProvider {
private data: Map<string, { [key: string]: string }> = new Map();
constructor() {
this.getDocumentModelIds(); // Immediately initiate the fetch request during construction
}
private async getDocumentModelIds() {
await importRequest().then((modelMap) => {
// Add all models to a list
modelMap.documentModels.map((value) =>
this.data.set(value.modelId, { value: value.modelId, de: value.modelId })
);
});
}
public getModelIdAsExternalEnum(): ReadonlyObjectMap<{ [key: string]: string }> {
const obj: { [key: string]: { [key: string]: string } } = {};
for (const [key, value] of this.data) {
obj[key] = value;
}
return obj as ReadonlyObjectMap<{ [key: string]: string }>;
}
}
My question is if anyone has a better idea of implementing an asynchronous fetching request and using the result as ExternalEnumProvider
Best regards,
Tjorben