private async Task RefreshDataAsync() { _refreshCts?.Cancel(); _refreshCts = new CancellationTokenSource(); var cancellationToken = _refreshCts.Token; var request = new ItemsProviderRequest(_itemsBefore, _visibleItemCapacity, cancellationToken); try { var result = await _itemsProvider(request); // Only apply result if the task was not canceled. if (!cancellationToken.IsCancellationRequested) { _itemCount = result.TotalItemCount; _loadedItems = result.Items; _loadedItemsStartIndex = request.StartIndex; StateHasChanged(); } } catch (Exception e) { if (e is OperationCanceledException oce && oce.CancellationToken == cancellationToken) { // No-op; we canceled the operation, so it's fine to suppress this exception. }
private async ValueTask RefreshDataCoreAsync(bool renderOnSuccess) { _refreshCts?.Cancel(); CancellationToken cancellationToken; if (_itemsProvider == DefaultItemsProvider) { // If we're using the DefaultItemsProvider (because the developer supplied a fixed // Items collection) we know it will complete synchronously, and there's no point // instantiating a new CancellationTokenSource _refreshCts = null; cancellationToken = CancellationToken.None; } else { _refreshCts = new CancellationTokenSource(); cancellationToken = _refreshCts.Token; } var request = new ItemsProviderRequest(_itemsBefore, _visibleItemCapacity, cancellationToken); try { var result = await _itemsProvider(request); // Only apply result if the task was not canceled. if (!cancellationToken.IsCancellationRequested) { _itemCount = result.TotalItemCount; _loadedItems = result.Items; _loadedItemsStartIndex = request.StartIndex; if (renderOnSuccess) { StateHasChanged(); } } } catch (Exception e) { if (e is OperationCanceledException oce && oce.CancellationToken == cancellationToken) { // No-op; we canceled the operation, so it's fine to suppress this exception. }