From 168a326348041bb80386b23f9fc4e719686ea59c Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Fri, 14 May 2021 06:19:54 -0700 Subject: [PATCH] Move lock. --- .../Components/Devices/DevicesFrame.razor.cs | 107 +++++++++--------- 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/Server/Components/Devices/DevicesFrame.razor.cs b/Server/Components/Devices/DevicesFrame.razor.cs index 59c7dd033..eb70220c6 100644 --- a/Server/Components/Devices/DevicesFrame.razor.cs +++ b/Server/Components/Devices/DevicesFrame.razor.cs @@ -90,11 +90,8 @@ public void Dispose() public void Refresh() { - lock (_devicesLock) - { - LoadDevices(); - InvokeAsync(StateHasChanged); - } + LoadDevices(); + InvokeAsync(StateHasChanged); } protected override async Task OnInitializedAsync() @@ -116,10 +113,7 @@ protected override async Task OnInitializedAsync() _sortableProperties.AddRange(sortableProperties); - lock (_devicesLock) - { - LoadDevices(); - } + LoadDevices(); } protected override bool ShouldRender() @@ -127,10 +121,7 @@ protected override bool ShouldRender() var shouldRender = base.ShouldRender(); if (shouldRender) { - lock (_devicesLock) - { - FilterDevices(); - } + FilterDevices(); } return shouldRender; } @@ -226,67 +217,73 @@ private string GetSortIcon() private void LoadDevices() { - _allDevices.Clear(); + lock (_devicesLock) + { + _allDevices.Clear(); - var devices = DataService.GetDevicesForUser(Username) - .OrderByDescending(x => x.IsOnline) - .ToList(); + var devices = DataService.GetDevicesForUser(Username) + .OrderByDescending(x => x.IsOnline) + .ToList(); - _allDevices.AddRange(devices); + _allDevices.AddRange(devices); - HighestVersion = _allDevices.Max(x => Version.TryParse(x.AgentVersion, out var result) ? result : default); + HighestVersion = _allDevices.Max(x => Version.TryParse(x.AgentVersion, out var result) ? result : default); + } FilterDevices(); } private void FilterDevices() { - _filteredDevices.Clear(); - _filteredDevices.AddRange(_allDevices); - - if (!string.IsNullOrWhiteSpace(_selectedSortProperty)) + lock (_devicesLock) { - var direction = _sortDirection == ListSortDirection.Ascending ? 1 : -1; - _filteredDevices.Sort((a, b) => + _filteredDevices.Clear(); + _filteredDevices.AddRange(_allDevices); + + if (!string.IsNullOrWhiteSpace(_selectedSortProperty)) { - if (a.IsOnline != b.IsOnline) + var direction = _sortDirection == ListSortDirection.Ascending ? 1 : -1; + _filteredDevices.Sort((a, b) => { - return b.IsOnline.CompareTo(a.IsOnline); - } + if (a.IsOnline != b.IsOnline) + { + return b.IsOnline.CompareTo(a.IsOnline); + } - var propInfo = _sortableProperties.Find(x => x.Name == _selectedSortProperty); + var propInfo = _sortableProperties.Find(x => x.Name == _selectedSortProperty); - var valueA = propInfo.GetValue(a); - var valueB = propInfo.GetValue(b); + var valueA = propInfo.GetValue(a); + var valueB = propInfo.GetValue(b); - return Comparer.Default.Compare(valueA, valueB) * direction; - }); - } + return Comparer.Default.Compare(valueA, valueB) * direction; + }); + } - if (_hideOfflineDevices) - { - _filteredDevices.RemoveAll(x => !x.IsOnline); - } + if (_hideOfflineDevices) + { + _filteredDevices.RemoveAll(x => !x.IsOnline); + } - if (_selectedGroupId == _deviceGroupNone) - { - _filteredDevices.RemoveAll(x => !string.IsNullOrWhiteSpace(x.DeviceGroupID)); - } - else if (_selectedGroupId != _deviceGroupAll) - { - _filteredDevices.RemoveAll(x => x.DeviceGroupID != _selectedGroupId); - } + if (_selectedGroupId == _deviceGroupNone) + { + _filteredDevices.RemoveAll(x => !string.IsNullOrWhiteSpace(x.DeviceGroupID)); + } + else if (_selectedGroupId != _deviceGroupAll) + { + _filteredDevices.RemoveAll(x => x.DeviceGroupID != _selectedGroupId); + } - if (!string.IsNullOrWhiteSpace(_filter)) - { - _filteredDevices.RemoveAll(x => - x.Alias?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && - x.CurrentUser?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && - x.DeviceName?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && - x.Notes?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && - x.Platform?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && - x.Tags?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true); + if (!string.IsNullOrWhiteSpace(_filter)) + { + _filteredDevices.RemoveAll(x => + x.Alias?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && + x.CurrentUser?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && + x.DeviceName?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && + x.Notes?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && + x.Platform?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true && + x.Tags?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true); + } } }