Skip to content

Commit

Permalink
Implemented IScopedEvents and IGlobalEvents for DI #9
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Dec 4, 2024
1 parent c9ffb4b commit 96916c6
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Piral.Blazor.Shared;
using System.IO.Pipelines;
using System.Runtime.Loader;
using System.Text;
Expand All @@ -7,12 +8,12 @@

namespace Piral.Blazor.Orchestrator.Connector;

internal class MfEmulatorConnector(IMfRepository repository, IEvents events) : IMfDebugConnector
internal class MfEmulatorConnector(IMfRepository repository, IGlobalEvents events) : IMfDebugConnector
{
private readonly IEnumerable<string> _styles = [];
private readonly IEnumerable<string> _scripts = ["_content/Piral.Blazor.Orchestrator/debug.js"];
private readonly IMfRepository _repository = repository;
private readonly IEvents _events = events;
private readonly IGlobalEvents _events = events;

public IEnumerable<string> Styles => _styles;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Collections.Concurrent;
using System.Text.Json;
using Piral.Blazor.Shared;

namespace Piral.Blazor.Orchestrator;

internal class GlobalEvents : IEvents
internal class EventBus : IGlobalEvents, IScopedEvents
{
private ConcurrentDictionary<string, List<Delegate>> _eventMap = new();

Expand Down
8 changes: 5 additions & 3 deletions src/Piral.Blazor.Orchestrator/Loader/MfLocalLoaderService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace Piral.Blazor.Orchestrator.Loader;
using Piral.Blazor.Shared;

internal class MfLocalLoaderService<T>(T originalLoader, IMfRepository repository, IPiralConfig config, IModuleContainerService container, IEvents events, IData data) : IMfLoaderService
namespace Piral.Blazor.Orchestrator.Loader;

internal class MfLocalLoaderService<T>(T originalLoader, IMfRepository repository, IPiralConfig config, IModuleContainerService container, IGlobalEvents events, IData data) : IMfLoaderService
where T : class, IMfLoaderService
{
private readonly T _originalLoader = originalLoader;
private readonly IMfRepository _repository = repository;
private readonly IModuleContainerService _container = container;
private readonly IPiralConfig _config = config;
private readonly IEvents _events = events;
private readonly IGlobalEvents _events = events;
private readonly IData _data = data;

public void ConnectMicrofrontends(CancellationToken cancellationToken)
Expand Down
3 changes: 2 additions & 1 deletion src/Piral.Blazor.Orchestrator/LocalMicrofrontendPackage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NuGet.Packaging;
using Piral.Blazor.Shared;
using System.Reflection;
using System.Text;
using System.Text.Json;
Expand All @@ -7,7 +8,7 @@

namespace Piral.Blazor.Orchestrator;

internal class LocalMicrofrontendPackage(string path, IPiralConfig config, IModuleContainerService container, IEvents events, IData data) :
internal class LocalMicrofrontendPackage(string path, IPiralConfig config, IModuleContainerService container, IGlobalEvents events, IData data) :
MicrofrontendPackage(MakePackage(path), config, container, events, data)
{
private readonly string _path = path;
Expand Down
6 changes: 3 additions & 3 deletions src/Piral.Blazor.Orchestrator/MfPackageService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using NuGet.Packaging;
using System.Text.Json.Nodes;
using Piral.Blazor.Shared;

namespace Piral.Blazor.Orchestrator;

internal class MfPackageService(IPiralConfig config, IModuleContainerService container, ISnapshotService snapshot, IEvents events, IData data) : IMfPackageService
internal class MfPackageService(IPiralConfig config, IModuleContainerService container, ISnapshotService snapshot, IGlobalEvents events, IData data) : IMfPackageService
{
private readonly IPiralConfig _config = config;
private readonly IModuleContainerService _container = container;
private readonly ISnapshotService _snapshot = snapshot;
private readonly IEvents _events = events;
private readonly IGlobalEvents _events = events;
private readonly IData _data = data;

public async Task<MicrofrontendPackage> LoadMicrofrontend(MfPackageMetadata entry)
Expand Down
6 changes: 3 additions & 3 deletions src/Piral.Blazor.Orchestrator/MicrofrontendPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class MicrofrontendPackage : IDisposable
private readonly MicrofrontendLoadContext _context;
public event EventHandler? PackageChanged;

public MicrofrontendPackage(MfPackageMetadata entry, IPiralConfig config, IModuleContainerService container, IEvents events, IData data)
public MicrofrontendPackage(MfPackageMetadata entry, IPiralConfig config, IModuleContainerService container, IGlobalEvents events, IData data)
{
_app = new (entry, events, data);
_config = config;
Expand Down Expand Up @@ -127,9 +127,9 @@ public async Task Destroy()

public abstract Task<Stream?> GetFile(string path);

sealed class RelatedMfAppService(MfPackageMetadata entry, IEvents events, IData data) : IMfAppService
sealed class RelatedMfAppService(MfPackageMetadata entry, IGlobalEvents events, IData data) : IMfAppService
{
private readonly IEvents _events = events;
private readonly IGlobalEvents _events = events;
private readonly IData _data = data;
private readonly MfDetails _meta = new () { Name = entry.Name, Version = entry.Version, Config = entry.Config ?? [] };

Expand Down
3 changes: 2 additions & 1 deletion src/Piral.Blazor.Orchestrator/RemoteMicrofrontendPackage.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using NuGet.Packaging;
using Piral.Blazor.Shared;
using System.Reflection;

namespace Piral.Blazor.Orchestrator;

internal class RemoteMicrofrontendPackage(MfPackageMetadata entry, List<PackageArchiveReader> packages, IPiralConfig config, IModuleContainerService container, IEvents events, IData data) :
internal class RemoteMicrofrontendPackage(MfPackageMetadata entry, List<PackageArchiveReader> packages, IPiralConfig config, IModuleContainerService container, IGlobalEvents events, IData data) :
MicrofrontendPackage(entry, config, container, events, data)
{
private readonly List<PackageArchiveReader> _packages = packages;
Expand Down
3 changes: 2 additions & 1 deletion src/Piral.Blazor.Orchestrator/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public static IServiceCollection AddMicrofrontends<TLoader>(this IServiceCollect
services.AddSingleton<IMfRepository, MfRepository>();
services.AddSingleton<INugetService, NugetService>();
services.AddSingleton<IMfPackageService, MfPackageService>();
services.AddSingleton<IEvents, GlobalEvents>();
services.AddSingleton<IGlobalEvents, EventBus>();
services.AddSingleton<IData, GlobalData>();
services.AddScoped<IScopedEvents, EventBus>();

if (config.IsEmulator)
{
Expand Down
22 changes: 22 additions & 0 deletions src/Piral.Blazor.Shared/IGlobalEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Piral.Blazor.Shared;

/// <summary>
/// Represents an event bus that works globally - i.e., beyond the session scope.
/// </summary>
public interface IGlobalEvents
{
/// <summary>
/// Dispatches an event using the given type.
/// </summary>
void DispatchEvent<T>(string type, T args);

/// <summary>
/// Adds the provided event listener.
/// </summary>
void AddEventListener<T>(string type, Action<T> handler);

/// <summary>
/// Removes the provided event listener.
/// </summary>
void RemoveEventListener<T>(string type, Action<T> handler);
}
4 changes: 2 additions & 2 deletions src/Piral.Blazor.Shared/IMfComponentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public interface IMfComponentService
{
event EventHandler ComponentsChanged;
event EventHandler ComponentsChanged;

IEnumerable<string> Styles { get; }
IEnumerable<string> Styles { get; }

IEnumerable<string> Scripts { get; }

Expand Down
41 changes: 13 additions & 28 deletions src/Piral.Blazor.Shared/IMfService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,21 @@
/// <summary>
/// Represents a set of helper functions to be used in micro frontend components.
/// </summary>
public interface IMfService
public interface IMfService : IGlobalEvents
{
/// <summary>
/// Dispatches an event using the given type.
/// </summary>
void DispatchEvent<T>(string type, T args);

/// <summary>
/// Adds the provided event listener.
/// </summary>
void AddEventListener<T>(string type, Action<T> handler);

/// <summary>
/// Removes the provided event listener.
/// </summary>
void RemoveEventListener<T>(string type, Action<T> handler);

/// <summary>
/// Gets the details for the current micro frontend.
/// </summary>
MfDetails Meta { get; }
/// <summary>
/// Gets the details for the current micro frontend.
/// </summary>
MfDetails Meta { get; }

/// <summary>
/// Tries to set some data available across the micro frontends.
/// </summary>
/// <typeparam name="T">The type of the data to set.</typeparam>
/// <param name="name">The name of the data entry.</param>
/// <param name="value">The data value to store.</param>
/// <returns>True if the data was set, otherwise false.</returns>
bool TrySetData<T>(string name, T value);
/// <summary>
/// Tries to set some data available across the micro frontends.
/// </summary>
/// <typeparam name="T">The type of the data to set.</typeparam>
/// <param name="name">The name of the data entry.</param>
/// <param name="value">The data value to store.</param>
/// <returns>True if the data was set, otherwise false.</returns>
bool TrySetData<T>(string name, T value);

/// <summary>
/// Tries to get some data available for all micro frontends.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Piral.Blazor.Orchestrator;
namespace Piral.Blazor.Shared;

/// <summary>
/// Represents the micro frontend shared event basis.
/// Represents an event bus that works within the current scope only.
/// </summary>
public interface IEvents
public interface IScopedEvents
{
/// <summary>
/// Dispatches an event using the given type.
Expand Down

0 comments on commit 96916c6

Please sign in to comment.