perf(adminui): RawTree renders a flat Virtualize list, pages 500 per level, caches menus (R04 P5)
The recursive RenderNode rendered every row of an expanded device, and built a fresh context-menu list + closures per node per render. RawTreeRows flattens the expanded tree (explicit stack) into rows rendered by one Virtualize; each level shows 500 children, then a 'Show more' row. RawNodeMenuCache keys menus weakly on the node instance and rebuilds on a picker-mode or Enabled change. Refs #745 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -98,4 +98,8 @@ public sealed class RawNode
|
||||
|
||||
/// <summary>Last load error message, if any.</summary>
|
||||
public string? Error { get; set; }
|
||||
|
||||
/// <summary>How many of <see cref="Children"/> the tree shows before a "Show more" row
|
||||
/// (<see cref="RawTreeRows.DefaultPageSize"/> per page).</summary>
|
||||
public int VisibleChildLimit { get; set; } = RawTreeRows.DefaultPageSize;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
|
||||
/// <summary>
|
||||
/// Caches each <see cref="RawNode"/>'s context-menu item list (R04 P5). <c>RawTree</c> used to build a
|
||||
/// fresh list (and fresh closures) per node on every render, and hand it to that row's
|
||||
/// <c>ContextMenu</c> as a new reference each time. The cache is keyed weakly on the node instance, so
|
||||
/// a reload (which replaces the child nodes) drops the stale entries with the nodes, and it rebuilds
|
||||
/// when anything the menu depends on (picker mode, the node's enabled state) differs.
|
||||
/// </summary>
|
||||
public sealed class RawNodeMenuCache
|
||||
{
|
||||
private readonly ConditionalWeakTable<RawNode, Entry> _entries = new();
|
||||
|
||||
/// <summary>Returns the cached menu for <paramref name="node"/>, building it on first use or after a dependency changed.</summary>
|
||||
/// <param name="node">The node.</param>
|
||||
/// <param name="pickerMode">Whether the tree is in picker mode (picker menus differ entirely).</param>
|
||||
/// <param name="build">Builds the menu for the node.</param>
|
||||
/// <returns>The menu items.</returns>
|
||||
public IReadOnlyList<ContextMenuItem> GetOrBuild(
|
||||
RawNode node, bool pickerMode, Func<RawNode, IReadOnlyList<ContextMenuItem>> build)
|
||||
{
|
||||
if (_entries.TryGetValue(node, out var entry)
|
||||
&& entry.PickerMode == pickerMode
|
||||
&& entry.Enabled == node.Enabled)
|
||||
{
|
||||
return entry.Items;
|
||||
}
|
||||
|
||||
var items = build(node);
|
||||
_entries.AddOrUpdate(node, new Entry(pickerMode, node.Enabled, items));
|
||||
return items;
|
||||
}
|
||||
|
||||
private sealed record Entry(bool PickerMode, bool Enabled, IReadOnlyList<ContextMenuItem> Items);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns;
|
||||
|
||||
/// <summary>What a flattened <c>RawTree</c> row renders.</summary>
|
||||
public enum RawTreeRowKind
|
||||
{
|
||||
/// <summary>A tree node row (expander + label + menu).</summary>
|
||||
Node,
|
||||
|
||||
/// <summary>The "Loading…" placeholder under an expanded container whose children are in flight.</summary>
|
||||
Loading,
|
||||
|
||||
/// <summary>The error line under an expanded container whose load failed.</summary>
|
||||
Error,
|
||||
|
||||
/// <summary>The "Show more" row under an expanded container with more children than its visible limit.</summary>
|
||||
ShowMore,
|
||||
}
|
||||
|
||||
/// <summary>One row of the flattened, visible Raw tree.</summary>
|
||||
/// <param name="Kind">What the row renders.</param>
|
||||
/// <param name="Node">The node the row belongs to (for non-<see cref="RawTreeRowKind.Node"/> rows, the container).</param>
|
||||
/// <param name="Depth">Indent depth; a placeholder row sits one level below its container.</param>
|
||||
/// <param name="Remaining">For <see cref="RawTreeRowKind.ShowMore"/>, how many children are still hidden; otherwise 0.</param>
|
||||
public readonly record struct RawTreeRow(RawTreeRowKind Kind, RawNode Node, int Depth, int Remaining = 0)
|
||||
{
|
||||
/// <summary>A key unique within the flattened list, stable across renders (for <c>@key</c>).</summary>
|
||||
public string Key => Kind switch
|
||||
{
|
||||
RawTreeRowKind.Node => Node.Key,
|
||||
RawTreeRowKind.Loading => "loading:" + Node.Key,
|
||||
RawTreeRowKind.Error => "error:" + Node.Key,
|
||||
_ => "more:" + Node.Key,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Flattens the expanded part of the Raw tree into the rows the <c>RawTree</c> component renders
|
||||
/// through one <c>Virtualize</c> (R04 P5). A recursive render of an expanded 5k-tag device rendered
|
||||
/// every row (and a <c>ContextMenu</c> per row) on every state change; a flat list lets the browser
|
||||
/// render only the rows in view. Each level also shows at most <see cref="RawNode.VisibleChildLimit"/>
|
||||
/// children (default <see cref="DefaultPageSize"/>), followed by a "Show more" row.
|
||||
/// </summary>
|
||||
public static class RawTreeRows
|
||||
{
|
||||
/// <summary>The number of children an expanded level shows before a "Show more" row.</summary>
|
||||
public const int DefaultPageSize = 500;
|
||||
|
||||
/// <summary>Flattens the visible tree, depth-first, in display order.</summary>
|
||||
/// <param name="roots">The top-level nodes.</param>
|
||||
/// <returns>The visible rows.</returns>
|
||||
public static List<RawTreeRow> Flatten(IReadOnlyList<RawNode> roots)
|
||||
{
|
||||
var rows = new List<RawTreeRow>();
|
||||
// An explicit stack instead of recursion: tag-group nesting depth is author-controlled.
|
||||
var stack = new Stack<RawTreeRow>();
|
||||
for (var i = roots.Count - 1; i >= 0; i--) stack.Push(new RawTreeRow(RawTreeRowKind.Node, roots[i], 0));
|
||||
|
||||
while (stack.Count > 0)
|
||||
{
|
||||
var row = stack.Pop();
|
||||
rows.Add(row);
|
||||
if (row.Kind != RawTreeRowKind.Node) continue;
|
||||
|
||||
var n = row.Node;
|
||||
if (!n.Expanded) continue;
|
||||
|
||||
var childDepth = row.Depth + 1;
|
||||
if (n.Loading)
|
||||
{
|
||||
rows.Add(new RawTreeRow(RawTreeRowKind.Loading, n, childDepth));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (n.Error is not null)
|
||||
{
|
||||
rows.Add(new RawTreeRow(RawTreeRowKind.Error, n, childDepth));
|
||||
continue;
|
||||
}
|
||||
|
||||
var shown = Math.Min(Math.Max(n.VisibleChildLimit, 0), n.Children.Count);
|
||||
var hidden = n.Children.Count - shown;
|
||||
|
||||
// Pushed in reverse so they pop in display order; the ShowMore row is pushed first so it pops
|
||||
// after the last shown child's whole (possibly expanded) subtree.
|
||||
if (hidden > 0) stack.Push(new RawTreeRow(RawTreeRowKind.ShowMore, n, childDepth, hidden));
|
||||
for (var i = shown - 1; i >= 0; i--) stack.Push(new RawTreeRow(RawTreeRowKind.Node, n.Children[i], childDepth));
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
/// <summary>Raises a container's visible-children limit by one page.</summary>
|
||||
/// <param name="node">The container whose "Show more" row was clicked.</param>
|
||||
public static void ShowMore(RawNode node) =>
|
||||
node.VisibleChildLimit = Math.Max(node.VisibleChildLimit, 0) + DefaultPageSize;
|
||||
}
|
||||
Reference in New Issue
Block a user