AppContext.partials.PoolProfileViewModels.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using NTMiner.Core;
  2. using NTMiner.Vms;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace NTMiner {
  6. public partial class AppContext {
  7. public class PoolProfileViewModels : ViewModelBase {
  8. public static readonly PoolProfileViewModels Instance = new PoolProfileViewModels();
  9. private readonly Dictionary<Guid, PoolProfileViewModel> _dicById = new Dictionary<Guid, PoolProfileViewModel>();
  10. private PoolProfileViewModels() {
  11. #if DEBUG
  12. Write.Stopwatch.Start();
  13. #endif
  14. BuildEventPath<PoolProfilePropertyChangedEvent>("矿池设置变更后刷新VM内存", LogEnum.DevConsole,
  15. action: message => {
  16. if (_dicById.TryGetValue(message.PoolId, out PoolProfileViewModel vm)) {
  17. vm.OnPropertyChanged(message.PropertyName);
  18. }
  19. });
  20. VirtualRoot.BuildEventPath<LocalContextReInitedEvent>("LocalContext刷新后刷新VM内存", LogEnum.DevConsole,
  21. action: message => {
  22. _dicById.Clear();
  23. });
  24. #if DEBUG
  25. var elapsedMilliseconds = Write.Stopwatch.Stop();
  26. if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds) {
  27. Write.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.ctor");
  28. }
  29. #endif
  30. }
  31. private readonly object _locker = new object();
  32. public PoolProfileViewModel GetOrCreatePoolProfile(Guid poolId) {
  33. PoolProfileViewModel poolProfile;
  34. if (!_dicById.TryGetValue(poolId, out poolProfile)) {
  35. lock (_locker) {
  36. if (!_dicById.TryGetValue(poolId, out poolProfile)) {
  37. poolProfile = new PoolProfileViewModel(NTMinerRoot.Instance.MinerProfile.GetPoolProfile(poolId));
  38. _dicById.Add(poolId, poolProfile);
  39. }
  40. }
  41. }
  42. return poolProfile;
  43. }
  44. }
  45. }
  46. }