Skip to content

Commit fb86fa4

Browse files
committed
Cleanups
1 parent 2bbfc3d commit fb86fa4

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

src/Components/Components/src/Infrastructure/ComponentStatePersistenceManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public Task PersistStateAsync(IPersistentComponentStateStore store, Renderer ren
6464

6565
async Task PauseAndPersistState()
6666
{
67+
State.PersistingState = true;
6768
await PauseAsync();
69+
State.PersistingState = false;
6870

6971
var data = new Dictionary<string, ReadOnlySequence<byte>>(StringComparer.Ordinal);
7072
foreach (var (key, value) in _currentState)

src/Components/Components/src/PersistentComponentState.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Buffers;
66
using System.Collections.Generic;
77
using System.Diagnostics.CodeAnalysis;
8-
using System.IO.Pipelines;
98
using System.Text.Json;
109
using System.Threading.Tasks;
1110
using Microsoft.AspNetCore.Components.Infrastructure;
@@ -20,6 +19,7 @@ public class PersistentComponentState
2019
{
2120
private IDictionary<string, ReadOnlySequence<byte>>? _existingState;
2221
private IDictionary<string, PooledByteBufferWriter> _currentState;
22+
2323
private readonly List<Func<Task>> _registeredCallbacks;
2424

2525
internal PersistentComponentState(
@@ -30,6 +30,8 @@ internal PersistentComponentState(
3030
_registeredCallbacks = pauseCallbacks;
3131
}
3232

33+
internal bool PersistingState { get; set; }
34+
3335
internal void InitializeExistingState(IDictionary<string, ReadOnlySequence<byte>> existingState)
3436
{
3537
if (_existingState != null)
@@ -110,6 +112,11 @@ public void Persist(string key, Action<IBufferWriter<byte>> valueWriter)
110112
throw new ArgumentNullException(nameof(valueWriter));
111113
}
112114

115+
if (!PersistingState)
116+
{
117+
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
118+
}
119+
113120
if (_currentState.ContainsKey(key))
114121
{
115122
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");
@@ -138,6 +145,11 @@ public void Persist(string key, Action<IBufferWriter<byte>> valueWriter)
138145
throw new ArgumentNullException(nameof(key));
139146
}
140147

148+
if (!PersistingState)
149+
{
150+
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
151+
}
152+
141153
if (_currentState.ContainsKey(key))
142154
{
143155
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");

src/Shared/Components/PooledByteBufferWritter.cs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,15 @@ public PooledByteBufferWriter(byte[] existingBuffer)
2727
_owned = true;
2828
}
2929

30-
public ReadOnlyMemory<byte> WrittenMemory
31-
{
32-
get
33-
{
34-
return _currentBuffer.AsMemory(0, _index);
35-
}
36-
}
30+
public ReadOnlyMemory<byte> WrittenMemory => _currentBuffer.AsMemory(0, _index);
3731

38-
public int WrittenCount
39-
{
40-
get
41-
{
42-
return _index;
43-
}
44-
}
32+
public int WrittenCount => _index;
4533

46-
public int Capacity
47-
{
48-
get
49-
{
50-
return _currentBuffer.Length;
51-
}
52-
}
34+
public int Capacity => _currentBuffer.Length;
5335

54-
public int FreeCapacity
55-
{
56-
get
57-
{
58-
return _currentBuffer.Length - _index;
59-
}
60-
}
36+
public int FreeCapacity => _currentBuffer.Length - _index;
6137

62-
public void Clear()
63-
{
64-
ClearHelper();
65-
}
38+
public void Clear() => ClearHelper();
6639

6740
private void ClearHelper()
6841
{

src/Shared/Components/PrerenderComponentApplicationStore.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,25 +66,33 @@ protected virtual PooledByteBufferWriter SerializeState(IReadOnlyDictionary<stri
6666
// the data with a memory pool here. We will change our serialization strategy in the future here
6767
// so that we can avoid this step.
6868
var buffer = new PooledByteBufferWriter();
69-
var jsonWriter = new Utf8JsonWriter(buffer);
70-
jsonWriter.WriteStartObject();
71-
foreach (var (key, value) in state)
69+
try
7270
{
73-
if (value.IsSingleSegment)
74-
{
75-
jsonWriter.WriteBase64String(key, value.First.Span);
76-
}
77-
else
71+
var jsonWriter = new Utf8JsonWriter(buffer);
72+
jsonWriter.WriteStartObject();
73+
foreach (var (key, value) in state)
7874
{
79-
WriteMultipleSegments(jsonWriter, key, value);
75+
if (value.IsSingleSegment)
76+
{
77+
jsonWriter.WriteBase64String(key, value.First.Span);
78+
}
79+
else
80+
{
81+
WriteMultipleSegments(jsonWriter, key, value);
82+
}
83+
jsonWriter.Flush();
8084
}
81-
jsonWriter.Flush();
82-
}
8385

84-
jsonWriter.WriteEndObject();
85-
jsonWriter.Flush();
86-
return buffer;
86+
jsonWriter.WriteEndObject();
87+
jsonWriter.Flush();
88+
return buffer;
8789

90+
}
91+
catch
92+
{
93+
buffer.Dispose();
94+
throw;
95+
}
8896
static void WriteMultipleSegments(Utf8JsonWriter jsonWriter, string key, ReadOnlySequence<byte> value)
8997
{
9098
byte[] unescapedArray = null;

0 commit comments

Comments
 (0)