Skip to content

Commit e59440e

Browse files
committed
refactor: 精简代码提高 Items 性能
1 parent e5203a2 commit e59440e

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -822,35 +822,37 @@ private void OnTouchEnd()
822822
TouchStart = false;
823823
}
824824

825-
private List<TreeViewItem<TItem>> Rows => GetTreeRows(Items);
826-
827825
private List<TreeViewItem<TItem>>? _rows = null;
828826

829-
#if NET9_0_OR_GREATER
830-
private readonly Lock _object = new();
831-
#else
832-
private readonly object _object = new();
833-
#endif
827+
private List<TreeViewItem<TItem>> Rows
828+
{
829+
get
830+
{
831+
_rows ??= GetTreeRows(Items);
832+
return _rows;
833+
}
834+
}
834835

835-
private List<TreeViewItem<TItem>> GetTreeRows(List<TreeViewItem<TItem>> items)
836+
/// <summary>
837+
/// 将带层次结构的数据转换为扁平化数据
838+
/// </summary>
839+
/// <param name="items"></param>
840+
/// <returns></returns>
841+
private static List<TreeViewItem<TItem>> GetTreeRows(List<TreeViewItem<TItem>> items)
836842
{
837-
lock (_object)
843+
var rows = new List<TreeViewItem<TItem>>();
844+
if (items != null)
838845
{
839-
var rows = new List<TreeViewItem<TItem>>();
840-
if (items != null)
846+
foreach (var item in items)
841847
{
842-
foreach (var item in items)
848+
rows.Add(item);
849+
if (item.IsExpand)
843850
{
844-
rows.Add(item);
845-
if (item.IsExpand)
846-
{
847-
rows.AddRange(GetTreeRows(item.Items));
848-
}
851+
rows.AddRange(GetTreeRows(item.Items));
849852
}
850853
}
851-
_rows ??= rows;
852-
return rows;
853854
}
855+
return rows;
854856
}
855857

856858
private static string? GetTreeRowStyle(TreeViewItem<TItem> item)

0 commit comments

Comments
 (0)