|
4 | 4 | | Project Topics Library |
5 | 5 | \=============================================================================================================================*/ |
6 | 6 | using System; |
| 7 | +using System.Collections.Generic; |
7 | 8 | using System.Linq; |
8 | 9 | using System.Threading.Tasks; |
9 | 10 | using System.Web.Mvc; |
@@ -198,22 +199,59 @@ protected async Task<T> AddNestedTopicsAsync( |
198 | 199 | bool allowPageGroups = true, |
199 | 200 | int tiers = 1 |
200 | 201 | ) { |
| 202 | + |
| 203 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 204 | + | Validate preconditions |
| 205 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
201 | 206 | tiers--; |
202 | 207 | if (sourceTopic == null) { |
203 | 208 | return null; |
204 | 209 | } |
205 | | - var viewModel = await _topicMappingService.MapAsync<T>(sourceTopic, Relationships.None); |
| 210 | + |
| 211 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 212 | + | Establish variables |
| 213 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
| 214 | + var taskQueue = new List<Task<T>>(); |
| 215 | + var children = new List<T>(); |
| 216 | + var viewModel = (T)null; |
| 217 | + |
| 218 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 219 | + | Map object |
| 220 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
| 221 | + viewModel = await _topicMappingService.MapAsync<T>(sourceTopic, Relationships.None); |
| 222 | + |
| 223 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 224 | + | Request mapping of children |
| 225 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
206 | 226 | if (tiers >= 0 && (allowPageGroups || !sourceTopic.ContentType.Equals("PageGroup")) && viewModel.Children.Count == 0) { |
207 | 227 | foreach (var topic in sourceTopic.Children.Where(t => t.IsVisible())) { |
208 | | - viewModel.Children.Add( |
209 | | - await AddNestedTopicsAsync( |
210 | | - topic, |
211 | | - allowPageGroups, |
212 | | - tiers |
213 | | - ) |
214 | | - ); |
| 228 | + taskQueue.Add(AddNestedTopicsAsync(topic, allowPageGroups, tiers)); |
215 | 229 | } |
216 | 230 | } |
| 231 | + |
| 232 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 233 | + | Process children |
| 234 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
| 235 | + while (taskQueue.Count > 0 && viewModel.Children.Count == 0) { |
| 236 | + var dtoTask = await Task.WhenAny(taskQueue); |
| 237 | + taskQueue.Remove(dtoTask); |
| 238 | + children.Add(await dtoTask); |
| 239 | + } |
| 240 | + |
| 241 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 242 | + | Add children to view model |
| 243 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
| 244 | + if (viewModel.Children.Count == 0) { |
| 245 | + lock (viewModel) { |
| 246 | + if (viewModel.Children.Count == 0) { |
| 247 | + children.ForEach(c => viewModel.Children.Add(c)); |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + /*------------------------------------------------------------------------------------------------------------------------ |
| 253 | + | Return view model |
| 254 | + \-----------------------------------------------------------------------------------------------------------------------*/ |
217 | 255 | return viewModel; |
218 | 256 | } |
219 | 257 |
|
|
0 commit comments