Skip to content

Commit

Permalink
Moving all logic to constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuergi committed Jan 28, 2024
1 parent 7329093 commit c9cafaf
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 427 deletions.
9 changes: 4 additions & 5 deletions src/OpenSmc.Application.Scope/ApplicationScopePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OpenSmc.Application.Scope;

public class ApplicationScopePlugin(IServiceProvider serviceProvider) : MessageHubPlugin<ApplicationScopePlugin, ApplicationScopeState>(serviceProvider),
public class ApplicationScopePlugin : MessageHubPlugin<ApplicationScopePlugin, ApplicationScopeState>,
IMessageHandler<SubscribeScopeRequest>,
IMessageHandler<UnsubscribeScopeRequest>,
IMessageHandler<DisposeScopeRequest>,
Expand All @@ -26,10 +26,8 @@ public class ApplicationScopePlugin(IServiceProvider serviceProvider) : MessageH
private IApplicationScope applicationScope;
private IScopeRegistry scopeRegistry;
private ISerializationService serializationService;

public override void Initialize(IMessageHub hub)
public ApplicationScopePlugin(IServiceProvider serviceProvider, IMessageHub hub) : base(hub)
{
base.Initialize(hub);
InitializeState(new());
applicationScope = hub.ServiceProvider.GetRequiredService<IApplicationScope>();
serializationService = hub.ServiceProvider.GetRequiredService<ISerializationService>();
Expand All @@ -39,9 +37,9 @@ public override void Initialize(IMessageHub hub)
scopeRegistry.InstanceRegistered += (_, scope) => TrackPropertyChanged(scope);
foreach (var scope in scopeRegistry.Scopes)
TrackPropertyChanged(scope);

}


