Skip to content

Commit a4933a3

Browse files
CR: Make KeyedItemInfo readonly
1 parent 35d93c4 commit a4933a3

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ private static void AppendDiffEntriesForRange(
127127
// Since diffContext.SiblingIndex only increases, we can be sure the values we
128128
// write at this point will remain correct, because there won't be any further
129129
// insertions/deletions at smaller sibling indices.
130-
oldKeyItemInfo.OldSiblingIndex = diffContext.SiblingIndex;
131-
keyedItemInfos[oldKey] = oldKeyItemInfo;
132-
newKeyItemInfo.NewSiblingIndex = diffContext.SiblingIndex;
133-
keyedItemInfos[newKey] = newKeyItemInfo;
130+
keyedItemInfos[oldKey] = oldKeyItemInfo.WithOldSiblingIndex(diffContext.SiblingIndex);
131+
keyedItemInfos[newKey] = newKeyItemInfo.WithNewSiblingIndex(diffContext.SiblingIndex);
134132
}
135133
else if (!hasMoreNew)
136134
{

src/Components/Components/src/Rendering/KeyedItemInfo.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
namespace Microsoft.AspNetCore.Components.Rendering
55
{
6-
// Used internally during diffing to track what we know about
7-
// keyed items and their positions
8-
internal struct KeyedItemInfo
6+
// Used internally during diffing to track what we know about keyed items and their positions
7+
internal readonly struct KeyedItemInfo
98
{
10-
public int OldIndex;
11-
public int NewIndex;
12-
public int OldSiblingIndex;
13-
public int NewSiblingIndex;
14-
public bool IsUnique;
9+
public readonly int OldIndex;
10+
public readonly int NewIndex;
11+
public readonly int OldSiblingIndex;
12+
public readonly int NewSiblingIndex;
13+
public readonly bool IsUnique;
1514

1615
public KeyedItemInfo(int oldIndex, int newIndex, bool isUnique)
1716
{
@@ -30,5 +29,18 @@ public KeyedItemInfo(int oldIndex, int newIndex, bool isUnique)
3029
// Guidance for developers is therefore to use distinct keys.
3130
IsUnique = isUnique;
3231
}
32+
33+
private KeyedItemInfo(in KeyedItemInfo copyFrom, int oldSiblingIndex, int newSiblingIndex)
34+
{
35+
this = copyFrom;
36+
OldSiblingIndex = oldSiblingIndex;
37+
NewSiblingIndex = newSiblingIndex;
38+
}
39+
40+
public KeyedItemInfo WithOldSiblingIndex(int oldSiblingIndex)
41+
=> new KeyedItemInfo(this, oldSiblingIndex, NewSiblingIndex);
42+
43+
public KeyedItemInfo WithNewSiblingIndex(int newSiblingIndex)
44+
=> new KeyedItemInfo(this, OldSiblingIndex, newSiblingIndex);
3345
}
3446
}

0 commit comments

Comments
 (0)