| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | |
|
| | 6 | | public class UserProfileFetcher : IDisposable |
| | 7 | | { |
| 12 | 8 | | private readonly Dictionary<string, List<Promise<UserProfile>>> pendingPromises = new Dictionary<string, List<Promis |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Look for profile in userProfilesCatalog or request kernel for a profile if not available |
| | 12 | | /// </summary> |
| | 13 | | /// <param name="userId">id of profile to fetch</param> |
| | 14 | | /// <returns>a promise of the user profile</returns> |
| | 15 | | public Promise<UserProfile> FetchProfile(string userId) |
| | 16 | | { |
| 22 | 17 | | Promise<UserProfile> promise = new Promise<UserProfile>(); |
| 22 | 18 | | if (UserProfileController.userProfilesCatalog.TryGetValue(userId, out UserProfile profile)) |
| | 19 | | { |
| 0 | 20 | | promise.Resolve(profile); |
| 0 | 21 | | return promise; |
| | 22 | | } |
| | 23 | |
|
| 22 | 24 | | if (!pendingPromises.TryGetValue(userId, out List<Promise<UserProfile>> promisesForUserId)) |
| | 25 | | { |
| 16 | 26 | | promisesForUserId = new List<Promise<UserProfile>>(); |
| 16 | 27 | | pendingPromises.Add(userId, promisesForUserId); |
| 16 | 28 | | WebInterface.RequestUserProfile(userId); |
| | 29 | | } |
| | 30 | |
|
| 22 | 31 | | promisesForUserId.Add(promise); |
| 22 | 32 | | return promise; |
| | 33 | | } |
| | 34 | |
|
| 36 | 35 | | public UserProfileFetcher() { UserProfileController.userProfilesCatalog.OnAdded += OnProfileAddedToCatalog; } |
| | 36 | |
|
| 24 | 37 | | public void Dispose() { UserProfileController.userProfilesCatalog.OnAdded -= OnProfileAddedToCatalog; } |
| | 38 | |
|
| | 39 | | private void OnProfileAddedToCatalog(string userId, UserProfile profile) |
| | 40 | | { |
| 0 | 41 | | if (!pendingPromises.TryGetValue(userId, out List<Promise<UserProfile>> promisesForUserId)) |
| | 42 | | { |
| 0 | 43 | | return; |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | for (int i = 0; i < promisesForUserId.Count; i++) |
| | 47 | | { |
| 0 | 48 | | if (promisesForUserId[i] == null) |
| | 49 | | continue; |
| | 50 | |
|
| 0 | 51 | | promisesForUserId[i].Resolve(profile); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | pendingPromises.Remove(userId); |
| 0 | 55 | | } |
| | 56 | | } |