using C4IT.FASD.Base; using C4IT.FASD.Cockpit.Communication; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using static C4IT.Logging.cLogManager; [assembly: InternalsVisibleTo("F4SD.Cockpit.Client.Test")] namespace FasdDesktopUi.Basics.Services.RelationService { internal class RelationService : IRelationService { private IEnumerable _relations = new List(); /// /// Asynchronously loads relations for the specified search results. /// /// This method initiates a task to gather relations for the provided search results and /// periodically checks for the completion of the task. Once relations are retrieved, an event /// is raised to notify listeners of the progress. /// Task id and pending information classes public async Task LoadRelationsAsync(IEnumerable relatedTo, CancellationToken token = default) { try { _relations = new List(); cF4sdStagedSearchResultRelationTaskId gatherRelationTask = await cFasdCockpitCommunicationBase.Instance.StartGatheringRelations(relatedTo, token); _ = Task.Run(async () => { const int maxRetryCount = 10; for (int i = 0; i < maxRetryCount; i++) { cF4sdStagedSearchResultRelations stagedRelations = await cFasdCockpitCommunicationBase.Instance.GetStagedRelations(gatherRelationTask.Id, token); stagedRelations.MergeAsRelationInfosWith(relatedTo); _relations = _relations.Union(stagedRelations.Relations); RelationsFound?.Invoke(this, new StagedSearchResultRelationsEventArgs() { RelatedTo = relatedTo, StagedResultRelations = stagedRelations }); if (stagedRelations?.IsComplete ?? false) break; } }); return gatherRelationTask; } catch (Exception ex) { LogException(ex); } return null; } public IEnumerable GetLoadedRelations() => _relations; public IRelationService Clone() { RelationService copy = (RelationService)MemberwiseClone(); copy._relations = _relations.Select(r => r).ToList(); return copy; } public event EventHandler RelationsFound; } }