Skip to content

Commit 21535cd

Browse files
rynowakRyan Nowak
authored and
Ryan Nowak
committed
WIP
1 parent 32e9188 commit 21535cd

24 files changed

+102
-103
lines changed

src/Components/Components/src/Rendering/NullDispatcher.cs renamed to src/Components/Blazor/Blazor/src/Rendering/NullDispatcher.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
using System;
55
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components;
67

7-
namespace Microsoft.AspNetCore.Components.Rendering
8+
namespace Microsoft.AspNetCore.Blazor.Rendering
89
{
910
internal class NullDispatcher : Dispatcher
1011
{
@@ -16,13 +17,6 @@ private NullDispatcher()
1617

1718
public override bool CheckAccess() => true;
1819

19-
// This will never fire, since we don't actually run background work.
20-
public override event UnhandledExceptionEventHandler UnhandledException
21-
{
22-
add { }
23-
remove { }
24-
}
25-
2620
public override Task InvokeAsync(Action workItem)
2721
{
2822
if (workItem is null)

src/Components/Blazor/Blazor/src/Rendering/WebAssemblyRenderer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public WebAssemblyRenderer(IServiceProvider serviceProvider, ILoggerFactory logg
3737
_webAssemblyRendererId = RendererRegistry.Current.Add(this);
3838
}
3939

40+
public override Dispatcher Dispatcher => NullDispatcher.Instance;
41+
4042
/// <summary>
4143
/// Attaches a new root component to the renderer,
4244
/// causing it to be displayed in the specified DOM element.

src/Components/Blazor/Build/test/RazorIntegrationTestBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,12 @@ protected class CompileToAssemblyResult
442442

443443
protected class TestRenderer : Renderer
444444
{
445-
public TestRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance, CreateDefaultDispatcher())
445+
public TestRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance)
446446
{
447447
}
448448

449+
public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault();
450+
449451
public RenderTreeFrame[] LatestBatchReferenceFrames { get; private set; }
450452

451453
public void AttachComponent(IComponent component)

src/Components/Components/perf/RenderTreeDiffBuilderBenchmark.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ public void ComputeDiff_SingleFormField()
8888
private class FakeRenderer : Renderer
8989
{
9090
public FakeRenderer()
91-
: base(new TestServiceProvider(), NullLoggerFactory.Instance, new RendererSynchronizationContextDispatcher())
91+
: base(new TestServiceProvider(), NullLoggerFactory.Instance)
9292
{
9393
}
9494

95+
public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault();
96+
9597
protected override void HandleException(Exception exception)
9698
{
9799
throw new NotImplementedException();

src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ public DataTransfer() { }
8585
public abstract partial class Dispatcher
8686
{
8787
protected Dispatcher() { }
88-
public abstract event System.UnhandledExceptionEventHandler UnhandledException;
8988
public abstract bool CheckAccess();
89+
public static Microsoft.AspNetCore.Components.Dispatcher CreateDefault() { throw null; }
9090
public abstract System.Threading.Tasks.Task InvokeAsync(System.Action workItem);
9191
public abstract System.Threading.Tasks.Task InvokeAsync(System.Func<System.Threading.Tasks.Task> workItem);
9292
public abstract System.Threading.Tasks.Task<TResult> InvokeAsync<TResult>(System.Func<System.Threading.Tasks.Task<TResult>> workItem);
9393
public abstract System.Threading.Tasks.Task<TResult> InvokeAsync<TResult>(System.Func<TResult> workItem);
94+
protected void OnUnhandledException(System.UnhandledExceptionEventArgs e) { }
9495
}
9596
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
9697
public readonly partial struct ElementRef
@@ -651,7 +652,8 @@ public EventFieldInfo() { }
651652
}
652653
public partial class HtmlRenderer : Microsoft.AspNetCore.Components.Rendering.Renderer
653654
{
654-
public HtmlRenderer(System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.Components.Dispatcher dispatcher, System.Func<string, string> htmlEncoder) : base (default(System.IServiceProvider), default(Microsoft.Extensions.Logging.ILoggerFactory)) { }
655+
public HtmlRenderer(System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, System.Func<string, string> htmlEncoder) : base (default(System.IServiceProvider), default(Microsoft.Extensions.Logging.ILoggerFactory)) { }
656+
public override Microsoft.AspNetCore.Components.Dispatcher Dispatcher { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
655657
protected override void HandleException(System.Exception exception) { }
656658
[System.Diagnostics.DebuggerStepThroughAttribute]
657659
public System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Rendering.ComponentRenderedText> RenderComponentAsync(System.Type componentType, Microsoft.AspNetCore.Components.ParameterCollection initialParameters) { throw null; }
@@ -670,12 +672,10 @@ public readonly partial struct RenderBatch
670672
public abstract partial class Renderer : System.IDisposable
671673
{
672674
public Renderer(System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { }
673-
public Renderer(System.IServiceProvider serviceProvider, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, Microsoft.AspNetCore.Components.Dispatcher dispatcher) { }
674-
public Microsoft.AspNetCore.Components.Dispatcher Dispatcher { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
675+
public abstract Microsoft.AspNetCore.Components.Dispatcher Dispatcher { get; }
675676
public event System.UnhandledExceptionEventHandler UnhandledSynchronizationException { add { } remove { } }
676677
protected internal virtual void AddToRenderQueue(int componentId, Microsoft.AspNetCore.Components.RenderFragment renderFragment) { }
677678
protected internal int AssignRootComponentId(Microsoft.AspNetCore.Components.IComponent component) { throw null; }
678-
public static Microsoft.AspNetCore.Components.Dispatcher CreateDefaultDispatcher() { throw null; }
679679
public virtual System.Threading.Tasks.Task DispatchEventAsync(int eventHandlerId, Microsoft.AspNetCore.Components.Rendering.EventFieldInfo fieldInfo, Microsoft.AspNetCore.Components.UIEventArgs eventArgs) { throw null; }
680680
public void Dispose() { }
681681
protected virtual void Dispose(bool disposing) { }

src/Components/Components/src/Dispatcher.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ namespace Microsoft.AspNetCore.Components
1212
/// </summary>
1313
public abstract class Dispatcher
1414
{
15+
/// <summary>
16+
/// Creates a default instance of <see cref="Dispatcher"/>.
17+
/// </summary>
18+
/// <returns>A <see cref="Dispatcher"/> instance.</returns>
19+
public static Dispatcher CreateDefault() => new RendererSynchronizationContextDispatcher();
20+
1521
/// <summary>
1622
/// Provides notifications of unhandled exceptions that occur within the dispatcher.
1723
/// </summary>
18-
public abstract event UnhandledExceptionEventHandler UnhandledException;
24+
internal event UnhandledExceptionEventHandler UnhandledException;
1925

2026
/// <summary>
2127
/// Returns a value that determines whether using the dispatcher to invoke a work item is required
@@ -51,5 +57,19 @@ public abstract class Dispatcher
5157
/// <param name="workItem">The asynchronous function to execute.</param>
5258
/// <returns>A <see cref="Task{TResult}"/> that will be completed when the function has finished executing.</returns>
5359
public abstract Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> workItem);
60+
61+
/// <summary>
62+
/// Called to notify listeners of an unhandled exception.
63+
/// </summary>
64+
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/>.</param>
65+
protected void OnUnhandledException(UnhandledExceptionEventArgs e)
66+
{
67+
if (e is null)
68+
{
69+
throw new ArgumentNullException(nameof(e));
70+
}
71+
72+
UnhandledException?.Invoke(this, e);
73+
}
5474
}
5575
}

src/Components/Components/src/Rendering/HtmlRenderer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.Runtime.CompilerServices;
78
using System.Runtime.ExceptionServices;
89
using System.Threading.Tasks;
910
using Microsoft.AspNetCore.Components.RenderTree;
@@ -28,14 +29,15 @@ public class HtmlRenderer : Renderer
2829
/// </summary>
2930
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> to use to instantiate components.</param>
3031
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
31-
/// <param name="dispatcher">The <see cref="Dispatcher"/> to be for invoking user actions into the <see cref="Renderer"/> context.</param>
3232
/// <param name="htmlEncoder">A <see cref="Func{T, TResult}"/> that will HTML encode the given string.</param>
33-
public HtmlRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, Dispatcher dispatcher, Func<string, string> htmlEncoder)
34-
: base(serviceProvider, loggerFactory, dispatcher)
33+
public HtmlRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, Func<string, string> htmlEncoder)
34+
: base(serviceProvider, loggerFactory)
3535
{
3636
_htmlEncoder = htmlEncoder;
3737
}
3838

39+
public override Dispatcher Dispatcher { get; } = new RendererSynchronizationContextDispatcher();
40+
3941
/// <inheritdoc />
4042
protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
4143
{

src/Components/Components/src/Rendering/Renderer.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,12 @@ public Renderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
6464
_componentFactory = new ComponentFactory(serviceProvider);
6565
_logger = loggerFactory.CreateLogger<Renderer>();
6666
_componentFactory = new ComponentFactory(serviceProvider);
67-
Dispatcher = NullDispatcher.Instance;
68-
}
69-
70-
/// <summary>
71-
/// Constructs an instance of <see cref="Renderer"/>.
72-
/// </summary>
73-
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> to be used when initializing components.</param>
74-
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
75-
/// <param name="dispatcher">The <see cref="Dispatcher"/> to be for invoking user actions into the <see cref="Renderer"/> context.</param>
76-
public Renderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, Dispatcher dispatcher)
77-
: this(serviceProvider, loggerFactory)
78-
{
79-
Dispatcher = dispatcher ?? NullDispatcher.Instance;
8067
}
8168

8269
/// <summary>
8370
/// Gets the <see cref="Microsoft.AspNetCore.Components.Dispatcher" /> associated with this <see cref="Renderer" />.
8471
/// </summary>
85-
public Dispatcher Dispatcher { get; }
86-
87-
/// <summary>
88-
/// Creates an <see cref="Dispatcher"/> that can be used with one or more <see cref="Renderer"/>.
89-
/// </summary>
90-
/// <returns>The <see cref="Dispatcher"/>.</returns>
91-
public static Dispatcher CreateDefaultDispatcher() => new RendererSynchronizationContextDispatcher();
72+
public abstract Dispatcher Dispatcher { get; }
9273

9374
/// <summary>
9475
/// Constructs a new component of the specified type.

src/Components/Components/src/Rendering/RendererSynchronizationContextDispatcher.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ internal class RendererSynchronizationContextDispatcher : Dispatcher
1414
public RendererSynchronizationContextDispatcher()
1515
{
1616
_context = new RendererSynchronizationContext();
17-
}
18-
19-
public override event UnhandledExceptionEventHandler UnhandledException
20-
{
21-
add
17+
_context.UnhandledException += (sender, e) =>
2218
{
23-
_context.UnhandledException += value;
24-
}
25-
remove
26-
{
27-
_context.UnhandledException -= value;
28-
}
19+
OnUnhandledException(e);
20+
};
2921
}
3022

3123
public override bool CheckAccess() => SynchronizationContext.Current == _context;

src/Components/Components/test/RenderTreeBuilderTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,13 @@ public Task SetParametersAsync(ParameterCollection parameters)
17951795

17961796
private class TestRenderer : Renderer
17971797
{
1798-
public TestRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance, new RendererSynchronizationContextDispatcher())
1798+
public TestRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance)
17991799
{
1800+
Dispatcher = new RendererSynchronizationContextDispatcher();
18001801
}
18011802

1803+
public override Dispatcher Dispatcher { get; }
1804+
18021805
protected override void HandleException(Exception exception)
18031806
=> throw new NotImplementedException();
18041807

src/Components/Components/test/RenderTreeDiffBuilderTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2208,10 +2208,12 @@ private static void AddWithKey(RenderTreeBuilder builder, object key, string att
22082208

22092209
private class FakeRenderer : Renderer
22102210
{
2211-
public FakeRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance, new RendererSynchronizationContextDispatcher())
2211+
public FakeRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance)
22122212
{
22132213
}
22142214

2215+
public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault();
2216+
22152217
protected override void HandleException(Exception exception)
22162218
=> throw new NotImplementedException();
22172219

src/Components/Components/test/RendererTest.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3373,10 +3373,12 @@ public void EventFieldInfoWorksWhenEventHandlerIdWasSuperseded()
33733373

33743374
private class NoOpRenderer : Renderer
33753375
{
3376-
public NoOpRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance, new RendererSynchronizationContextDispatcher())
3376+
public NoOpRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance)
33773377
{
33783378
}
33793379

3380+
public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault();
3381+
33803382
public new int AssignRootComponentId(IComponent component)
33813383
=> base.AssignRootComponentId(component);
33823384

0 commit comments

Comments
 (0)