Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/Components/Components/src/PersistentComponentState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,10 @@ public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> call
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
}

if (_currentState.ContainsKey(key))
if (!_currentState.TryAdd(key, JsonSerializer.SerializeToUtf8Bytes(instance, JsonSerializerOptionsProvider.Options)))
{
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");
}

_currentState.Add(key, JsonSerializer.SerializeToUtf8Bytes(instance, JsonSerializerOptionsProvider.Options));
}

[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
Expand All @@ -102,12 +100,10 @@ internal void PersistAsJson(string key, object instance, [DynamicallyAccessedMem
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
}

if (_currentState.ContainsKey(key))
if (!_currentState.TryAdd(key, JsonSerializer.SerializeToUtf8Bytes(instance, type, JsonSerializerOptionsProvider.Options)))
{
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");
}

_currentState.Add(key, JsonSerializer.SerializeToUtf8Bytes(instance, type, JsonSerializerOptionsProvider.Options));
}

/// <summary>
Expand All @@ -124,12 +120,10 @@ internal void PersistAsBytes(string key, byte[] data)
throw new InvalidOperationException("Persisting state is only allowed during an OnPersisting callback.");
}

if (_currentState.ContainsKey(key))
if (!_currentState.TryAdd(key, data))
{
throw new ArgumentException($"There is already a persisted object under the same key '{key}'");
}

_currentState.Add(key, data);
}

/// <summary>
Expand Down
Loading