IMessageDelivery IMessageHandler<SubscribeScopeRequest>.HandleMessage(IMessageDelivery<SubscribeScopeRequest> request)
{
var subscribeScopeRequest = request.Message;
Expand Down Expand Up @@ -94,6 +92,7 @@ private IMessageDelivery SubscribeScope(IMessageDelivery<SubscribeScopeRequest>

private static readonly MethodInfo GetScopeMethod = ReflectionHelper.GetMethodGeneric<ApplicationScopePlugin>(x => x.GetScope<object>(null));


// ReSharper disable once UnusedMethodReturnValue.Local
private object GetScope<TScope>(IMessageDelivery<SubscribeScopeRequest> request)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ public record ExpressionSynchronizationHubState(IInternalMutableScope Applicatio
public ImmutableDictionary<string, ExpressionRegistryItem> RegisteredExpressions { get; init; } = ImmutableDictionary<string, ExpressionRegistryItem>.Empty;
}

public class ExpressionSynchronizationPlugin(IServiceProvider serviceProvider) : MessageHubPlugin<ExpressionSynchronizationPlugin, ExpressionSynchronizationHubState>(serviceProvider),
public class ExpressionSynchronizationPlugin : MessageHubPlugin<ExpressionSynchronizationPlugin, ExpressionSynchronizationHubState>,
IMessageHandler<SubscribeToEvaluationRequest>,
IMessageHandler<UnsubscribeFromEvaluationRequest>,
IMessageHandler<ScopePropertyChangedEvent>
{
[Inject] private IApplicationScope applicationScope;

public override void Initialize(IMessageHub hub)
public ExpressionSynchronizationPlugin(IServiceProvider serviceProvider, IMessageHub hub) : base(hub)
{
base.Initialize(hub);

// ReSharper disable once SuspiciousTypeConversion.Global
InitializeState(new((IInternalMutableScope)applicationScope, new(hub)));

}


IMessageDelivery IMessageHandler<ScopePropertyChangedEvent>.HandleMessage(IMessageDelivery<ScopePropertyChangedEvent> delivery)
{
InvalidateExpressions(delivery.Message.Scope, delivery.Message.Property);
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSmc.Layout/Composition/LayoutStackPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public class LayoutStackPlugin :
private readonly LayoutDefinition layoutDefinition;


public LayoutStackPlugin(IServiceProvider serviceProvider) : base(serviceProvider)
public LayoutStackPlugin(IMessageHub hub) : base(hub)
{
}


public LayoutStackPlugin(LayoutDefinition layoutDefinition, IServiceProvider serviceProvider) : base(serviceProvider)
public LayoutStackPlugin(LayoutDefinition layoutDefinition, IMessageHub hub) : base(hub)
{
this.layoutDefinition = layoutDefinition;
UpdateState(_ => layoutDefinition.InitialState);
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSmc.Layout/LayoutClient/LayoutClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static MessageHubConfiguration AddLayoutClient(this MessageHubConfigurat
conf = options(conf);
return mhConfiguration.WithBuildupAction(hub =>
{
hub.AddPlugin(new LayoutClientPlugin(conf, hub.ServiceProvider));
hub.AddPlugin(new LayoutClientPlugin(conf, hub));
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/OpenSmc.Layout/LayoutClient/LayoutClientHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public IEnumerable<AreaChangedEvent> GetAreasByControlId(string controlId)

public record LayoutClientConfiguration(object RefreshMessage, object LayoutHostAddress, string MainArea = "");

public class LayoutClientPlugin(LayoutClientConfiguration Configuration, IServiceProvider serviceProvider) : MessageHubPlugin<LayoutClientPlugin, LayoutClientState>(serviceProvider),
public class LayoutClientPlugin(LayoutClientConfiguration configuration, IMessageHub hub) : MessageHubPlugin<LayoutClientPlugin, LayoutClientState>(hub),
IMessageHandler<AreaChangedEvent>,
IMessageHandler<GetRequest<AreaChangedEvent>>
{
public override void Initialize(LayoutClientState state)
{
base.Initialize(state);
InitializeState(new(Configuration));
Hub.Post(Configuration.RefreshMessage, o => o.WithTarget(State.Configuration.LayoutHostAddress));
InitializeState(new(configuration));
Hub.Post(configuration.RefreshMessage, o => o.WithTarget(State.Configuration.LayoutHostAddress));
}


Expand Down
2 changes: 1 addition & 1 deletion src/OpenSmc.Layout/RemoteViewPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public override void Dispose()
base.Dispose();
}

protected RemoteViewPlugin(IServiceProvider serviceProvider) : base(serviceProvider)
protected RemoteViewPlugin(IMessageHub hub) : base(hub)
{
}
}
11 changes: 1 addition & 10 deletions src/OpenSmc.Layout/SmappExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,10 @@ internal static void AddLayout(this IMessageHub hub, Func<LayoutDefinition, Layo
ld = layoutDefinition(ld);
var mainLayoutAddress = MainLayoutAddress(hub.Address);
var layoutHub = hub.GetHostedHub(mainLayoutAddress, config => config
.AddPlugin(h => new LayoutStackPlugin(hub.ServiceProvider)));
.AddPlugin(h => new LayoutStackPlugin(hub)));
hub.ConnectTo(layoutHub);

}

private static IMessageHub CreateLayoutHub(IServiceProvider serviceProvider, UiControlAddress address)
{
return serviceProvider.CreateMessageHub
(address,
config => config
.AddPlugin(h => new LayoutStackPlugin(serviceProvider))
);
}

public static UiControlAddress MainLayoutAddress(object address) => new("Main", address);
}
2 changes: 1 addition & 1 deletion src/OpenSmc.Layout/UiControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public MessageAndAddress ClickMessage
public class GenericUiControlPlugin<TControl> : UiControlPlugin<TControl>
where TControl : UiControl
{
protected GenericUiControlPlugin(IServiceProvider serviceProvider) : base(serviceProvider)
protected GenericUiControlPlugin(IMessageHub hub) : base(hub)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/OpenSmc.Layout/UiControlPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public IMessageDelivery<TMessage> Post<TMessage>(TMessage message, Func<PostOpti
return Hub.Post(message, options);
}

protected UiControlPlugin(IServiceProvider serviceProvider) : base(serviceProvider)
protected UiControlPlugin(IMessageHub hub) : base(hub)
{
}
}
2 changes: 1 addition & 1 deletion src/OpenSmc.Layout/Views/Expand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected virtual async Task<IMessageDelivery> ExpandAsync(IMessageDelivery<Expa
return delivery.Processed();
}

protected ExpandableUiPlugin(IServiceProvider serviceProvider) : base(serviceProvider)
protected ExpandableUiPlugin(IMessageHub hub) : base(hub)
{
}
}
2 changes: 1 addition & 1 deletion src/OpenSmc.Messaging.Hub/IMessageHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace OpenSmc.Messaging;

public interface IMessageHub : IMessageHandlerRegistry, IAsyncDisposable, IDisposable
{
internal static TimeSpan DefaultTimeout => TimeSpan.FromSeconds(3);
internal static TimeSpan DefaultTimeout => TimeSpan.FromSeconds(3000);
IMessageDelivery<TMessage> Post<TMessage>(TMessage message, Func<PostOptions, PostOptions> options = null);
IMessageDelivery DeliverMessage(IMessageDelivery delivery);
object Address { get; }
Expand Down
3 changes: 1 addition & 2 deletions src/OpenSmc.Messaging.Hub/IMessageHubPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public interface IMessageHubPlugin : IAsyncDisposable
{
void Initialize(IMessageHub hub);
bool Filter(IMessageDelivery delivery);
Task<IMessageDelivery> DeliverMessageAsync(IMessageDelivery delivery);
bool IsDeferred(IMessageDelivery delivery);
}
14 changes: 3 additions & 11 deletions src/OpenSmc.Messaging.Hub/IMessageService.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
using System;
using System.Threading.Tasks;

namespace OpenSmc.Messaging;
namespace OpenSmc.Messaging;

public interface IMessageService : IAsyncDisposable
{
object Address { get; }
public IDisposable Defer(Predicate<IMessageDelivery> deferredFilter);
IMessageDelivery IncomingMessage(IMessageDelivery message);
IMessageDelivery Forward(IMessageDelivery delivery, object address) => IncomingMessage(delivery.ForwardTo(address));
IMessageDelivery Post<TMessage>(TMessage message, PostOptions opt);

void Initialize(AsyncDelivery messageHandler);

void Schedule(Func<Task> action);

void Schedule(Action action) => Schedule(() =>
{
action();
return Task.CompletedTask;
});

Task<bool> FlushAsync();

void Start();
}
Loading

0 comments on commit c9cafaf

Please sign in to comment.