using System.Runtime.CompilerServices; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Uns; /// /// Caches each 's context-menu item list (R04 P5). RawTree used to build a /// fresh list (and fresh closures) per node on every render, and hand it to that row's /// ContextMenu 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. /// public sealed class RawNodeMenuCache { private readonly ConditionalWeakTable _entries = new(); /// Returns the cached menu for , building it on first use or after a dependency changed. /// The node. /// Whether the tree is in picker mode (picker menus differ entirely). /// Builds the menu for the node. /// The menu items. public IReadOnlyList GetOrBuild( RawNode node, bool pickerMode, Func> 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 Items); }