@page "/"
@if (MyList != null && MyList.Count > 0)
{
@foreach (var item in MyList)
{
<li>@item</li>
}
}
@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Threading;
@using System.Threading.Tasks;
@code
{
static int counter;
private Dictionary<Guid, List<string>> MyList { get; set; }
private Timer Timer { get; set; }
// A simple lock to make sure that multiple refreshes don't overlap
private bool _isRefreshing;
protected override async Task OnInitializedAsync()
{
await UpdateMyList(); // the initial loading of the list
await base.OnInitializedAsync();
Timer = new Timer(async _ =>
{
await RefreshList();
}, null, 0, 100);
}
private async Task UpdateMyList()
{
_isRefreshing = true;
MyList = await ServiceGetItemsMock(); // external service to get list contents
_isRefreshing = false;
}
private async Task RefreshList()
{
if (!_isRefreshing)
{
await UpdateMyList();
await InvokeAsync(StateHasChanged);
}
}
private async Task<Dictionary<Guid, List<string>>> ServiceGetItemsMock()
{
await Task.Delay(1000);
Dictionary<Guid, List<string>> dictionary = new Dictionary<Guid, List<string>>();
for (int i = 0; i < counter+2; i++)
{
List<string> list = new List<string>();
for (int x = 0; x < 10; x++)
{
list.Add($"item - {x}");
}
dictionary.Add(Guid.NewGuid(), list);
}
counter++;
return dictionary;
}
}