private async Task <IEnumerable <Document> > ProcessUserDocuments(IEnumerable <UserDocument> userDocuments) { var workItems = CreateWorkItemsForProcessingOperations(userDocuments).ToList(); workItems.ForEach(item => userWorkItemsService.UpdateWorkItemForUser(User.GetUserId(), item)); var tasks = from source in userDocuments join workItem in workItems on source.Id equals workItem.Id select ProcessUserDocument(source, workItem); var results = await Task.WhenAll(tasks); ProcessedDocuments = ProcessedDocuments.Union(results); return(results); }
/// <summary> /// Determines the active user and appends the document processing operations to their active work items. /// </summary> /// <param name="userDocuments">The documents whose processing will become active work items.</param> /// <returns>A <see cref="Task"/> representing the ongoing operation.</returns> private IEnumerable <WorkItem> CreateWorkItemsForProcessingOperations(IEnumerable <UserDocument> userDocuments) => from document in userDocuments let isCached = ProcessedDocuments.Any(d => d.Name.EqualsIgnoreCase(document.Name)) let state = isCached ? TaskState.Complete : TaskState.Pending let statusMessage = isCached ? "Analysis Complete." : "Pending" let percentComplete = isCached ? 100 : 0 select new WorkItem { Id = document.Id, Name = document.Name, PercentComplete = percentComplete, State = state, StatusMessage = statusMessage };