59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace TIAMWebApp.Shared.Application.Interfaces
|
|
{
|
|
public abstract class ComponentUpdateServiceBase : IComponentUpdateService
|
|
{
|
|
protected Dictionary<Type, IComponentUpdateItem> ComponentsByType = [];
|
|
|
|
public virtual void CallRequestRefreshAll()
|
|
{
|
|
foreach (var component in ComponentsByType.Values)
|
|
component.CallRequestRefresh();
|
|
}
|
|
|
|
public void CallRequestRefresh<T>() where T : class, IComponent
|
|
{
|
|
if (ComponentsByType.TryGetValue(typeof(T), out var componentUpdateItem))
|
|
componentUpdateItem.CallRequestRefresh();
|
|
|
|
}
|
|
|
|
public IComponentUpdateItem GetOrAddComponent<T>() where T : class, IComponent
|
|
{
|
|
var componentType = typeof(T);
|
|
|
|
if (ComponentsByType.TryGetValue(componentType, out var componentUpdateItem))
|
|
return componentUpdateItem;
|
|
|
|
componentUpdateItem = new ComponentUpdateItem();
|
|
ComponentsByType.Add(componentType, componentUpdateItem);
|
|
|
|
return componentUpdateItem;
|
|
}
|
|
}
|
|
|
|
public class ComponentUpdateItem : IComponentUpdateItem
|
|
{
|
|
public event Action? RefreshRequested;
|
|
public virtual void CallRequestRefresh()
|
|
{
|
|
RefreshRequested?.Invoke();
|
|
}
|
|
}
|
|
|
|
public interface IComponentUpdateItem
|
|
{
|
|
event Action? RefreshRequested;
|
|
void CallRequestRefresh();
|
|
}
|
|
|
|
public interface IComponentUpdateService
|
|
{
|
|
void CallRequestRefreshAll();
|
|
void CallRequestRefresh<T>() where T : class, IComponent;
|
|
|
|
IComponentUpdateItem GetOrAddComponent<T>() where T : class, IComponent;
|
|
}
|
|
}
|