Improve handling for shared values in Blazor Server (#21299)

This commit is contained in:
Safia Abdalla 2020-06-01 15:45:07 -07:00 committed by GitHub
parent 96e8d89f2a
commit e66ad54db4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 9 deletions

View File

@ -0,0 +1,27 @@
using System;
using Xunit;
namespace Microsoft.AspNetCore.Blazor.Rendering
{
public class RenderRegistryTest
{
[Fact]
public void RendererRegistry_Find_ThrowsErrorOnNonWASM()
{
// Act
Exception ex = Assert.Throws<ArgumentException>(() => RendererRegistry.Find(123));
// Assert
Assert.Equal("There is no renderer with ID 123.", ex.Message);
}
[Fact]
public void RendererRegistry_Remove_DoesNothingOnNonWASM()
{
// Act
var result = RendererRegistry.TryRemove(123);
// Assert
Assert.False(result);
}
}
}

View File

@ -537,8 +537,8 @@ namespace Microsoft.AspNetCore.Components
return ConvertToNullableBoolCore(obj, culture, out value); return ConvertToNullableBoolCore(obj, culture, out value);
} }
internal static BindParser<bool> ConvertToBool = ConvertToBoolCore; internal readonly static BindParser<bool> ConvertToBool = ConvertToBoolCore;
internal static BindParser<bool?> ConvertToNullableBool = ConvertToNullableBoolCore; internal readonly static BindParser<bool?> ConvertToNullableBool = ConvertToNullableBoolCore;
private static bool ConvertToBoolCore(object obj, CultureInfo culture, out bool value) private static bool ConvertToBoolCore(object obj, CultureInfo culture, out bool value)
{ {

View File

@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Components.Forms
/// </summary> /// </summary>
public class InputNumber<TValue> : InputBase<TValue> public class InputNumber<TValue> : InputBase<TValue>
{ {
private static string _stepAttributeValue; // Null by default, so only allows whole numbers as per HTML spec private readonly static string _stepAttributeValue; // Null by default, so only allows whole numbers as per HTML spec
static InputNumber() static InputNumber()
{ {

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Microsoft.AspNetCore.Components.WebAssembly.Rendering namespace Microsoft.AspNetCore.Components.WebAssembly.Rendering
{ {
@ -14,27 +15,36 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Rendering
// them even though we might still receive incoming events from JS. // them even though we might still receive incoming events from JS.
private static int _nextId; private static int _nextId;
private static Dictionary<int, WebAssemblyRenderer> _renderers = new Dictionary<int, WebAssemblyRenderer>(); private static Dictionary<int, WebAssemblyRenderer> _renderers;
static RendererRegistry()
{
bool _isWebAssembly = RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"));
if (_isWebAssembly)
{
_renderers = new Dictionary<int, WebAssemblyRenderer>();
}
}
internal static WebAssemblyRenderer Find(int rendererId) internal static WebAssemblyRenderer Find(int rendererId)
{ {
return _renderers.ContainsKey(rendererId) return _renderers != null && _renderers.ContainsKey(rendererId)
? _renderers[rendererId] ? _renderers?[rendererId]
: throw new ArgumentException($"There is no renderer with ID {rendererId}."); : throw new ArgumentException($"There is no renderer with ID {rendererId}.");
} }
public static int Add(WebAssemblyRenderer renderer) public static int Add(WebAssemblyRenderer renderer)
{ {
var id = _nextId++; var id = _nextId++;
_renderers.Add(id, renderer); _renderers?.Add(id, renderer);
return id; return id;
} }
public static bool TryRemove(int rendererId) public static bool TryRemove(int rendererId)
{ {
if (_renderers.ContainsKey(rendererId)) if (_renderers != null && _renderers.ContainsKey(rendererId))
{ {
_renderers.Remove(rendererId); _renderers?.Remove(rendererId);
return true; return true;
} }
else else