Repository: dotnet/aspnetcore
File: src/Components/WebAssembly/WebAssembly/src/Rendering/WebAssemblyRenderer.cs (C#)

Code before:
internal sealed partial class WebAssemblyRenderer : WebRenderer
{
    private readonly ILogger _logger;
    private readonly Dispatcher _dispatcher;
    private readonly ResourceAssetCollection _resourceCollection;
    private readonly IInternalJSImportMethods _jsMethods;
    private readonly ComponentStatePersistenceManager _componentStatePersistenceManager;
    private static readonly RendererInfo _componentPlatform = new("WebAssembly", isInteractive: true);

    public WebAssemblyRenderer(IServiceProvider serviceProvider, ResourceAssetCollection resourceCollection, ILoggerFactory loggerFactory, JSComponentInterop jsComponentInterop)
        : base(serviceProvider, loggerFactory, DefaultWebAssemblyJSRuntime.Instance.ReadJsonSerializerOptions(), jsComponentInterop)
    {
        _logger = loggerFactory.CreateLogger<WebAssemblyRenderer>();
        _jsMethods = serviceProvider.GetRequiredService<IInternalJSImportMethods>();
        _componentStatePersistenceManager = serviceProvider.GetRequiredService<ComponentStatePersistenceManager>();

        // if SynchronizationContext.Current is null, it means we are on the single-threaded runtime
        _dispatcher = WebAssemblyDispatcher._mainSynchronizationContext == null
            ? NullDispatcher.Instance
            : new WebAssemblyDispatcher();

        _resourceCollection = resourceCollection;

        ElementReferenceContext = DefaultWebAssemblyJSRuntime.Instance.ElementReferenceContext;
        DefaultWebAssemblyJSRuntime.Instance.OnUpdateRootComponents += OnUpdateRootComponents;
        GetOrCreateWebRootComponentManager();
    }

    [UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "These are root components which belong to the user and are in assemblies that don't get trimmed.")]
    private void OnUpdateRootComponents(RootComponentOperationBatch batch, string appState)
    {
        var webRootComponentManager = GetOrCreateWebRootComponentManager();
        TaskCompletionSource? taskCompletionSource = null;
        var stateUpdateTask = Task.CompletedTask;
        var store = !string.IsNullOrEmpty(appState) ? new PrerenderComponentApplicationStore(appState) : null;
        if (store != null)
        {
            taskCompletionSource = new TaskCompletionSource();
            stateUpdateTask = EnqueueRestore(taskCompletionSource.Task, _componentStatePersistenceManager, store);
        }

        webRootComponentManager.SetCurrentUpdateTask(stateUpdateTask);

        for (var i = 0; i < batch.Operations.Length; i++)
        {
            var operation = batch.Operations[i];
            switch (operation.Type)
            {
                case RootComponentOperationType.Add:
                    _ = webRootComponentManager.AddRootComponentAsync(
                        operation.SsrComponentId,

Review comment (suggestion):
`OnUpdateRootComponents` alrady calls `GetOrCreateWebRootComponentManager` so Wasm rendering does not need additional lazy-initialization,

Code after:
internal sealed partial class WebAssemblyRenderer : WebRenderer
{
    private readonly ILogger _logger;
    private readonly Dispatcher _dispatcher;
    private readonly ResourceAssetCollection _resourceCollection;
    private readonly IInternalJSImportMethods _jsMethods;
    private readonly ComponentStatePersistenceManager _componentStatePersistenceManager;
    private static readonly RendererInfo _componentPlatform = new("WebAssembly", isInteractive: true);

    public WebAssemblyRenderer(IServiceProvider serviceProvider, ResourceAssetCollection resourceCollection, ILoggerFactory loggerFactory, JSComponentInterop jsComponentInterop)
        : base(serviceProvider, loggerFactory, DefaultWebAssemblyJSRuntime.Instance.ReadJsonSerializerOptions(), jsComponentInterop)
    {
        _logger = loggerFactory.CreateLogger<WebAssemblyRenderer>();
        _jsMethods = serviceProvider.GetRequiredService<IInternalJSImportMethods>();
        _componentStatePersistenceManager = serviceProvider.GetRequiredService<ComponentStatePersistenceManager>();

        // if SynchronizationContext.Current is null, it means we are on the single-threaded runtime
        _dispatcher = WebAssemblyDispatcher._mainSynchronizationContext == null
            ? NullDispatcher.Instance
            : new WebAssemblyDispatcher();

        _resourceCollection = resourceCollection;

        ElementReferenceContext = DefaultWebAssemblyJSRuntime.Instance.ElementReferenceContext;
        DefaultWebAssemblyJSRuntime.Instance.OnUpdateRootComponents += OnUpdateRootComponents;
    }

    [UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "These are root components which belong to the user and are in assemblies that don't get trimmed.")]
    private void OnUpdateRootComponents(RootComponentOperationBatch batch, string appState)
    {
        var webRootComponentManager = GetOrCreateWebRootComponentManager();
        TaskCompletionSource? taskCompletionSource = null;
        var stateUpdateTask = Task.CompletedTask;
        var store = !string.IsNullOrEmpty(appState) ? new PrerenderComponentApplicationStore(appState) : null;
        if (store != null)
        {
            taskCompletionSource = new TaskCompletionSource();
            stateUpdateTask = EnqueueRestore(taskCompletionSource.Task, _componentStatePersistenceManager, store);
        }

        webRootComponentManager.SetCurrentUpdateTask(stateUpdateTask);

        for (var i = 0; i < batch.Operations.Length; i++)
        {
            var operation = batch.Operations[i];
            switch (operation.Type)
            {
                case RootComponentOperationType.Add:
                    _ = webRootComponentManager.AddRootComponentAsync(
                        operation.SsrComponentId,
                        operation.Descriptor!.ComponentType,