@@ -19,6 +19,7 @@ public abstract class Renderer : IDisposable
19
19
private readonly ComponentFactory _componentFactory ;
20
20
private readonly Dictionary < int , ComponentState > _componentStateById = new Dictionary < int , ComponentState > ( ) ;
21
21
private readonly Dictionary < int , EventCallback > _eventBindings = new Dictionary < int , EventCallback > ( ) ;
22
+ private IDispatcher _dispatcher { get ; }
22
23
23
24
private int _nextComponentId = 0 ; // TODO: change to 'long' when Mono .NET->JS interop supports it
24
25
private bool _isBatchInProgress ;
@@ -32,15 +33,15 @@ public event UnhandledExceptionEventHandler UnhandledSynchronizationException
32
33
{
33
34
add
34
35
{
35
- if ( ! ( Dispatcher is RendererSynchronizationContext rendererSynchronizationContext ) )
36
+ if ( ! ( _dispatcher is RendererSynchronizationContext rendererSynchronizationContext ) )
36
37
{
37
38
return ;
38
39
}
39
40
rendererSynchronizationContext . UnhandledException += value ;
40
41
}
41
42
remove
42
43
{
43
- if ( ! ( Dispatcher is RendererSynchronizationContext rendererSynchronizationContext ) )
44
+ if ( ! ( _dispatcher is RendererSynchronizationContext rendererSynchronizationContext ) )
44
45
{
45
46
return ;
46
47
}
@@ -64,25 +65,11 @@ public Renderer(IServiceProvider serviceProvider)
64
65
/// <param name="dispatcher">The <see cref="IDispatcher"/> to be for invoking user actions into the <see cref="Renderer"/> context.</param>
65
66
public Renderer ( IServiceProvider serviceProvider , IDispatcher dispatcher ) : this ( serviceProvider )
66
67
{
67
- Dispatcher = dispatcher ;
68
+ _dispatcher = dispatcher ;
68
69
}
69
70
70
- /// <summary>
71
- /// Gets the <see cref="IDispatcher"/>
72
- /// </summary>
73
- protected IDispatcher Dispatcher { get ; }
74
-
75
71
internal RenderBatchBuilder RenderBatchBuilder { get ; } = new RenderBatchBuilder ( ) ;
76
72
77
- /// <summary>
78
- /// Gets a property that determines if this instance of <see cref="Renderer"/> can render a batch.
79
- ///
80
- /// <para>
81
- /// Derivied types can use this property to force queuing up renders for a batch.
82
- /// </para>
83
- /// </summary>
84
- protected virtual bool CanRenderBatch { get ; } = true ;
85
-
86
73
/// <summary>
87
74
/// Creates an <see cref="IDispatcher"/> that can be used with one or more <see cref="Renderer"/>.
88
75
/// </summary>
@@ -103,7 +90,8 @@ protected IComponent InstantiateComponent(Type componentType)
103
90
/// </summary>
104
91
/// <param name="component">The component.</param>
105
92
/// <returns>The component's assigned identifier.</returns>
106
- protected int AssignRootComponentId ( IComponent component )
93
+ // Internal for unit testing
94
+ protected internal int AssignRootComponentId ( IComponent component )
107
95
=> AttachAndInitComponent ( component , - 1 ) . ComponentId ;
108
96
109
97
/// <summary>
@@ -262,13 +250,13 @@ public Task DispatchEventAsync(int eventHandlerId, UIEventArgs eventArgs)
262
250
public virtual Task Invoke ( Action workItem )
263
251
{
264
252
// This is for example when we run on a system with a single thread, like WebAssembly.
265
- if ( Dispatcher == null )
253
+ if ( _dispatcher == null )
266
254
{
267
255
workItem ( ) ;
268
256
return Task . CompletedTask ;
269
257
}
270
258
271
- if ( SynchronizationContext . Current == Dispatcher )
259
+ if ( SynchronizationContext . Current == _dispatcher )
272
260
{
273
261
// This is an optimization for when the dispatcher is also a syncronization context, like in the default case.
274
262
// No need to dispatch. Avoid deadlock by invoking directly.
@@ -277,7 +265,7 @@ public virtual Task Invoke(Action workItem)
277
265
}
278
266
else
279
267
{
280
- return Dispatcher . Invoke ( workItem ) ;
268
+ return _dispatcher . Invoke ( workItem ) ;
281
269
}
282
270
}
283
271
@@ -289,20 +277,20 @@ public virtual Task Invoke(Action workItem)
289
277
public virtual Task InvokeAsync ( Func < Task > workItem )
290
278
{
291
279
// This is for example when we run on a system with a single thread, like WebAssembly.
292
- if ( Dispatcher == null )
280
+ if ( _dispatcher == null )
293
281
{
294
282
return workItem ( ) ;
295
283
}
296
284
297
- if ( SynchronizationContext . Current == Dispatcher )
285
+ if ( SynchronizationContext . Current == _dispatcher )
298
286
{
299
287
// This is an optimization for when the dispatcher is also a syncronization context, like in the default case.
300
288
// No need to dispatch. Avoid deadlock by invoking directly.
301
289
return workItem ( ) ;
302
290
}
303
291
else
304
292
{
305
- return Dispatcher . InvokeAsync ( workItem ) ;
293
+ return _dispatcher . InvokeAsync ( workItem ) ;
306
294
}
307
295
}
308
296
@@ -416,7 +404,7 @@ private void EnsureSynchronizationContext()
416
404
// Plus, any other logic that mutates state accessed during rendering also
417
405
// needs not to run concurrently with rendering so should be dispatched to
418
406
// the renderer's sync context.
419
- if ( Dispatcher is SynchronizationContext synchronizationContext && SynchronizationContext . Current != synchronizationContext )
407
+ if ( _dispatcher is SynchronizationContext synchronizationContext && SynchronizationContext . Current != synchronizationContext )
420
408
{
421
409
throw new InvalidOperationException (
422
410
"The current thread is not associated with the renderer's synchronization context. " +
@@ -435,22 +423,14 @@ private ComponentState GetOptionalComponentState(int componentId)
435
423
? componentState
436
424
: null ;
437
425
438
- /// <summary>
439
- /// Processes the render queue.
440
- /// </summary>
441
- protected void ProcessRenderQueue ( )
426
+ private void ProcessRenderQueue ( )
442
427
{
443
- if ( ! CanRenderBatch )
444
- {
445
- return ;
446
- }
447
-
448
428
var updateDisplayTask = Task . CompletedTask ;
449
429
_isBatchInProgress = true ;
450
430
try
451
431
{
452
432
// Process render queue until empty
453
- while ( CanRenderBatch && RenderBatchBuilder . ComponentRenderQueue . Count > 0 )
433
+ while ( RenderBatchBuilder . ComponentRenderQueue . Count > 0 )
454
434
{
455
435
var nextToRender = RenderBatchBuilder . ComponentRenderQueue . Dequeue ( ) ;
456
436
RenderInExistingBatch ( nextToRender ) ;
0 commit comments