Skip to content

Commit 7b556dd

Browse files
committed
tmp
1 parent b080d20 commit 7b556dd

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

src/Components/Components/src/Lifetime/ComponentStatePersistenceManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Buffers;
66
using System.Collections.Generic;
77
using System.IO.Pipelines;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
810
using System.Threading.Tasks;
911
using Microsoft.AspNetCore.Components.Lifetime;
1012
using Microsoft.AspNetCore.Components.RenderTree;

src/Components/Components/src/Lifetime/PersistentComponentState.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public void Persist(string key, Action<IBufferWriter<byte>> valueWriter)
117117
var pipe = new Pipe();
118118
_currentState.Add(key, pipe);
119119
valueWriter(pipe.Writer);
120+
pipe.Writer.Complete();
120121
}
121122

122123
/// <summary>
@@ -145,6 +146,7 @@ public void Persist(string key, Action<IBufferWriter<byte>> valueWriter)
145146
var pipe = new Pipe();
146147
_currentState.Add(key, pipe);
147148
JsonSerializer.Serialize(new Utf8JsonWriter(pipe.Writer), instance, JsonSerializerOptionsProvider.Options);
149+
pipe.Writer.Complete();
148150
}
149151

150152
/// <summary>

src/Components/Components/src/Microsoft.AspNetCore.Components.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
@@ -18,6 +18,7 @@
1818
<ItemGroup>
1919
<Reference Include="Microsoft.Extensions.Logging.Abstractions" />
2020
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
21+
<Reference Include="System.IO.Pipelines" />
2122
</ItemGroup>
2223

2324
<!--

src/Shared/Components/PrerenderComponentApplicationStore.cs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,21 @@ public PrerenderComponentApplicationStore(string existingState)
2222
if (existingState is null)
2323
{
2424
throw new ArgumentNullException(nameof(existingState));
25-
}
25+
}
2626

27-
ExistingState = JsonSerializer.Deserialize<Dictionary<string, ReadOnlySequence<byte>>>(Convert.FromBase64String(existingState)) ??
28-
throw new ArgumentException(nameof(existingState));
27+
var state = JsonSerializer.Deserialize<Dictionary<string, byte[]>>(Convert.FromBase64String(existingState));
28+
if (state == null)
29+
{
30+
throw new ArgumentException("Could not deserialize state correctly", nameof(existingState));
31+
}
32+
33+
var stateDictionary = new Dictionary<string, ReadOnlySequence<byte>>();
34+
foreach (var (key, value) in state)
35+
{
36+
stateDictionary.Add(key, new ReadOnlySequence<byte>(value));
37+
}
38+
39+
ExistingState = stateDictionary;
2940
}
3041

3142
#nullable enable
@@ -41,7 +52,37 @@ public Task<IDictionary<string, ReadOnlySequence<byte>>> GetPersistedStateAsync(
4152

4253
protected virtual byte[] SerializeState(IReadOnlyDictionary<string, ReadOnlySequence<byte>> state)
4354
{
44-
return JsonSerializer.SerializeToUtf8Bytes(state);
55+
// System.Text.Json doesn't support serializing ReadonlySequence<byte> so we need to buffer
56+
// the data with a memory pool here. We will change our serialization strategy in the future here
57+
// so that we can avoid this step.
58+
var pool = MemoryPool<byte>.Shared;
59+
var memory = new List<IMemoryOwner<byte>>();
60+
var serialization = new Dictionary<string, Memory<byte>>();
61+
try
62+
{
63+
foreach (var (key, value) in state)
64+
{
65+
IMemoryOwner<byte> buffer = null;
66+
if (value.Length < pool.MaxBufferSize)
67+
{
68+
buffer = pool.Rent((int)value.Length);
69+
memory.Add(buffer);
70+
value.CopyTo(buffer.Memory.Span.Slice(0, (int)value.Length));
71+
}
72+
73+
serialization.Add(key, buffer != null ? buffer.Memory.Slice(0, (int)value.Length) : value.ToArray());
74+
}
75+
76+
return JsonSerializer.SerializeToUtf8Bytes(serialization, JsonSerializerOptionsProvider.Options);
77+
}
78+
finally
79+
{
80+
serialization.Clear();
81+
foreach (var item in memory)
82+
{
83+
item.Dispose();
84+
}
85+
}
4586
}
4687

4788
public Task PersistStateAsync(IReadOnlyDictionary<string, ReadOnlySequence<byte>> state)

0 commit comments

Comments
 (0)