I'm building an Angular Material mat-tree with dynamic data (based on this example).
When the user expend a node, a progress-bar is shown while the list of child nodes is retrieved from the server.
The base code looks like this:
toggleNode(node: DynamicFlatNode, expand: boolean) {
if (expand) {
// Shows progress bar
node.isLoading = true;
this
.database
.getChildren(node.id)
.subscribe(
values => {
// Create and add the descendant nodes
...
},
error => { console.error(error); },
() => {
// Hides progress bar
node.isLoading = false;
});
} else {
// Remove descendant nodes
...
}
}
The issue is that if the data retrieving is too quick, the progress bar just blink.
I would like to add a delay so the progress bar is shown at least, let say, 250ms.
I want the time of visibility to be min(250ms, data retrieving time) not 250 ms + data retrieving time.
My current solution is to use a dummy promise, that starts before data retrieving and is awaited in the complete callback:
toggleNode(node: DynamicFlatNode, expand: boolean) {
if (expand) {
// Shows progress bar
node.isLoading = true;
const delay = new Promise(resolve => setTimeout(resolve, 250));
this
.database
.getChildren(node.id)
.subscribe(
values => {
// Create and add the descendant nodes
...
},
error => { console.error(error); },
async () => {
await delay;
// Hides progress bar
node.isLoading = false;
});
} else {
// Remove descendant nodes
...
}
}
Is there a more concise way of doing this?