Skip to content

Commit

Permalink
#397 - Drop playing component observers.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Aug 2, 2021
1 parent 729502f commit c0c4020
Showing 1 changed file with 0 additions and 85 deletions.
85 changes: 0 additions & 85 deletions src/Money.Blazor.Host/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Blazored.LocalStorage;
using Blazored.SessionStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
Expand All @@ -12,11 +10,8 @@
using Money.Models;
using Money.Services;
using Neptuo.Events;
using Neptuo.Events.Handlers;
using Neptuo.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -91,9 +86,6 @@ private static void ConfigureServices(IServiceCollection services)
.AddBlazoredLocalStorage()
.AddBlazoredSessionStorage();

services
.AddTransient<IComponentActivator, ComponentActivator>();

services
.AddSingleton<TemplateService>();

Expand All @@ -119,81 +111,4 @@ private static void StartupServices(IServiceProvider services)
services.GetRequiredService<VisibilityState>();
}
}

class ComponentActivator : IComponentActivator
{
public IComponent CreateInstance(Type componentType)
{
Console.WriteLine($"CA: '{componentType.FullName}'.");

if (componentType.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEventHandler<>)))
{
Console.WriteLine($"CA: '{componentType.FullName}' as 'EventAttachedComponent'.");
return new EventAttachedComponent() { Type = componentType };
}
else if(componentType.IsGenericType && componentType.GetGenericTypeDefinition() == typeof(Wrapped<>))
{
componentType = componentType.GetGenericArguments()[0];
}

return (IComponent)Activator.CreateInstance(componentType);
}
}

class Wrapped<T> : IComponent
{
public void Attach(RenderHandle renderHandle)
{
throw new NotImplementedException();
}

public Task SetParametersAsync(ParameterView parameters)
{
throw new NotImplementedException();
}
}

class EventAttachedComponent : IComponent
{
private RenderHandle _renderHandle;
private RenderFragment _cachedRenderFragment;

public EventAttachedComponent()
{
_cachedRenderFragment = Render;
}

public Type Type { get; set; }
public IDictionary<string, object> Parameters { get; set; } = new Dictionary<string, object>();

public void Attach(RenderHandle renderHandle)
{
_renderHandle = renderHandle;
}

public Task SetParametersAsync(ParameterView parameters)
{
Parameters.Clear();
foreach (var entry in parameters)
Parameters[entry.Name] = entry.Value;

_renderHandle.Render(_cachedRenderFragment);
return Task.CompletedTask;
}

void Render(RenderTreeBuilder builder)
{
builder.OpenComponent(0, typeof(Wrapped<>).MakeGenericType(Type));

if (Parameters != null)
{
foreach (var entry in Parameters)
{
builder.AddAttribute(1, entry.Key, entry.Value);
}
}

builder.CloseComponent();
}
}
}

0 comments on commit c0c4020

Please sign in to comment.